-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
209 lines (179 loc) · 4.7 KB
/
sketch.js
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
// test mode parameters
const testMode = true;
const testIterations = 1;
// asset container array definitions
let testValues;
let compoSrc;
// object structures
let objects = [
{
type: "triangle",
vertices: [
{x: 50, y: 80},
{x: 80, y: 70},
{x: 90, y: 110}
]
},
{
type: "triangle",
vertices: [
{x: 180, y: 130},
{x: 240, y: 170},
{x: 140, y: 190}
]
},
{
type: "triangle",
vertices: [
{x: 130, y: 100},
{x: 150, y: 110},
{x: 135, y: 60}
]
}
];
let camera = {
pos: {x: 50, y: 200},
dir: 20,
fov: 60,
speed: 1
};
// preload runs before setup and draw
function preload() {
if(testMode) {
testValues = loadStrings('test_values.txt');
compoSrc = loadStrings('compo.js?a='+random(0, 9999999));
}
}
// setup runs once at start up
function setup() {
createCanvas(256, 256);
if(testMode) {
test();
}
}
// draw is called each frame
function draw() {
background(0);
if(keyIsDown(LEFT_ARROW)) {
camera.dir -= camera.speed;
if (camera.dir < 0)
camera.dir += 360;
}
if(keyIsDown(RIGHT_ARROW)) {
camera.dir += camera.speed;
if (camera.dir >= 360)
camera.dir -= 360;
}
if(keyIsDown(UP_ARROW)) {
camera.pos.x += camera.speed * cos(camera.dir / 180 * PI);
camera.pos.y += camera.speed * sin(camera.dir / 180 * PI);
}
if(keyIsDown(DOWN_ARROW)) {
camera.pos.x -= camera.speed * cos(camera.dir / 180 * PI);
camera.pos.y -= camera.speed * sin(camera.dir / 180 * PI);
}
drawObjects();
shootRay();
drawCamera();
}
// draws all objects (triangles) to the canvas
function drawObjects() {
let objectColor = color(150, 155, 100);
let objectBorderColor = color(255, 255, 0);
fill(objectColor);
stroke(objectBorderColor);
strokeWeight(1);
for(i = 0; i < objects.length; ++i) {
let o = objects[i];
beginShape(TRIANGLES);
for(j = 0; j < o.vertices.length; ++j) {
let v = o.vertices[j];
vertex(v.x, v.y);
}
endShape(CLOSE);
}
}
// draws camera position and direction
function drawCamera() {
let cameraColor = color(255, 204, 0);
fill(cameraColor);
stroke(cameraColor);
strokeWeight(3);
circle(camera.pos.x, camera.pos.y, 10);
const directionRayLength = 30;
let directionRayPos = {
x: camera.pos.x + directionRayLength * cos(camera.dir / 180 * PI),
y: camera.pos.y + directionRayLength * sin(camera.dir / 180 * PI)
};
line(camera.pos.x, camera.pos.y, directionRayPos.x, directionRayPos.y);
}
// shoots a ray from camera position and shows hit
// point using a circle around the camera
function shootRay() {
let rayColor = color(255, 0, 255);
stroke(rayColor);
noFill();
strokeWeight(1);
let distanceHit = getClosestLineDistance(camera.pos);
circle(camera.pos.x, camera.pos.y, 2*distanceHit);
}
// gets the closest line hit point in all objects
function getClosestLineDistance(p) {
let minDistance = 256;
for(i = 0; i < objects.length; ++i) {
let o = objects[i];
let d = getDistanceToTriangle(p, o.vertices);
if(d < minDistance) minDistance = d;
}
return minDistance;
}
// gets the closest line hit point of a triangle
function getDistanceToTriangle(p, vertices) {
let minDistance = 256;
let d = f(p, vertices[0], vertices[1]);
if(d < minDistance) minDistance = d;
d = f(p, vertices[1], vertices[2]);
if(d < minDistance) minDistance = d;
d = f(p, vertices[2], vertices[0]);
if(d < minDistance) minDistance = d;
return minDistance;
}
function test() {
// create two dimensioned distance buffer
let d = [];
for(let i = 0; i < 256; ++i) {
d[i] = [];
}
// get initial time in milliseconds
let timeStart = millis();
// do the test for all 256x256 coordinates
// with defined number of iterations
for(let k = testIterations; k > 0; --k) {
for(let y = 255; y >= 0; --y) {
for(let x = 255; x >= 0; --x) {
d[x][y] = getClosestLineDistance({x: x, y: y});
}
}
}
// calculate average execution time
let averageExecutionTime = (millis() - timeStart) / testIterations;
console.log(`Test execution time: ${averageExecutionTime} ms`);
// evaluate test results and find a success ratio
let success = 0;
for(i = 0; i < 256; ++i) {
for(j = 0; j < 256; ++j) {
if(abs(d[i][j] - testValues[i * 256 + j]) < 0.01) success++;
}
}
successRatio = success / 655.36;
console.log(`Success ratio: ${successRatio}%`);
// right trim source code, replace new lines
// with single space characters and
// find script size
for(i = 0; i < compoSrc.length; ++i) {
compoSrc[i] = compoSrc[i].replace(/~+$/, '');
}
compoSrc = compoSrc.join(" ").trim();
codeLength = compoSrc.length;
console.log(`Code length: ${codeLength} bytes`);
}