comparing methods of creating skybox material in three.js

岁酱吖の 提交于 2019-12-01 06:46:30

问题


When it comes to making skyboxes in three.js, I have seen two different schools of thought. Assuming that we have the code

var imagePrefix = "images/mountains-";
var directions  = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
var imageSuffix = ".jpg";
var skyGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );    

In both methods, one creates a really big cube and applies textures. The difference is whether shaders are used. For example:

Material without using shader:

var materialArray = [];
for (var i = 0; i < 6; i++)
    materialArray.push( new THREE.MeshBasicMaterial({
        map: THREE.ImageUtils.loadTexture( imagePrefix + directions[i] + imageSuffix ),
        side: THREE.BackSide
    }));
var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
scene.add( skyBox );

Material using shader:

var imageURLs = [];
for (var i = 0; i < 6; i++)
    imageURLs.push( imagePrefix + directions[i] + imageSuffix );
var textureCube = THREE.ImageUtils.loadTextureCube( imageURLs );
var shader = THREE.ShaderLib[ "cube" ];
shader.uniforms[ "tCube" ].value = textureCube;
var skyMaterial = new THREE.ShaderMaterial( {
    fragmentShader: shader.fragmentShader,
    vertexShader: shader.vertexShader,
    uniforms: shader.uniforms,
    depthWrite: false,
    side: THREE.BackSide
} );
var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
scene.add( skyBox );

My own informal performance tests show no significant difference in FPS using 2048x2048 images for textures. The shader-free code is easier (at least for me) to understand. Are there situations in which there is an advantage to using the shader-based texture?


回答1:


You have a conceptual misunderstanding.

For WebGL, both methods involve shaders. MeshBasicMaterial has a vertex and fragment shader that has been written for you for convenience.

The primary difference between the two examples is the second example uses a cube map for input.

You would use that approach if you were already using the same cube map as an environment map in a reflective material, for example.

The first example is just another way to render a skybox, and is the only one of the two that will work with CanvasRenderer.

three.js r.58



来源:https://stackoverflow.com/questions/16310880/comparing-methods-of-creating-skybox-material-in-three-js

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