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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With