BlueGit

timfa raytracer

texture

timfa Tim Falken · pushed by timfa

Authored 2021-07-01 · 5 years ago

Hash
d4c309d651aa117b85e3689a639358844e0261e2
Parent
1f3b58ed
7 files changed +72 −4
images/aplle.jpg added Binary file +0 −0

The contents of this binary file are not shown.

images/stone.png added Binary file +0 −0

The contents of this binary file are not shown.

images/wood.png added Binary file +0 −0

The contents of this binary file are not shown.

js/Canvas.js +3 −0
@@ -9,6 +9,9 @@ var Canvas =
9 9
10 10 get context()
11 11 {
12 GameWindow.canvasContext.webkitImageSmoothingEnabled = false;
13 GameWindow.canvasContext.mozImageSmoothingEnabled = false;
14 GameWindow.canvasContext.imageSmoothingEnabled = false;
12 15 return GameWindow.canvasContext;
13 16 },
14 17
@@ -9,6 +9,9 @@ var Canvas =
9
9
10 get context()
10 get context()
11 {
11 {
12 GameWindow.canvasContext.webkitImageSmoothingEnabled = false;
13 GameWindow.canvasContext.mozImageSmoothingEnabled = false;
14 GameWindow.canvasContext.imageSmoothingEnabled = false;
12 return GameWindow.canvasContext;
15 return GameWindow.canvasContext;
13 },
16 },
14
17
js/Player.js +22 −3
@@ -121,7 +121,12 @@ var Player =
121 121
122 122 for(var i = 0; i < this.horizontalResolution; i++)
123 123 {
124 var rot = this.rotation - rad*(this.horizontalResolution / 2) + rad*i;
124 var offsetP = -0.5 + (i / this.horizontalResolution);
125 var offset = offsetP * this.horizontalResolution;
126
127 var rotationalOffset = offset * rad;
128
129 var rot = this.rotation + rotationalOffset;
125 130
126 131 if(rot < 0)
127 132 rot += Math.PI * 2;
@@ -136,6 +141,8 @@ var Player =
136 141 B: rayHit.tile.Color.B
137 142 }
138 143
144 var light = 1;
145
139 146 switch(rayHit.dir)
140 147 {
141 148 case "north":
@@ -144,16 +151,19 @@ var Player =
144 151 color.R *= 0.9;
145 152 color.G *= 0.9;
146 153 color.B *= 0.9;
154 light = 0.9;
147 155 break;
148 156 case "west":
149 157 color.R *= 0.8;
150 158 color.G *= 0.8;
151 159 color.B *= 0.8;
160 light = 0.8;
152 161 break;
153 162 case "south":
154 163 color.R *= 0.7;
155 164 color.G *= 0.7;
156 165 color.B *= 0.7;
166 light = 0.7;
157 167 break;
158 168 }
159 169
@@ -161,13 +171,22 @@ var Player =
161 171 rayHit.distance *= Math.cos(deltaAngle);
162 172
163 173 var xLine = i * (GameWindow.width / this.horizontalResolution);
164 var widthLine = (GameWindow.width / this.horizontalResolution) + 1;
174 var widthLine = (GameWindow.width / this.horizontalResolution);
165 175
166 176 var height = (World.size * GameWindow.height) / rayHit.distance;
167 177
168 178 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
179 var imgyLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
169 180
170 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
181 if(rayHit.tile.Image)
182 {
183 var subImg = Math.min(rayHit.tile.Image.g_image.width - widthLine, Math.max(1, rayHit.tileX * rayHit.tile.Image.g_image.width));
184 rayHit.tile.Image.draw(subImg, 0, widthLine, rayHit.tile.Image.height, xLine, imgyLine + height/2, widthLine, height, 0, light);
185 }
186 else
187 {
188 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
189 }
171 190 }
172 191
173 192 var gunImg = null;
@@ -121,7 +121,12 @@ var Player =
121
121
122 for(var i = 0; i < this.horizontalResolution; i++)
122 for(var i = 0; i < this.horizontalResolution; i++)
123 {
123 {
124 var rot = this.rotation - rad*(this.horizontalResolution / 2) + rad*i;
124 var offsetP = -0.5 + (i / this.horizontalResolution);
125 var offset = offsetP * this.horizontalResolution;
126
127 var rotationalOffset = offset * rad;
128
129 var rot = this.rotation + rotationalOffset;
125
130
126 if(rot < 0)
131 if(rot < 0)
127 rot += Math.PI * 2;
132 rot += Math.PI * 2;
@@ -136,6 +141,8 @@ var Player =
136 B: rayHit.tile.Color.B
141 B: rayHit.tile.Color.B
137 }
142 }
138
143
144 var light = 1;
145
139 switch(rayHit.dir)
146 switch(rayHit.dir)
140 {
147 {
141 case "north":
148 case "north":
@@ -144,16 +151,19 @@ var Player =
144 color.R *= 0.9;
151 color.R *= 0.9;
145 color.G *= 0.9;
152 color.G *= 0.9;
146 color.B *= 0.9;
153 color.B *= 0.9;
154 light = 0.9;
147 break;
155 break;
148 case "west":
156 case "west":
149 color.R *= 0.8;
157 color.R *= 0.8;
150 color.G *= 0.8;
158 color.G *= 0.8;
151 color.B *= 0.8;
159 color.B *= 0.8;
160 light = 0.8;
152 break;
161 break;
153 case "south":
162 case "south":
154 color.R *= 0.7;
163 color.R *= 0.7;
155 color.G *= 0.7;
164 color.G *= 0.7;
156 color.B *= 0.7;
165 color.B *= 0.7;
166 light = 0.7;
157 break;
167 break;
158 }
168 }
159
169
@@ -161,13 +171,22 @@ var Player =
161 rayHit.distance *= Math.cos(deltaAngle);
171 rayHit.distance *= Math.cos(deltaAngle);
162
172
163 var xLine = i * (GameWindow.width / this.horizontalResolution);
173 var xLine = i * (GameWindow.width / this.horizontalResolution);
164 var widthLine = (GameWindow.width / this.horizontalResolution) + 1;
174 var widthLine = (GameWindow.width / this.horizontalResolution);
165
175
166 var height = (World.size * GameWindow.height) / rayHit.distance;
176 var height = (World.size * GameWindow.height) / rayHit.distance;
167
177
168 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
178 var yLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
179 var imgyLine = (GameWindow.height / 2 - height / 2) + (-this.pitch * (World.size * GameWindow.height - height) * 0.005);
169
180
170 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
181 if(rayHit.tile.Image)
182 {
183 var subImg = Math.min(rayHit.tile.Image.g_image.width - widthLine, Math.max(1, rayHit.tileX * rayHit.tile.Image.g_image.width));
184 rayHit.tile.Image.draw(subImg, 0, widthLine, rayHit.tile.Image.height, xLine, imgyLine + height/2, widthLine, height, 0, light);
185 }
186 else
187 {
188 Canvas.drawRect(xLine, yLine, widthLine, height, "rgb(" + color.R + "," + color.G + "," + color.B + ")");
189 }
171 }
190 }
172
191
173 var gunImg = null;
192 var gunImg = null;
js/Tiles.js +2 −0
@@ -18,6 +18,7 @@ var Tiles =
18 18 G: 155,
19 19 B: 155
20 20 },
21 Image: new image("images/stone.png", 16, 16),
21 22 Solid: true
22 23 },
23 24 Finish:
@@ -38,6 +39,7 @@ var Tiles =
38 39 G: 102,
39 40 B: 50
40 41 },
42 Image: new image("images/wood.png", 16, 16),
41 43 Solid: true
42 44 },
43 45 findByID(id)
@@ -18,6 +18,7 @@ var Tiles =
18 G: 155,
18 G: 155,
19 B: 155
19 B: 155
20 },
20 },
21 Image: new image("images/stone.png", 16, 16),
21 Solid: true
22 Solid: true
22 },
23 },
23 Finish:
24 Finish:
@@ -38,6 +39,7 @@ var Tiles =
38 G: 102,
39 G: 102,
39 B: 50
40 B: 50
40 },
41 },
42 Image: new image("images/wood.png", 16, 16),
41 Solid: true
43 Solid: true
42 },
44 },
43 findByID(id)
45 findByID(id)
js/World.js +45 −1
@@ -34,6 +34,10 @@ var World =
34 34 var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit;
35 35
36 36 var hDir = "north";
37
38 tileXpercentage = 0;
39 tileXp = 0;
40 tileYp = 0;
37 41
38 42 distDone = 0;
39 43
@@ -63,6 +67,9 @@ var World =
63 67
64 68 while (distDone < maxDist)
65 69 {
70 tileXp = ((hrayX % this.size) + 1) / this.size;
71 tileYp = ((hrayY % this.size) + 1) / this.size;
72
66 73 hmapX = Math.floor(hrayX / this.size);
67 74 hmapY = Math.floor(hrayY / this.size);
68 75 var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width];
@@ -81,9 +88,26 @@ var World =
81 88 }
82 89 }
83 90
91 switch(hDir)
92 {
93 case "north":
94 tileXpercentage = tileXp;
95 break;
96 case "south":
97 tileXpercentage = 1 - tileXp;
98 break;
99 case "east":
100 tileXpercentage = 1 - tileYp;
101 break;
102 case "west":
103 tileXpercentage = tileYp;
104 break;
105 }
106
84 107 var hResult =
85 108 {
86 109 tile: htileHit,
110 tileX: tileXpercentage,
87 111 endX: hrayX,
88 112 endY: hrayY,
89 113 dir: hDir
@@ -116,9 +140,12 @@ var World =
116 140 rayY = y;
117 141 distDone = maxDist;
118 142 }
119
143
120 144 while (distDone < maxDist)
121 145 {
146 tileXp = ((rayX % this.size) + 1) / this.size;
147 tileYp = ((rayY % this.size) + 1) / this.size;
148
122 149 mapX = Math.floor(rayX / this.size);
123 150 mapY = Math.floor(rayY / this.size);
124 151 var tileHitId = this.level.tiles[mapX + mapY * this.level.width];
@@ -136,10 +163,27 @@ var World =
136 163 distDone += 1;
137 164 }
138 165 }
166
167 switch(vDir)
168 {
169 case "north":
170 tileXpercentage = tileXp;
171 break;
172 case "south":
173 tileXpercentage = 1 - tileXp;
174 break;
175 case "east":
176 tileXpercentage = 1 - tileYp;
177 break;
178 case "west":
179 tileXpercentage = tileYp;
180 break;
181 }
139 182
140 183 var vResult =
141 184 {
142 185 tile: tileHit,
186 tileX: tileXpercentage,
143 187 endX: rayX,
144 188 endY: rayY,
145 189 dir: vDir
@@ -34,6 +34,10 @@ var World =
34 var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit;
34 var hrayX, hrayY, hoffX, hoffY, distDone, hmapX, hmapY, htileHit;
35
35
36 var hDir = "north";
36 var hDir = "north";
37
38 tileXpercentage = 0;
39 tileXp = 0;
40 tileYp = 0;
37
41
38 distDone = 0;
42 distDone = 0;
39
43
@@ -63,6 +67,9 @@ var World =
63
67
64 while (distDone < maxDist)
68 while (distDone < maxDist)
65 {
69 {
70 tileXp = ((hrayX % this.size) + 1) / this.size;
71 tileYp = ((hrayY % this.size) + 1) / this.size;
72
66 hmapX = Math.floor(hrayX / this.size);
73 hmapX = Math.floor(hrayX / this.size);
67 hmapY = Math.floor(hrayY / this.size);
74 hmapY = Math.floor(hrayY / this.size);
68 var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width];
75 var htileHitId = this.level.tiles[hmapX + hmapY * this.level.width];
@@ -81,9 +88,26 @@ var World =
81 }
88 }
82 }
89 }
83
90
91 switch(hDir)
92 {
93 case "north":
94 tileXpercentage = tileXp;
95 break;
96 case "south":
97 tileXpercentage = 1 - tileXp;
98 break;
99 case "east":
100 tileXpercentage = 1 - tileYp;
101 break;
102 case "west":
103 tileXpercentage = tileYp;
104 break;
105 }
106
84 var hResult =
107 var hResult =
85 {
108 {
86 tile: htileHit,
109 tile: htileHit,
110 tileX: tileXpercentage,
87 endX: hrayX,
111 endX: hrayX,
88 endY: hrayY,
112 endY: hrayY,
89 dir: hDir
113 dir: hDir
@@ -116,9 +140,12 @@ var World =
116 rayY = y;
140 rayY = y;
117 distDone = maxDist;
141 distDone = maxDist;
118 }
142 }
119
143
120 while (distDone < maxDist)
144 while (distDone < maxDist)
121 {
145 {
146 tileXp = ((rayX % this.size) + 1) / this.size;
147 tileYp = ((rayY % this.size) + 1) / this.size;
148
122 mapX = Math.floor(rayX / this.size);
149 mapX = Math.floor(rayX / this.size);
123 mapY = Math.floor(rayY / this.size);
150 mapY = Math.floor(rayY / this.size);
124 var tileHitId = this.level.tiles[mapX + mapY * this.level.width];
151 var tileHitId = this.level.tiles[mapX + mapY * this.level.width];
@@ -136,10 +163,27 @@ var World =
136 distDone += 1;
163 distDone += 1;
137 }
164 }
138 }
165 }
166
167 switch(vDir)
168 {
169 case "north":
170 tileXpercentage = tileXp;
171 break;
172 case "south":
173 tileXpercentage = 1 - tileXp;
174 break;
175 case "east":
176 tileXpercentage = 1 - tileYp;
177 break;
178 case "west":
179 tileXpercentage = tileYp;
180 break;
181 }
139
182
140 var vResult =
183 var vResult =
141 {
184 {
142 tile: tileHit,
185 tile: tileHit,
186 tileX: tileXpercentage,
143 endX: rayX,
187 endX: rayX,
144 endY: rayY,
188 endY: rayY,
145 dir: vDir
189 dir: vDir