I get a sky box effect of webgl,and want to achieve that with threejs ,but until now ,I do not get it,and I debug the code of threejs ,I found the effect of webgl use texImage2D to do TEXTURE_CUBE,threejs use texStorage2D and texSubImage2D to do that。when use the “ fragColor = texture(u_skybox, normalize( t.xyz / t.w));” to get the color,color is error and is not wanted,so I want to ask a question,How to get effect with threejs,just like webgl example(WebGL 天空盒)。and the different between texImage2D and texStorage2D 、 texSubImage2D。thank you。
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 2000);
camera.position.set(Math.cos(.1), 0, Math.sin( .1));
camera.lookAt(0,0,0)
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.PlaneGeometry(2, 2);
const loader = new THREE.CubeTextureLoader();
loader.setPath( 'examples/textures/cube/Bridge2/' );
loader.load([ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] ,
function (textureCube) {
shader = {
vertexShader:`
in vec3 position;
out vec4 v_position;
void main() {
v_position = vec4(position,0.0);
gl_Position = vec4(position, 1.0);
gl_Position.z = 1.0;
}`,
fragmentShader: `
precision highp float;
precision highp int;
uniform samplerCube u_skybox;
uniform mat4 u_viewDirectionProjectionInverse;
out highp vec4 fragColor;
in vec4 v_position;
void main() {
vec4 t = u_viewDirectionProjectionInverse * v_position;
fragColor = texture(u_skybox, normalize( t.xyz / t.w));
}`,
uniforms: {
"u_skybox": {value: textureCube},
},
}
material = new THREE.RawShaderMaterial({
// material = new THREE.ShaderMaterial({
name: 'SkyShader',
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
side: THREE.FrontSide,
});
material.glslVersion =THREE.GLSL3;
const plane = new THREE.Mesh(geometry, material);
scene.add(plane);
camera.updateWorldMatrix();
const pj = new THREE.Matrix4()
pj.copy(camera.projectionMatrix)
const ps = new THREE.Matrix4()
ps.copy(camera.matrixWorldInverse)
ps[12] = 0;
ps[13] = 0;
ps[14] = 0;
pj.multiply(ps);
shader.uniforms.u_viewDirectionProjectionInverse = {value: pj}
});