forked from creikey/rpgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreedee.glsl
395 lines (294 loc) · 9.14 KB
/
threedee.glsl
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
@module threedee
@ctype mat4 Mat4
@ctype vec4 Vec4
@ctype vec3 Vec3
@ctype vec2 Vec2
// for this block, define a variable called `model_space_pos` to be used as an input
@block vs_compute_light_output
world_space_frag_pos = model * vec4(model_space_pos, 1.0);
vec4 frag_pos = view * world_space_frag_pos;
//@Speed I think we can just take the third row here and be fine.
light_dir = normalize(inverse(directional_light_space_matrix) * vec4(0.0, 0.0, -1.0, 0.0)).xyz;
light_space_fragment_position = directional_light_space_matrix * vec4(world_space_frag_pos.xyz, 1.0);
@end
@vs vs_skeleton
in vec3 pos_in;
in vec2 uv_in;
in vec4 indices_in; // is a sokol SG_VERTEXFORMAT_USHORT4N, a 16 bit unsigned integer treated as a floating point number due to webgl compatibility
in vec4 weights_in;
out vec3 pos;
out vec2 uv;
out vec4 light_space_fragment_position;
out vec3 light_dir;
out vec4 world_space_frag_pos;
uniform skeleton_vs_params {
mat4 model;
mat4 view;
mat4 projection;
mat4 directional_light_space_matrix;
vec2 bones_tex_size;
};
uniform sampler2D bones_tex;
float decode_normalized_float32(vec4 v)
{
float sign = 2.0 * v.x - 1.0;
return sign * (v.z*255.0 + v.y);
}
void main() {
vec4 total_position = vec4(0.0f);
for(int bone_influence_index = 0; bone_influence_index < 4; bone_influence_index++)
{
float index_float = indices_in[bone_influence_index];
int index = int(index_float * 65535.0);
float weight = weights_in[bone_influence_index];
float y_coord = (0.5 + index)/bones_tex_size.y;
mat4 bone_mat;
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < 4; col++)
{
bone_mat[col][row] = decode_normalized_float32(texture(bones_tex, vec2((0.5 + col*4 + row)/bones_tex_size.x, y_coord)));
}
}
vec4 local_position = bone_mat * vec4(pos_in, 1.0f);
total_position += local_position * weight;
}
gl_Position = projection * view * model * total_position;
//gl_Position = projection * view * model * vec4(pos_in, 1.0);
pos = gl_Position.xyz;
uv = uv_in;
vec3 model_space_pos = (total_position).xyz;
@include_block vs_compute_light_output
}
@end
@vs vs
in vec3 pos_in;
in vec2 uv_in;
out vec3 pos;
out vec2 uv;
out vec4 light_space_fragment_position;
out vec3 light_dir;
out vec4 world_space_frag_pos;
uniform vs_params {
mat4 model;
mat4 view;
mat4 projection;
mat4 directional_light_space_matrix;
};
void main() {
pos = pos_in;
uv = uv_in;
gl_Position = projection * view * model * vec4(pos_in, 1.0);
vec3 model_space_pos = (vec4(pos_in, 1.0f)).xyz;
@include_block vs_compute_light_output
}
@end
@fs fs
uniform sampler2D tex;
uniform sampler2D shadow_map;
uniform fs_params {
int shadow_map_dimension;
};
in vec3 pos;
in vec2 uv;
in vec4 light_space_fragment_position;
in vec3 light_dir;
in vec4 world_space_frag_pos;
out vec4 frag_color;
float decodeDepth(vec4 rgba) {
return dot(rgba, vec4(1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0));
}
float do_shadow_sample(sampler2D shadowMap, vec2 uv, float scene_depth, float n_dot_l) {
{
//WebGL does not support GL_CLAMP_TO_BORDER, or border colors at all it seems, so we have to check explicitly.
//This will probably slow down other versions which do support texture borders, but the current system does
// not provide a non-overly complex way to include/not-include this code based on the backend. So here it is.
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
return 1.0;
}
float map_depth = decodeDepth(texture(shadowMap, uv));
// float bias = max(0.03f * (1.0f - n_dot_l), 0.005f);
// bias = clamp(bias, 0.0, 0.01);
float offset_scale_N = sqrt(1 - n_dot_l*n_dot_l);
float offset_scale_L = offset_scale_N / n_dot_l;
float bias = 0.0004 * offset_scale_N + 0.0001 * offset_scale_L;
map_depth += bias;
return step(scene_depth, map_depth);
}
float bilinear_shadow_sample(sampler2D shadowMap, vec2 uv, int texture_width, int texture_height, float scene_depth_light_space, float n_dot_l) {
vec2 texture_dim = vec2(float(texture_width), float(texture_height));
vec2 texel_dim = vec2(1.0 / float(texture_width ), 1.0 / float(texture_height));
vec2 texel_uv = uv * vec2(texture_dim);
vec2 texel_uv_floor = floor(texel_uv) * texel_dim;
vec2 texel_uv_ceil = ceil(texel_uv) * texel_dim;
vec2 uv_0 = texel_uv_floor;
vec2 uv_1 = vec2(texel_uv_ceil.x , texel_uv_floor.y);
vec2 uv_2 = vec2(texel_uv_floor.x, texel_uv_ceil.y );
vec2 uv_3 = vec2(texel_uv_ceil.x , texel_uv_ceil.y );
float bl = do_shadow_sample(shadowMap, uv_0, scene_depth_light_space, n_dot_l);
float br = do_shadow_sample(shadowMap, uv_1, scene_depth_light_space, n_dot_l);
float tl = do_shadow_sample(shadowMap, uv_2, scene_depth_light_space, n_dot_l);
float tr = do_shadow_sample(shadowMap, uv_3, scene_depth_light_space, n_dot_l);
vec2 interp = fract(texel_uv);
float bot = mix(bl, br, interp.x);
float top = mix(tl, tr, interp.x);
float result = mix(bot, top, interp.y);
return result;
}
float calculate_shadow_factor(sampler2D shadowMap, vec4 light_space_fragment_position, float n_dot_l) {
float shadow = 1.0;
vec3 projected_coords = light_space_fragment_position.xyz / light_space_fragment_position.w;
if(projected_coords.z > 1.0)
return shadow;
projected_coords = projected_coords * 0.5f + 0.5f;
float current_depth = projected_coords.z;
vec2 shadow_uv = projected_coords.xy;
float texel_step_size = 1.0 / float(shadow_map_dimension);
for (int x=-2; x<=2; x++) {
for (int y=-2; y<=2; y++) {
vec2 off = vec2(x*texel_step_size, y*texel_step_size);
// shadow += do_shadow_sample(shadowMap, shadow_uv+off, current_depth);
shadow += bilinear_shadow_sample(shadowMap, shadow_uv+off, shadow_map_dimension, shadow_map_dimension, current_depth, n_dot_l);
}
}
shadow /= 25.0;
return shadow;
}
void main() {
vec4 col = texture(tex, uv);
if(col.a < 0.5)
{
discard;
}
else
{
vec3 normal = normalize(cross(dFdx(world_space_frag_pos.xyz), dFdy(world_space_frag_pos.xyz)));
float n_dot_l = clamp(dot(normal, light_dir), 0.0, 1.0);
float shadow_factor = calculate_shadow_factor(shadow_map, light_space_fragment_position, n_dot_l);
float lighting_factor = shadow_factor * n_dot_l;
lighting_factor = lighting_factor * 0.5 + 0.5;
frag_color = vec4(col.rgb*lighting_factor, 1.0);
//frag_color = vec4(col.rgb, 1.0);
}
}
@end
@fs fs_shadow_mapping
uniform sampler2D tex;
in vec3 pos;
in vec2 uv;
in vec4 light_space_fragment_position;
in vec3 light_dir;
in vec4 world_space_frag_pos;
out vec4 frag_color;
vec4 encodeDepth(float v) {
vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * v;
enc = fract(enc);
enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
return enc;
}
void main() {
vec4 col = texture(tex, uv);
if(col.a < 0.5)
{
discard;
}
float depth = gl_FragCoord.z;
frag_color = encodeDepth(depth);
}
@end
@vs vs_twodee
in vec3 position;
in vec2 texcoord0;
out vec2 uv;
out vec2 pos;
void main() {
gl_Position = vec4(position.xyz, 1.0);
uv = texcoord0;
pos = position.xy;
}
@end
@fs fs_twodee
uniform sampler2D twodee_tex;
uniform twodee_fs_params {
vec4 tint;
// both in clip space
vec2 clip_ul;
vec2 clip_lr;
float alpha_clip_threshold;
vec2 tex_size;
};
in vec2 uv;
in vec2 pos;
out vec4 frag_color;
void main() {
// clip space is from [-1,1] [left, right]of screen on X, and [-1,1] [bottom, top] of screen on Y
if(pos.x < clip_ul.x || pos.x > clip_lr.x || pos.y < clip_lr.y || pos.y > clip_ul.y) discard;
frag_color = texture(twodee_tex, uv) * tint;
if(frag_color.a <= alpha_clip_threshold)
{
discard;
}
//frag_color = vec4(pos.x,0.0,0.0,1.0);
}
@end
@fs fs_twodee_outline
uniform sampler2D twodee_tex;
uniform twodee_fs_params {
vec4 tint;
// both in clip space
vec2 clip_ul;
vec2 clip_lr;
float alpha_clip_threshold;
vec2 tex_size;
};
in vec2 uv;
in vec2 pos;
out vec4 frag_color;
void main() {
// clip space is from [-1,1] [left, right]of screen on X, and [-1,1] [bottom, top] of screen on Y
if(pos.x < clip_ul.x || pos.x > clip_lr.x || pos.y < clip_lr.y || pos.y > clip_ul.y) discard;
float left = texture(twodee_tex, uv + vec2(-1, 0)/tex_size).a;
float right = texture(twodee_tex, uv + vec2(1, 0)/tex_size).a;
float up = texture(twodee_tex, uv + vec2(0, 1)/tex_size).a;
float down = texture(twodee_tex, uv + vec2(0, -1)/tex_size).a;
if(
false
|| left > 0.1 && right < 0.1
|| left < 0.1 && right > 0.1
|| up < 0.1 && down > 0.1
|| up > 0.1 && down < 0.1
)
{
frag_color = vec4(1.0);
}
else
{
frag_color = vec4(0.0);
}
}
@end
@fs fs_outline
uniform sampler2D tex;
in vec3 pos;
in vec2 uv;
in vec4 light_space_fragment_position;
in vec3 light_dir;
in vec4 world_space_frag_pos;
out vec4 frag_color;
void main() {
vec4 col = texture(tex, uv);
if(col.a < 0.5)
{
discard;
}
frag_color = vec4(vec3(1.0), col.a);
}
@end
@program mesh vs fs
@program armature vs_skeleton fs
@program mesh_shadow_mapping vs fs_shadow_mapping
@program armature_shadow_mapping vs_skeleton fs_shadow_mapping
@program mesh_outline vs fs_outline
@program armature_outline vs_skeleton fs_outline
@program twodee vs_twodee fs_twodee
@program twodee_outline vs_twodee fs_twodee_outline