forked from alanmacleod/simple-raytracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
223 lines (157 loc) · 5.66 KB
/
index.html
File metadata and controls
223 lines (157 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<html>
<head>
<title>very simple raytracer</title>
<script src="3d/Vector3.js"></script>
<script src="3d/Box.js"></script>
<script src="3d/Sphere.js"></script>
<script src="3d/Plane.js"></script>
</head>
<body>
<canvas id="display" width="800" height="600" style="border: 1px solid gray">
</canvas>
</body>
<script>
var SWIDTH = 800, SHEIGHT=600;
var DepthShadingDistance = 20;
var Camera = new Vector3([0,0,-5]);
var ProjectionPlane = genProjectionPlane(SWIDTH, SHEIGHT);
var SceneObjects = [];
var bx = 1.35, by = -3, bz = 3, bsize = 4.5;
var testobj0 = new BoxAABB(new Vector3([bx, by, bz]), new Vector3([bx+bsize, by+bsize, bz+bsize]), [32,128,200,255]);
var testobj1 = new Sphere(new Vector3([2.5, 2, 5]), 3);
var testobj2 = new Sphere(new Vector3([-4, -2, 18]), 10);
var testobj3 = new Sphere(new Vector3([-2, 0, 6]), 3.3);
var testPlane = new Plane(new Vector3([0,1,0]), 3.5);
var lastRayObjMatch = -1;
testobj2.colour = [0,0,255,255];
testobj3.colour = [0,255,0,255];
SceneObjects.push(testobj0);
SceneObjects.push(testobj1);
SceneObjects.push(testobj2);
SceneObjects.push(testobj3);
SceneObjects.push(testPlane);
var canvas = document.getElementById('display');
var context = canvas.getContext('2d');
var imgdata = context.getImageData(0,0,SWIDTH,SHEIGHT);
var dx = (ProjectionPlane.xright - ProjectionPlane.xleft) / SWIDTH;
var dy = (ProjectionPlane.ybottom - ProjectionPlane.ytop) / SHEIGHT;
var wx, wy;
wy = ProjectionPlane.ytop;
var time_start = Date.now();
for (var y=0; y < SHEIGHT; y++)
{
wx = ProjectionPlane.xleft;
for (var x=0; x < SWIDTH; x++)
{
var projPixel = new Vector3([wx, wy, 0]);
var dir = projPixel.subtract(Camera);
dir.normalise();
var Ray = {
origin: Camera,
direction: dir
};
var result = Raytrace(Ray);//{dist: 0, hit:false};//
if (result.hit) {
var col = result.object.colour;
var scale = 1.1;
// cheap ass depth-shading
var shade = result.dist / (DepthShadingDistance*scale);
//if (shade > 1) shade = 1;
shade = scale-shade;
var r = col[0] * shade, g = col[1]*shade, b = col[2] *shade;
if (r > 255) r=255; if (g > 255) g=255; if (b>255) b=255;
if (r<0) r=0; if (g<0) g= 0; if (b<0) b=0;
var a = col[3];
col = [];
col[0] = r;
col[1] = g;
col[2] = b;
col[3] = a;
var rayIntercept = Ray.origin.add(Ray.direction);
rayIntercept.scale(result.dist);
if (result.object.getIntersectionCoordinate)
{
var tex = result.object.getIntersectionCoordinate(rayIntercept);
var scale = 32;
var pattern = (((tex.u * scale) % 1) > 0.5) ^ (((tex.v * scale) % 1) > 0.5);
var col2 = [];
col2[0] = col[0] * 0.8;
col2[1] = col[1] * 0.8;
col2[2] = col[2] * 0.8;
col2[3] = col[3];
var scale2 = Math.max(0, tex.nhit.dot(dir.scale(-1)));
col = colourMix(col, col2, pattern);
col[0] = Math.floor(col[0] * scale2);
col[1] = Math.floor(col[1] * scale2);
col[2] = Math.floor(col[2] * scale2);
col[3] = 255;
}
pset(x, y, col);
}
wx += dx;
}
wy += dy;
}
context.putImageData(imgdata, 0,0);
var ms = Date.now() - time_start;
console.log("Render time: "+ms+" ms");
function colourMix(c1, c2, mix)
{
var n = [];
n[0] = (c1[0] * (1 - mix)) + c2[0] * mix;
n[1] = (c1[1] * (1 - mix)) + c2[1] * mix;
n[2] = (c1[2] * (1 - mix)) + c2[2] * mix;
n[3] = 255;
return n;
}
function Raytrace(ray)
{
var tracepoint = {dist: 1000000};
var newLastRayObjMatch = -2;
for (var t=0; t<SceneObjects.length; t++)
{
var obj = SceneObjects[t];
var result = obj.intersect(ray);
if (result.hit == true)
{
if (result.dist < tracepoint.dist)
{
tracepoint.hit = true;
tracepoint.object = obj;
tracepoint.dist = result.dist;
newLastRayObjMatch = t;
}
}
}
/*
below; reorder scene objects so we raytrace our last matched object FIRST on the next ray...
works but no significant speed improvement doh :/
if (lastRayObjMatch != newLastRayObjMatch)
{
lastRayObjMatch = newLastRayObjMatch;
var lastObj = SceneObjects.splice(lastRayObjMatch, 1)[0];
SceneObjects.splice(0,0,lastObj);
}
*/
return tracepoint;
}
function pset(x,y,c)
{
var ofs = y * 4 * SWIDTH + (x*4);
imgdata.data[ofs+0] = c[0];
imgdata.data[ofs+1] = c[1];
imgdata.data[ofs+2] = c[2];
imgdata.data[ofs+3] = c[3];
}
function genProjectionPlane(w, h)
{
var uw = w / 100;
var uh = h / 100;
var xmin = -(uw/2);
var xmax = uw/2;
var ymin = -(uh/2);
var ymax = uh/2;
return {xleft:xmin, xright:xmax, ybottom:ymin, ytop:ymax}
}
</script>
</html>