Debian Codegraph @

servers/rendering/renderer_rd/shaders/canvas.glsl

raw
1 #[vertex]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 #ifdef USE_ATTRIBUTES
8 layout(location = 0) in vec2 vertex_attrib;
9 layout(location = 3) in vec4 color_attrib;
10 layout(location = 4) in vec2 uv_attrib;
11
12 #if defined(CUSTOM0_USED)
13 layout(location = 6) in vec4 custom0_attrib;
14 #endif
15
16 #if defined(CUSTOM1_USED)
17 layout(location = 7) in vec4 custom1_attrib;
18 #endif
19
20 layout(location = 10) in uvec4 bone_attrib;
21 layout(location = 11) in vec4 weight_attrib;
22
23 #endif
24
25 #include "canvas_uniforms_inc.glsl"
26
27 layout(location = 0) out vec4 uv_vertex_interp;
28 layout(location = 1) out vec4 color_interp;
29
30 #ifndef USE_ATTRIBUTES
31 // Varyings so the per-instance info can be used in the fragment shader
32 layout(location = 2) out flat vec4 varying_A;
33 layout(location = 3) out flat uvec4 varying_B;
34 layout(location = 4) out flat uvec4 varying_C;
35
36 #ifdef USE_NINEPATCH
37 layout(location = 5) out flat vec4 varying_D;
38 layout(location = 6) out flat vec4 varying_E;
39 layout(location = 7) out flat vec4 varying_F;
40 layout(location = 8) out vec2 pixel_size_interp;
41 #endif // USE_NINEPATCH
42 #endif // !USE_ATTRIBUTES
43
44 #define read_draw_data_color_texture_pixel_size params.color_texture_pixel_size
45
46 #ifdef USE_ATTRIBUTES
47
48 #define read_draw_data_world_x params.world_x
49 #define read_draw_data_world_y params.world_y
50 #define read_draw_data_world_ofs params.world_ofs
51 #define read_draw_data_modulation params.modulation
52 #define read_draw_data_flags params.flags
53 #define read_draw_data_instance_offset params.instance_uniforms_ofs
54 #define read_draw_data_lights params.lights
55
56 #else // !USE_ATTRIBUTES
57
58 layout(location = 8) in vec4 attrib_A;
59 layout(location = 9) in vec4 attrib_B;
60 layout(location = 10) in vec4 attrib_C;
61 layout(location = 11) in vec4 attrib_D;
62 layout(location = 12) in vec4 attrib_E;
63 #ifdef USE_PRIMITIVE
64 layout(location = 13) in uvec4 attrib_F;
65 #else // !USE_PRIMITIVE
66 layout(location = 13) in vec4 attrib_F;
67 #endif // USE_PRIMITIVE
68 layout(location = 14) in uvec4 attrib_G;
69 layout(location = 15) in uvec4 attrib_H;
70
71 #define read_draw_data_world_x attrib_A.xy
72 #define read_draw_data_world_y attrib_A.zw
73 #define read_draw_data_world_ofs attrib_B.xy
74
75 #ifdef USE_PRIMITIVE
76
77 #define read_draw_data_point_a attrib_C.xy
78 #define read_draw_data_point_b attrib_C.zw
79 #define read_draw_data_point_c attrib_D.xy
80 #define read_draw_data_uv_a attrib_D.zw
81 #define read_draw_data_uv_b attrib_E.xy
82 #define read_draw_data_uv_c attrib_E.zw
83
84 #define read_draw_data_color_a_rg attrib_F.x
85 #define read_draw_data_color_a_ba attrib_F.y
86 #define read_draw_data_color_b_rg attrib_F.z
87 #define read_draw_data_color_b_ba attrib_F.w
88 #define read_draw_data_color_c_rg attrib_G.x
89 #define read_draw_data_color_c_ba attrib_G.y
90
91 #else // !USE_PRIMITIVE
92
93 #define read_draw_data_ninepatch_pixel_size (attrib_B.zw)
94 #define read_draw_data_modulation attrib_C
95 #define read_draw_data_ninepatch_margins attrib_D
96 #define read_draw_data_dst_rect attrib_E
97 #define read_draw_data_src_rect attrib_F
98
99 #endif // USE_PRIMITIVE
100
101 #define read_draw_data_flags attrib_G.z
102 #define read_draw_data_instance_offset attrib_G.w
103 #define read_draw_data_lights attrib_H
104
105 #endif // USE_ATTRIBUTES
106
107 #ifdef MATERIAL_UNIFORMS_USED
108 /* clang-format off */
109 layout(set = 1, binding = 0, std140) uniform MaterialUniforms {
110 #MATERIAL_UNIFORMS
111 } material;
112 /* clang-format on */
113 #endif
114
115 #GLOBALS
116
117 #ifdef USE_ATTRIBUTES
118 vec3 srgb_to_linear(vec3 color) {
119 return mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045)));
120 }
121 #endif
122
123 void main() {
124 #ifndef USE_ATTRIBUTES
125 varying_A = vec4(read_draw_data_world_x, read_draw_data_world_y);
126 #ifdef USE_PRIMITIVE
127 varying_B = uvec4(read_draw_data_flags, read_draw_data_instance_offset, 0.0, 0.0);
128 #else
129 varying_B = uvec4(read_draw_data_flags, read_draw_data_instance_offset, packHalf2x16(read_draw_data_src_rect.xy), packHalf2x16(read_draw_data_src_rect.zw));
130 #endif
131 varying_C = read_draw_data_lights;
132 #ifdef USE_NINEPATCH
133 varying_D = read_draw_data_ninepatch_margins;
134 varying_E = vec4(read_draw_data_dst_rect.z, read_draw_data_dst_rect.w, read_draw_data_ninepatch_pixel_size.x, read_draw_data_ninepatch_pixel_size.y);
135 varying_F = read_draw_data_src_rect;
136 #endif // USE_NINEPATCH
137 #endif // !USE_ATTRIBUTES
138
139 vec4 instance_custom = vec4(0.0);
140 #if defined(CUSTOM0_USED)
141 vec4 custom0 = vec4(0.0);
142 #endif
143 #if defined(CUSTOM1_USED)
144 vec4 custom1 = vec4(0.0);
145 #endif
146
147 #ifdef USE_PRIMITIVE
148
149 //weird bug,
150 //this works
151 vec2 vertex;
152 vec2 uv;
153 vec4 color;
154
155 if (gl_VertexIndex == 0) {
156 vertex = read_draw_data_point_a;
157 uv = read_draw_data_uv_a;
158 color = vec4(unpackHalf2x16(read_draw_data_color_a_rg), unpackHalf2x16(read_draw_data_color_a_ba));
159 } else if (gl_VertexIndex == 1) {
160 vertex = read_draw_data_point_b;
161 uv = read_draw_data_uv_b;
162 color = vec4(unpackHalf2x16(read_draw_data_color_b_rg), unpackHalf2x16(read_draw_data_color_b_ba));
163 } else {
164 vertex = read_draw_data_point_c;
165 uv = read_draw_data_uv_c;
166 color = vec4(unpackHalf2x16(read_draw_data_color_c_rg), unpackHalf2x16(read_draw_data_color_c_ba));
167 }
168
169 uvec4 bones = uvec4(0, 0, 0, 0);
170 vec4 bone_weights = vec4(0.0);
171
172 #elif defined(USE_ATTRIBUTES)
173
174 vec2 vertex = vertex_attrib;
175 vec4 color = color_attrib;
176 if (bool(canvas_data.flags & CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR)) {
177 color.rgb = srgb_to_linear(color.rgb);
178 }
179 color *= read_draw_data_modulation;
180 vec2 uv = uv_attrib;
181
182 #if defined(CUSTOM0_USED)
183 custom0 = custom0_attrib;
184 #endif
185
186 #if defined(CUSTOM1_USED)
187 custom1 = custom1_attrib;
188 #endif
189
190 uvec4 bones = bone_attrib;
191 vec4 bone_weights = weight_attrib;
192 #else // !USE_ATTRIBUTES
193
194 vec2 vertex_base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
195 vec2 vertex_base = vertex_base_arr[gl_VertexIndex];
196
197 vec2 uv = read_draw_data_src_rect.xy + abs(read_draw_data_src_rect.zw) * ((read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT) != 0 ? vertex_base.yx : vertex_base.xy);
198 vec4 color = read_draw_data_modulation;
199 vec2 vertex = read_draw_data_dst_rect.xy + abs(read_draw_data_dst_rect.zw) * mix(vertex_base, vec2(1.0, 1.0) - vertex_base, lessThan(read_draw_data_src_rect.zw, vec2(0.0, 0.0)));
200 uvec4 bones = uvec4(0, 0, 0, 0);
201
202 #endif // USE_ATTRIBUTES
203
204 mat4 model_matrix = mat4(vec4(read_draw_data_world_x, 0.0, 0.0), vec4(read_draw_data_world_y, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(read_draw_data_world_ofs, 0.0, 1.0));
205
206 #ifdef USE_ATTRIBUTES
207
208 uint instancing = params.batch_flags & BATCH_FLAGS_INSTANCING_MASK;
209
210 if (instancing > 1) {
211 // trails
212
213 uint stride = 2 + 1 + 1; //particles always uses this format
214
215 uint trail_size = instancing;
216
217 uint offset = trail_size * stride * gl_InstanceIndex;
218
219 vec4 pcolor;
220 vec2 new_vertex;
221 {
222 uint boffset = offset + bone_attrib.x * stride;
223 new_vertex = (vec4(vertex, 0.0, 1.0) * mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy * weight_attrib.x;
224 pcolor = transforms.data[boffset + 2] * weight_attrib.x;
225 }
226 if (weight_attrib.y > 0.001) {
227 uint boffset = offset + bone_attrib.y * stride;
228 new_vertex += (vec4(vertex, 0.0, 1.0) * mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy * weight_attrib.y;
229 pcolor += transforms.data[boffset + 2] * weight_attrib.y;
230 }
231 if (weight_attrib.z > 0.001) {
232 uint boffset = offset + bone_attrib.z * stride;
233 new_vertex += (vec4(vertex, 0.0, 1.0) * mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy * weight_attrib.z;
234 pcolor += transforms.data[boffset + 2] * weight_attrib.z;
235 }
236 if (weight_attrib.w > 0.001) {
237 uint boffset = offset + bone_attrib.w * stride;
238 new_vertex += (vec4(vertex, 0.0, 1.0) * mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy * weight_attrib.w;
239 pcolor += transforms.data[boffset + 2] * weight_attrib.w;
240 }
241
242 instance_custom = transforms.data[offset + 3];
243
244 vertex = new_vertex;
245 color *= pcolor;
246 } else if (instancing == 1) {
247 uint stride = 2 + bitfieldExtract(params.batch_flags, BATCH_FLAGS_INSTANCING_HAS_COLORS_SHIFT, 1) + bitfieldExtract(params.batch_flags, BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA_SHIFT, 1);
248
249 uint offset = stride * gl_InstanceIndex;
250
251 mat4 matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
252 offset += 2;
253
254 if (bool(params.batch_flags & BATCH_FLAGS_INSTANCING_HAS_COLORS)) {
255 color *= transforms.data[offset];
256 offset += 1;
257 }
258
259 if (bool(params.batch_flags & BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA)) {
260 instance_custom = transforms.data[offset];
261 }
262
263 matrix = transpose(matrix);
264 model_matrix = model_matrix * matrix;
265 }
266 #endif // USE_ATTRIBUTES
267
268 float point_size = 1.0;
269
270 #ifdef USE_WORLD_VERTEX_COORDS
271 vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
272 #endif
273 {
274 #CODE : VERTEX
275 }
276
277 #ifdef USE_NINEPATCH
278 pixel_size_interp = abs(read_draw_data_dst_rect.zw) * vertex_base;
279 #endif
280
281 #if !defined(SKIP_TRANSFORM_USED) && !defined(USE_WORLD_VERTEX_COORDS)
282 vertex = (model_matrix * vec4(vertex, 0.0, 1.0)).xy;
283 #endif
284
285 color_interp = color;
286
287 vertex = (canvas_data.canvas_transform * vec4(vertex, 0.0, 1.0)).xy;
288
289 if (canvas_data.use_pixel_snap) {
290 vertex = floor(vertex + 0.5);
291 // precision issue on some hardware creates artifacts within texture
292 // offset uv by a small amount to avoid
293 uv += 1e-5;
294 }
295
296 uv_vertex_interp = vec4(uv, vertex);
297
298 gl_Position = canvas_data.screen_transform * vec4(vertex, 0.0, 1.0);
299
300 #ifdef USE_POINT_SIZE
301 gl_PointSize = point_size;
302 #endif
303 }
304
305 #[fragment]
306
307 #version 450
308
309 #VERSION_DEFINES
310
311 #include "canvas_uniforms_inc.glsl"
312
313 layout(location = 0) in vec4 uv_vertex_interp;
314 layout(location = 1) in vec4 color_interp;
315
316 #define read_draw_data_color_texture_pixel_size params.color_texture_pixel_size
317
318 #ifdef USE_ATTRIBUTES
319
320 #define read_draw_data_world_x params.world_x
321 #define read_draw_data_world_y params.world_y
322 #define read_draw_data_flags params.flags
323 #define read_draw_data_instance_offset params.instance_uniforms_ofs
324 #define read_draw_data_lights params.lights
325
326 #else // !USE_ATTRIBUTES
327
328 // Can all be flat as they are the same for the whole batched instance
329 layout(location = 2) in flat vec4 varying_A;
330
331 #define read_draw_data_world_x varying_A.xy
332 #define read_draw_data_world_y varying_A.zw
333
334 layout(location = 3) in flat uvec4 varying_B;
335 layout(location = 4) in flat uvec4 varying_C;
336 #define read_draw_data_flags varying_B.x
337 #define read_draw_data_instance_offset varying_B.y
338 #define read_draw_data_src_rect (varying_B.zw)
339 #define read_draw_data_lights varying_C
340
341 #ifdef USE_NINEPATCH
342 layout(location = 5) in flat vec4 varying_D;
343 layout(location = 6) in flat vec4 varying_E;
344 layout(location = 7) in flat vec4 varying_F;
345 layout(location = 8) in vec2 pixel_size_interp;
346 #define read_draw_data_ninepatch_margins varying_D
347 #define read_draw_data_dst_rect_z varying_E.x
348 #define read_draw_data_dst_rect_w varying_E.y
349 #define read_draw_data_ninepatch_pixel_size (varying_E.zw)
350 #define read_draw_data_src_rect_ninepatch (varying_F);
351
352 #endif // USE_NINEPATCH
353
354 #endif // USE_ATTRIBUTES
355
356 layout(location = 0) out vec4 frag_color;
357
358 #ifdef MATERIAL_UNIFORMS_USED
359 /* clang-format off */
360 layout(set = 1, binding = 0, std140) uniform MaterialUniforms {
361 #MATERIAL_UNIFORMS
362 } material;
363 /* clang-format on */
364 #endif
365
366 vec2 screen_uv_to_sdf(vec2 p_uv) {
367 return canvas_data.screen_to_sdf * p_uv;
368 }
369
370 float texture_sdf(vec2 p_sdf) {
371 vec2 uv = p_sdf * canvas_data.sdf_to_tex.xy + canvas_data.sdf_to_tex.zw;
372 float d = texture(sampler2D(sdf_texture, SAMPLER_LINEAR_CLAMP), uv).r;
373 d *= SDF_MAX_LENGTH;
374 return d * canvas_data.tex_to_sdf;
375 }
376
377 vec2 texture_sdf_normal(vec2 p_sdf) {
378 vec2 uv = p_sdf * canvas_data.sdf_to_tex.xy + canvas_data.sdf_to_tex.zw;
379
380 const float EPSILON = 0.001;
381 return normalize(vec2(
382 texture(sampler2D(sdf_texture, SAMPLER_LINEAR_CLAMP), uv + vec2(EPSILON, 0.0)).r - texture(sampler2D(sdf_texture, SAMPLER_LINEAR_CLAMP), uv - vec2(EPSILON, 0.0)).r,
383 texture(sampler2D(sdf_texture, SAMPLER_LINEAR_CLAMP), uv + vec2(0.0, EPSILON)).r - texture(sampler2D(sdf_texture, SAMPLER_LINEAR_CLAMP), uv - vec2(0.0, EPSILON)).r));
384 }
385
386 vec2 sdf_to_screen_uv(vec2 p_sdf) {
387 return p_sdf * canvas_data.sdf_to_screen;
388 }
389
390 // Emulate textureProjLod by doing it manually because the source texture is not an actual depth texture that can be used for this operation.
391 // Since the sampler is configured to nearest use one textureGather tap to emulate bilinear.
392 float texture_shadow(vec4 p) {
393 // Manually round p to the nearest texel because textureGather uses strange rounding rules.
394 vec2 unit_p = floor(p.xy / canvas_data.shadow_pixel_size) * canvas_data.shadow_pixel_size;
395 float depth = p.z;
396 float fx = fract(p.x / canvas_data.shadow_pixel_size);
397 vec2 tap = textureGather(sampler2D(shadow_atlas_texture, shadow_sampler), unit_p.xy).zw;
398 return mix(step(tap.y, depth), step(tap.x, depth), fx);
399 }
400
401 #GLOBALS
402
403 #ifdef LIGHT_CODE_USED
404
405 vec4 light_compute(
406 vec3 light_vertex,
407 vec3 light_position,
408 vec3 normal,
409 vec4 light_color,
410 float light_energy,
411 vec4 specular_shininess,
412 inout vec4 shadow_modulate,
413 vec2 screen_uv,
414 vec2 uv,
415 vec4 color, bool is_directional) {
416 vec4 light = vec4(0.0);
417 vec3 light_direction = vec3(0.0);
418
419 if (is_directional) {
420 light_direction = normalize(mix(vec3(light_position.xy, 0.0), vec3(0, 0, 1), light_position.z));
421 light_position = vec3(0.0);
422 } else {
423 light_direction = normalize(light_position - light_vertex);
424 }
425
426 #CODE : LIGHT
427
428 return light;
429 }
430
431 #endif
432
433 #ifdef USE_NINEPATCH
434
435 float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) {
436 float tex_size = 1.0 / tex_pixel_size;
437
438 if (pixel < margin_begin) {
439 return pixel * tex_pixel_size;
440 } else if (pixel >= draw_size - margin_end) {
441 return (tex_size - (draw_size - pixel)) * tex_pixel_size;
442 } else {
443 draw_center -= 1 - int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER_SHIFT, 1));
444
445 // np_repeat is passed as uniform using NinePatchRect::AxisStretchMode enum.
446 if (np_repeat == 0) { // Stretch.
447 // Convert to ratio.
448 float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end);
449 // Scale to source texture.
450 return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size;
451 } else if (np_repeat == 1) { // Tile.
452 // Convert to offset.
453 float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end);
454 // Scale to source texture.
455 return (margin_begin + ofs) * tex_pixel_size;
456 } else if (np_repeat == 2) { // Tile Fit.
457 // Calculate scale.
458 float src_area = draw_size - margin_begin - margin_end;
459 float dst_area = tex_size - margin_begin - margin_end;
460 float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5));
461 // Convert to ratio.
462 float ratio = (pixel - margin_begin) / src_area;
463 ratio = mod(ratio * scale, 1.0);
464 // Scale to source texture.
465 return (margin_begin + ratio * dst_area) * tex_pixel_size;
466 } else { // Shouldn't happen, but silences compiler warning.
467 return 0.0;
468 }
469 }
470 }
471
472 #endif
473
474 vec3 light_normal_compute(vec3 light_vec, vec3 normal, vec3 base_color, vec3 light_color, vec4 specular_shininess, bool specular_shininess_used) {
475 float cNdotL = max(0.0, dot(normal, light_vec));
476
477 if (specular_shininess_used) {
478 //blinn
479 vec3 view = vec3(0.0, 0.0, 1.0); // not great but good enough
480 vec3 half_vec = normalize(view + light_vec);
481
482 float cNdotV = max(dot(normal, view), 0.0);
483 float cNdotH = max(dot(normal, half_vec), 0.0);
484 float cVdotH = max(dot(view, half_vec), 0.0);
485 float cLdotH = max(dot(light_vec, half_vec), 0.0);
486 float shininess = exp2(15.0 * specular_shininess.a + 1.0) * 0.25;
487 float blinn = pow(cNdotH, shininess);
488 blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI));
489 float s = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75);
490
491 return specular_shininess.rgb * light_color * s + light_color * base_color * cNdotL;
492 } else {
493 return light_color * base_color * cNdotL;
494 }
495 }
496
497 //float distance = length(shadow_pos);
498 vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
499 #ifdef LIGHT_CODE_USED
500 ,
501 vec3 shadow_modulate
502 #endif
503 ) {
504 float shadow;
505 uint shadow_mode = light_array.data[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
506
507 if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
508 vec2 unit_p = floor(shadow_uv.xy / canvas_data.shadow_pixel_size) * canvas_data.shadow_pixel_size;
509 float depth_sample = texture(sampler2D(shadow_atlas_texture, shadow_sampler), unit_p.xy).r;
510 shadow = step(depth_sample, shadow_uv.z);
511 } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
512 vec4 shadow_pixel_size = vec4(light_array.data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0);
513 shadow = 0.0;
514 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 2.0);
515 shadow += texture_shadow(shadow_uv - shadow_pixel_size);
516 shadow += texture_shadow(shadow_uv);
517 shadow += texture_shadow(shadow_uv + shadow_pixel_size);
518 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 2.0);
519 shadow /= 5.0;
520 } else { //PCF13
521 vec4 shadow_pixel_size = vec4(light_array.data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0);
522 shadow = 0.0;
523 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 6.0);
524 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 5.0);
525 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 4.0);
526 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 3.0);
527 shadow += texture_shadow(shadow_uv - shadow_pixel_size * 2.0);
528 shadow += texture_shadow(shadow_uv - shadow_pixel_size);
529 shadow += texture_shadow(shadow_uv);
530 shadow += texture_shadow(shadow_uv + shadow_pixel_size);
531 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 2.0);
532 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 3.0);
533 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 4.0);
534 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 5.0);
535 shadow += texture_shadow(shadow_uv + shadow_pixel_size * 6.0);
536 shadow /= 13.0;
537 }
538
539 vec4 shadow_color = unpackUnorm4x8(light_array.data[light_base].shadow_color);
540 #ifdef LIGHT_CODE_USED
541 shadow_color.rgb *= shadow_modulate;
542 #endif
543
544 shadow_color.a *= light_color.a; //respect light alpha
545
546 return mix(light_color, shadow_color, shadow);
547 }
548
549 void light_blend_compute(uint light_base, vec4 light_color, inout vec3 color) {
550 uint blend_mode = light_array.data[light_base].flags & LIGHT_FLAGS_BLEND_MASK;
551
552 switch (blend_mode) {
553 case LIGHT_FLAGS_BLEND_MODE_ADD: {
554 color.rgb += light_color.rgb * light_color.a;
555 } break;
556 case LIGHT_FLAGS_BLEND_MODE_SUB: {
557 color.rgb -= light_color.rgb * light_color.a;
558 } break;
559 case LIGHT_FLAGS_BLEND_MODE_MIX: {
560 color.rgb = mix(color.rgb, light_color.rgb, light_color.a);
561 } break;
562 }
563 }
564
565 float msdf_median(float r, float g, float b) {
566 return max(min(r, g), min(max(r, g), b));
567 }
568
569 void main() {
570 vec4 color = color_interp;
571 vec2 uv = uv_vertex_interp.xy;
572 vec2 vertex = uv_vertex_interp.zw;
573
574 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
575 vec4 src_rect = vec4(unpackHalf2x16(read_draw_data_src_rect.x), unpackHalf2x16(read_draw_data_src_rect.y));
576 vec4 region_rect = src_rect;
577 #else
578 vec4 region_rect = vec4(0.0, 0.0, 1.0 / read_draw_data_color_texture_pixel_size);
579 #endif
580
581 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
582
583 #ifdef USE_NINEPATCH
584
585 int draw_center = 2;
586 uv = vec2(
587 map_ninepatch_axis(pixel_size_interp.x, abs(read_draw_data_dst_rect_z), read_draw_data_ninepatch_pixel_size.x, read_draw_data_ninepatch_margins.x, read_draw_data_ninepatch_margins.z, int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT, 2)), draw_center),
588 map_ninepatch_axis(pixel_size_interp.y, abs(read_draw_data_dst_rect_w), read_draw_data_ninepatch_pixel_size.y, read_draw_data_ninepatch_margins.y, read_draw_data_ninepatch_margins.w, int(bitfieldExtract(read_draw_data_flags, INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT, 2)), draw_center));
589
590 if (draw_center == 0) {
591 color.a = 0.0;
592 }
593
594 vec4 ninepatch_src_rect = read_draw_data_src_rect_ninepatch;
595
596 uv = uv * ninepatch_src_rect.zw + ninepatch_src_rect.xy; //apply region if needed
597
598 #endif
599 if (bool(read_draw_data_flags & INSTANCE_FLAGS_CLIP_RECT_UV)) {
600 vec2 half_texpixel = read_draw_data_color_texture_pixel_size * 0.5;
601 uv = clamp(uv, src_rect.xy + half_texpixel, src_rect.xy + abs(src_rect.zw) - half_texpixel);
602 }
603
604 #endif
605
606 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
607 // only used by TYPE_RECT
608 if (sc_use_msdf()) {
609 float px_range = params.msdf.x;
610 float outline_thickness = params.msdf.y;
611
612 vec4 msdf_sample = texture(sampler2D(color_texture, texture_sampler), uv);
613 vec2 msdf_size = vec2(textureSize(sampler2D(color_texture, texture_sampler), 0));
614 vec2 dest_size = vec2(1.0) / fwidth(uv);
615 float px_size = max(0.5 * dot((vec2(px_range) / msdf_size), dest_size), 1.0);
616 float d = msdf_median(msdf_sample.r, msdf_sample.g, msdf_sample.b);
617
618 if (outline_thickness > 0) {
619 float cr = clamp(outline_thickness, 0.0, (px_range / 2.0) - 1.0) / px_range;
620 d = min(d, msdf_sample.a);
621 float a = clamp((d - 0.5 + cr) * px_size, 0.0, 1.0);
622 color.a = a * color.a;
623 } else {
624 float a = clamp((d - 0.5) * px_size + 0.5, 0.0, 1.0);
625 color.a = a * color.a;
626 }
627 } else if (sc_use_lcd()) {
628 vec4 lcd_sample = texture(sampler2D(color_texture, texture_sampler), uv);
629 if (lcd_sample.a == 1.0) {
630 color.rgb = lcd_sample.rgb * color.a;
631 } else {
632 color = vec4(0.0, 0.0, 0.0, 0.0);
633 }
634 } else {
635 #else
636 {
637 #endif
638 color *= texture(sampler2D(color_texture, texture_sampler), uv);
639 }
640
641 uint light_count = read_draw_data_flags & 15u; //max 15 lights
642 bool using_light = ((light_count + canvas_data.directional_light_count) > 0) && sc_use_lighting();
643
644 vec3 normal;
645
646 #if defined(NORMAL_USED)
647 bool normal_used = true;
648 #else
649 bool normal_used = false;
650 #endif
651
652 if (normal_used || (using_light && bool(params.batch_flags & BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED))) {
653 normal.xy = texture(sampler2D(normal_texture, texture_sampler), uv).xy * vec2(2.0, -2.0) - vec2(1.0, -1.0);
654
655 #if !defined(USE_ATTRIBUTES) && !defined(USE_PRIMITIVE)
656 if (bool(read_draw_data_flags & INSTANCE_FLAGS_TRANSPOSE_RECT)) {
657 normal.xy = normal.yx;
658 }
659 normal.xy *= sign(src_rect.zw);
660 #endif
661 normal.z = sqrt(max(0.0, 1.0 - dot(normal.xy, normal.xy)));
662 normal_used = true;
663 } else {
664 normal = vec3(0.0, 0.0, 1.0);
665 }
666
667 vec4 specular_shininess;
668
669 #if defined(SPECULAR_SHININESS_USED)
670
671 bool specular_shininess_used = true;
672 #else
673 bool specular_shininess_used = false;
674 #endif
675
676 if (specular_shininess_used || (using_light && normal_used && bool(params.batch_flags & BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED))) {
677 specular_shininess = texture(sampler2D(specular_texture, texture_sampler), uv);
678 specular_shininess *= unpackUnorm4x8(params.specular_shininess);
679 specular_shininess_used = true;
680 } else {
681 specular_shininess = vec4(1.0);
682 }
683
684 #if defined(SCREEN_UV_USED)
685 vec2 screen_uv = gl_FragCoord.xy * canvas_data.screen_pixel_size;
686 #else
687 vec2 screen_uv = vec2(0.0);
688 #endif
689
690 vec3 light_vertex = vec3(vertex, 0.0);
691 vec2 shadow_vertex = vertex;
692
693 {
694 float normal_map_depth = 1.0;
695
696 #if defined(NORMAL_MAP_USED)
697 vec3 normal_map = vec3(0.0, 0.0, 1.0);
698 normal_used = true;
699 #endif
700
701 #CODE : FRAGMENT
702
703 #if defined(NORMAL_MAP_USED)
704 normal = mix(vec3(0.0, 0.0, 1.0), normal_map * vec3(2.0, -2.0, 1.0) - vec3(1.0, -1.0, 0.0), normal_map_depth);
705 #endif
706 }
707
708 if (normal_used) {
709 //convert by item transform
710 normal.xy = mat2(normalize(read_draw_data_world_x), normalize(read_draw_data_world_y)) * normal.xy;
711 //convert by canvas transform
712 normal = normalize((canvas_data.canvas_normal_transform * vec4(normal, 0.0)).xyz);
713 }
714
715 vec4 base_color = color;
716
717 #ifdef MODE_LIGHT_ONLY
718 float light_only_alpha = 0.0;
719 #elif !defined(MODE_UNSHADED)
720 color *= canvas_data.canvas_modulation;
721 #endif
722
723 #if !defined(MODE_UNSHADED)
724 if (sc_use_lighting()) {
725 // Directional Lights
726
727 for (uint i = 0; i < canvas_data.directional_light_count; i++) {
728 uint light_base = i;
729
730 vec2 direction = light_array.data[light_base].position;
731 vec4 light_color = light_array.data[light_base].color;
732
733 #ifdef LIGHT_CODE_USED
734
735 vec4 shadow_modulate = vec4(1.0);
736 light_color = light_compute(light_vertex, vec3(direction, light_array.data[light_base].height), normal, light_color, light_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, true);
737 #else
738
739 if (normal_used) {
740 vec3 light_vec = normalize(mix(vec3(direction, 0.0), vec3(0, 0, 1), light_array.data[light_base].height));
741 light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
742 } else {
743 light_color.rgb *= base_color.rgb;
744 }
745 #endif
746
747 if (bool(light_array.data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW)) {
748 vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array.data[light_base].shadow_matrix[0], light_array.data[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
749
750 vec4 shadow_uv = vec4(shadow_pos.x, light_array.data[light_base].shadow_y_ofs, shadow_pos.y * light_array.data[light_base].shadow_zfar_inv, 1.0);
751
752 light_color = light_shadow_compute(light_base, light_color, shadow_uv
753 #ifdef LIGHT_CODE_USED
754 ,
755 shadow_modulate.rgb
756 #endif
757 );
758 }
759
760 light_blend_compute(light_base, light_color, color.rgb);
761 #ifdef MODE_LIGHT_ONLY
762 light_only_alpha += light_color.a;
763 #endif
764 }
765
766 // Positional Lights
767
768 for (uint i = 0; i < MAX_LIGHTS_PER_ITEM; i++) {
769 if (i >= light_count) {
770 break;
771 }
772 uint light_base = bitfieldExtract(read_draw_data_lights[i >> 2], (int(i) & 0x3) * 8, 8);
773
774 vec2 tex_uv = (vec4(vertex, 0.0, 1.0) * mat4(light_array.data[light_base].texture_matrix[0], light_array.data[light_base].texture_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
775 vec2 tex_uv_atlas = tex_uv * light_array.data[light_base].atlas_rect.zw + light_array.data[light_base].atlas_rect.xy;
776
777 if (any(lessThan(tex_uv, vec2(0.0, 0.0))) || any(greaterThanEqual(tex_uv, vec2(1.0, 1.0)))) {
778 //if outside the light texture, light color is zero
779 continue;
780 }
781
782 vec4 light_color = textureLod(sampler2D(atlas_texture, texture_sampler), tex_uv_atlas, 0.0);
783 vec4 light_base_color = light_array.data[light_base].color;
784
785 #ifdef LIGHT_CODE_USED
786
787 vec4 shadow_modulate = vec4(1.0);
788 vec3 light_position = vec3(light_array.data[light_base].position, light_array.data[light_base].height);
789
790 light_color.rgb *= light_base_color.rgb;
791 light_color = light_compute(light_vertex, light_position, normal, light_color, light_base_color.a, specular_shininess, shadow_modulate, screen_uv, uv, base_color, false);
792 #else
793
794 light_color.rgb *= light_base_color.rgb * light_base_color.a;
795
796 if (normal_used) {
797 vec3 light_pos = vec3(light_array.data[light_base].position, light_array.data[light_base].height);
798 vec3 pos = light_vertex;
799 vec3 light_vec = normalize(light_pos - pos);
800
801 light_color.rgb = light_normal_compute(light_vec, normal, base_color.rgb, light_color.rgb, specular_shininess, specular_shininess_used);
802 } else {
803 light_color.rgb *= base_color.rgb;
804 }
805 #endif
806
807 if (bool(light_array.data[light_base].flags & LIGHT_FLAGS_HAS_SHADOW) && bool(read_draw_data_flags & (INSTANCE_FLAGS_SHADOW_MASKED << i))) {
808 vec2 shadow_pos = (vec4(shadow_vertex, 0.0, 1.0) * mat4(light_array.data[light_base].shadow_matrix[0], light_array.data[light_base].shadow_matrix[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))).xy; //multiply inverse given its transposed. Optimizer removes useless operations.
809
810 vec2 pos_norm = normalize(shadow_pos);
811 vec2 pos_abs = abs(pos_norm);
812 vec2 pos_box = pos_norm / max(pos_abs.x, pos_abs.y);
813 vec2 pos_rot = pos_norm * mat2(vec2(0.7071067811865476, -0.7071067811865476), vec2(0.7071067811865476, 0.7071067811865476)); //is there a faster way to 45 degrees rot?
814 float tex_ofs;
815 float distance;
816 if (pos_rot.y > 0) {
817 if (pos_rot.x > 0) {
818 tex_ofs = pos_box.y * 0.125 + 0.125;
819 distance = shadow_pos.x;
820 } else {
821 tex_ofs = pos_box.x * -0.125 + (0.25 + 0.125);
822 distance = shadow_pos.y;
823 }
824 } else {
825 if (pos_rot.x < 0) {
826 tex_ofs = pos_box.y * -0.125 + (0.5 + 0.125);
827 distance = -shadow_pos.x;
828 } else {
829 tex_ofs = pos_box.x * 0.125 + (0.75 + 0.125);
830 distance = -shadow_pos.y;
831 }
832 }
833
834 distance *= light_array.data[light_base].shadow_zfar_inv;
835
836 //float distance = length(shadow_pos);
837 vec4 shadow_uv = vec4(tex_ofs, light_array.data[light_base].shadow_y_ofs, distance, 1.0);
838
839 light_color = light_shadow_compute(light_base, light_color, shadow_uv
840 #ifdef LIGHT_CODE_USED
841 ,
842 shadow_modulate.rgb
843 #endif
844 );
845 }
846
847 light_blend_compute(light_base, light_color, color.rgb);
848 #ifdef MODE_LIGHT_ONLY
849 light_only_alpha += light_color.a;
850 #endif
851 }
852 }
853 #endif
854
855 #ifdef MODE_LIGHT_ONLY
856 color.a *= light_only_alpha;
857 #endif
858
859 frag_color = color;
860 }