我正在尝试学习 THREE.js,但无法显示一个简单的天空盒。我已经尝试了 100 种不同的方法,但似乎没有任何效果 - 太少的文档与许多不同版本的结合让我完全束手无策。我已经走到了尽头!
我认为这可能与加载图像所需的时间有关,但我尝试放入一些“加载器”代码无济于事(因为它似乎没有做太多,所以将其删除)。我愿意相信我只是没有以“正确的方式”去做——我很难找到适用于当前版本的示例。
<html>
<head> <title>Skybox Demo</title> <style>canvas { width: 100%; height: 100% }</style>
<body>
<script src="https://raw.github.com/mrdoob/three.js/master/build/three.js"></script>
<script>
var container;
var renderer;
var cameraCube, sceneCube;
var skyboxMesh;
var cubeTarget;
var mouseX = 0;
var mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
container = document.createElement('div');
document.body.appendChild(container);
cameraCube = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
sceneCube = new THREE.Scene();
// Load cube textures
var path = "textures/cube/";
var format = '.jpg';
var urls = [ path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format ];
var skyCubeTexture = THREE.ImageUtils.loadTextureCube(urls);
skyCubeTexture.format = THREE.RGBFormat;
// Cube shader
var shader = THREE.ShaderUtils.lib["cube"];
shader.uniforms["tCube"].texture = skyCubeTexture;
var material = new THREE.ShaderMaterial({
fragmentShader : shader.fragmentShader,
vertexShader : shader.vertexShader,
uniforms : shader.uniforms,
depthWrite : false,
side : THREE.BackSide
});
var skyboxMesh = new THREE.Mesh( new THREE.CubeGeometry(100, 100, 100), material);
sceneCube.add(skyboxMesh);
// Renderer
renderer = new THREE.WebGLRenderer( {antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.autoClear = false;
container.appendChild( renderer.domElement );
}
function animate() {
render();
requestAnimationFrame( animate );
}
function render() {
var timer = - new Date().getTime() * 0.0002;
cameraCube.position.x = 10 * Math.cos( timer );
cameraCube.position.z = 10 * Math.sin( timer );
cameraCube.lookAt({x:0, y:0, z:0});
renderer.clear();
renderer.render(sceneCube, cameraCube);
}
</script>
</body>
</html>