forked from KilledByAPixel/Drive13K
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdraw.js
318 lines (279 loc) · 9.89 KB
/
draw.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
'use strict';
let lightDirection, lightColor, ambientColor, fogColor;
let cubeMesh, quadMesh, shadowMesh, cylinderMesh, carMesh, carWheel;
const WHITE = rgb(1,1,1);
const BLACK = rgb(0,0,0);
const RED = rgb(1,0,0);
const YELLOW = rgb(1,1,0);
const GREEN = rgb(0,1,0);
const CYAN = rgb(1,1,0);
const BLUE = rgb(0,0,1);
const MAGENTA= rgb(0,1,1);
const GRAY = rgb(.5,.5,.5);
///////////////////////////////////////////////////////////////////////////////
function drawInit()
{
{
// cube
const points = [vec3(-1,1),vec3(1,1),vec3(1,-1),vec3(-1,-1)];
cubeMesh = new Mesh().buildExtrude(points);
}
{
// quad
const points = [vec3(-1,1),vec3(-1,1),vec3(1,1),vec3(-1,-1),vec3(1,-1),vec3(1,-1)];
const normals = points.map(p=>vec3(0,0,9));
const uvs = points.map(p=>p.multiply(vec3(.5,-.5,.5)).add(vec3(.5)));
quadMesh = new Mesh(points, normals, uvs);
shadowMesh = quadMesh.copy().transform(0,vec3(PI/2,0,0));
}
{
// cylinder
const points = [];
const sides = 12;
for(let i=sides; i--;)
{
const a = i/sides*PI*2;
points.push(vec3(1,0,0).rotateZ(a));
}
cylinderMesh = new Mesh().buildExtrude(points);
/*
const profile = [
vec3(0.2, -1), // Narrow base
vec3(0.5, -0.5), // Widening towards the bottom
vec3(0.3, 0), // Narrowing at the middle
vec3(0.7, 0.5), // Widening towards the top
vec3(0.1, 1) // Narrow neck
];
cylinderMesh = new Mesh().buildLathe(profile);*/
}
{
// car bottom
const points =
[
vec3(-1,.5),
vec3(-.7,.4),
vec3(-.2,.5),
vec3(.1,.5),
vec3(1,.2),
vec3(1,.2),
vec3(1,0),
vec3(-1,0),
]
//const points = [vec3(-1,1),vec3(1,1),vec3(1,-1),vec3(-1,-1)];
carMesh = new Mesh().buildExtrude(points,.5);
carMesh.transform(0,vec3(0,-PI/2));
//debugMesh=carMesh
carWheel = cylinderMesh.copy();
carWheel.transform(0,vec3(0,-PI/2));
//debugMesh=cylinderMesh
//carMesh.combine(cylinderMesh);
}
}
///////////////////////////////////////////////////////////////////////////////
class Tile
{
constructor(pos, size)
{
const bleedPixels = 2;
const textureSize = generativeCanvasSize;
this.pos = pos.add(vec3(bleedPixels)).divide(textureSize);
this.size = size.add(vec3(-2*bleedPixels)).divide(textureSize);
}
}
///////////////////////////////////////////////////////////////////////////////
class Mesh
{
constructor(points, normals, uvs)
{
this.points = points;
this.normals = normals;
this.uvs = uvs;
}
copy()
{
return new Mesh(this.points, this.normals, this.uvs);
}
buildExtrude(facePoints, size=1, topScale=1)
{
// convert list of 2d points into a 3d shape
const points = [], normals = [];
const vertCount = facePoints.length + 2;
for (let k=2; k--;)
for (let i=vertCount; i--;)
{
// build top and bottom of mesh
const j = clamp(i-1, 0, vertCount-3); // degenerate tri at ends
const h = j>>1;
const point = facePoints[j%2 == (vertCount%2 ? 0 : k) ? vertCount-3-h : h ].scale(k?topScale:1);
point.z = k?size:-size;
points.push(point);
normals.push(vec3(0,0,point.z));
}
for (let i = facePoints.length; i--;)
{
// build sides of mesh
const point1 = facePoints[i];
const point2 = facePoints[(i+1)%facePoints.length];
const s = vec3(0,0,size);
const pointA = point1.scale(topScale).add(s);
const pointB = point2.scale(topScale).add(s);
const pointC = point1.subtract(s);
const pointD = point2.subtract(s);
const sidePoints = [pointA, pointA, pointB, pointC, pointD, pointD];
const normal = pointA.subtract(pointB).cross(pointA.subtract(pointC)).normalize();
points.push(...sidePoints);
for (const p of sidePoints)
normals.push(normal);
}
this.points = points;
this.normals = normals;
return this;
}
buildLathe(facePoints, segments=16)
{
// convert list of 2D points into a lathe 3D shape
const points = [], normals = [];
const angleStep = PI*2/segments;
for (let i=segments; i--;)
{
const angle2 = (i+.5)*angleStep;
// todo fix normals are not tilted correctly
const normal = vec3(1,0).rotateY(angle2);
for (let j = facePoints.length; j--;)
{
const point1 = facePoints[j].rotateY(i*angleStep);
const point2 = facePoints[j].rotateY((i+1)*angleStep);
// add vertices for the lathe shape
points.push(point2);
points.push(point1);
normals.push(normal);
normals.push(normal);
if (!j)
{
// degenrate triangles at the end of the shape
points.push(point1);
points.push(point1);
normals.push(normal);
normals.push(normal);
}
}
}
this.points = points;
this.normals = normals;
return this;
}
combine(mesh, pos, rot, scale)
{
const m = buildMatrix(pos, rot, scale);
const m2 = buildMatrix(0, rot);
this.points.push(...mesh.points.map(p=>p.transform(m)));
this.normals && this.normals.push(...mesh.normals.map(p=>p.transform(m2)));
this.uvs && this.uvs.push(...mesh.uvs);
return this;
}
transform(pos, rot, scale)
{
const m = buildMatrix(pos, rot, scale);
const m2 = buildMatrix(0, rot);
this.points = this.points.map(p=>p.transform(m));
this.normals = this.normals.map(p=>p.transform(m2));
return this;
}
render(transform, color)
{
glPushPoints(this.points, this.normals, color);
glRender(transform);
}
renderUnlit(transform, color, unlit=true)
{
if (unlit)
glPushMonoColoredPoints(this.points, color);
else
glPushPoints(this.points, this.normals, color);
glRender(transform);
}
renderTile(transform, color, tile)
{
ASSERT(tile instanceof Tile);
const uvs = this.uvs.map(uv=>vec3(uv.x*tile.size.x+tile.pos.x,uv.y*tile.size.y+tile.pos.y));
glPushPoints(this.points, this.normals, color, uvs);
glRender(transform);
}
/*transformAndPush(transform, color)
{
//this.render(transform, color);
//return
function getInverseTranspose(domMatrix) {
// Clone the DOMMatrix to avoid modifying the original
const inverseMatrix = domMatrix.inverse();
// Transpose the upper-left 3x3 part of the matrix
return new DOMMatrix([
inverseMatrix.m11, inverseMatrix.m21, inverseMatrix.m31, 0,
inverseMatrix.m12, inverseMatrix.m22, inverseMatrix.m32, 0,
inverseMatrix.m13, inverseMatrix.m23, inverseMatrix.m33, 0,
0, 0, 0, 1
]);
}
const inv = getInverseTranspose(transform);
const points = this.points.map(p=>p.transformDOM(transform));
const normals = this.normals.map(p=>p.transformDOM(inv));
glPushPoints(points, normals, color);
}*/
}
///////////////////////////////////////////////////////////////////////////////
function pushGradient(pos, size, color, color2)
{
const flip = size.x < 0;
size = size.abs();
const mesh = quadMesh;
const points = mesh.points.map(p=>vec3(p.x*size.x+pos.x,p.y*size.y+pos.y,p.z*size.z+pos.z));
let colors = [];
for (let i = 0; i < points.length; i++)
colors[i] = i>2 ? color2 : color;
glPushColoredPoints(points, colors);
}
function pushSprite(pos, size, color, tile)
{
const flip = size.x < 0;
size = size.abs();
const mesh = quadMesh;
const points = mesh.points.map(p=>vec3(p.x*size.x+pos.x,p.y*size.y+pos.y,p.z*size.z+pos.z));
if (tile)
{
ASSERT(tile instanceof Tile);
if (flip)
{
tile.pos.x += tile.size.x;
tile.size.x *= -1;
}
const uvs = mesh.uvs.map(uv=>vec3(uv.x*tile.size.x+tile.pos.x,uv.y*tile.size.y+tile.pos.y));
glPushPoints(points, 0, color, uvs);
}
else
glPushPoints(points, 0, color);
}
function pushShadow(pos, xSize, zSize, rotation, shape=1)
{
const color = hsl(0,0,0,.5)
const size = vec3(xSize,0,zSize);
const tile = getGenerativeTile(vec3(shape,0));
const mesh = shadowMesh;
let points = mesh.points;
if (rotation)
{
const m2 = buildMatrix(pos, vec3(rotation.x,0,rotation.z), size);
const m3 = buildMatrix(0, vec3(0,rotation.y,0), 0);
const m1 = m2.multiply(m3);
const marix = buildMatrix(pos,rotation,size);
points = points.map(p=>p.transform(m1));
}
else
points = points.map(p=>vec3(p.x*size.x+pos.x,pos.y,p.z*size.z+pos.z));
const uvs = mesh.uvs.map(uv=>vec3(uv.x*tile.size.x+tile.pos.x,uv.y*tile.size.y+tile.pos.y));
glPushPoints(points, 0, color, uvs);
}
function getGenerativeTile(pos)
{
const w = generativeTileSize;
return new Tile(pos.scale(w), generativeTileSizeVec3);
}