Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShaderEmbeds.generated.hpp
Go to the documentation of this file.
1#pragma once
2
4
5inline constexpr const char* BATCH = R"esshader(#pragma shader "Batch"
6#pragma version 300 es
7
8
9
10#pragma feature LIT
11
12
13
14
15
16
17#pragma feature ALPHA_CLIP
18
19
20#pragma feature PREMULTIPLIED_COLOR
21
22
23
24
25
26#pragma feature INSTANCED
27
28#pragma vertex
29layout(location = 0) in vec3 a_position;
30layout(location = 1) in vec4 a_color;
31layout(location = 2) in vec2 a_texCoord;
32layout(location = 3) in float a_texIndex;
33layout(location = 4) in float a_sdfBias;
34#ifdef INSTANCED
35
36layout(location = 5) in vec4 a_xform;
37layout(location = 6) in vec2 a_offset;
38layout(location = 7) in vec4 a_tint;
39#endif
40
41out vec4 v_color;
42out vec2 v_texCoord;
43flat out int v_texIndex;
44#ifdef LIT
45out highp vec2 v_worldPos;
46flat out highp float v_lightLayer;
47#else
48out float v_sdfBias;
49#endif
50
51void main() {
52#ifdef INSTANCED
53 vec3 position = vec3(a_xform.xy * a_position.x + a_xform.zw * a_position.y + a_offset, a_position.z);
54 v_color = a_color * a_tint;
55#else
56 vec3 position = a_position;
57 v_color = a_color;
58#endif
59 gl_Position = u_projection * vec4(position, 1.0);
60 v_texCoord = a_texCoord;
61 v_texIndex = int(a_texIndex);
62#ifdef LIT
63 v_worldPos = position.xy;
64 v_lightLayer = a_sdfBias;
65#else
66 v_sdfBias = a_sdfBias;
67#endif
68}
69#pragma end
70
71#pragma fragment
72
73
74precision highp float;
75
76in vec4 v_color;
77in vec2 v_texCoord;
78flat in int v_texIndex;
79#ifdef LIT
80in highp vec2 v_worldPos;
81flat in highp float v_lightLayer;
82#else
83in float v_sdfBias;
84#endif
85
86
87
88
89uniform sampler2D u_textures[8];
90
91out vec4 fragColor;
92
93void main() {
94
95
96 if ((v_texIndex & 256) != 0 && ((int(gl_FragCoord.x) + int(gl_FragCoord.y)) & 1) != ((v_texIndex >> 9) & 1)) discard;
97 int slot = v_texIndex & 7;
98 vec4 texColor;
99 if (slot == 0) texColor = texture(u_textures[0], v_texCoord);
100 else if (slot == 1) texColor = texture(u_textures[1], v_texCoord);
101 else if (slot == 2) texColor = texture(u_textures[2], v_texCoord);
102 else if (slot == 3) texColor = texture(u_textures[3], v_texCoord);
103 else if (slot == 4) texColor = texture(u_textures[4], v_texCoord);
104 else if (slot == 5) texColor = texture(u_textures[5], v_texCoord);
105 else if (slot == 6) texColor = texture(u_textures[6], v_texCoord);
106 else texColor = texture(u_textures[7], v_texCoord);
107#ifdef ES_LINEAR
108
109
110 vec4 tint = vec4(srgbToLinear(v_color.rgb), v_color.a);
111#else
112 vec4 tint = v_color;
113#endif
114#ifdef PREMULTIPLIED_COLOR
115 tint.rgb *= tint.a;
116#endif
117#ifdef LIT
118 vec4 base = texColor * tint;
119 fragColor = vec4(applyLighting2DLayer(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos, v_lightLayer), base.a);
120#else
121 int mode = (v_texIndex >> 3) & 31;
122 vec2 duvx = dFdx(v_texCoord);
123 vec2 duvy = dFdy(v_texCoord);
124 float distWidth = fwidth(texColor.a);
125 if (mode == 1) {
126 ivec2 page;
127 if (slot == 0) page = textureSize(u_textures[0], 0);
128 else if (slot == 1) page = textureSize(u_textures[1], 0);
129 else if (slot == 2) page = textureSize(u_textures[2], 0);
130 else if (slot == 3) page = textureSize(u_textures[3], 0);
131 else if (slot == 4) page = textureSize(u_textures[4], 0);
132 else if (slot == 5) page = textureSize(u_textures[5], 0);
133 else if (slot == 6) page = textureSize(u_textures[6], 0);
134 else page = textureSize(u_textures[7], 0);
135 vec2 acrossX = duvx * vec2(page);
136 vec2 acrossY = duvy * vec2(page);
137 float texelsPerPixel = sqrt(0.5 * (dot(acrossX, acrossX) + dot(acrossY, acrossY)));
138 float dist = texColor.a;
139 float pixelsPerUnit = min(1.0 / max(distWidth, 1e-6), texColor.r * 255.0 / max(texelsPerPixel, 1e-6));
140 float screenPxDist = (dist - 0.5 + v_sdfBias) * pixelsPerUnit;
141 float coverage = v_sdfBias < -1.0 ? dist : clamp(screenPxDist + 0.5, 0.0, 1.0);
142 fragColor = vec4(tint.rgb, tint.a * coverage);
143 } else if (mode == 2) {
144 fragColor = vec4(tint.rgb, tint.a * texColor.a);
145 } else if (mode == 3) {
146 fragColor = vec4(tint.rgb, tint.a * texColor.r * texColor.a);
147 } else {
148 #ifdef ALPHA_CLIP
149 if (texColor.a < 0.5) discard;
150 #endif
151 fragColor = texColor * tint;
152 }
153#endif
154#ifdef ES_LINEAR
155 #ifdef DISPLAY_OUTPUT
156 #ifdef PREMULTIPLIED_COLOR
157 fragColor.rgb = linearToSrgb(fragColor.rgb / max(fragColor.a, 1e-5)) * fragColor.a;
158 #else
159 fragColor.rgb = linearToSrgb(fragColor.rgb);
160 #endif
161 #endif
162#endif
163}
164#pragma end
165
166#pragma vertex wgsl
167struct VSIn {
168 @location(0) a_position : vec3f,
169 @location(1) a_color : vec4f,
170 @location(2) a_texCoord : vec2f,
171 @location(3) a_texIndex : f32,
172 @location(4) a_sdfBias : f32,
173#ifdef INSTANCED
174 @location(5) a_xform : vec4f,
175 @location(6) a_offset : vec2f,
176 @location(7) a_tint : vec4f,
177#endif
178};
179struct VSOut {
180 @builtin(position) pos : vec4f,
181 @location(0) v_color : vec4f,
182 @location(1) v_texCoord : vec2f,
183 @location(2) v_texIndex : f32,
184#ifdef LIT
185 @location(3) v_worldPos : vec2f,
186 @location(5) @interpolate(flat) v_lightLayer : f32,
187#else
188 @location(4) v_sdfBias : f32,
189#endif
190};
191
192@vertex fn vs_main(v : VSIn) -> VSOut {
193 var out : VSOut;
194#ifdef INSTANCED
195 let position = vec3f(v.a_xform.xy * v.a_position.x + v.a_xform.zw * v.a_position.y + v.a_offset, v.a_position.z);
196 out.v_color = v.a_color * v.a_tint;
197#else
198 let position = v.a_position;
199 out.v_color = v.a_color;
200#endif
201 out.pos = frame.projection * vec4f(position, 1.0);
202 out.v_texCoord = v.a_texCoord;
203 out.v_texIndex = v.a_texIndex;
204#ifdef LIT
205 out.v_worldPos = position.xy;
206 out.v_lightLayer = v.a_sdfBias;
207#else
208 out.v_sdfBias = v.a_sdfBias;
209#endif
210
211 out.pos.y = -out.pos.y;
212 return out;
213}
214#pragma end
215
216#pragma fragment wgsl
217@group(1) @binding(0) var t0 : texture_2d<f32>;
218@group(1) @binding(1) var t1 : texture_2d<f32>;
219@group(1) @binding(2) var t2 : texture_2d<f32>;
220@group(1) @binding(3) var t3 : texture_2d<f32>;
221@group(1) @binding(4) var t4 : texture_2d<f32>;
222@group(1) @binding(5) var t5 : texture_2d<f32>;
223@group(1) @binding(6) var t6 : texture_2d<f32>;
224@group(1) @binding(7) var t7 : texture_2d<f32>;
225@group(1) @binding(8) var s0 : sampler;
226@group(1) @binding(9) var s1 : sampler;
227@group(1) @binding(10) var s2 : sampler;
228@group(1) @binding(11) var s3 : sampler;
229@group(1) @binding(12) var s4 : sampler;
230@group(1) @binding(13) var s5 : sampler;
231@group(1) @binding(14) var s6 : sampler;
232@group(1) @binding(15) var s7 : sampler;
233
234struct VSOut {
235 @builtin(position) pos : vec4f,
236 @location(0) v_color : vec4f,
237 @location(1) v_texCoord : vec2f,
238 @location(2) v_texIndex : f32,
239#ifdef LIT
240 @location(3) v_worldPos : vec2f,
241 @location(5) @interpolate(flat) v_lightLayer : f32,
242#else
243 @location(4) v_sdfBias : f32,
244#endif
245};
246
247@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
248 let idx = i32(v.v_texIndex + 0.5);
249
250 if ((idx & 256) != 0 && ((i32(v.pos.x) + i32(v.pos.y)) & 1) != ((idx >> 9u) & 1)) { discard; }
251 let slot = idx & 7;
252 let duvx = dpdx(v.v_texCoord);
253 let duvy = dpdy(v.v_texCoord);
254 var texColor : vec4f;
255 if (slot == 0) { texColor = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy); }
256 else if (slot == 1) { texColor = textureSampleGrad(t1, s1, v.v_texCoord, duvx, duvy); }
257 else if (slot == 2) { texColor = textureSampleGrad(t2, s2, v.v_texCoord, duvx, duvy); }
258 else if (slot == 3) { texColor = textureSampleGrad(t3, s3, v.v_texCoord, duvx, duvy); }
259 else if (slot == 4) { texColor = textureSampleGrad(t4, s4, v.v_texCoord, duvx, duvy); }
260 else if (slot == 5) { texColor = textureSampleGrad(t5, s5, v.v_texCoord, duvx, duvy); }
261 else if (slot == 6) { texColor = textureSampleGrad(t6, s6, v.v_texCoord, duvx, duvy); }
262 else { texColor = textureSampleGrad(t7, s7, v.v_texCoord, duvx, duvy); }
263#ifdef ES_LINEAR
264 var tint = vec4f(srgbToLinear(v.v_color.rgb), v.v_color.a);
265#else
266 var tint = v.v_color;
267#endif
268#ifdef PREMULTIPLIED_COLOR
269 tint = vec4f(tint.rgb * tint.a, tint.a);
270#endif
271#ifdef LIT
272 let base = texColor * tint;
273 let shown = vec4f(applyLighting2DLayer(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos, v.v_lightLayer), base.a);
274#else
275 let mode = (idx >> 3u) & 31;
276 let distWidth = fwidth(texColor.a);
277 var shown : vec4f;
278 if (mode == 1) {
279 var page : vec2u;
280 if (slot == 0) { page = textureDimensions(t0); }
281 else if (slot == 1) { page = textureDimensions(t1); }
282 else if (slot == 2) { page = textureDimensions(t2); }
283 else if (slot == 3) { page = textureDimensions(t3); }
284 else if (slot == 4) { page = textureDimensions(t4); }
285 else if (slot == 5) { page = textureDimensions(t5); }
286 else if (slot == 6) { page = textureDimensions(t6); }
287 else { page = textureDimensions(t7); }
288 let acrossX = duvx * vec2f(page);
289 let acrossY = duvy * vec2f(page);
290 let texelsPerPixel = sqrt(0.5 * (dot(acrossX, acrossX) + dot(acrossY, acrossY)));
291 let dist = texColor.a;
292 let pixelsPerUnit = min(1.0 / max(distWidth, 1e-6), texColor.r * 255.0 / max(texelsPerPixel, 1e-6));
293 let screenPxDist = (dist - 0.5 + v.v_sdfBias) * pixelsPerUnit;
294 let coverage = select(clamp(screenPxDist + 0.5, 0.0, 1.0), dist, v.v_sdfBias < -1.0);
295 shown = vec4f(tint.rgb, tint.a * coverage);
296 } else if (mode == 2) {
297 shown = vec4f(tint.rgb, tint.a * texColor.a);
298 } else if (mode == 3) {
299 shown = vec4f(tint.rgb, tint.a * texColor.r * texColor.a);
300 } else {
301 #ifdef ALPHA_CLIP
302 if (texColor.a < 0.5) { discard; }
303 #endif
304 shown = texColor * tint;
305 }
306#endif
307#ifdef ES_LINEAR
308 #ifdef DISPLAY_OUTPUT
309 #ifdef PREMULTIPLIED_COLOR
310 return vec4f(linearToSrgb(shown.rgb / max(shown.a, 1e-5)) * shown.a, shown.a);
311 #else
312 return vec4f(linearToSrgb(shown.rgb), shown.a);
313 #endif
314 #else
315 return shown;
316 #endif
317#else
318 return shown;
319#endif
320}
321#pragma end
322)esshader";
323
324inline constexpr const char* BLIT = R"esshader(#pragma shader "Blit"
325#pragma version 300 es
326#pragma domain PostProcess
327
328
329#pragma param u_displayLut texture default(white)
330#pragma param u_displayLutAmount float default(0)
331
332#pragma fragment
333precision highp float;
334
335in vec2 v_texCoord;
336uniform sampler2D u_texture;
337#if defined(ES_COPY_DEPTH) || defined(ES_SELECTION_SCENE_DEPTH)
338uniform highp sampler2D u_sceneDepth;
339#endif
340#ifdef ES_SELECTION_OUTLINE
341
342
343uniform highp sampler2D u_selectionMask;
344#endif
345out vec4 fragColor;
346
347#ifdef ES_UPSCALE
348
349
350
351vec4 upscale(vec2 uv) {
352 vec2 size = vec2(textureSize(u_texture, 0));
353 vec2 pos = uv * size;
354 vec2 centre = floor(pos - 0.5) + 0.5;
355 vec2 f = pos - centre;
356 vec2 f2 = f * f;
357 vec2 f3 = f2 * f;
358 vec2 w0 = f2 - 0.5 * (f3 + f);
359 vec2 w1 = 1.5 * f3 - 2.5 * f2 + 1.0;
360 vec2 w3 = 0.5 * (f3 - f2);
361 vec2 w2 = 1.0 - w0 - w1 - w3;
362 vec2 w12 = w1 + w2;
363 vec2 t0 = (centre - 1.0) / size;
364 vec2 t3 = (centre + 2.0) / size;
365 vec2 t12 = (centre + w2 / w12) / size;
366 vec4 sum = texture(u_texture, vec2(t12.x, t0.y)) * (w12.x * w0.y)
367 + texture(u_texture, vec2(t0.x, t12.y)) * (w0.x * w12.y)
368 + texture(u_texture, t12) * (w12.x * w12.y)
369 + texture(u_texture, vec2(t3.x, t12.y)) * (w3.x * w12.y)
370 + texture(u_texture, vec2(t12.x, t3.y)) * (w12.x * w3.y);
371 sum /= (w12.x * w0.y + w0.x * w12.y + w12.x * w12.y + w3.x * w12.y + w12.x * w3.y);
372 ivec2 base = ivec2(centre - 0.5);
373 ivec2 hi = ivec2(size) - 1;
374 vec4 a = texelFetch(u_texture, clamp(base, ivec2(0), hi), 0);
375 vec4 b = texelFetch(u_texture, clamp(base + ivec2(1, 0), ivec2(0), hi), 0);
376 vec4 d = texelFetch(u_texture, clamp(base + ivec2(0, 1), ivec2(0), hi), 0);
377 vec4 e = texelFetch(u_texture, clamp(base + ivec2(1, 1), ivec2(0), hi), 0);
378 return clamp(sum, min(min(a, b), min(d, e)), max(max(a, b), max(d, e)));
379}
380#endif
381
382#ifdef ES_DISPLAY_LUT
383vec3 displayLookup(vec3 c) {
384 const float size = 32.0;
385 float b = clamp(c.b, 0.0, 1.0) * (size - 1.0);
386 float slice0 = floor(b);
387 float slice1 = min(slice0 + 1.0, size - 1.0);
388 float u = (clamp(c.r, 0.0, 1.0) * (size - 1.0) + 0.5) / (size * size);
389 float v = (clamp(c.g, 0.0, 1.0) * (size - 1.0) + 0.5) / size;
390 vec3 a = texture(u_displayLut, vec2(u + slice0 / size, v)).rgb;
391 vec3 d = texture(u_displayLut, vec2(u + slice1 / size, v)).rgb;
392 return mix(a, d, b - slice0);
393}
394#endif
395
396void main() {
397#ifdef ES_SELECTION_OUTLINE
398
399
400
401
402 ivec2 maskSize = textureSize(u_selectionMask, 0);
403 ivec2 at = clamp(ivec2(v_texCoord * vec2(maskSize)), ivec2(0), maskSize - ivec2(1));
404 vec4 here = texelFetch(u_selectionMask, at, 0);
405 if (here.a > 0.5) discard;
406 bool ring = false;
407 bool seen = false;
408 for (int y = -2; y <= 2; ++y) {
409 for (int x = -2; x <= 2; ++x) {
410 if (x * x + y * y > 5) continue;
411 ivec2 q = clamp(at + ivec2(x, y), ivec2(0), maskSize - ivec2(1));
412 vec4 m = texelFetch(u_selectionMask, q, 0);
413 if (m.a < 0.5) continue;
414 ring = true;
415 vec3 selected = worldFromDepth(v_texCoord, m.z);
416 float selectedDistance = u_camera.w > 0.5 ? length(selected - u_camera.xyz) : -dot(selected, u_camera.xyz);
417 if (here.g >= selectedDistance * 0.97) seen = true;
418 }
419 }
420 if (!ring) discard;
421
422 float strength = seen ? 1.0 : 0.4;
423 fragColor = vec4(vec3(0.961, 0.533, 0.122) * strength, strength);
424 return;
425#endif
426#ifdef ES_SELECTION_SCENE_DEPTH
427
428
429 ivec2 sceneSize = textureSize(u_sceneDepth, 0);
430 float sceneZ = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(sceneSize)), ivec2(0), sceneSize - ivec2(1)), 0).r;
431 vec3 scenePoint = worldFromDepth(v_texCoord, sceneZ);
432 float sceneDistance = u_camera.w > 0.5 ? length(scenePoint - u_camera.xyz) : -dot(scenePoint, u_camera.xyz);
433 fragColor = vec4(0.0, sceneZ >= 1.0 ? 1e30 : sceneDistance, 0.0, 0.0);
434 return;
435#endif
436#ifdef ES_COPY_DEPTH
437
438
439
440
441
442 ivec2 depthSize = textureSize(u_sceneDepth, 0);
443 float rawDepth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(depthSize)), ivec2(0), depthSize - ivec2(1)), 0).r;
444 vec3 scenePoint = worldFromDepth(v_texCoord, rawDepth);
445 float eyeDistance = u_camera.w > 0.5 ? length(scenePoint - u_camera.xyz) : -dot(scenePoint, u_camera.xyz);
446 bool sky = rawDepth >= 1.0;
447 fragColor = vec4(sky ? 65504.0 : min(eyeDistance, 65504.0), sky ? 1.0 : 0.0, sky ? 65504.0 : min(eyeDistance / 1024.0, 65504.0), 0.5);
448 return;
449#endif
450#ifdef ES_UPSCALE
451 vec4 c = upscale(v_texCoord);
452#else
453 vec4 c = texture(u_texture, v_texCoord);
454#endif
455
456
457#ifdef ES_COPY_EXACT
458
459
460 fragColor = c;
461 return;
462#endif
463#ifdef ES_COPY
464
465
466 fragColor = vec4(c.rgb, 0.5);
467 return;
468#endif
469#ifdef ES_RGBM
470
471
472 if (c.a <= 0.001) { fragColor = vec4(0.0); return; }
473 vec3 h = clamp(c.rgb, vec3(0.0), vec3(8.0));
474 float m = clamp(max(max(h.r, h.g), max(h.b, 1e-6)) / 8.0, 1.0 / 255.0, 1.0);
475 m = ceil(m * 255.0) / 255.0;
476 fragColor = vec4(h / (m * 8.0), m);
477 return;
478#endif
479 c = vec4(c.rgb * exp2(u_time.z), c.a);
480#ifdef ES_TONEMAP
481
482
483
484 c = vec4(acesFilmic(c.rgb), c.a);
485#endif
486#ifdef ES_TONEMAP_NEUTRAL
487 c = vec4(pbrNeutral(c.rgb), c.a);
488#endif
489#ifdef ES_LINEAR
490
491
492 vec3 shown = linearToSrgb(c.rgb);
493#else
494 vec3 shown = c.rgb;
495#endif
496#ifdef ES_DISPLAY_LUT
497
498 shown = mix(shown, displayLookup(shown), clamp(u_displayLutAmount, 0.0, 1.0));
499#endif
500 fragColor = vec4(shown, c.a);
501}
502#pragma end
503
504#pragma fragment wgsl
505#ifdef ES_UPSCALE
506fn upscale(uv : vec2f) -> vec4f {
507 let size = vec2f(textureDimensions(t0, 0));
508 let pos = uv * size;
509 let centre = floor(pos - vec2f(0.5)) + vec2f(0.5);
510 let f = pos - centre;
511 let f2 = f * f;
512 let f3 = f2 * f;
513 let w0 = f2 - 0.5 * (f3 + f);
514 let w1 = 1.5 * f3 - 2.5 * f2 + vec2f(1.0);
515 let w3 = 0.5 * (f3 - f2);
516 let w2 = vec2f(1.0) - w0 - w1 - w3;
517 let w12 = w1 + w2;
518 let tapLo = (centre - vec2f(1.0)) / size;
519 let tapHi = (centre + vec2f(2.0)) / size;
520 let tapMid = (centre + w2 / w12) / size;
521 var sum = textureSampleLevel(t0, s0, vec2f(tapMid.x, tapLo.y), 0.0) * (w12.x * w0.y)
522 + textureSampleLevel(t0, s0, vec2f(tapLo.x, tapMid.y), 0.0) * (w0.x * w12.y)
523 + textureSampleLevel(t0, s0, tapMid, 0.0) * (w12.x * w12.y)
524 + textureSampleLevel(t0, s0, vec2f(tapHi.x, tapMid.y), 0.0) * (w3.x * w12.y)
525 + textureSampleLevel(t0, s0, vec2f(tapMid.x, tapHi.y), 0.0) * (w12.x * w3.y);
526 sum = sum / (w12.x * w0.y + w0.x * w12.y + w12.x * w12.y + w3.x * w12.y + w12.x * w3.y);
527 let base = vec2i(centre - vec2f(0.5));
528 let hi = vec2i(size) - vec2i(1);
529 let a = textureLoad(t0, clamp(base, vec2i(0), hi), 0);
530 let b = textureLoad(t0, clamp(base + vec2i(1, 0), vec2i(0), hi), 0);
531 let d = textureLoad(t0, clamp(base + vec2i(0, 1), vec2i(0), hi), 0);
532 let e = textureLoad(t0, clamp(base + vec2i(1, 1), vec2i(0), hi), 0);
533 return clamp(sum, min(min(a, b), min(d, e)), max(max(a, b), max(d, e)));
534}
535#endif
536
537#ifdef ES_DISPLAY_LUT
538fn displayLookup(c : vec3f) -> vec3f {
539 let size = 32.0;
540 let b = clamp(c.b, 0.0, 1.0) * (size - 1.0);
541 let slice0 = floor(b);
542 let slice1 = min(slice0 + 1.0, size - 1.0);
543 let u = (clamp(c.r, 0.0, 1.0) * (size - 1.0) + 0.5) / (size * size);
544 let v = (clamp(c.g, 0.0, 1.0) * (size - 1.0) + 0.5) / size;
545 let a = textureSampleLevel(u_displayLut, u_displayLut_s, vec2f(u + slice0 / size, v), 0.0).rgb;
546 let d = textureSampleLevel(u_displayLut, u_displayLut_s, vec2f(u + slice1 / size, v), 0.0).rgb;
547 return mix(a, d, b - slice0);
548}
549#endif
550
551@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
552#ifdef ES_SELECTION_OUTLINE
553
554 let maskSize = vec2i(textureDimensions(t15, 0));
555 let at = clamp(vec2i(v.v_texCoord * vec2f(maskSize)), vec2i(0), maskSize - vec2i(1));
556 let here = textureLoad(t15, at, 0);
557 if (here.a > 0.5) { discard; }
558 var ring = false;
559 var seen = false;
560 for (var y = -2; y <= 2; y++) {
561 for (var x = -2; x <= 2; x++) {
562 if (x * x + y * y > 5) { continue; }
563 let q = clamp(at + vec2i(x, y), vec2i(0), maskSize - vec2i(1));
564 let m = textureLoad(t15, q, 0);
565 if (m.a < 0.5) { continue; }
566 ring = true;
567 let selected = worldFromDepth(v.v_texCoord, m.z);
568 var selectedDistance = -dot(selected, frame.camera.xyz);
569 if (frame.camera.w > 0.5) { selectedDistance = length(selected - frame.camera.xyz); }
570 if (here.g >= selectedDistance * 0.97) { seen = true; }
571 }
572 }
573 if (!ring) { discard; }
574 let strength = select(0.4, 1.0, seen);
575 return vec4f(vec3f(0.961, 0.533, 0.122) * strength, strength);
576#endif
577#ifdef ES_SELECTION_SCENE_DEPTH
578 let sceneSize = vec2i(textureDimensions(t7, 0));
579 let sceneZ = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(sceneSize)), vec2i(0), sceneSize - vec2i(1)), 0);
580 let scenePoint = worldFromDepth(v.v_texCoord, sceneZ);
581 var sceneDistance = -dot(scenePoint, frame.camera.xyz);
582 if (frame.camera.w > 0.5) { sceneDistance = length(scenePoint - frame.camera.xyz); }
583 return vec4f(0.0, select(sceneDistance, 1e30, sceneZ >= 1.0), 0.0, 0.0);
584#endif
585#ifdef ES_COPY_DEPTH
586 let depthSize = vec2i(textureDimensions(t7, 0));
587 let rawDepth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(depthSize)), vec2i(0), depthSize - vec2i(1)), 0);
588 let scenePoint = worldFromDepth(v.v_texCoord, rawDepth);
589 var eyeDistance = -dot(scenePoint, frame.camera.xyz);
590 if (frame.camera.w > 0.5) { eyeDistance = length(scenePoint - frame.camera.xyz); }
591 let sky = rawDepth >= 1.0;
592 return vec4f(select(min(eyeDistance, 65504.0), 65504.0, sky), select(0.0, 1.0, sky), select(min(eyeDistance / 1024.0, 65504.0), 65504.0, sky), 0.5);
593#endif
594#ifdef ES_UPSCALE
595 var c = upscale(v.v_texCoord);
596#else
597 var c = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
598#endif
599#ifdef ES_COPY_EXACT
600 return c;
601#endif
602#ifdef ES_COPY
603 return vec4f(c.rgb, 0.5);
604#endif
605#ifdef ES_RGBM
606 if (c.a <= 0.001) { return vec4f(0.0); }
607 let h = clamp(c.rgb, vec3f(0.0), vec3f(8.0));
608 var m = clamp(max(max(h.r, h.g), max(h.b, 1e-6)) / 8.0, 1.0 / 255.0, 1.0);
609 m = ceil(m * 255.0) / 255.0;
610 return vec4f(h / (m * 8.0), m);
611#endif
612 c = vec4f(c.rgb * exp2(tc.u_time.z), c.a);
613#ifdef ES_TONEMAP
614 c = vec4f(acesFilmic(c.rgb), c.a);
615#endif
616#ifdef ES_TONEMAP_NEUTRAL
617 c = vec4f(pbrNeutral(c.rgb), c.a);
618#endif
619#ifdef ES_LINEAR
620 var shown = linearToSrgb(c.rgb);
621#else
622 var shown = c.rgb;
623#endif
624#ifdef ES_DISPLAY_LUT
625 shown = mix(shown, displayLookup(shown), clamp(mc.u_displayLutAmount, 0.0, 1.0));
626#endif
627 return vec4f(shown, c.a);
628}
629#pragma end
630)esshader";
631
632inline constexpr const char* LIGHTSHAPE2D = R"esshader(#pragma shader "LightShape2D"
633#pragma version 300 es
634
635
636
637
638
639
640
641
642#pragma vertex
643layout(location = 0) in vec2 a_position;
644layout(location = 1) in vec2 a_texCoord;
645layout(location = 2) in vec4 a_channel;
646
647out vec2 v_texCoord;
648out vec4 v_channel;
649
650void main() {
651 gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
652 v_texCoord = a_texCoord;
653 v_channel = a_channel;
654}
655#pragma end
656
657#pragma fragment
658precision highp float;
659
660in vec2 v_texCoord;
661in vec4 v_channel;
662
663uniform sampler2D u_cookie;
664
665out vec4 fragColor;
666
667void main() {
668
669
670 fragColor = v_channel * texture(u_cookie, v_texCoord).a;
671}
672#pragma end
673
674#pragma vertex wgsl
675struct VSIn {
676 @location(0) a_position : vec2f,
677 @location(1) a_texCoord : vec2f,
678 @location(2) a_channel : vec4f,
679};
680struct VSOut {
681 @builtin(position) pos : vec4f,
682 @location(0) v_texCoord : vec2f,
683 @location(1) v_channel : vec4f,
684};
685
686@vertex fn vs_main(v : VSIn) -> VSOut {
687 var out : VSOut;
688 out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
689 out.v_texCoord = v.a_texCoord;
690 out.v_channel = v.a_channel;
691
692 out.pos.y = -out.pos.y;
693 return out;
694}
695#pragma end
696
697#pragma fragment wgsl
698struct VSOut {
699 @builtin(position) pos : vec4f,
700 @location(0) v_texCoord : vec2f,
701 @location(1) v_channel : vec4f,
702};
703
704@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
705 return v.v_channel * textureSample(t0, s0, v.v_texCoord).a;
706}
707#pragma end
708)esshader";
709
710inline constexpr const char* MESH = R"esshader(#pragma shader "Mesh"
711#pragma version 300 es
712
713
714
715
716
717#pragma domain Lit
718
719
720
721
722#pragma feature MESH_NORMALS
723
724
725
726
727#pragma feature LIT
728
729
730
731
732#pragma feature SKINNED
733
734
735
736
737#pragma feature NORMAL_MAP
738
739
740
741
742
743#pragma feature MESH_LIGHTMAP
744
745
746#pragma feature MESH_TANGENTS
747
748
749
750
751
752#pragma feature SHADOW_DEPTH
753
754
755
756
757#pragma feature SHADOW_ALPHA
758
759
760
761#pragma feature PREPASS_COVERAGE
762
763
764
765
766
767
768#pragma feature WEIGHT_HEAT
769
770
771
772
773
774
775#pragma vertex
776layout(location = 0) in vec3 a_position;
777layout(location = 1) in vec4 a_color;
778layout(location = 2) in vec2 a_texCoord;
779#ifdef MESH_NORMALS
780layout(location = 3) in vec3 a_normal;
781#endif
782#ifdef SKINNED
783layout(location = 5) in uvec4 a_joints;
784layout(location = 6) in vec4 a_weights;
785#endif
786#ifdef MESH_LIGHTMAP
787layout(location = 7) in vec2 a_texCoord1;
788#endif
789#ifdef MESH_TANGENTS
790layout(location = 4) in vec4 a_tangent;
791#endif
792
793
794
795
796
797
798#ifdef SKINNED
799
800layout(std140) uniform SkinConstants {
801 mat4 u_bones[256];
802};
803#endif
804
805
806
807
808
809
810@@ES_MORPH@@
811
812
813
814invariant gl_Position;
815
816out vec2 v_texCoord;
817out vec4 v_color;
818#ifdef MESH_LIGHTMAP
819
820
821
822
823
824
825
826out highp vec3 v_lightmap;
827#endif
828#ifdef LIT
829out highp vec3 v_worldNormal;
830
831
832out highp vec3 v_worldPos;
833#ifdef MESH_TANGENTS
834out highp vec4 v_worldTangent;
835#endif
836#endif
837#ifdef SHADOW_DEPTH
838
839
840
841
842
843
844out highp vec4 v_shadowClip;
845#endif
846#ifdef ES_VELOCITY
847
848
849
850
851out highp vec4 v_velCur;
852out highp vec4 v_velPrev;
853#endif
854
855
856
857
858#ifdef ES_MATERIAL_VERTEX
859
860highp vec4 es_vertexColor;
861
862highp vec2 es_vertexUv1;
863@@ES_VERTEX_HOOK@@
864#endif
865
866#if defined(ES_VELOCITY) && defined(SKINNED)
867
868highp mat4 esPrevBone(uint joint) {
869 int at = 8 + int(joint) * 3;
870 highp vec4 r0 = esInstanceTexel(at);
871 highp vec4 r1 = esInstanceTexel(at + 1);
872 highp vec4 r2 = esInstanceTexel(at + 2);
873 return mat4(vec4(r0.x, r1.x, r2.x, 0.0), vec4(r0.y, r1.y, r2.y, 0.0),
874 vec4(r0.z, r1.z, r2.z, 0.0), vec4(r0.w, r1.w, r2.w, 1.0));
875}
876#endif
877
878void main() {
879 vec3 local = a_position;
880#ifdef MESH_NORMALS
881 vec3 localNormal = a_normal;
882#else
883 vec3 localNormal = vec3(0.0, 0.0, 1.0);
884#endif
885 esApplyMorph(local, localNormal);
886#ifdef ES_MATERIAL_VERTEX
887 es_vertexColor = a_color;
888#ifdef MESH_LIGHTMAP
889 es_vertexUv1 = a_texCoord1;
890#else
891 es_vertexUv1 = vec2(0.0);
892#endif
893 esMaterialVertex(local, localNormal, a_texCoord);
894#endif
895#ifdef SKINNED
896
897
898 mat4 skin = a_weights.x * u_bones[a_joints.x]
899 + a_weights.y * u_bones[a_joints.y]
900 + a_weights.z * u_bones[a_joints.z]
901 + a_weights.w * u_bones[a_joints.w];
902 vec4 world = skin * vec4(local, 1.0);
903#else
904 mat4 model = esInstanceModel();
905 vec4 world = model * vec4(local, 1.0);
906#endif
907 gl_Position = u_projection * world;
908 v_texCoord = a_texCoord;
909
910#ifdef ES_MATERIAL_VERTEX
911 v_color = es_vertexColor * esInstanceTint();
912#else
913 v_color = a_color * esInstanceTint();
914#endif
915#if defined(WEIGHT_HEAT) && defined(SKINNED)
916 {
917 uint chosen = uint(esInstanceTint().x);
918 vec4 own = vec4(equal(a_joints, uvec4(chosen)));
919 float heat = dot(own, a_weights);
920
921 v_color = vec4(heat < 0.01 ? vec3(0.1) : mix(vec3(0.2, 0.4, 1.0), vec3(1.0, 0.25, 0.2), clamp(heat, 0.0, 1.0)), 1.0);
922 }
923#endif
924#ifdef MESH_LIGHTMAP
925 highp vec4 lmRect = esInstanceLightmapRect();
926 v_lightmap = vec3(a_texCoord1 * lmRect.xy + lmRect.zw,
927 lmRect.x > 0.0 ? 1.0 : 0.0);
928#endif
929#ifdef SHADOW_DEPTH
930 v_shadowClip = gl_Position;
931#endif
932#ifdef ES_VELOCITY
933 {
934
935
936#ifdef SKINNED
937
938 highp mat4 prevSkin = a_weights.x * esPrevBone(a_joints.x) + a_weights.y * esPrevBone(a_joints.y)
939 + a_weights.z * esPrevBone(a_joints.z) + a_weights.w * esPrevBone(a_joints.w);
940 highp vec4 L = prevSkin * vec4(local, 1.0);
941#else
942 highp vec4 L = vec4(local, 1.0);
943#endif
944 highp vec4 before = esInstanceTint();
945 v_velCur = gl_Position;
946 highp vec4 prev = vec4(dot(esInstanceTexel(4), L), dot(esInstanceTexel(5), L),
947 dot(esInstanceTexel(6), L), dot(esInstanceTexel(7), L));
948 v_velPrev = vec4(prev.xy + before.xy * prev.w, prev.zw);
949 }
950#endif
951
952
953 v_probeSlot = float(ES_INSTANCE_ID);
954#ifdef LIT
955#if defined(MESH_NORMALS) && defined(SKINNED)
956 v_worldNormal = mat3(skin) * localNormal;
957#elif defined(MESH_NORMALS)
958 v_worldNormal = esInstanceNormalMatrix() * localNormal;
959#else
960
961 v_worldNormal = vec3(0.0, 0.0, 1.0);
962#endif
963 v_worldPos = world.xyz;
964#ifdef MESH_TANGENTS
965#ifdef SKINNED
966 v_worldTangent = vec4(mat3(skin) * a_tangent.xyz, a_tangent.w);
967#else
968 mat3 tangentModel = mat3(esInstanceModel());
969
970 v_worldTangent = vec4(tangentModel * a_tangent.xyz, determinant(tangentModel) < 0.0 ? -a_tangent.w : a_tangent.w);
971#endif
972#endif
973#endif
974}
975#pragma end
976
977#pragma fragment
978precision mediump float;
979
980in vec2 v_texCoord;
981in vec4 v_color;
982#ifdef LIT
983in highp vec3 v_worldNormal;
984in highp vec3 v_worldPos;
985#endif
986#ifdef SHADOW_DEPTH
987in highp vec4 v_shadowClip;
988#endif
989#ifdef ES_VELOCITY
990in highp vec4 v_velCur;
991in highp vec4 v_velPrev;
992#endif
993
994uniform sampler2D u_texture;
995#ifdef NORMAL_MAP
996uniform sampler2D u_normalMap;
997#endif
998
999#ifdef ES_GBUFFER
1000
1001
1002
1003
1004layout(location = 0) out vec4 fragColor;
1005layout(location = 1) out vec4 fragSurface;
1006
1007layout(location = 2) out vec4 fragAlbedo;
1008
1009vec2 packNormal(vec3 n) {
1010 n /= (abs(n.x) + abs(n.y) + abs(n.z));
1011 vec2 sgn = vec2(n.x >= 0.0 ? 1.0 : -1.0, n.y >= 0.0 ? 1.0 : -1.0);
1012 vec2 e = n.z >= 0.0 ? n.xy : (1.0 - abs(n.yx)) * sgn;
1013 return e * 0.5 + 0.5;
1014}
1015#else
1016out vec4 fragColor;
1017#endif
1018
1019
1020
1021highp vec2 coverageUv() {
1022#ifdef ES_UV_TRANSFORM
1023 highp vec2 uv = v_texCoord * u_uvTiling;
1024 if (u_uvRotation != 0.0) {
1025 float c = cos(u_uvRotation), sn = sin(u_uvRotation);
1026 uv = vec2(c * uv.x + sn * uv.y, -sn * uv.x + c * uv.y);
1027 }
1028 return uv + u_uvOffset;
1029#else
1030 return v_texCoord;
1031#endif
1032}
1033
1034void main() {
1035#ifdef ES_VELOCITY
1036
1037
1038
1039 highp vec2 now = v_velCur.xy / v_velCur.w;
1040 highp vec2 was = v_velPrev.xy / max(v_velPrev.w, 1e-6);
1041 fragColor = vec4((was - now) * 0.5, gl_FragCoord.z, v_velPrev.w > 1e-6 ? 1.0 : 0.0);
1042#elif defined(SHADOW_DEPTH)
1043#ifdef PREPASS_COVERAGE
1044
1045
1046 highp float baseAlpha = texture(u_texture, coverageUv()).a * v_color.a * u_tint.a;
1047 highp float coverage = (baseAlpha - u_alphaCutoff) / max(fwidth(baseAlpha), 1e-4) + 0.5;
1048 if (coverage <= 0.0) discard;
1049 fragColor = vec4(0.0, 0.0, 0.0, min(coverage, 1.0));
1050#else
1051#ifdef SHADOW_ALPHA
1052
1053
1054 if (texture(u_texture, coverageUv()).a * v_color.a < u_alphaCutoff) discard;
1055#endif
1056
1057
1058
1059 fragColor = vec4(packDepth(clamp(v_shadowClip.z / v_shadowClip.w * 0.5 + 0.5,
1060 0.0, 1.0)), 1.0);
1061#endif
1062#else
1063 vec4 base = texture(u_texture, v_texCoord) * v_color;
1064#ifdef LIT
1065 highp vec3 N = normalize(v_worldNormal);
1066#ifdef NORMAL_MAP
1067 N = perturbNormal(N, v_worldPos, v_texCoord, sampleNormal(u_normalMap, v_texCoord));
1068#endif
1069
1070 if (!gl_FrontFacing) N = -N;
1071
1072
1073
1074
1075
1076
1077 vec3 shaded = applyLightingPBR(base.rgb, N, v_worldPos, viewDirection(v_worldPos),
1078 0.0, 1.0, 1.0, 1.0);
1079 fragColor = vec4(shaded, base.a);
1080#ifdef ES_GBUFFER
1081 fragSurface = vec4(packNormal(normalize(N)), 1.0, 0.75);
1082 fragAlbedo = vec4(base.rgb, 1.0);
1083#endif
1084#else
1085#ifdef MESH_LIGHTMAP
1086
1087
1088
1089 fragColor = vec4(base.rgb * mix(vec3(1.0), bakedIrradiance(), v_lightmap.z), base.a);
1090#else
1091 fragColor = base;
1092#endif
1093#ifdef ES_GBUFFER
1094
1095
1096 fragSurface = vec4(packNormal(vec3(0.0, 1.0, 0.0)), 1.0, 0.75);
1097 fragAlbedo = vec4(base.rgb, 1.0);
1098#endif
1099#endif
1100#endif
1101}
1102#pragma end
1103
1104#pragma vertex wgsl
1105#ifdef SKINNED
1106struct SkinConstants { bones : array<mat4x4f, 256> };
1107@group(0) @binding(5) var<uniform> skin : SkinConstants;
1108#endif
1109
1110
1111@@ES_MORPH@@
1112
1113struct VSIn {
1114 @location(0) a_position : vec3f,
1115 @location(1) a_color : vec4f,
1116 @location(2) a_texCoord : vec2f,
1117#ifdef MESH_NORMALS
1118 @location(3) a_normal : vec3f,
1119#endif
1120#ifdef SKINNED
1121 @location(5) a_joints : vec4u,
1122 @location(6) a_weights : vec4f,
1123#endif
1124#ifdef MESH_LIGHTMAP
1125 @location(7) a_texCoord1 : vec2f,
1126#endif
1127#ifdef MESH_TANGENTS
1128 @location(4) a_tangent : vec4f,
1129#endif
1130
1131 @builtin(vertex_index) vertexIndex : u32,
1132};
1133struct VSOut {
1134 @builtin(position) @invariant pos : vec4f,
1135 @location(0) v_texCoord : vec2f,
1136 @location(1) v_color : vec4f,
1137#ifdef LIT
1138 @location(2) v_worldNormal : vec3f,
1139 @location(3) v_worldPos : vec3f,
1140#ifdef MESH_TANGENTS
1141 @location(8) v_worldTangent : vec4f,
1142#endif
1143#endif
1144#ifdef SHADOW_DEPTH
1145
1146 @location(4) v_shadowClip : vec4f,
1147#endif
1148#ifdef ES_VELOCITY
1149 @location(4) v_velCur : vec4f,
1150 @location(7) v_velPrev : vec4f,
1151#endif
1152#ifdef MESH_LIGHTMAP
1153 @location(5) v_lightmap : vec3f,
1154#endif
1155
1156
1157 @location(6) @interpolate(flat) v_probeSlot : f32,
1158};
1159
1160#ifdef ES_MATERIAL_VERTEX
1161var<private> es_vertexColor : vec4f;
1162var<private> es_vertexUv1 : vec2f;
1163@@ES_VERTEX_HOOK@@
1164#endif
1165
1166#ifdef ES_VELOCITY
1167#ifdef SKINNED
1168
1169fn esPrevBone(joint : u32) -> mat4x4f {
1170 let at = 8 + i32(joint) * 3;
1171 let r0 = esInstanceTexel(at);
1172 let r1 = esInstanceTexel(at + 1);
1173 let r2 = esInstanceTexel(at + 2);
1174 return mat4x4f(vec4f(r0.x, r1.x, r2.x, 0.0), vec4f(r0.y, r1.y, r2.y, 0.0),
1175 vec4f(r0.z, r1.z, r2.z, 0.0), vec4f(r0.w, r1.w, r2.w, 1.0));
1176}
1177#endif
1178#endif
1179
1180@vertex fn vs_main(v : VSIn, @builtin(instance_index) inst : u32) -> VSOut {
1181 g_instanceId = i32(inst);
1182 var local = v.a_position;
1183#ifdef MESH_NORMALS
1184 var localNormal = v.a_normal;
1185#else
1186 var localNormal = vec3f(0.0, 0.0, 1.0);
1187#endif
1188
1189
1190 esApplyMorph(&local, &localNormal, v.vertexIndex);
1191#ifdef ES_MATERIAL_VERTEX
1192 es_vertexColor = v.a_color;
1193#ifdef MESH_LIGHTMAP
1194 es_vertexUv1 = v.a_texCoord1;
1195#else
1196 es_vertexUv1 = vec2f(0.0);
1197#endif
1198 esMaterialVertex(&local, &localNormal, v.a_texCoord);
1199#endif
1200#ifdef SKINNED
1201
1202
1203 let pose = v.a_weights.x * skin.bones[v.a_joints.x]
1204 + v.a_weights.y * skin.bones[v.a_joints.y]
1205 + v.a_weights.z * skin.bones[v.a_joints.z]
1206 + v.a_weights.w * skin.bones[v.a_joints.w];
1207 let world = pose * vec4f(local, 1.0);
1208#else
1209 let world = esInstanceModel() * vec4f(local, 1.0);
1210#endif
1211
1212 var out : VSOut;
1213 out.pos = frame.projection * world;
1214 out.v_texCoord = v.a_texCoord;
1215#ifdef ES_MATERIAL_VERTEX
1216 out.v_color = es_vertexColor * esInstanceTint();
1217#else
1218 out.v_color = v.a_color * esInstanceTint();
1219#endif
1220#ifdef WEIGHT_HEAT
1221#ifdef SKINNED
1222 {
1223 let chosen = u32(esInstanceTint().x);
1224 let own = vec4f(v.a_joints == vec4u(chosen));
1225 let heat = dot(own, v.a_weights);
1226 out.v_color = vec4f(select(mix(vec3f(0.2, 0.4, 1.0), vec3f(1.0, 0.25, 0.2), clamp(heat, 0.0, 1.0)),
1227 vec3f(0.1), heat < 0.01), 1.0);
1228 }
1229#endif
1230#endif
1231#ifdef MESH_LIGHTMAP
1232 let lmRect = esInstanceLightmapRect();
1233 out.v_lightmap = vec3f(v.a_texCoord1 * lmRect.xy + lmRect.zw,
1234 select(0.0, 1.0, lmRect.x > 0.0));
1235#endif
1236#ifdef SHADOW_DEPTH
1237 out.v_shadowClip = out.pos;
1238#endif
1239#ifdef ES_VELOCITY
1240 {
1241
1242#ifdef SKINNED
1243
1244 let prevSkin = v.a_weights.x * esPrevBone(v.a_joints.x) + v.a_weights.y * esPrevBone(v.a_joints.y)
1245 + v.a_weights.z * esPrevBone(v.a_joints.z) + v.a_weights.w * esPrevBone(v.a_joints.w);
1246 let L = prevSkin * vec4f(local, 1.0);
1247#else
1248 let L = vec4f(local, 1.0);
1249#endif
1250 let before = esInstanceTint();
1251 out.v_velCur = out.pos;
1252 let prev = vec4f(dot(esInstanceTexel(4), L), dot(esInstanceTexel(5), L),
1253 dot(esInstanceTexel(6), L), dot(esInstanceTexel(7), L));
1254 out.v_velPrev = vec4f(prev.xy + before.xy * prev.w, prev.zw);
1255 }
1256#endif
1257
1258 out.v_probeSlot = f32(inst);
1259#ifdef LIT
1260#ifdef MESH_NORMALS
1261#ifdef SKINNED
1262 out.v_worldNormal = mat3x3f(pose[0].xyz, pose[1].xyz, pose[2].xyz) * localNormal;
1263#else
1264 out.v_worldNormal = esInstanceNormalMatrix() * localNormal;
1265#endif
1266#else
1267
1268)esshader"
1269 R"esshader( out.v_worldNormal = vec3f(0.0, 0.0, 1.0);
1270#endif
1271 out.v_worldPos = world.xyz;
1272#ifdef MESH_TANGENTS
1273#ifdef SKINNED
1274 out.v_worldTangent = vec4f(mat3x3f(pose[0].xyz, pose[1].xyz, pose[2].xyz) * v.a_tangent.xyz, v.a_tangent.w);
1275#else
1276 let tangentModel = mat3x3f(esInstanceModel()[0].xyz, esInstanceModel()[1].xyz, esInstanceModel()[2].xyz);
1277 out.v_worldTangent = vec4f(tangentModel * v.a_tangent.xyz,
1278 select(v.a_tangent.w, -v.a_tangent.w, determinant(tangentModel) < 0.0));
1279#endif
1280#endif
1281#endif
1282
1283 out.pos.y = -out.pos.y;
1284 return out;
1285}
1286#pragma end
1287
1288#pragma fragment wgsl
1289@group(1) @binding(0) var t0 : texture_2d<f32>;
1290@group(1) @binding(8) var s0 : sampler;
1291#ifdef NORMAL_MAP
1292@group(1) @binding(1) var t1 : texture_2d<f32>;
1293@group(1) @binding(9) var s1 : sampler;
1294#endif
1295#ifdef ES_RECEIVE_SHADOW
1296
1297
1298@group(1) @binding(2) var t2 : texture_2d<f32>;
1299@group(1) @binding(10) var s2 : sampler;
1300#endif
1301#ifdef ES_ENV_MAP
1302
1303
1304
1305@group(1) @binding(3) var t3 : texture_2d<f32>;
1306@group(1) @binding(11) var s3 : sampler;
1307#endif
1308#ifdef MESH_LIGHTMAP
1309
1310@group(1) @binding(5) var t5 : texture_2d<f32>;
1311@group(1) @binding(13) var s5 : sampler;
1312#endif
1313
1314
1315@group(1) @binding(7) var t7 : texture_2d<f32>;
1316@group(1) @binding(15) var s7 : sampler;
1317
1318struct VSOut {
1319 @builtin(position) pos : vec4f,
1320 @location(0) v_texCoord : vec2f,
1321 @location(1) v_color : vec4f,
1322#ifdef LIT
1323 @location(2) v_worldNormal : vec3f,
1324 @location(3) v_worldPos : vec3f,
1325#ifdef MESH_TANGENTS
1326 @location(8) v_worldTangent : vec4f,
1327#endif
1328#endif
1329#ifdef SHADOW_DEPTH
1330 @location(4) v_shadowClip : vec4f,
1331#endif
1332#ifdef ES_VELOCITY
1333 @location(4) v_velCur : vec4f,
1334 @location(7) v_velPrev : vec4f,
1335#endif
1336#ifdef MESH_LIGHTMAP
1337 @location(5) v_lightmap : vec3f,
1338#endif
1339
1340
1341 @location(6) @interpolate(flat) v_probeSlot : f32,
1342};
1343
1344
1345fn coverageUv(uv0 : vec2f) -> vec2f {
1346#ifdef ES_UV_TRANSFORM
1347 var uv = uv0 * mc.u_uvTiling;
1348 if (mc.u_uvRotation != 0.0) {
1349 let c = cos(mc.u_uvRotation);
1350 let sn = sin(mc.u_uvRotation);
1351 uv = vec2f(c * uv.x + sn * uv.y, -sn * uv.x + c * uv.y);
1352 }
1353 return uv + mc.u_uvOffset;
1354#else
1355 return uv0;
1356#endif
1357}
1358
1359#ifdef ES_GBUFFER
1360
1361struct FragOut {
1362 @location(0) color : vec4f,
1363 @location(1) surface : vec4f,
1364 @location(2) albedo : vec4f,
1365};
1366
1367fn packNormal(n : vec3f) -> vec2f {
1368 let a = n / (abs(n.x) + abs(n.y) + abs(n.z));
1369 let sgn = vec2f(select(-1.0, 1.0, a.x >= 0.0), select(-1.0, 1.0, a.y >= 0.0));
1370 var e = a.xy;
1371 if (a.z < 0.0) { e = (vec2f(1.0) - abs(a.yx)) * sgn; }
1372 return e * 0.5 + 0.5;
1373}
1374
1375@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> FragOut {
1376#else
1377@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {
1378#endif
1379
1380
1381 g_probeSlot = v.v_probeSlot;
1382#ifdef MESH_LIGHTMAP
1383 g_lightmap = v.v_lightmap;
1384#endif
1385#ifdef MESH_TANGENTS
1386#ifdef LIT
1387 g_worldTangent = v.v_worldTangent;
1388#endif
1389#endif
1390#ifdef ES_VELOCITY
1391
1392 let now = v.v_velCur.xy / v.v_velCur.w;
1393 let was = v.v_velPrev.xy / max(v.v_velPrev.w, 1e-6);
1394 return vec4f((was - now) * 0.5, v.pos.z, select(0.0, 1.0, v.v_velPrev.w > 1e-6));
1395#elif defined(SHADOW_DEPTH)
1396#ifdef PREPASS_COVERAGE
1397
1398 let cuv = coverageUv(v.v_texCoord);
1399 let duvx = dpdx(cuv);
1400 let duvy = dpdy(cuv);
1401 let baseAlpha = textureSampleGrad(t0, s0, cuv, duvx, duvy).a * v.v_color.a * mc.u_tint.a;
1402 let edge = max(fwidth(baseAlpha), 1e-4);
1403 let coverage = (baseAlpha - mc.u_alphaCutoff) / max(edge, 1e-4) + 0.5;
1404 if (coverage <= 0.0) { discard; }
1405 return vec4f(0.0, 0.0, 0.0, min(coverage, 1.0));
1406#else
1407#ifdef SHADOW_ALPHA
1408
1409 let cuv = coverageUv(v.v_texCoord);
1410 let duvx = dpdx(cuv);
1411 let duvy = dpdy(cuv);
1412 if (textureSampleGrad(t0, s0, cuv, duvx, duvy).a * v.v_color.a < mc.u_alphaCutoff) { discard; }
1413#endif
1414
1415
1416
1417 return vec4f(packDepth(clamp(v.v_shadowClip.z / v.v_shadowClip.w * 0.5 + 0.5,
1418 0.0, 1.0)), 1.0);
1419#endif
1420#else
1421
1422
1423 let duvx = dpdx(v.v_texCoord);
1424 let duvy = dpdy(v.v_texCoord);
1425 let base = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy) * v.v_color;
1426#ifdef LIT
1427 var N = normalize(v.v_worldNormal);
1428#ifdef NORMAL_MAP
1429 N = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormalGrad(t1, s1, v.v_texCoord, duvx, duvy));
1430#endif
1431 if (!front) { N = -N; }
1432
1433
1434
1435
1436 let shaded = applyLightingPBR(base.rgb, N, v.v_worldPos, viewDirection(v.v_worldPos),
1437 0.0, 1.0, 1.0, 1.0);
1438#ifdef ES_GBUFFER
1439 var frag : FragOut;
1440 frag.color = vec4f(shaded, base.a);
1441 frag.surface = vec4f(packNormal(normalize(N)), 1.0, 0.75);
1442 frag.albedo = vec4f(base.rgb, 1.0);
1443 return frag;
1444#else
1445 return vec4f(shaded, base.a);
1446#endif
1447#else
1448#ifdef MESH_LIGHTMAP
1449
1450 let unlit = vec4f(base.rgb * mix(vec3f(1.0), bakedIrradiance(), vec3f(v.v_lightmap.z)), base.a);
1451#else
1452 let unlit = base;
1453#endif
1454#ifdef ES_GBUFFER
1455
1456 var frag : FragOut;
1457 frag.color = unlit;
1458 frag.surface = vec4f(packNormal(vec3f(0.0, 1.0, 0.0)), 1.0, 0.75);
1459 frag.albedo = vec4f(base.rgb, 1.0);
1460 return frag;
1461#else
1462 return unlit;
1463#endif
1464#endif
1465#endif
1466}
1467#pragma end
1468)esshader";
1469
1470inline constexpr const char* PARTICLE = R"esshader(#pragma shader "ParticleInstance"
1471#pragma version 300 es
1472
1473
1474
1475
1476
1477#pragma fragment
1478precision mediump float;
1479
1480in vec4 v_color;
1481in vec2 v_texCoord;
1482
1483uniform sampler2D u_textures[8];
1484
1485out vec4 fragColor;
1486
1487void main() {
1488 vec4 texColor = texture(u_textures[0], v_texCoord);
1489 fragColor = texColor * v_color;
1490}
1491#pragma end
1492
1493
1494
1495#pragma fragment wgsl
1496@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
1497
1498 let texColor = textureSampleGrad(t0, s0, v.v_texCoord, dpdx(v.v_texCoord), dpdy(v.v_texCoord));
1499 return texColor * v.v_color;
1500}
1501#pragma end
1502)esshader";
1503
1504inline constexpr const char* SHADOW2D = R"esshader(#pragma shader "Shadow2D"
1505#pragma version 300 es
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516#pragma vertex
1517layout(location = 0) in vec2 a_position;
1518layout(location = 1) in float a_shadow;
1519layout(location = 2) in vec4 a_channel;
1520
1521out float v_shadow;
1522out vec4 v_channel;
1523
1524void main() {
1525 gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
1526 v_shadow = a_shadow;
1527 v_channel = a_channel;
1528}
1529#pragma end
1530
1531#pragma fragment
1532precision highp float;
1533
1534in float v_shadow;
1535in vec4 v_channel;
1536
1537out vec4 fragColor;
1538
1539void main() {
1540 fragColor = v_channel * v_shadow;
1541}
1542#pragma end
1543
1544#pragma vertex wgsl
1545struct VSIn {
1546 @location(0) a_position : vec2f,
1547 @location(1) a_shadow : f32,
1548 @location(2) a_channel : vec4f,
1549};
1550struct VSOut {
1551 @builtin(position) pos : vec4f,
1552 @location(0) v_shadow : f32,
1553 @location(1) v_channel : vec4f,
1554};
1555
1556@vertex fn vs_main(v : VSIn) -> VSOut {
1557 var out : VSOut;
1558 out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
1559 out.v_shadow = v.a_shadow;
1560 out.v_channel = v.a_channel;
1561
1562 out.pos.y = -out.pos.y;
1563 return out;
1564}
1565#pragma end
1566
1567#pragma fragment wgsl
1568struct VSOut {
1569 @builtin(position) pos : vec4f,
1570 @location(0) v_shadow : f32,
1571 @location(1) v_channel : vec4f,
1572};
1573
1574@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
1575 return v.v_channel * v.v_shadow;
1576}
1577#pragma end
1578)esshader";
1579
1580inline constexpr const char* SHAPE = R"esshader(#pragma shader "Shape"
1581#pragma version 300 es
1582
1583#pragma vertex
1584layout(location = 0) in vec2 a_position;
1585layout(location = 1) in vec2 a_texCoord;
1586layout(location = 2) in vec4 a_color;
1587layout(location = 3) in vec4 a_shapeInfo;
1588
1589out vec2 v_uv;
1590out vec4 v_color;
1591out vec4 v_shapeInfo;
1592
1593void main() {
1594 gl_Position = u_projection * vec4(a_position, 0.0, 1.0);
1595 v_uv = a_texCoord;
1596 v_color = a_color;
1597 v_shapeInfo = a_shapeInfo;
1598}
1599#pragma end
1600
1601#pragma fragment
1602precision mediump float;
1603
1604in vec2 v_uv;
1605in vec4 v_color;
1606in vec4 v_shapeInfo;
1607
1608out vec4 fragColor;
1609
1610void main() {
1611 vec2 halfSize = v_shapeInfo.yz;
1612 float cornerRadius = v_shapeInfo.w;
1613 vec2 p = v_uv * halfSize;
1614
1615 float dist;
1616 float shapeType = v_shapeInfo.x;
1617
1618 if (shapeType < 0.5) {
1619 float r = min(halfSize.x, halfSize.y);
1620 dist = length(p) - r;
1621 } else if (shapeType < 1.5) {
1622 float r = min(halfSize.x, halfSize.y);
1623 vec2 elongation = halfSize - vec2(r);
1624 vec2 q = abs(p) - elongation;
1625 dist = length(max(q, 0.0)) - r;
1626 } else {
1627 float r = min(cornerRadius, min(halfSize.x, halfSize.y));
1628 vec2 q = abs(p) - halfSize + vec2(r);
1629 dist = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
1630 }
1631
1632 float fw = fwidth(dist);
1633 float alpha = 1.0 - smoothstep(-fw, fw, dist);
1634 if (alpha < 0.001) discard;
1635 fragColor = vec4(v_color.rgb, v_color.a * alpha);
1636}
1637#pragma end
1638
1639#pragma vertex wgsl
1640struct VSIn {
1641 @location(0) a_position : vec2f,
1642 @location(1) a_texCoord : vec2f,
1643 @location(2) a_color : vec4f,
1644 @location(3) a_shapeInfo : vec4f,
1645};
1646struct VSOut {
1647 @builtin(position) pos : vec4f,
1648 @location(0) v_uv : vec2f,
1649 @location(1) v_color : vec4f,
1650 @location(2) v_shapeInfo : vec4f,
1651};
1652
1653@vertex fn vs_main(v : VSIn) -> VSOut {
1654 var out : VSOut;
1655 out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
1656 out.v_uv = v.a_texCoord;
1657 out.v_color = v.a_color;
1658 out.v_shapeInfo = v.a_shapeInfo;
1659
1660 out.pos.y = -out.pos.y;
1661 return out;
1662}
1663#pragma end
1664
1665#pragma fragment wgsl
1666struct VSOut {
1667 @builtin(position) pos : vec4f,
1668 @location(0) v_uv : vec2f,
1669 @location(1) v_color : vec4f,
1670 @location(2) v_shapeInfo : vec4f,
1671};
1672
1673@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
1674 let halfSize = v.v_shapeInfo.yz;
1675 let cornerRadius = v.v_shapeInfo.w;
1676 let p = v.v_uv * halfSize;
1677 let shapeType = v.v_shapeInfo.x;
1678
1679 var dist : f32;
1680 if (shapeType < 0.5) {
1681 let r = min(halfSize.x, halfSize.y);
1682 dist = length(p) - r;
1683 } else if (shapeType < 1.5) {
1684 let r = min(halfSize.x, halfSize.y);
1685 let elongation = halfSize - vec2f(r, r);
1686 let q = abs(p) - elongation;
1687 dist = length(max(q, vec2f(0.0, 0.0))) - r;
1688 } else {
1689 let r = min(cornerRadius, min(halfSize.x, halfSize.y));
1690 let q = abs(p) - halfSize + vec2f(r, r);
1691 dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - r;
1692 }
1693
1694 let fw = fwidth(dist);
1695 let alpha = 1.0 - smoothstep(-fw, fw, dist);
1696 if (alpha < 0.001) { discard; }
1697 return vec4f(v.v_color.rgb, v.v_color.a * alpha);
1698}
1699#pragma end
1700)esshader";
1701
1702inline constexpr const char* SKY = R"esshader(#pragma shader "Sky"
1703#pragma version 300 es
1704#pragma domain Lit
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714#pragma vertex
1715layout(location = 0) in vec3 a_position;
1716
1717out highp vec3 v_worldPos;
1718
1719void main() {
1720 gl_Position = u_projection * vec4(a_position, 1.0);
1721 v_worldPos = a_position;
1722}
1723#pragma end
1724
1725#pragma fragment
1726precision highp float;
1727
1728in highp vec3 v_worldPos;
1729out vec4 fragColor;
1730
1731#ifdef ES_SKY_PANORAMA
1732uniform sampler2D u_texture;
1733
1734
1735
1736highp vec3 skyTexel(in ivec2 p, in ivec2 size) {
1737 ivec2 q = ivec2(((p.x % size.x) + size.x) % size.x, clamp(p.y, 0, size.y - 1));
1738 highp vec4 t = texelFetch(u_texture, q, 0);
1739 return t.rgb * t.rgb * (t.a * t.a * u_envParams.y);
1740}
1741
1742
1743
1744highp vec3 skyPanorama(in highp vec3 d) {
1745 ivec2 size = textureSize(u_texture, 0);
1746 highp float u = 0.5 - atan(d.x, d.z) / 6.28318530718;
1747 highp float v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
1748 highp vec2 f = vec2(u * float(size.x), v * float(size.y)) - 0.5;
1749 ivec2 i = ivec2(floor(f));
1750 highp vec2 t = f - floor(f);
1751 return mix(mix(skyTexel(i, size), skyTexel(i + ivec2(1, 0), size), t.x),
1752 mix(skyTexel(i + ivec2(0, 1), size), skyTexel(i + ivec2(1, 1), size), t.x), t.y);
1753}
1754#endif
1755
1756void main() {
1757#ifdef ES_SKY_PANORAMA
1758 fragColor = vec4(skyPanorama(envDirection(-viewDirection(v_worldPos))), 1.0);
1759#else
1760
1761
1762 fragColor = vec4(envSampleMip(-viewDirection(v_worldPos), 0.0), 1.0);
1763#endif
1764}
1765#pragma end
1766
1767#pragma vertex wgsl
1768struct VSIn {
1769 @location(0) a_position : vec3f,
1770};
1771struct VSOut {
1772 @builtin(position) pos : vec4f,
1773 @location(0) v_worldPos : vec3f,
1774};
1775
1776@vertex fn vs_main(v : VSIn) -> VSOut {
1777 var out : VSOut;
1778 out.pos = frame.projection * vec4f(v.a_position, 1.0);
1779 out.v_worldPos = v.a_position;
1780
1781 out.pos.y = -out.pos.y;
1782 return out;
1783}
1784#pragma end
1785
1786#pragma fragment wgsl
1787
1788
1789
1790
1791@group(1) @binding(0) var t0 : texture_2d<f32>;
1792@group(1) @binding(8) var s0 : sampler;
1793@group(1) @binding(3) var t3 : texture_2d<f32>;
1794@group(1) @binding(11) var s3 : sampler;
1795
1796@group(1) @binding(7) var t7 : texture_2d<f32>;
1797@group(1) @binding(15) var s7 : sampler;
1798
1799
1800
1801
1802struct VSOut {
1803 @builtin(position) pos : vec4f,
1804 @location(0) v_worldPos : vec3f,
1805};
1806
1807#ifdef ES_SKY_PANORAMA
1808fn skyTexel(p : vec2i, size : vec2i) -> vec3f {
1809 let q = vec2i(((p.x % size.x) + size.x) % size.x, clamp(p.y, 0, size.y - 1));
1810 let t = textureLoad(t0, q, 0);
1811 return t.rgb * t.rgb * (t.a * t.a * lc.u_envParams.y);
1812}
1813
1814fn skyPanorama(d : vec3f) -> vec3f {
1815 let size = vec2i(textureDimensions(t0, 0));
1816 let u = 0.5 - atan2(d.x, d.z) / 6.28318530718;
1817 let v = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265359;
1818 let f = vec2f(u * f32(size.x), v * f32(size.y)) - 0.5;
1819 let i = vec2i(floor(f));
1820 let t = f - floor(f);
1821 return mix(mix(skyTexel(i, size), skyTexel(i + vec2i(1, 0), size), t.x),
1822 mix(skyTexel(i + vec2i(0, 1), size), skyTexel(i + vec2i(1, 1), size), t.x), t.y);
1823}
1824#endif
1825
1826@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
1827#ifdef ES_SKY_PANORAMA
1828 return vec4f(skyPanorama(envDirection(-viewDirection(v.v_worldPos))), 1.0);
1829#else
1830 return vec4f(envSampleMip(-viewDirection(v.v_worldPos), 0.0), 1.0);
1831#endif
1832}
1833#pragma end
1834)esshader";
1835
1836} // namespace esengine::ShaderEmbeds
constexpr const char * LIGHTSHAPE2D
constexpr const char * PARTICLE
constexpr const char * SHADOW2D
constexpr const char * BATCH