Rotating a skybox in Three.js

对着背影说爱祢 提交于 2021-01-28 01:42:09

问题


I have a skybox created using:

skybox_path = 'images/skybox_images/';
skybox_name = 'Stars_2';

var urls = [
            skybox_path + skybox_name + '_right1.png',
            skybox_path + skybox_name + '_left2.png',
            skybox_path + skybox_name + '_top3.png',
            skybox_path + skybox_name + '_bottom4.png',
            skybox_path + skybox_name + '_front5.png',
            skybox_path + skybox_name + '_back6.png'
          ];

var cubemap = THREE.ImageUtils.loadTextureCube(urls); // load textures
cubemap.format = THREE.RGBFormat;

var shader = THREE.ShaderLib['cube']; // init cube shader from built-in lib
shader.uniforms['tCube'].value = cubemap; // apply textures to shader

// create shader material
var skyBoxMaterial = new THREE.ShaderMaterial( {
  fragmentShader: shader.fragmentShader,
  vertexShader: shader.vertexShader,
  uniforms: shader.uniforms,
  depthWrite: false,
  side: THREE.BackSide
});


// create skybox mesh
var skybox = new THREE.Mesh(
  new THREE.CubeGeometry(1000, 1000, 1000),
  skyBoxMaterial
);

scene.add(skybox);

This works fine, however I now wish to rotate the skybox within the animation loop.

If I use:

skybox.rotation.x += 0.01;

within a render loop, the skybox cube rotates, but the textures don't. I get a very interesting and insightful effect with the cube moving but the textures remaining fixed, but this isn't my desired effect, I'd like the textures to move with the cube.


回答1:


I found the solution, the method I posted in my question for creating the skybox needs to be changed if we want to animate its orientation without unusual perspective effects.

The method to create a skybox which enables it to be rotated is as follows:

    var urls = [
            skybox_path + skybox_name + '_right1.png',
            skybox_path + skybox_name + '_left2.png',
            skybox_path + skybox_name + '_top3.png',
            skybox_path + skybox_name + '_bottom4.png',
            skybox_path + skybox_name + '_front5.png',
            skybox_path + skybox_name + '_back6.png'
          ];

 var materialArray = [];
 for (var i = 0; i < 6; i++)
  materialArray.push( new THREE.MeshBasicMaterial({
   map: THREE.ImageUtils.loadTexture( urls[i] ),
   side: THREE.BackSide
  }));

 var skyGeometry = new THREE.CubeGeometry( 5000, 5000, 5000 );
 var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
 var skybox = new THREE.Mesh( skyGeometry, skyMaterial );

This solved my problem.



来源:https://stackoverflow.com/questions/33787382/rotating-a-skybox-in-three-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!