javascript - React + three.js:TextureLoader 在尝试加载图像时抛出错误
问题描述
我一直在尝试将 three.js 添加到 React 项目中,并且大部分都成功了。但是,我无法弄清楚为什么我的纹理图像没有加载。我正在运行我自己的本地服务器,添加了一个回调方法以在加载完成时运行,并尝试从多个位置加载我的图像,但到目前为止没有任何效果。
我的图像存储在 中public/animation/js/img/texture1.png
,我的动画文件存储在 中public/animation/js/animation.js
,这里是我现在正在使用的代码:
const Pilot = function () {
let faceMaterial;
const geometry = new THREE.CircleGeometry(10, 128);
const manager = new THREE.LoadingManager();
manager.onStart = function (url, itemsLoaded, itemsTotal) {
console.log('Started loading file: ',
url,
'.\nLoaded ',
itemsLoaded,
' of ',
itemsTotal,
' files.');
};
manager.onLoad = () => {
console.log('Loading complete!');
};
manager.onProgress = function (url, itemsLoaded, itemsTotal) {
console.log('Loading file: ',
url,
'.\nLoaded ',
itemsLoaded,
' of ',
itemsTotal,
' files.');
};
manager.onError = function (url) {
console.error( 'There was an error loading ', url);
};
const textureLoader = new THREE.TextureLoader(manager);
textureLoader.setCrossOrigin('anonymous');
textureLoader.load('/img/texture1.png',
texture => {
faceMaterial = new THREE.MeshFaceMaterial([
new THREE.MeshBasicMaterial({map: texture}),
new THREE.MeshBasicMaterial({map: texture}),
new THREE.MeshBasicMaterial({map: texture}),
new THREE.MeshBasicMaterial({map: texture}),
new THREE.MeshBasicMaterial({map: texture}),
new THREE.MeshBasicMaterial({map: texture})
]);
},
undefined,
err => {
console.error('An error occured:', err);
}
);
this.mesh = new THREE.Mesh(geometry, faceMaterial);
this.mesh.name = 'profile';
};
解决方案
这可能是由于您的图像路径是绝对的,即:'/img/texture1.png'
您的 Web 服务器从哪个目录运行?如果您的 Web 服务器是从/public
目录运行的,那么您可能需要更新传递给您的 textureLoaderload
方法的图像路径,如下所示:
textureLoader.load('/animation/js/img/texture1.png',
您还需要确保您的网格使用加载纹理数据的相同材质实例。您可以对代码进行以下调整以确保这一点:
// Declare material instance outside of texture load handler
const faceMaterial = new THREE.MeshFaceMaterial([
new THREE.MeshBasicMaterial(),
new THREE.MeshBasicMaterial(),
new THREE.MeshBasicMaterial(),
new THREE.MeshBasicMaterial(),
new THREE.MeshBasicMaterial(),
new THREE.MeshBasicMaterial()
])
const textureLoader = new THREE.TextureLoader(manager);
textureLoader.setCrossOrigin('anonymous');
textureLoader.load('/animation/js/img/texture1.png',
texture => {
// When texture loads, apply the texture to the map of each sub
// material of faceMaterial instance
faceMaterial.materials[0].map = texture;
faceMaterial.materials[1].map = texture;
faceMaterial.materials[2].map = texture;
faceMaterial.materials[3].map = texture;
faceMaterial.materials[4].map = texture;
faceMaterial.materials[5].map = texture;
},
undefined,
err => {
console.error('An error occured:', err);
}
);
// Apply the faceMaterial instance declared above, to your mesh
this.mesh = new THREE.Mesh(geometry, faceMaterial);
this.mesh.name = 'profile';
推荐阅读
- javascript - OnClose 不起作用
- datetime - DateTime.now() 在真机(小米米 4i)中返回错误的时间(-7:30)
- python - 有效年份值的“ValueError:年份超出范围”
- php - PHP套接字客户端连接到多个远程服务器以合并输出
- php - Twig 输出在 html 中的错误位置
- php - Laravel 无法读取表单数据
- c# - 仅 WPF 绑定到自动实现的属性
- sparql - 如何使用 SPARQL 代码获取 RDF 文件或本体中所有谓词的列表?
- php - How to add another parameter to use in the php file while including a php file using shortcode in wordpress
- python-3.x - 使用 open() 和 .write 时新文件中的第一行丢失