/
LowPolyPhong.txt
119 lines (90 loc) · 3.12 KB
/
LowPolyPhong.txt
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<shader>{
"name":"LowPolyPhong",
"ubo":[ "UBOTransform", "UBOLighting", "UBOModel" ],
"options": { "modelMatrix":true },
"uniforms":[
{ "name":"u_basecolor", "type":"vec3" }
]
}<\shader>
<materials>[
{ "name":"LowPolyPhong", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "ff7f7f" }
]},
{ "name":"LowPolyPhong_green", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "00ff00" }
]},
{ "name":"LowPolyPhong_yellow", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "FFFF00" }
]},
{ "name":"LowPolyPhong_gray", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "A0A0A0" }
]},
{ "name":"LowPolyPhong_litegray", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "BEBEBE" }
]},
{ "name":"LowPolyPhong_purple", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "666699" }
]},
{ "name":"LowPolyPhong_orange", "uniforms":[
{ "name":"u_basecolor", "type":"rgb", "value": "e68a00" }
]}
]<\materials>
<vertex>
#version 300 es
layout(location=0) in vec3 a_position;
uniform UBOTransform{
mat4 projViewMatrix;
vec3 cameraPos;
float globalTime;
vec2 screenSize;
};
uniform UBOModel{
mat4 modelMatrix;
mat3 normalMatrix;
};
out vec3 v_cameraPos;
out vec3 v_worldPos;
void main(void){
//Need to pass Camera pos turned to WorldSpace avoid inverse
//vCameraPos = (inverse(matCameraView) * vec4(posCamera,1.0)).xyz;
v_cameraPos = cameraPos;
vec4 wpos = modelMatrix * vec4(a_position,1.0);
v_worldPos = wpos.xyz;
gl_Position = projViewMatrix * wpos;
}
<\vertex>
<fragment>
#version 300 es
precision mediump float;
uniform UBOLighting{
vec3 lightPosition;
vec3 lightDirection;
vec3 lightColor;
};
uniform vec3 u_basecolor;
in vec3 v_cameraPos;
in vec3 v_worldPos;
out vec4 oFragColor;
//const vec3 uLightPos = vec3(1.0, 6.0, -4.0);
//const vec3 uLightColor = vec3(1.0, 1.0, 1.0);
const float uAmbientStrength = 0.5;
const float uDiffuseStrength = 0.5;
const float uSpecularStrength = 0.2f; //0.15
const float uSpecularShininess = 1.0f; //256.0
void main(void){
vec3 pixelNorm = normalize( cross( dFdx(v_worldPos), dFdy(v_worldPos) ) ); //Calc the Normal of the Rasterizing Pixel
//Ambient Lighting
vec3 cAmbient = lightColor * uAmbientStrength;
//Diffuse Lighting
vec3 lightVector = normalize(lightPosition - v_worldPos); //light direction based on pixel world position
float diffuseAngle = max( dot(pixelNorm,lightVector) ,0.0); //Angle between Light Direction and Pixel Direction (1==90d)
vec3 cDiffuse = lightColor * diffuseAngle * uDiffuseStrength;
//Specular Lighting
vec3 camVector = normalize(v_cameraPos - v_worldPos); //Camera Direction based on pixel world position
vec3 reflectVector = reflect(-lightVector, pixelNorm); //Reflective direction of line from pixel direction as pivot.
float specular = pow( max( dot(reflectVector,camVector) ,0.0), uSpecularShininess ); //Angle of reflected light and camera eye
vec3 cSpecular = lightColor * specular * uSpecularStrength;
//Final Color
oFragColor = vec4(u_basecolor * (cAmbient + cDiffuse + cSpecular), 1.0);
}
<\fragment>