BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / gl / builtinShaders.js

builtinShaders.js

builtinShaders.js 6.7 KiB Download Raw
1// Built-in screen-space / effect shaders. Games can use these without fetching files.
2// Expand this set over time into the default framework shader bundle.
3
4Shader.Builtin = Shader.Builtin || {};
5
6Shader.Builtin.BLOOM_BRIGHT_FRAG = [
7 "precision mediump float;",
8 "uniform sampler2D u_texture;",
9 "uniform float u_threshold;",
10 "uniform float u_softKnee;",
11 "varying vec2 v_texCoord;",
12 "void main() {",
13 " vec4 color = texture2D(u_texture, v_texCoord);",
14 " float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));",
15 " float knee = max(u_softKnee, 0.001);",
16 " float soft = smoothstep(u_threshold, u_threshold + knee, lum);",
17 " gl_FragColor = vec4(color.rgb * soft, 1.0);",
18 "}"
19].join("\n");
20
21// Max-filter dilate: fattens 1–4px hotspots so later blur has something to spread.
22Shader.Builtin.BLOOM_DILATE_FRAG = [
23 "precision mediump float;",
24 "uniform sampler2D u_texture;",
25 "uniform vec2 u_resolution;",
26 "uniform float u_radius;",
27 "varying vec2 v_texCoord;",
28 "void main() {",
29 " vec2 px = u_radius / max(u_resolution, vec2(1.0));",
30 " vec4 c = texture2D(u_texture, v_texCoord);",
31 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, 0.0)));",
32 " c = max(c, texture2D(u_texture, v_texCoord - vec2(px.x, 0.0)));",
33 " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px.y)));",
34 " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px.y)));",
35 " c = max(c, texture2D(u_texture, v_texCoord + px));",
36 " c = max(c, texture2D(u_texture, v_texCoord - px));",
37 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px.x, -px.y)));",
38 " c = max(c, texture2D(u_texture, v_texCoord + vec2(-px.x, px.y)));",
39 " vec2 px2 = px * 2.0;",
40 " c = max(c, texture2D(u_texture, v_texCoord + vec2(px2.x, 0.0)));",
41 " c = max(c, texture2D(u_texture, v_texCoord - vec2(px2.x, 0.0)));",
42 " c = max(c, texture2D(u_texture, v_texCoord + vec2(0.0, px2.y)));",
43 " c = max(c, texture2D(u_texture, v_texCoord - vec2(0.0, px2.y)));",
44 " gl_FragColor = c;",
45 "}"
46].join("\n");
47
48// Wider separable blur (13-tap). u_direction (1,0)/(0,1); u_blurSize in texels.
49Shader.Builtin.BLOOM_BLUR_FRAG = [
50 "precision mediump float;",
51 "uniform sampler2D u_texture;",
52 "uniform vec2 u_resolution;",
53 "uniform vec2 u_direction;",
54 "uniform float u_blurSize;",
55 "varying vec2 v_texCoord;",
56 "void main() {",
57 " vec2 step = u_direction * u_blurSize / max(u_resolution, vec2(1.0));",
58 " vec4 c = vec4(0.0);",
59 " c += texture2D(u_texture, v_texCoord - step * 6.0) * 0.009;",
60 " c += texture2D(u_texture, v_texCoord - step * 5.0) * 0.019;",
61 " c += texture2D(u_texture, v_texCoord - step * 4.0) * 0.038;",
62 " c += texture2D(u_texture, v_texCoord - step * 3.0) * 0.071;",
63 " c += texture2D(u_texture, v_texCoord - step * 2.0) * 0.111;",
64 " c += texture2D(u_texture, v_texCoord - step) * 0.147;",
65 " c += texture2D(u_texture, v_texCoord) * 0.160;",
66 " c += texture2D(u_texture, v_texCoord + step) * 0.147;",
67 " c += texture2D(u_texture, v_texCoord + step * 2.0) * 0.111;",
68 " c += texture2D(u_texture, v_texCoord + step * 3.0) * 0.071;",
69 " c += texture2D(u_texture, v_texCoord + step * 4.0) * 0.038;",
70 " c += texture2D(u_texture, v_texCoord + step * 5.0) * 0.019;",
71 " c += texture2D(u_texture, v_texCoord + step * 6.0) * 0.009;",
72 " gl_FragColor = c;",
73 "}"
74].join("\n");
75
76// Combine: add bloom while keeping / boosting chroma (avoids gray wash at high intensity).
77Shader.Builtin.BLOOM_COMBINE_FRAG = [
78 "precision mediump float;",
79 "uniform sampler2D u_texture;",
80 "uniform sampler2D u_scene;",
81 "uniform float u_intensity;",
82 "uniform float u_saturation;",
83 "uniform float u_colorPreserve;",
84 "varying vec2 v_texCoord;",
85 "void main() {",
86 " vec4 scene = texture2D(u_scene, v_texCoord);",
87 " vec4 bloom = texture2D(u_texture, v_texCoord);",
88 " vec3 bloomCol = bloom.rgb;",
89 " float bloomLum = dot(bloomCol, vec3(0.299, 0.587, 0.114));",
90 " bloomCol = mix(vec3(bloomLum), bloomCol, u_saturation);",
91 " float sceneLum = dot(scene.rgb, vec3(0.299, 0.587, 0.114));",
92 " vec3 sceneHue = scene.rgb / max(sceneLum, 0.04);",
93 " bloomCol *= mix(vec3(1.0), sceneHue, clamp(u_colorPreserve, 0.0, 1.0));",
94 " bloomCol *= u_intensity;",
95 // Soft add: keeps punchy color instead of clipping hard to white/gray.
96 " vec3 rgb = scene.rgb + bloomCol * (1.0 - scene.rgb * 0.35);",
97 " float bloomA = max(bloomCol.r, max(bloomCol.g, bloomCol.b));",
98 " float a = max(scene.a, min(bloomA, 1.0));",
99 " gl_FragColor = vec4(rgb, a);",
100 "}"
101].join("\n");
102
103/**
104 * Multi-pass bloom for a RenderLayer / scene.postShaders.
105 *
106 * options:
107 * - threshold, softKnee, intensity, blurSize
108 * - saturation: bloom chroma boost (1 = neutral, >1 = juicier)
109 * - colorPreserve: 0–1 tint bloom with the original sprite hue
110 * - smallSources: dilate tiny bright pixels before blur (good for 3–4px bullets)
111 * - dilate: explicit dilate radius in texels (overrides smallSources default)
112 * - scales: number of H/V blur scale pairs (default 3 with smallSources, else 2)
113 */
114Shader.getBloomPostChain = function (options)
115{
116 options = options || {};
117 const threshold = options.threshold != null ? options.threshold : 0.4;
118 const softKnee = options.softKnee != null ? options.softKnee : 0.3;
119 const intensity = options.intensity != null ? options.intensity : 1.25;
120 const blurSize = options.blurSize != null ? options.blurSize : 3.0;
121 const saturation = options.saturation != null ? options.saturation : 1.0;
122 const colorPreserve = options.colorPreserve != null ? options.colorPreserve : 0.0;
123 const smallSources = !!options.smallSources;
124
125 let dilateRadius = 0;
126 if (options.dilate != null)
127 { dilateRadius = options.dilate; }
128 else if (smallSources)
129 { dilateRadius = 2.5; }
130
131 const scales = options.scales != null
132 ? options.scales
133 : (smallSources ? 3 : 2);
134
135 if (!Shader._bloomBright)
136 {
137 Shader._bloomBright = Shader.screenFragment(Shader.Builtin.BLOOM_BRIGHT_FRAG);
138 Shader._bloomDilate = Shader.screenFragment(Shader.Builtin.BLOOM_DILATE_FRAG);
139 Shader._bloomBlur = Shader.screenFragment(Shader.Builtin.BLOOM_BLUR_FRAG);
140 Shader._bloomCombine = Shader.screenFragment(Shader.Builtin.BLOOM_COMBINE_FRAG);
141 }
142
143 const chain = [
144 { shader: Shader._bloomBright, uniforms: { u_threshold: threshold, u_softKnee: softKnee } }
145 ];
146
147 if (dilateRadius > 0)
148 {
149 chain.push({
150 shader: Shader._bloomDilate,
151 uniforms: { u_radius: dilateRadius }
152 });
153 }
154
155 const scaleFactors = [1, 2.2, 4.5, 8];
156 for (let i = 0; i < scales; i++)
157 {
158 const size = blurSize * scaleFactors[Math.min(i, scaleFactors.length - 1)];
159 chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [1, 0], u_blurSize: size } });
160 chain.push({ shader: Shader._bloomBlur, uniforms: { u_direction: [0, 1], u_blurSize: size } });
161 }
162
163 chain.push({
164 shader: Shader._bloomCombine,
165 uniforms: {
166 u_intensity: intensity,
167 u_saturation: saturation,
168 u_colorPreserve: colorPreserve
169 }
170 });
171 return chain;
172};