-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge-renderer-worker.js
More file actions
454 lines (382 loc) · 15.1 KB
/
edge-renderer-worker.js
File metadata and controls
454 lines (382 loc) · 15.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Edge Renderer Web Worker
// Handles distributed rendering calculations
class EdgeRendererWorker {
constructor() {
this.isRunning = false;
this.workQueue = [];
this.results = new Map();
// Listen for messages from main thread
self.addEventListener('message', (event) => {
const { type, data, id } = event.data;
switch (type) {
case 'START':
this.start();
break;
case 'STOP':
this.stop();
break;
case 'RENDER_TASK':
this.processRenderTask(data, id);
break;
case 'GEOMETRY_PROCESSING':
this.processGeometry(data, id);
break;
case 'PARTICLE_SIMULATION':
this.simulateParticles(data, id);
break;
case 'LIGHTING_CALCULATION':
this.calculateLighting(data, id);
break;
}
});
}
start() {
this.isRunning = true;
this.processQueue();
}
stop() {
this.isRunning = false;
}
processQueue() {
if (!this.isRunning || this.workQueue.length === 0) {
setTimeout(() => this.processQueue(), 16); // ~60fps
return;
}
const task = this.workQueue.shift();
this.executeTask(task);
// Continue processing
setTimeout(() => this.processQueue(), 1);
}
executeTask(task) {
const { type, data, id } = task;
try {
let result;
switch (type) {
case 'RENDER_TASK':
result = this.processRenderTask(data, id);
break;
case 'GEOMETRY_PROCESSING':
result = this.processGeometry(data, id);
break;
case 'PARTICLE_SIMULATION':
result = this.simulateParticles(data, id);
break;
case 'LIGHTING_CALCULATION':
result = this.calculateLighting(data, id);
break;
}
// Send result back to main thread
self.postMessage({
type: 'RESULT',
id: id,
data: result
});
} catch (error) {
self.postMessage({
type: 'ERROR',
id: id,
error: error.message
});
}
}
processRenderTask(data, id) {
const { vertices, indices, matrix, time } = data;
// Perform vertex transformations
const transformedVertices = new Float32Array(vertices.length);
for (let i = 0; i < vertices.length; i += 3) {
const x = vertices[i];
const y = vertices[i + 1];
const z = vertices[i + 2];
// Apply transformation matrix
const transformedX = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12];
const transformedY = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13];
const transformedZ = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14];
transformedVertices[i] = transformedX;
transformedVertices[i + 1] = transformedY;
transformedVertices[i + 2] = transformedZ;
}
// Perform frustum culling
const visibleIndices = this.frustumCull(transformedVertices, indices);
return {
vertices: transformedVertices,
indices: visibleIndices,
triangleCount: visibleIndices.length / 3
};
}
processGeometry(data, id) {
const { type, parameters } = data;
switch (type) {
case 'ICOSPHERE':
return this.generateIcosphere(parameters.radius, parameters.subdivisions);
case 'PLANE':
return this.generatePlane(parameters.width, parameters.height, parameters.segments);
case 'CUBE':
return this.generateCube(parameters.size);
case 'TORUS':
return this.generateTorus(parameters.majorRadius, parameters.minorRadius, parameters.majorSegments, parameters.minorSegments);
}
}
generateIcosphere(radius, subdivisions) {
const vertices = [];
const normals = [];
const texCoords = [];
const indices = [];
// Create base icosahedron
const t = (1.0 + Math.sqrt(5.0)) / 2.0;
const positions = [
[-1, t, 0], [1, t, 0], [-1, -t, 0], [1, -t, 0],
[0, -1, t], [0, 1, t], [0, -1, -t], [0, 1, -t],
[t, 0, -1], [t, 0, 1], [-t, 0, -1], [-t, 0, 1]
];
// Normalize vertices
positions.forEach(pos => {
const length = Math.sqrt(pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2]);
pos[0] /= length;
pos[1] /= length;
pos[2] /= length;
});
// Create faces
const faces = [
[0, 11, 5], [0, 5, 1], [0, 1, 7], [0, 7, 10], [0, 10, 11],
[1, 5, 9], [5, 11, 4], [11, 10, 2], [10, 7, 6], [7, 1, 8],
[3, 9, 4], [3, 4, 2], [3, 2, 6], [3, 6, 8], [3, 8, 9],
[4, 9, 5], [2, 4, 11], [6, 2, 10], [8, 6, 7], [9, 8, 1]
];
// Subdivide
for (let i = 0; i < subdivisions; i++) {
this.subdivideIcosahedron(positions, faces);
}
// Convert to arrays
faces.forEach(face => {
face.forEach(vertexIndex => {
const pos = positions[vertexIndex];
vertices.push(pos[0] * radius, pos[1] * radius, pos[2] * radius);
normals.push(pos[0], pos[1], pos[2]);
texCoords.push(0.5, 0.5);
});
});
// Create indices
for (let i = 0; i < faces.length; i++) {
indices.push(i * 3, i * 3 + 1, i * 3 + 2);
}
return {
vertices: new Float32Array(vertices),
normals: new Float32Array(normals),
texCoords: new Float32Array(texCoords),
indices: new Uint16Array(indices)
};
}
generatePlane(width, height, segments) {
const vertices = [];
const normals = [];
const texCoords = [];
const indices = [];
const stepX = width / segments;
const stepY = height / segments;
for (let y = 0; y <= segments; y++) {
for (let x = 0; x <= segments; x++) {
vertices.push(
(x * stepX) - width / 2,
0,
(y * stepY) - height / 2
);
normals.push(0, 1, 0);
texCoords.push(x / segments, y / segments);
}
}
for (let y = 0; y < segments; y++) {
for (let x = 0; x < segments; x++) {
const a = y * (segments + 1) + x;
const b = a + 1;
const c = a + segments + 1;
const d = c + 1;
indices.push(a, b, c);
indices.push(b, d, c);
}
}
return {
vertices: new Float32Array(vertices),
normals: new Float32Array(normals),
texCoords: new Float32Array(texCoords),
indices: new Uint16Array(indices)
};
}
generateCube(size) {
const half = size / 2;
const vertices = [
// Front face
-half, -half, half, half, -half, half, half, half, half, -half, half, half,
// Back face
-half, -half, -half, -half, half, -half, half, half, -half, half, -half, -half,
// Top face
-half, half, -half, -half, half, half, half, half, half, half, half, -half,
// Bottom face
-half, -half, -half, half, -half, -half, half, -half, half, -half, -half, half,
// Right face
half, -half, -half, half, half, -half, half, half, half, half, -half, half,
// Left face
-half, -half, -half, -half, -half, half, -half, half, half, -half, half, -half
];
const normals = [
// Front face
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
// Back face
0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1,
// Top face
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
// Bottom face
0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
// Right face
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
// Left face
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0
];
const texCoords = [
// Front face
0, 0, 1, 0, 1, 1, 0, 1,
// Back face
1, 0, 1, 1, 0, 1, 0, 0,
// Top face
0, 1, 0, 0, 1, 0, 1, 1,
// Bottom face
1, 1, 0, 1, 0, 0, 1, 0,
// Right face
1, 0, 1, 1, 0, 1, 0, 0,
// Left face
0, 0, 1, 0, 1, 1, 0, 1
];
const indices = [
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // back
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // bottom
16, 17, 18, 16, 18, 19, // right
20, 21, 22, 20, 22, 23 // left
];
return {
vertices: new Float32Array(vertices),
normals: new Float32Array(normals),
texCoords: new Float32Array(texCoords),
indices: new Uint16Array(indices)
};
}
generateTorus(majorRadius, minorRadius, majorSegments, minorSegments) {
const vertices = [];
const normals = [];
const texCoords = [];
const indices = [];
for (let i = 0; i <= majorSegments; i++) {
const u = (i / majorSegments) * Math.PI * 2;
const cosU = Math.cos(u);
const sinU = Math.sin(u);
for (let j = 0; j <= minorSegments; j++) {
const v = (j / minorSegments) * Math.PI * 2;
const cosV = Math.cos(v);
const sinV = Math.sin(v);
const x = (majorRadius + minorRadius * cosV) * cosU;
const y = minorRadius * sinV;
const z = (majorRadius + minorRadius * cosV) * sinU;
vertices.push(x, y, z);
// Calculate normal
const nx = cosV * cosU;
const ny = sinV;
const nz = cosV * sinU;
normals.push(nx, ny, nz);
texCoords.push(i / majorSegments, j / minorSegments);
}
}
for (let i = 0; i < majorSegments; i++) {
for (let j = 0; j < minorSegments; j++) {
const a = i * (minorSegments + 1) + j;
const b = a + 1;
const c = a + minorSegments + 1;
const d = c + 1;
indices.push(a, b, c);
indices.push(b, d, c);
}
}
return {
vertices: new Float32Array(vertices),
normals: new Float32Array(normals),
texCoords: new Float32Array(texCoords),
indices: new Uint16Array(indices)
};
}
simulateParticles(data, id) {
const { particles, deltaTime, gravity } = data;
const updatedParticles = [];
particles.forEach(particle => {
// Update position based on velocity
const newPosition = [
particle.position[0] + particle.velocity[0] * deltaTime,
particle.position[1] + particle.velocity[1] * deltaTime,
particle.position[2] + particle.velocity[2] * deltaTime
];
// Update velocity based on gravity
const newVelocity = [
particle.velocity[0] + gravity[0] * deltaTime,
particle.velocity[1] + gravity[1] * deltaTime,
particle.velocity[2] + gravity[2] * deltaTime
];
// Update life
const newLife = particle.life - deltaTime * 0.5;
updatedParticles.push({
position: newPosition,
velocity: newVelocity,
life: newLife > 0 ? newLife : 1.0,
size: particle.size
});
});
return updatedParticles;
}
calculateLighting(data, id) {
const { vertices, normals, lightPosition, lightColor, materialColor } = data;
const litVertices = new Float32Array(vertices.length);
for (let i = 0; i < vertices.length; i += 3) {
const vertex = [vertices[i], vertices[i + 1], vertices[i + 2]];
const normal = [normals[i], normals[i + 1], normals[i + 2]];
// Calculate light direction
const lightDir = [
lightPosition[0] - vertex[0],
lightPosition[1] - vertex[1],
lightPosition[2] - vertex[2]
];
// Normalize light direction
const length = Math.sqrt(lightDir[0] * lightDir[0] + lightDir[1] * lightDir[1] + lightDir[2] * lightDir[2]);
lightDir[0] /= length;
lightDir[1] /= length;
lightDir[2] /= length;
// Calculate dot product
const dot = normal[0] * lightDir[0] + normal[1] * lightDir[1] + normal[2] * lightDir[2];
const intensity = Math.max(0, dot);
// Apply lighting
litVertices[i] = vertex[0];
litVertices[i + 1] = vertex[1];
litVertices[i + 2] = vertex[2];
}
return litVertices;
}
frustumCull(vertices, indices) {
// Simple frustum culling - in a real implementation, this would be more sophisticated
const visibleIndices = [];
for (let i = 0; i < indices.length; i += 3) {
const i1 = indices[i] * 3;
const i2 = indices[i + 1] * 3;
const i3 = indices[i + 2] * 3;
// Check if triangle is in front of camera
const z1 = vertices[i1 + 2];
const z2 = vertices[i2 + 2];
const z3 = vertices[i3 + 2];
if (z1 > 0.1 && z2 > 0.1 && z3 > 0.1) {
visibleIndices.push(indices[i], indices[i + 1], indices[i + 2]);
}
}
return new Uint16Array(visibleIndices);
}
subdivideIcosahedron(positions, faces) {
// Simplified subdivision - in a real implementation, this would be more complex
// For now, we'll just use the basic icosahedron
}
}
// Initialize the worker
new EdgeRendererWorker();