I’m new to GLSL and shader, and here is my code:
const vertexShader = `
attribute vec3 aPosition;
attribute vec3 aColor;
varying vec3 vColor;
varying vec3 vNormal;
void main(){
vColor = aColor;
vNormal = normal;
vec3 transformed = position.xyz;
transformed += aPosition;
gl_Position = projectionMatrix* modelViewMatrix * vec4(transformed, 1.);
}
`
const fragmentShader = `
uniform vec3 color;
varying vec3 vColor;
vec3 lightColor = vec3(1.0, 1.0, 1.0);
uniform vec3 lightDirection;
varying vec3 vNormal;
void main(){
vec3 norm = normalize(vNormal);
float nDotL = clamp(dot(-lightDirection, norm), 0.0, 1.0);
vec4 diffuseColor = vec4(lightColor, 1.0) * vec4(vColor, 1.0) * nDotL;
gl_FragColor = vec4( color * vColor, 1.0) * diffuseColor;
}
`
const material = new Three.ShaderMaterial({
uniforms: {
color: { value: new Three.Color(0xffffff) },
lightDirection: { value: new Three.Vector3(1.0, 1.0, 1.0).normalize() }
},
fragmentShader,
vertexShader
})
I want to add directional light to my custom ShaderMaterial(point light and ambient light too, if possible), but I found the result of the above code is strange. The original color I appointed to the mesh is entirely gone, only left with white color. Besides, the light seems too intensive. So does anyone can help me modify my code? Your help is highly appreciated.