问题
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.
回答1:
If you want to use scene lights with ShaderMaterial
, in addition to setting lights: true
, you need to create your uniforms using this pattern:
var uniforms = THREE.UniformsUtils.merge( [
THREE.UniformsLib[ "ambient" ],
THREE.UniformsLib[ "lights" ]
] );
var material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').innerHTML,
fragmentShader: document.getElementById('fragmentShader').innerHTML,
lights: true
} );
Updated fiddle: https://jsfiddle.net/zhkvcajs/3/
three.js r.74
来源:https://stackoverflow.com/questions/35596705/using-lights-in-three-js-shader