-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshaders.js
189 lines (147 loc) · 3.87 KB
/
shaders.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const defaultVS = `
attribute vec3 in_pos;
attribute vec3 in_normal;
attribute vec4 in_color;
attribute vec2 in_tex0;
uniform mat4 u_world;
uniform mat4 u_view;
uniform mat4 u_proj;
uniform vec4 u_matColor;
uniform vec4 u_surfaceProps;
uniform vec3 u_ambLight;
uniform vec3 u_lightDir;
uniform vec3 u_lightCol;
varying highp vec4 v_color;
varying highp vec2 v_tex0;
void main() {
gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0);
v_tex0 = in_tex0;
v_color = in_color;
v_color.rgb += u_ambLight*u_surfaceProps.x;
vec3 N = mat3(u_world) * in_normal;
float L = max(0.0, dot(N, -normalize(u_lightDir)));
v_color.rgb += L*u_lightCol*u_surfaceProps.z;
v_color = clamp(v_color, 0.0, 1.0);
v_color *= u_matColor;
}
`;
const defaultFS = `
uniform sampler2D tex;
uniform highp float u_alphaRef;
varying highp vec4 v_color;
varying highp vec2 v_tex0;
void main() {
gl_FragColor = v_color*texture2D(tex, v_tex0);
if(gl_FragColor.a < u_alphaRef)
discard;
}
`;
const envVS = `
attribute vec3 in_pos;
attribute vec3 in_normal;
attribute vec4 in_color;
attribute vec2 in_tex0;
uniform mat4 u_world;
uniform mat4 u_view;
uniform mat4 u_proj;
uniform mat4 u_env;
uniform vec4 u_matColor;
uniform vec4 u_surfaceProps;
uniform vec3 u_ambLight;
uniform vec3 u_lightDir;
uniform vec3 u_lightCol;
varying highp vec4 v_color0;
varying highp vec4 v_color1;
varying highp vec2 v_tex0;
varying highp vec2 v_tex1;
void main() {
gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0);
v_tex0 = in_tex0;
v_color0 = in_color;
v_color0.rgb += u_ambLight*u_surfaceProps.x;
vec3 N = mat3(u_world) * in_normal;
float L = max(0.0, dot(N, -normalize(u_lightDir)));
v_color0.rgb += L*u_lightCol*u_surfaceProps.z;
v_color0 = clamp(v_color0, 0.0, 1.0);
v_color0 *= u_matColor;
v_color1 = v_color0*u_surfaceProps.y;
v_tex1 = (u_env*vec4(N, 1.0)).xy;
}
`;
const envFS = `
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform highp float u_alphaRef;
varying highp vec4 v_color0;
varying highp vec4 v_color1;
varying highp vec2 v_tex0;
varying highp vec2 v_tex1;
void main() {
gl_FragColor = v_color0*texture2D(tex0, v_tex0);
if(gl_FragColor.a < u_alphaRef)
discard;
gl_FragColor.rgb += (v_color1*texture2D(tex1, v_tex1)).rgb;
}
`;
const carPS2VS = `
attribute vec3 in_pos;
attribute vec3 in_normal;
attribute vec4 in_color;
attribute vec2 in_tex0;
attribute vec2 in_tex1;
uniform mat4 u_world;
uniform mat4 u_view;
uniform mat4 u_proj;
uniform mat4 u_env;
uniform vec4 u_matColor;
uniform vec4 u_surfaceProps;
uniform vec3 u_ambLight;
uniform vec3 u_lightDir;
uniform vec3 u_lightCol;
varying highp vec4 v_color0;
varying highp vec4 v_color1;
varying highp vec4 v_color2;
varying highp vec2 v_tex0;
varying highp vec2 v_tex1;
varying highp vec2 v_tex2;
void main() {
gl_Position = u_proj * u_view * u_world * vec4(in_pos, 1.0);
v_tex0 = in_tex0;
v_color0 = in_color;
v_color0.rgb += u_ambLight*u_surfaceProps.x;
vec3 N = mat3(u_world) * in_normal;
float L = max(0.0, dot(N, -normalize(u_lightDir)));
v_color0.rgb += L*u_lightCol*u_surfaceProps.z;
v_color0 = clamp(v_color0, 0.0, 1.0);
v_color0 *= u_matColor;
v_tex1 = in_tex1;
v_color1 = vec4(1.5*u_surfaceProps.w);
N = mat3(u_view) * N;
vec3 D = mat3(u_view) * u_lightDir;
N = D - 2.0*N*dot(N, D);
v_tex2 = (N.xy + vec2(1.0, 1.0))/2.0;
if(N.z < 0.0)
v_color2 = vec4(0.75*u_surfaceProps.y);
else
v_color2 = vec4(0.0);
}
`;
const carPS2FS = `
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform highp float u_alphaRef;
varying highp vec4 v_color0;
varying highp vec4 v_color1;
varying highp vec4 v_color2;
varying highp vec2 v_tex0;
varying highp vec2 v_tex1;
varying highp vec2 v_tex2;
void main() {
gl_FragColor = v_color0*texture2D(tex0, v_tex0);
if(gl_FragColor.a < u_alphaRef)
discard;
gl_FragColor.rgb += (v_color1*texture2D(tex1, v_tex1)).rgb;
gl_FragColor.rgb += (v_color2*texture2D(tex2, v_tex2)).rgb;
}
`;