dimanche 2 août 2015

Threejs isolated reflection shader using cubeCamera not working

jsFiddle http://ift.tt/1HjBYBV demonstrates my problem.

I'm trying to re-create the reflection effect in ThreeJS using a RawShaderMaterial. There are several examples of this effect, which is pretty standard.

A cubeCamera is created and passed into the shader to provide the samplerCube:

material.uniforms.reflectionSampler.value = cubeCamera.renderTarget;

A reflection vector is calculated in the vertex shader:

varying vec3 vReflect;
...
vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) {
    return normalize( ( vec4( normal, 0.0 ) * matrix ).xyz );
}
...
vec3 worldNormal = transformDirection( objectNormal, modelMatrix );
vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
vReflect = reflect( cameraToVertex, worldNormal );

It's used in the fragment shader to read from the cubeCamera's render:

gl_FragColor = vec4( vNormal.y ) + textureCube( reflectionSampler, vReflect );

However, as you can see in the jsFiddle, the effect does not appear to be working correctly. It appears to be reading the sample position incorrectly from the samplerCube. I've tried fiddling with everything





Aucun commentaire:

Enregistrer un commentaire