BlueGit

timfa raytracer

Files

raytracer / src / textureColumn.js

textureColumn.js

textureColumn.js 1.1 KiB Download Raw
1/**
2 * Texture-column helpers for Wolfenstein-style wall strips.
3 * Each screen column samples exactly one source pixel column.
4 */
5function textureColumnIndex (tileX, texWidth)
6{
7 if (!(texWidth > 0))
8 { return 0; }
9
10 var sx = Math.floor(Number(tileX) * texWidth);
11 if (sx < 0)
12 { sx = 0; }
13 if (sx > texWidth - 1)
14 { sx = texWidth - 1; }
15 return sx;
16}
17
18function textureColumnUv (tileX, texWidth)
19{
20 var sx = textureColumnIndex(tileX, texWidth);
21 var w = texWidth > 0 ? texWidth : 1;
22 return {
23 sx: sx,
24 sw: 1,
25 u0: sx / w,
26 u1: (sx + 1) / w,
27 v0: 0,
28 v1: 1
29 };
30}
31
32/** Face U from hit position within a cell (no +1 bias). */
33function faceTileX (hitCoord, cellSize, face)
34{
35 var local = hitCoord % cellSize;
36 if (local < 0)
37 { local += cellSize; }
38 var t = local / cellSize;
39 switch (face)
40 {
41 case "north": return t;
42 case "south": return 1 - t;
43 case "east": return 1 - t;
44 case "west": return t;
45 default: return t;
46 }
47}
48
49if (typeof module !== "undefined" && module.exports)
50{
51 module.exports = {
52 textureColumnIndex: textureColumnIndex,
53 textureColumnUv: textureColumnUv,
54 faceTileX: faceTileX
55 };
56}