-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex-lighting.vert
41 lines (29 loc) · 1006 Bytes
/
vertex-lighting.vert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform mat4 uModelMatrix;
uniform mat4 uViewMatrix;
uniform mat3 uNMatrix;
uniform mat4 uProjectionMatrix;
uniform bool uUseTexture;
varying vec4 vColor;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
uniform vec3 uLightPosition;
uniform vec3 uAmbientColor;
uniform vec3 uDirectionalColor;
uniform vec3 uSpecularColor;
void main(void) {
vec4 mvPosition = uViewMatrix * uModelMatrix * vec4( aVertexPosition, 1.0 );
gl_Position = uProjectionMatrix * mvPosition;
if ( uUseTexture ) {
vTextureCoord = aTextureCoord;
} else {
vColor = aVertexColor;
}
vec3 normal = uNMatrix * aVertexNormal;
vec3 lightDirection = normalize( (uViewMatrix * vec4( uLightPosition, 1 )).xyz - mvPosition.xyz );
float directionalWeighting = max( dot( normal, lightDirection ), 0.0 );
vLightWeighting = uAmbientColor + uDirectionalColor * directionalWeighting;
}