private async parseMaterial(osgStateSet: any) { let material = new THREE.MeshBasicMaterial({ // side: THREE.DoubleSide, }); //THREE.FrontSide 背面 // THREE.BackSide 前面 // THREE.DoubleSide 双面 let osgImage = osgStateSet.TextureAttributeList[0].value.StateAttribute.Image; // let texture = this.parseImage(osgImage); let fileName = osgImage.Name; const isJPEG = fileName.search(/.jpe?g($|?)/i) > 0; const isPNG = fileName.search(/.png($|?)/i) > 0; if (!isPNG && !isJPEG) { return; } let mimeType = isPNG ? 'image/png' : 'image/jpeg'; let imageUri: any = new Blob([osgImage.Data], { type: mimeType }); let base64 = await this.blobToBase64(imageUri); // debugger let imageUrl: any = URL.createObjectURL(imageUri); try { let texture = new THREE.TextureLoader().load(base64, () => { texture.needsUpdate = true; // texture.format = THREE.RGBAFormat; texture.type = THREE.UnsignedShort5551Type; texture.minFilter = THREE.LinearMipmapNearestFilter; texture.magFilter = THREE.LinearMipmapNearestFilter; texture.generateMipmaps = false; // texture.format = THREE.RGBAIntegerFormat; // texture.minFilter = THREE.NearestFilter; // texture.magFilter = THREE.NearestFilter; // texture.minFilter = THREE.LinearMipMapLinearFilter; // texture.magFilter = THREE.LinearFilter; // texture.wrapS = THREE.RepeatWrapping; // texture.wrapT = THREE.RepeatWrapping; // texture.anisotropy = 1; // texture.generateMipmaps = false; // texture.encoding = THREE.sRGBEncoding; }); imageUri = null; osgImage = null; imageUrl = null; if (texture) { material.map = texture; } } catch (e) { console.log('纹理加载出错', e); } osgStateSet = null; return material; } private blobToBase64(blob: any) { return new Promise((resolve, reject) => { const fileReader = new FileReader(); // readAsDataURL fileReader.readAsDataURL(blob); fileReader.onload = (e: any) => { resolve(e.target.result); }; }); }没有显示base64图片的纹理,也没有报错,请找出问题,并给出详细代码
时间: 2024-03-31 17:36:25 浏览: 139
在代码中,你使用了一个Blob对象来加载纹理图片,并将其转换成了base64编码,但是在加载纹理时,却直接使用了base64编码字符串来加载纹理,这个可能是导致问题的原因。正确的做法应该是使用图片的URL来加载纹理,而不是直接使用base64编码字符串。
具体修改代码如下:
```typescript
private async parseMaterial(osgStateSet: any) {
let material = new THREE.MeshBasicMaterial({
// side: THREE.DoubleSide,
});
//THREE.FrontSide 背面
// THREE.BackSide 前面
// THREE.DoubleSide 双面
let osgImage = osgStateSet.TextureAttributeList[0].value.StateAttribute.Image;
// let texture = this.parseImage(osgImage);
let fileName = osgImage.Name;
const isJPEG = fileName.search(/.jpe?g($|?)/i) > 0;
const isPNG = fileName.search(/.png($|?)/i) > 0;
if (!isPNG && !isJPEG) {
return;
}
let mimeType = isPNG ? 'image/png' : 'image/jpeg';
let imageUri: any = new Blob([osgImage.Data], { type: mimeType });
try {
// 使用图片的URL来加载纹理
let texture = await new Promise<THREE.Texture>((resolve) => {
let imageUrl = URL.createObjectURL(imageUri);
new THREE.TextureLoader().load(imageUrl, (texture) => {
// 在加载完成的回调函数中,手动释放URL对象,避免内存泄漏问题
URL.revokeObjectURL(imageUrl);
texture.needsUpdate = true;
// texture.format = THREE.RGBAFormat;
texture.type = THREE.UnsignedShort5551Type;
texture.minFilter = THREE.LinearMipmapNearestFilter;
texture.magFilter = THREE.LinearMipmapNearestFilter;
texture.generateMipmaps = false;
// texture.format = THREE.RGBAIntegerFormat;
// texture.minFilter = THREE.NearestFilter;
// texture.magFilter = THREE.NearestFilter;
// texture.minFilter = THREE.LinearMipMapLinearFilter;
// texture.magFilter = THREE.LinearFilter;
// texture.wrapS = THREE.RepeatWrapping;
// texture.wrapT = THREE.RepeatWrapping;
// texture.anisotropy = 1;
// texture.generateMipmaps = false;
// texture.encoding = THREE.sRGBEncoding;
resolve(texture);
});
});
if (texture) {
material.map = texture;
}
} catch (e) {
console.log('纹理加载出错', e);
}
osgStateSet = null;
return material;
}
private blobToBase64(blob: any) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
// readAsDataURL
fileReader.readAsDataURL(blob);
fileReader.onload = (e: any) => {
resolve(e.target.result);
};
});
}
```
其中,使用了一个Promise对象来等待纹理加载完成后返回,同时在加载完成的回调函数中,手动释放URL对象,避免内存泄漏问题。另外,建议在加载纹理时,设置合适的minFilter和magFilter,以获得更好的渲染效果。
阅读全文