matrix-engine-wgpu 1.3.13 → 1.3.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +23 -0
- package/src/engine/ball.js +0 -482
- package/src/engine/cube.js +0 -496
- package/src/engine/engine.js +0 -476
- package/src/engine/final/adaptJSON1.js +0 -53
- package/src/engine/final/utils2.js +0 -63
- package/src/engine/lights.js +0 -126
- package/src/engine/loader-obj.js +0 -473
- package/src/engine/materials.js +0 -307
- package/src/engine/matrix-class.js +0 -252
- package/src/engine/mesh-obj.js +0 -553
- package/src/engine/mesh.js +0 -475
- package/src/engine/raycast.js +0 -219
- package/src/engine/utils.js +0 -881
- package/src/libs/mat.js +0 -0
- package/src/multilang/lang.js +0 -35
- package/src/physics/matrix-ammo.js +0 -361
- package/src/shaders/fragment.video.wgsl.js +0 -83
- package/src/shaders/fragment.wgsl.js +0 -75
- package/src/shaders/shaders.js +0 -51
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-fs.glsl +0 -56
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-vs.glsl +0 -75
- package/src/shaders/vertex.wgsl.js +0 -49
- package/src/shaders/vertexShadow.wgsl.js +0 -20
- package/src/sounds/sounds.js +0 -69
- package/src/world.js +0 -427
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "matrix-engine-wgpu",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.16",
|
|
4
4
|
"description": "obj sequence anim +HOTFIX raycast, webGPU powered pwa application. Crazy fast rendering with AmmoJS physics support. Simple raycaster hit object added.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
package/readme.md
CHANGED
|
@@ -142,6 +142,29 @@ app.cameras.WASD.pitch = 0.2;
|
|
|
142
142
|
|
|
143
143
|
---
|
|
144
144
|
|
|
145
|
+
💡 Lighting System
|
|
146
|
+
|
|
147
|
+
Matrix Engine WGPU now supports independent light entities, meaning lights are no longer tied to the camera. You can freely place and configure lights in the scene, and they will affect objects based on their type and parameters.
|
|
148
|
+
|
|
149
|
+
Supported Light Types
|
|
150
|
+
|
|
151
|
+
SpotLight – Emits light in a cone shape with configurable cutoff angles.
|
|
152
|
+
|
|
153
|
+
(Planned: PointLight, DirectionalLight, AmbientLight)
|
|
154
|
+
|
|
155
|
+
Features
|
|
156
|
+
|
|
157
|
+
✅ Independent from camera (lights follow their own transform, not the player view)
|
|
158
|
+
|
|
159
|
+
✅ Uniform buffer system for per-light data
|
|
160
|
+
|
|
161
|
+
✅ Works with existing scene objects and materials
|
|
162
|
+
|
|
163
|
+
✅ Supports multiple lights (attach as many as you want to the scene)
|
|
164
|
+
|
|
165
|
+
✅ Shadow-ready (spotlight shadows implemented, extendable to others)
|
|
166
|
+
|
|
167
|
+
|
|
145
168
|
### Object Interaction (Raycasting)
|
|
146
169
|
|
|
147
170
|
The raycast returns:
|
package/src/engine/ball.js
DELETED
|
@@ -1,482 +0,0 @@
|
|
|
1
|
-
import {UNLIT_SHADER} from "../shaders/shaders";
|
|
2
|
-
import {mat4, vec3} from 'wgpu-matrix';
|
|
3
|
-
import {Position, Rotation} from "./matrix-class";
|
|
4
|
-
import {createInputHandler} from "./engine";
|
|
5
|
-
|
|
6
|
-
export default class MEBall {
|
|
7
|
-
|
|
8
|
-
constructor(canvas, device, context, o) {
|
|
9
|
-
this.context = context;
|
|
10
|
-
this.device = device;
|
|
11
|
-
|
|
12
|
-
// The input handler
|
|
13
|
-
this.inputHandler = createInputHandler(window, canvas);
|
|
14
|
-
this.cameras = o.cameras;
|
|
15
|
-
this.scale = o.scale;
|
|
16
|
-
console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
|
|
17
|
-
this.mainCameraParams = {
|
|
18
|
-
type: o.mainCameraParams.type,
|
|
19
|
-
responseCoef: o.mainCameraParams.responseCoef
|
|
20
|
-
} // | WASD 'arcball' };
|
|
21
|
-
|
|
22
|
-
this.lastFrameMS = 0;
|
|
23
|
-
|
|
24
|
-
this.entityArgPass = o.entityArgPass;
|
|
25
|
-
|
|
26
|
-
this.SphereLayout = {
|
|
27
|
-
vertexStride: 8 * 4,
|
|
28
|
-
positionsOffset: 0,
|
|
29
|
-
normalOffset: 3 * 4,
|
|
30
|
-
uvOffset: 6 * 4,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
if(typeof o.raycast === 'undefined') {
|
|
34
|
-
this.raycast = {
|
|
35
|
-
enabled: false,
|
|
36
|
-
radius: 2
|
|
37
|
-
};
|
|
38
|
-
} else {
|
|
39
|
-
this.raycast = o.raycast;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this.texturesPaths = [];
|
|
43
|
-
|
|
44
|
-
o.texturesPaths.forEach((t) => {
|
|
45
|
-
this.texturesPaths.push(t)
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
this.position = new Position(o.position.x, o.position.y, o.position.z)
|
|
49
|
-
this.rotation = new Rotation(o.rotation.x, o.rotation.y, o.rotation.z);
|
|
50
|
-
this.rotation.rotationSpeed.x = o.rotationSpeed.x;
|
|
51
|
-
this.rotation.rotationSpeed.y = o.rotationSpeed.y;
|
|
52
|
-
this.rotation.rotationSpeed.z = o.rotationSpeed.z;
|
|
53
|
-
|
|
54
|
-
this.shaderModule = device.createShaderModule({code: UNLIT_SHADER});
|
|
55
|
-
this.presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
56
|
-
|
|
57
|
-
this.pipeline = device.createRenderPipeline({
|
|
58
|
-
layout: 'auto',
|
|
59
|
-
vertex: {
|
|
60
|
-
module: this.shaderModule,
|
|
61
|
-
entryPoint: 'vertexMain',
|
|
62
|
-
buffers: [
|
|
63
|
-
{
|
|
64
|
-
arrayStride: this.SphereLayout.vertexStride,
|
|
65
|
-
attributes: [
|
|
66
|
-
{
|
|
67
|
-
// position
|
|
68
|
-
shaderLocation: 0,
|
|
69
|
-
offset: this.SphereLayout.positionsOffset,
|
|
70
|
-
format: 'float32x3',
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
// normal
|
|
74
|
-
shaderLocation: 1,
|
|
75
|
-
offset: this.SphereLayout.normalOffset,
|
|
76
|
-
format: 'float32x3',
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
// uv
|
|
80
|
-
shaderLocation: 2,
|
|
81
|
-
offset: this.SphereLayout.uvOffset,
|
|
82
|
-
format: 'float32x2',
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
},
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
fragment: {
|
|
89
|
-
module: this.shaderModule,
|
|
90
|
-
entryPoint: 'fragmentMain',
|
|
91
|
-
targets: [
|
|
92
|
-
{
|
|
93
|
-
format: this.presentationFormat,
|
|
94
|
-
},
|
|
95
|
-
],
|
|
96
|
-
},
|
|
97
|
-
primitive: {
|
|
98
|
-
topology: 'triangle-list',
|
|
99
|
-
|
|
100
|
-
// Backface culling since the sphere is solid piece of geometry.
|
|
101
|
-
// Faces pointing away from the camera will be occluded by faces
|
|
102
|
-
// pointing toward the camera.
|
|
103
|
-
cullMode: 'back',
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
// Enable depth testing so that the fragment closest to the camera
|
|
107
|
-
// is rendered in front.
|
|
108
|
-
depthStencil: {
|
|
109
|
-
depthWriteEnabled: true,
|
|
110
|
-
depthCompare: 'less',
|
|
111
|
-
format: 'depth24plus',
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
this.depthTexture = device.createTexture({
|
|
116
|
-
size: [canvas.width, canvas.height],
|
|
117
|
-
format: 'depth24plus',
|
|
118
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
this.uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
122
|
-
this.uniformBuffer = device.createBuffer({
|
|
123
|
-
size: this.uniformBufferSize,
|
|
124
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Fetch the images and upload them into a GPUTexture.
|
|
128
|
-
this.texture0 = null;
|
|
129
|
-
this.moonTexture = null;
|
|
130
|
-
|
|
131
|
-
this.settings = {
|
|
132
|
-
useRenderBundles: true,
|
|
133
|
-
asteroidCount: 15,
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
this.loadTex0(this.texturesPaths, device).then(() => {
|
|
137
|
-
this.loadTex1(this.texturesPaths, device).then(() => {
|
|
138
|
-
this.sampler = device.createSampler({
|
|
139
|
-
magFilter: 'linear',
|
|
140
|
-
minFilter: 'linear',
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
this.transform = mat4.create();
|
|
144
|
-
mat4.identity(this.transform);
|
|
145
|
-
|
|
146
|
-
// Create one large central planet surrounded by a large ring of asteroids
|
|
147
|
-
this.planet = this.createGeometry(this.scale);
|
|
148
|
-
this.planet.bindGroup = this.createSphereBindGroup(this.texture0, this.transform);
|
|
149
|
-
|
|
150
|
-
var asteroids = [
|
|
151
|
-
this.createGeometry(12, 8, 6, 0.15),
|
|
152
|
-
];
|
|
153
|
-
|
|
154
|
-
this.renderables = [this.planet];
|
|
155
|
-
|
|
156
|
-
// this.ensureEnoughAsteroids(asteroids, this.transform);
|
|
157
|
-
this.renderPassDescriptor = {
|
|
158
|
-
colorAttachments: [
|
|
159
|
-
{
|
|
160
|
-
view: undefined,
|
|
161
|
-
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
|
|
162
|
-
loadOp: this.entityArgPass.loadOp,
|
|
163
|
-
storeOp: this.entityArgPass.storeOp,
|
|
164
|
-
},
|
|
165
|
-
],
|
|
166
|
-
depthStencilAttachment: {
|
|
167
|
-
view: this.depthTexture.createView(),
|
|
168
|
-
depthClearValue: 1.0,
|
|
169
|
-
depthLoadOp: this.entityArgPass.depthLoadOp,
|
|
170
|
-
depthStoreOp: this.entityArgPass.depthStoreOp,
|
|
171
|
-
},
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const aspect = canvas.width / canvas.height;
|
|
175
|
-
this.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 100.0);
|
|
176
|
-
this.modelViewProjectionMatrix = mat4.create();
|
|
177
|
-
|
|
178
|
-
this.frameBindGroup = device.createBindGroup({
|
|
179
|
-
layout: this.pipeline.getBindGroupLayout(0),
|
|
180
|
-
entries: [
|
|
181
|
-
{
|
|
182
|
-
binding: 0,
|
|
183
|
-
resource: {
|
|
184
|
-
buffer: this.uniformBuffer,
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
],
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
// The render bundle can be encoded once and re-used as many times as needed.
|
|
192
|
-
// Because it encodes all of the commands needed to render at the GPU level,
|
|
193
|
-
// those commands will not need to execute the associated JavaScript code upon
|
|
194
|
-
// execution or be re-validated, which can represent a significant time savings.
|
|
195
|
-
//
|
|
196
|
-
// However, because render bundles are immutable once created, they are only
|
|
197
|
-
// appropriate for rendering content where the same commands will be executed
|
|
198
|
-
// every time, with the only changes being the contents of the buffers and
|
|
199
|
-
// textures used. Cases where the executed commands differ from frame-to-frame,
|
|
200
|
-
// such as when using frustrum or occlusion culling, will not benefit from
|
|
201
|
-
// using render bundles as much.
|
|
202
|
-
this.renderBundle;
|
|
203
|
-
this.updateRenderBundle();
|
|
204
|
-
|
|
205
|
-
})
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
ensureEnoughAsteroids(asteroids, transform) {
|
|
211
|
-
for(let i = this.renderables.length;i <= this.settings.asteroidCount;++i) {
|
|
212
|
-
// Place copies of the asteroid in a ring.
|
|
213
|
-
const radius = Math.random() * 1.7 + 1.25;
|
|
214
|
-
const angle = Math.random() * Math.PI * 2;
|
|
215
|
-
const x = Math.sin(angle) * radius;
|
|
216
|
-
const y = (Math.random() - 0.5) * 0.015;
|
|
217
|
-
const z = Math.cos(angle) * radius;
|
|
218
|
-
|
|
219
|
-
mat4.identity(transform);
|
|
220
|
-
mat4.translate(transform, [x, y, z], transform);
|
|
221
|
-
mat4.rotateX(transform, Math.random() * Math.PI, transform);
|
|
222
|
-
mat4.rotateY(transform, Math.random() * Math.PI, transform);
|
|
223
|
-
this.renderables.push({
|
|
224
|
-
...asteroids[i % asteroids.length],
|
|
225
|
-
bindGroup: this.createSphereBindGroup(this.moonTexture, transform),
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
updateRenderBundle() {
|
|
231
|
-
console.log('updateRenderBundle')
|
|
232
|
-
const renderBundleEncoder = this.device.createRenderBundleEncoder({
|
|
233
|
-
colorFormats: [this.presentationFormat],
|
|
234
|
-
depthStencilFormat: 'depth24plus',
|
|
235
|
-
});
|
|
236
|
-
this.renderScene(renderBundleEncoder);
|
|
237
|
-
this.renderBundle = renderBundleEncoder.finish();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
createGeometry(radius, widthSegments = 8, heightSegments = 4, randomness = 0) {
|
|
241
|
-
|
|
242
|
-
const sphereMesh = this.createSphereMesh(radius, widthSegments, heightSegments, randomness);
|
|
243
|
-
// Create a vertex buffer from the sphere data.
|
|
244
|
-
const vertices = this.device.createBuffer({
|
|
245
|
-
size: sphereMesh.vertices.byteLength,
|
|
246
|
-
usage: GPUBufferUsage.VERTEX,
|
|
247
|
-
mappedAtCreation: true,
|
|
248
|
-
});
|
|
249
|
-
new Float32Array(vertices.getMappedRange()).set(sphereMesh.vertices);
|
|
250
|
-
vertices.unmap();
|
|
251
|
-
|
|
252
|
-
const indices = this.device.createBuffer({
|
|
253
|
-
size: sphereMesh.indices.byteLength,
|
|
254
|
-
usage: GPUBufferUsage.INDEX,
|
|
255
|
-
mappedAtCreation: true,
|
|
256
|
-
});
|
|
257
|
-
new Uint16Array(indices.getMappedRange()).set(sphereMesh.indices);
|
|
258
|
-
indices.unmap();
|
|
259
|
-
|
|
260
|
-
return {
|
|
261
|
-
vertices,
|
|
262
|
-
indices,
|
|
263
|
-
indexCount: sphereMesh.indices.length,
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
createSphereBindGroup(texture, transform) {
|
|
268
|
-
|
|
269
|
-
const uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
270
|
-
const uniformBuffer = this.device.createBuffer({
|
|
271
|
-
size: uniformBufferSize,
|
|
272
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
273
|
-
mappedAtCreation: true,
|
|
274
|
-
});
|
|
275
|
-
new Float32Array(uniformBuffer.getMappedRange()).set(transform);
|
|
276
|
-
uniformBuffer.unmap();
|
|
277
|
-
|
|
278
|
-
const bindGroup = this.device.createBindGroup({
|
|
279
|
-
layout: this.pipeline.getBindGroupLayout(1),
|
|
280
|
-
entries: [
|
|
281
|
-
{
|
|
282
|
-
binding: 0,
|
|
283
|
-
resource: {
|
|
284
|
-
buffer: uniformBuffer,
|
|
285
|
-
},
|
|
286
|
-
},
|
|
287
|
-
{
|
|
288
|
-
binding: 1,
|
|
289
|
-
resource: this.sampler,
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
binding: 2,
|
|
293
|
-
resource: texture.createView(),
|
|
294
|
-
},
|
|
295
|
-
],
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
return bindGroup;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
getTransformationMatrix(pos) {
|
|
302
|
-
// const viewMatrix = mat4.identity();
|
|
303
|
-
const now = Date.now();
|
|
304
|
-
const deltaTime = (now - this.lastFrameMS) / this.mainCameraParams.responseCoef;
|
|
305
|
-
this.lastFrameMS = now;
|
|
306
|
-
|
|
307
|
-
// const viewMatrix = mat4.identity(); ORI
|
|
308
|
-
const camera = this.cameras[this.mainCameraParams.type];
|
|
309
|
-
const viewMatrix = camera.update(deltaTime, this.inputHandler());
|
|
310
|
-
|
|
311
|
-
mat4.translate(viewMatrix, vec3.fromValues(pos.x, pos.y, pos.z), viewMatrix);
|
|
312
|
-
|
|
313
|
-
mat4.rotateX(viewMatrix, Math.PI * this.rotation.getRotX(), viewMatrix);
|
|
314
|
-
mat4.rotateY(viewMatrix, Math.PI * this.rotation.getRotY(), viewMatrix);
|
|
315
|
-
mat4.rotateZ(viewMatrix, Math.PI * this.rotation.getRotZ(), viewMatrix);
|
|
316
|
-
|
|
317
|
-
mat4.multiply(this.projectionMatrix, viewMatrix, this.modelViewProjectionMatrix);
|
|
318
|
-
return this.modelViewProjectionMatrix;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
async loadTex1(texPaths, device) {
|
|
322
|
-
return new Promise(async (resolve) => {
|
|
323
|
-
const response = await fetch(texPaths[0]);
|
|
324
|
-
const imageBitmap = await createImageBitmap(await response.blob());
|
|
325
|
-
|
|
326
|
-
this.moonTexture = device.createTexture({
|
|
327
|
-
size: [imageBitmap.width, imageBitmap.height, 1],
|
|
328
|
-
format: 'rgba8unorm',
|
|
329
|
-
usage:
|
|
330
|
-
GPUTextureUsage.TEXTURE_BINDING |
|
|
331
|
-
GPUTextureUsage.COPY_DST |
|
|
332
|
-
GPUTextureUsage.RENDER_ATTACHMENT,
|
|
333
|
-
});
|
|
334
|
-
var moonTexture = this.moonTexture
|
|
335
|
-
device.queue.copyExternalImageToTexture(
|
|
336
|
-
{source: imageBitmap},
|
|
337
|
-
{texture: moonTexture},
|
|
338
|
-
[imageBitmap.width, imageBitmap.height]
|
|
339
|
-
);
|
|
340
|
-
resolve()
|
|
341
|
-
})
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async loadTex0(paths, device) {
|
|
345
|
-
return new Promise(async (resolve) => {
|
|
346
|
-
const response = await fetch(paths[0]);
|
|
347
|
-
const imageBitmap = await createImageBitmap(await response.blob());
|
|
348
|
-
console.log('loadTex0 WHAT IS THIS -> ', this)
|
|
349
|
-
this.texture0 = device.createTexture({
|
|
350
|
-
size: [imageBitmap.width, imageBitmap.height, 1],
|
|
351
|
-
format: 'rgba8unorm',
|
|
352
|
-
usage:
|
|
353
|
-
GPUTextureUsage.TEXTURE_BINDING |
|
|
354
|
-
GPUTextureUsage.COPY_DST |
|
|
355
|
-
GPUTextureUsage.RENDER_ATTACHMENT,
|
|
356
|
-
});
|
|
357
|
-
var texture0 = this.texture0
|
|
358
|
-
device.queue.copyExternalImageToTexture(
|
|
359
|
-
{source: imageBitmap},
|
|
360
|
-
{texture: texture0},
|
|
361
|
-
[imageBitmap.width, imageBitmap.height]
|
|
362
|
-
);
|
|
363
|
-
resolve()
|
|
364
|
-
})
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
createSphereMesh(radius, widthSegments = 3, heightSegments = 3, randomness = 0) {
|
|
369
|
-
const vertices = [];
|
|
370
|
-
const indices = [];
|
|
371
|
-
|
|
372
|
-
widthSegments = Math.max(3, Math.floor(widthSegments));
|
|
373
|
-
heightSegments = Math.max(2, Math.floor(heightSegments));
|
|
374
|
-
|
|
375
|
-
const firstVertex = vec3.create();
|
|
376
|
-
const vertex = vec3.create();
|
|
377
|
-
const normal = vec3.create();
|
|
378
|
-
|
|
379
|
-
let index = 0;
|
|
380
|
-
const grid = [];
|
|
381
|
-
|
|
382
|
-
// generate vertices, normals and uvs
|
|
383
|
-
for(let iy = 0;iy <= heightSegments;iy++) {
|
|
384
|
-
const verticesRow = [];
|
|
385
|
-
const v = iy / heightSegments;
|
|
386
|
-
// special case for the poles
|
|
387
|
-
let uOffset = 0;
|
|
388
|
-
if(iy === 0) {
|
|
389
|
-
uOffset = 0.5 / widthSegments;
|
|
390
|
-
} else if(iy === heightSegments) {
|
|
391
|
-
uOffset = -0.5 / widthSegments;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
for(let ix = 0;ix <= widthSegments;ix++) {
|
|
395
|
-
const u = ix / widthSegments;
|
|
396
|
-
// Poles should just use the same position all the way around.
|
|
397
|
-
if(ix == widthSegments) {
|
|
398
|
-
vec3.copy(firstVertex, vertex);
|
|
399
|
-
} else if(ix == 0 || (iy != 0 && iy !== heightSegments)) {
|
|
400
|
-
const rr = radius + (Math.random() - 0.5) * 2 * randomness * radius;
|
|
401
|
-
// vertex
|
|
402
|
-
vertex[0] = -rr * Math.cos(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
403
|
-
vertex[1] = rr * Math.cos(v * Math.PI);
|
|
404
|
-
vertex[2] = rr * Math.sin(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
405
|
-
if(ix == 0) {
|
|
406
|
-
vec3.copy(vertex, firstVertex);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
vertices.push(...vertex);
|
|
410
|
-
|
|
411
|
-
// normal
|
|
412
|
-
vec3.copy(vertex, normal);
|
|
413
|
-
vec3.normalize(normal, normal);
|
|
414
|
-
vertices.push(...normal);
|
|
415
|
-
// uv
|
|
416
|
-
vertices.push(u + uOffset, 1 - v);
|
|
417
|
-
verticesRow.push(index++);
|
|
418
|
-
}
|
|
419
|
-
grid.push(verticesRow);
|
|
420
|
-
}
|
|
421
|
-
// indices
|
|
422
|
-
for(let iy = 0;iy < heightSegments;iy++) {
|
|
423
|
-
for(let ix = 0;ix < widthSegments;ix++) {
|
|
424
|
-
const a = grid[iy][ix + 1];
|
|
425
|
-
const b = grid[iy][ix];
|
|
426
|
-
const c = grid[iy + 1][ix];
|
|
427
|
-
const d = grid[iy + 1][ix + 1];
|
|
428
|
-
|
|
429
|
-
if(iy !== 0) indices.push(a, b, d);
|
|
430
|
-
if(iy !== heightSegments - 1) indices.push(b, c, d);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
return {
|
|
435
|
-
vertices: new Float32Array(vertices),
|
|
436
|
-
indices: new Uint16Array(indices),
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// Render bundles function as partial, limited render passes, so we can use the
|
|
441
|
-
// same code both to render the scene normally and to build the render bundle.
|
|
442
|
-
renderScene(passEncoder) {
|
|
443
|
-
if(typeof this.renderables === 'undefined') return;
|
|
444
|
-
passEncoder.setPipeline(this.pipeline);
|
|
445
|
-
passEncoder.setBindGroup(0, this.frameBindGroup);
|
|
446
|
-
// Loop through every renderable object and draw them individually.
|
|
447
|
-
// (Because many of these meshes are repeated, with only the transforms
|
|
448
|
-
// differing, instancing would be highly effective here. This sample
|
|
449
|
-
// intentionally avoids using instancing in order to emulate a more complex
|
|
450
|
-
// scene, which helps demonstrate the potential time savings a render bundle
|
|
451
|
-
// can provide.)
|
|
452
|
-
let count = 0;
|
|
453
|
-
for(const renderable of this.renderables) {
|
|
454
|
-
passEncoder.setBindGroup(1, renderable.bindGroup);
|
|
455
|
-
passEncoder.setVertexBuffer(0, renderable.vertices);
|
|
456
|
-
passEncoder.setIndexBuffer(renderable.indices, 'uint16');
|
|
457
|
-
passEncoder.drawIndexed(renderable.indexCount);
|
|
458
|
-
|
|
459
|
-
if(++count > this.settings.asteroidCount) {
|
|
460
|
-
break;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
draw = () => {
|
|
466
|
-
if(this.moonTexture == null) {
|
|
467
|
-
// console.log('not ready')
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
const transformationMatrix = this.getTransformationMatrix(this.position);
|
|
471
|
-
this.device.queue.writeBuffer(
|
|
472
|
-
this.uniformBuffer,
|
|
473
|
-
0,
|
|
474
|
-
transformationMatrix.buffer,
|
|
475
|
-
transformationMatrix.byteOffset,
|
|
476
|
-
transformationMatrix.byteLength
|
|
477
|
-
);
|
|
478
|
-
this.renderPassDescriptor.colorAttachments[0].view = this.context
|
|
479
|
-
.getCurrentTexture()
|
|
480
|
-
.createView();
|
|
481
|
-
}
|
|
482
|
-
}
|