Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lights in three.js shader

I'm trying to access the scene's lights from a shader in three.js.

This question is nearly a duplicate of Three.js ShaderMaterial issue with lights but the comments on that question aren't helping me resolve the issue.

Here is the vertex shader:

#if NUM_DIR_LIGHTS > 0
    struct DirectionalLight {
        vec3 direction;
        vec3 color;
        int shadow;
        float shadowBias;
        float shadowRadius;
        vec2 shadowMapSize;
     };
     uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
#endif
  varying vec3 color;
  void main() {
     float r = directionalLights[0].color.r;
     color = vec3(r,1.0,0.0);
     gl_Position = projectionMatrix * modelViewMatrix * vec4(position , 1.0);
  }

and the relevant ShaderMaterial:

var material = new THREE.ShaderMaterial({
    uniforms: THREE.UniformsLib['lights'],
    vertexShader: document.getElementById('vertexShader').innerHTML,
    fragmentShader: document.getElementById('fragmentShader').innerHTML,
    lights : true
  });

I've posted the entire example code here: https://jsfiddle.net/zhkvcajs/

Removing the lights : true renders a green knot, but it's not getting the directionalLights information that should change the knot's color. Apparently, lights : true is required for that, but causes an error.

like image 909
turtlegraphics Avatar asked Feb 24 '16 08:02

turtlegraphics


Video Answer


1 Answers

If you want to use scene lights with ShaderMaterial, you need to set lights: true.

var material = new THREE.ShaderMaterial( {
    uniforms: uniforms,
    vertexShader: document.getElementById('vertexShader').innerHTML,
    fragmentShader: document.getElementById('fragmentShader').innerHTML,
    lights: true
} );

three.js r.126

like image 90
WestLangley Avatar answered Sep 30 '22 12:09

WestLangley