Search code examples
javascriptwebglthree.jsskybox

three.js skybox assigned to camera


I'm trying to make skybox assigned to player camera. When camera moves(also skybox moves with it), texture get stretched. How to get rid of this?

Code:

var textureCube = THREE.ImageUtils.loadTextureCube( urls );
textureCube.format = THREE.RGBFormat;
var shader = THREE.ShaderUtils.lib[ "cube" ];
shader.uniforms[ "tCube" ].value = textureCube;

cubematerial = new THREE.ShaderMaterial({
    fragmentShader: shader.fragmentShader,
    vertexShader: shader.vertexShader,
    uniforms: shader.uniforms,
    depthWrite: false,
    side: THREE.BackSide
});
skyBox = new THREE.Mesh(new THREE.CubeGeometry(1000,1000,1000), cubematerial);
camera.add(skyBox);

Solution

  • So, after digging into Three.js examples, I found a way how to do this. http://learningthreejs.com/blog/2011/08/15/lets-do-a-sky/ is outdated. A way used in examples is to add skybox into second scene with fixed camera, and render both scenes. Look at webgl_materials_cars.html example.

    Also because I use 3rd person camera assigned to character, I must get world rotation from character camera to skybox camera. This can be done on render with:

    function render(){
    <...>
        skyboxCamera.rotation.setEulerFromRotationMatrix( new THREE.Matrix4().extractRotation( camera.matrixWorld ), skyboxCamera.eulerOrder );
        renderer.render(skyboxScene, skyboxCamera);
        renderer.render(scene, camera);
    <...>
    }