我在我的Three.js画布中插入了一个天空盒,如下所示:
// SkyBox
var urls = [
themePath + 'assets/img/sky/pos-x.jpg',
themePath + 'assets/img/sky/neg-x.jpg',
themePath + 'assets/img/sky/pos-y.jpg',
themePath + 'assets/img/sky/neg-y.jpg',
themePath + 'assets/img/sky/pos-z.jpg',
themePath + 'assets/img/sky/neg-z.jpg'
]
window.cubemap = THREE.ImageUtils.loadTextureCube(urls);
cubemap.format = THREE.RGBFormat;
window.shader = THREE.ShaderLib['cube'];
shader.uniforms['tCube'].value = cubemap;
window.skyBoxMaterial = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
});
window.skybox = new THREE.Mesh(
new THREE.BoxGeometry(1000, 1000, 1000),
skyBoxMaterial
);
skybox.position.set(0, 200, 0);
scene.add(skybox);
我的问题是图像根本没有加载或完全加载,具体取决于浏览器。
在Chrome上,它们几乎总是被加载(有时一张脸没有加载)。在Firefox上,他们几乎从未第一次加载(我得到黑色背景)。我试图添加一个settimer来等待一个完整的加载,但它不会改变任何东西。它们似乎已加载或未加载或部分加载,但无需等待。
我做错了什么吗?是否有特定格式的图像?
答案 0 :(得分:1)
事实上非常容易。 ' loadTextureCube'有一个回调函数。代码必须更改为:
var urls = [
themePath + 'assets/img/sky/pos-x.jpg',
themePath + 'assets/img/sky/neg-x.jpg',
themePath + 'assets/img/sky/pos-y.jpg',
themePath + 'assets/img/sky/neg-y.jpg',
themePath + 'assets/img/sky/pos-z.jpg',
themePath + 'assets/img/sky/neg-z.jpg'
]
window.cubemap = THREE.ImageUtils.loadTextureCube(urls, undefined, function() {
cubemap.format = THREE.RGBFormat;
window.shader = THREE.ShaderLib['cube'];
shader.uniforms['tCube'].value = cubemap;
window.skyBoxMaterial = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
});
window.skybox = new THREE.Mesh(
new THREE.BoxGeometry(1000, 1000, 1000),
skyBoxMaterial
);
skybox.position.set(0, 200, 0);
scene.add(skybox);
});