BlueGit

timfa raytracer

Files

raytracer / src / floorGpu.js

floorGpu.js

floorGpu.js 9.4 KiB Download Raw
1/**
2 * GPU floor/ceiling + point lights (WebGL1 fragment shader via frameworkv3 drawSprite).
3 * Uses the same camera-plane rays + perp rowDist as Camera / FloorCast (no fish-eye).
4 */
5var FloorGpu =
6{
7 MAX_LIGHTS: 8,
8 _shader: null,
9 _whiteTex: null,
10 _gridTex: null,
11 _gridW: 0,
12 _gridH: 0,
13 _gridKey: "",
14 _stoneTex: null,
15 _woodTex: null,
16 _stoneSrc: null,
17 _woodSrc: null,
18
19 _ensureShader: function ()
20 {
21 if (this._shader)
22 { return this._shader; }
23 var frag = [
24 "precision mediump float;",
25 "uniform sampler2D u_texture;",
26 "uniform sampler2D u_stone;",
27 "uniform sampler2D u_wood;",
28 "uniform sampler2D u_grid;",
29 "uniform float u_alpha;",
30 "uniform vec2 u_resolution;",
31 "uniform vec2 u_player;",
32 "uniform vec2 u_dir;",
33 "uniform vec2 u_plane;",
34 "uniform float u_pitch;",
35 "uniform float u_cellSize;",
36 "uniform float u_posZ;",
37 "uniform vec2 u_levelSize;",
38 "uniform float u_ambient;",
39 "uniform float u_lightCount;",
40 "uniform vec4 u_l0;",
41 "uniform vec4 u_l1;",
42 "uniform vec4 u_l2;",
43 "uniform vec4 u_l3;",
44 "uniform vec4 u_l4;",
45 "uniform vec4 u_l5;",
46 "uniform vec4 u_l6;",
47 "uniform vec4 u_l7;",
48 "uniform vec3 u_c0;",
49 "uniform vec3 u_c1;",
50 "uniform vec3 u_c2;",
51 "uniform vec3 u_c3;",
52 "uniform vec3 u_c4;",
53 "uniform vec3 u_c5;",
54 "uniform vec3 u_c6;",
55 "uniform vec3 u_c7;",
56 "uniform vec2 u_a0;",
57 "uniform vec2 u_a1;",
58 "uniform vec2 u_a2;",
59 "uniform vec2 u_a3;",
60 "uniform vec2 u_a4;",
61 "uniform vec2 u_a5;",
62 "uniform vec2 u_a6;",
63 "uniform vec2 u_a7;",
64 "varying vec2 v_texCoord;",
65 "vec4 lightPos(int i) {",
66 " if (i==0) return u_l0; if (i==1) return u_l1; if (i==2) return u_l2; if (i==3) return u_l3;",
67 " if (i==4) return u_l4; if (i==5) return u_l5; if (i==6) return u_l6; return u_l7;",
68 "}",
69 "vec3 lightCol(int i) {",
70 " if (i==0) return u_c0; if (i==1) return u_c1; if (i==2) return u_c2; if (i==3) return u_c3;",
71 " if (i==4) return u_c4; if (i==5) return u_c5; if (i==6) return u_c6; return u_c7;",
72 "}",
73 "vec2 lightAtt(int i) {",
74 " if (i==0) return u_a0; if (i==1) return u_a1; if (i==2) return u_a2; if (i==3) return u_a3;",
75 " if (i==4) return u_a4; if (i==5) return u_a5; if (i==6) return u_a6; return u_a7;",
76 "}",
77 "void main() {",
78 " float sx = gl_FragCoord.x;",
79 " float sy = u_resolution.y - gl_FragCoord.y;",
80 " float mid = u_resolution.y * 0.5 + (-u_pitch * u_resolution.y * 0.25);",
81 " float p = sy - mid;",
82 " if (abs(p) < 0.5) { gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return; }",
83 " float isCeil = step(p, 0.0);",
84 " float rowDist = u_posZ / abs(p);",
85 " float camX = (2.0 * sx / u_resolution.x) - 1.0;",
86 " vec2 rayDir = u_dir + u_plane * camX;",
87 " float wx = u_player.x + rayDir.x * rowDist;",
88 " float wy = u_player.y + rayDir.y * rowDist;",
89 " float cx = floor(wx / u_cellSize);",
90 " float cy = floor(wy / u_cellSize);",
91 " if (cx < 0.0 || cy < 0.0 || cx >= u_levelSize.x || cy >= u_levelSize.y) {",
92 " gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return;",
93 " }",
94 " vec2 guv = (vec2(cx, cy) + 0.5) / u_levelSize;",
95 " vec4 cell = texture2D(u_grid, guv);",
96 " float floorId = floor(cell.r * 255.0 + 0.5);",
97 " float ceilId = floor(cell.g * 255.0 + 0.5);",
98 " float tileId = floor(cell.b * 255.0 + 0.5);",
99 " float texId = mix(floorId, ceilId, isCeil);",
100 " if (texId < 0.5) {",
101 " if (tileId > 2.5) texId = 2.0;",
102 " else if (tileId > 0.5) texId = 1.0;",
103 " }",
104 " if (texId < 0.5) { gl_FragColor = vec4(0.0, 0.0, 0.0, u_alpha); return; }",
105 " vec2 tuv = fract(vec2(wx, wy) / u_cellSize);",
106 " vec3 albedo = mix(texture2D(u_stone, tuv).rgb, texture2D(u_wood, tuv).rgb, step(1.5, texId));",
107 " float wz = mix(0.0, u_cellSize, isCeil);",
108 " vec3 nrm = mix(vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0), isCeil);",
109 " vec3 lit = vec3(u_ambient);",
110 " for (int i = 0; i < 8; i++) {",
111 " if (float(i) >= u_lightCount) break;",
112 " vec4 Lp = lightPos(i);",
113 " vec3 toL = Lp.xyz - vec3(wx, wy, wz);",
114 " float dist = length(toL);",
115 " vec2 attP = lightAtt(i);",
116 " float t = 1.0 - dist / max(attP.x, 0.001);",
117 " if (t <= 0.0) continue;",
118 " float att = t;",
119 " if (attP.y > 1.5) att = t * t;",
120 " if (attP.y > 2.5) att = att * t;",
121 " vec3 ld = toL / max(dist, 0.0001);",
122 " float ndotl = dot(nrm, ld);",
123 " if (ndotl <= 0.0) continue;",
124 " lit += Lp.w * att * ndotl * lightCol(i);",
125 " }",
126 " gl_FragColor = vec4(min(albedo * lit, vec3(1.0)), u_alpha);",
127 "}"
128 ].join("\n");
129 this._shader = new Shader(Shader.DEFAULT_VERT, frag);
130 return this._shader;
131 },
132
133 _texIdByte: function (id)
134 {
135 if (id === "stone") return 1;
136 if (id === "wood") return 2;
137 return 0;
138 },
139
140 _uploadGrid: function (gl, level)
141 {
142 var w = level.width;
143 var h = level.height;
144 var key = (level.id || "") + "|" + w + "x" + h + "|" + (level._rev || 0);
145 if (this._gridTex && this._gridKey === key && this._gridW === w && this._gridH === h)
146 { return; }
147 var data = new Uint8Array(w * h * 4);
148 var floors = level.floors || [];
149 var ceilings = level.ceilings || [];
150 var tiles = level.tiles || [];
151 for (var y = 0; y < h; y++)
152 {
153 for (var x = 0; x < w; x++)
154 {
155 var i = x + y * w;
156 var o = i * 4;
157 data[o] = this._texIdByte(floors[i]);
158 data[o + 1] = this._texIdByte(ceilings[i]);
159 data[o + 2] = tiles[i] || 0;
160 data[o + 3] = 255;
161 }
162 }
163 if (!this._gridTex)
164 { this._gridTex = gl.createTexture(); }
165 gl.bindTexture(gl.TEXTURE_2D, this._gridTex);
166 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
167 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
168 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
169 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
170 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
171 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
172 this._gridW = w;
173 this._gridH = h;
174 this._gridKey = key;
175 },
176
177 _uploadAlbedo: function (gl, htmlImg, which)
178 {
179 var prev = which === "stone" ? this._stoneSrc : this._woodSrc;
180 var tex = which === "stone" ? this._stoneTex : this._woodTex;
181 if (tex && prev === htmlImg)
182 { return tex; }
183 if (!tex)
184 { tex = gl.createTexture(); }
185 gl.bindTexture(gl.TEXTURE_2D, tex);
186 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
187 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
188 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
189 // CLAMP + fract() in shader: WebGL1 forbids REPEAT on NPOT (stone 400²).
190 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
191 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
192 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImg);
193 if (which === "stone")
194 {
195 this._stoneTex = tex;
196 this._stoneSrc = htmlImg;
197 }
198 else
199 {
200 this._woodTex = tex;
201 this._woodSrc = htmlImg;
202 }
203 return tex;
204 },
205
206 _ensureWhite: function (glc)
207 {
208 if (this._whiteTex)
209 { return this._whiteTex; }
210 var c = document.createElement("canvas");
211 c.width = 1;
212 c.height = 1;
213 var ctx = c.getContext("2d");
214 ctx.fillStyle = "#ffffff";
215 ctx.fillRect(0, 0, 1, 1);
216 this._whiteTex = glc.getTexture(c);
217 return this._whiteTex;
218 },
219
220 invalidateGrid: function ()
221 {
222 this._gridKey = "";
223 },
224
225 draw: function (glc, screenW, screenH, player, level, floorImages, cellSize)
226 {
227 if (!glc || !level || !floorImages || !floorImages.stone || !floorImages.wood)
228 { return false; }
229 if (typeof Shader === "undefined")
230 { return false; }
231 cellSize = cellSize || 32;
232 var gl = glc.gl;
233 var shader = this._ensureShader();
234 this._uploadGrid(gl, level);
235 this._uploadAlbedo(gl, floorImages.stone, "stone");
236 this._uploadAlbedo(gl, floorImages.wood, "wood");
237 var white = this._ensureWhite(glc);
238
239 var f = Camera.forward(player.rotation);
240 var plane = Camera.plane(player.rotation);
241 var lights = level.lights || [];
242 var n = Math.min(lights.length, this.MAX_LIGHTS);
243 var uniforms = {
244 u_resolution: [screenW, screenH],
245 u_player: [player.x, player.y],
246 u_dir: [f.x, f.y],
247 u_plane: [plane.x, plane.y],
248 u_pitch: player.pitch || 0,
249 u_cellSize: cellSize,
250 u_posZ: (typeof FloorCast !== "undefined")
251 ? FloorCast.posZ(screenH, cellSize)
252 : (cellSize * 0.5) * screenH,
253 u_levelSize: [level.width, level.height],
254 u_ambient: level.ambient != null ? level.ambient : 0.38,
255 u_lightCount: n
256 };
257 var i;
258 for (i = 0; i < this.MAX_LIGHTS; i++)
259 {
260 if (i < n)
261 {
262 var L = lights[i];
263 uniforms["u_l" + i] = [
264 L.x, L.y, L.z != null ? L.z : 16,
265 L.intensity == null ? 1 : L.intensity
266 ];
267 uniforms["u_c" + i] = [
268 L.color ? L.color[0] : 1,
269 L.color ? L.color[1] : 1,
270 L.color ? L.color[2] : 1
271 ];
272 uniforms["u_a" + i] = [
273 L.range || 64,
274 L.falloff == null ? 2 : L.falloff
275 ];
276 }
277 else
278 {
279 uniforms["u_l" + i] = [0, 0, 0, 0];
280 uniforms["u_c" + i] = [0, 0, 0];
281 uniforms["u_a" + i] = [1, 2];
282 }
283 }
284
285 glc.drawSprite({
286 texture: white,
287 shader: shader,
288 x: screenW / 2,
289 y: screenH / 2,
290 w: screenW,
291 h: screenH,
292 rotationDeg: 0,
293 alpha: 1,
294 uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },
295 textures: {
296 u_stone: this._stoneTex,
297 u_wood: this._woodTex,
298 u_grid: this._gridTex
299 },
300 uniforms: uniforms
301 });
302 return true;
303 }
304};
305
306if (typeof module !== "undefined" && module.exports)
307{ module.exports = FloorGpu; }