three.js纹理贴图不显示
1、材质问题, 比如MeshNormalMaterial材质不可以
2、引入图片问题
//这种引入方式不可以
loader.load('./ylj.jpg', function(texture) {}
//这种引入方式可以:
var ylj = require('./ylj.jpg')
loader.load(ylj, function(texture) {}
3、渲染方法在图片加载之前调用了
这种错误最有可能,添加其他模型流程是同步的:
添加mesh----渲染
纹理图片添加是异步的:
添加mesh-->加载纹理图片--->渲染--->纹理图片加载成功---->??(加载成功了没有重新渲染一次)
//一种正确的写法
var ylj = require('./ylj.jpg')
var loader = new THREE.TextureLoader()
loader.load(ylj, function(texture) {
var planeGeometry = new THREE.PlaneGeometry(400, 200);
var planeMaterial = new THREE.MeshLambertMaterial({
map:texture,
color: 0xaaaaaa
});
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.y = -5;
plane.receiveShadow = true;
scene.add(plane);
//这行渲染的方法,必须保证要在load图片成功后调用一次才可以,要么成功回调中调用一次,要么这里不调用但是渲染的方法是循环执行的
renderer.render(scene,camera)
})