reze-engine 0.1.5 → 0.1.6

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/src/engine.ts CHANGED
@@ -1,1136 +1,1826 @@
1
- import { Camera } from "./camera"
2
- import { Quat, Vec3 } from "./math"
3
- import { Model } from "./model"
4
- import { PmxLoader } from "./pmx-loader"
5
- import { Physics } from "./physics"
6
-
7
- export interface EngineStats {
8
- fps: number
9
- frameTime: number // ms
10
- gpuMemory: number // MB (estimated total GPU memory)
11
- }
12
-
13
- export class Engine {
14
- private canvas: HTMLCanvasElement
15
- private device!: GPUDevice
16
- private context!: GPUCanvasContext
17
- private presentationFormat!: GPUTextureFormat
18
- public camera!: Camera
19
- private cameraUniformBuffer!: GPUBuffer
20
- private cameraMatrixData = new Float32Array(36)
21
- private lightUniformBuffer!: GPUBuffer
22
- private lightData = new Float32Array(64)
23
- private lightCount = 0
24
- private vertexBuffer!: GPUBuffer
25
- private vertexCount: number = 0
26
- private indexBuffer?: GPUBuffer
27
- private resizeObserver: ResizeObserver | null = null
28
- private depthTexture!: GPUTexture
29
- private pipeline!: GPURenderPipeline
30
- private outlinePipeline!: GPURenderPipeline
31
- private jointsBuffer!: GPUBuffer
32
- private weightsBuffer!: GPUBuffer
33
- private skinMatrixBuffer?: GPUBuffer
34
- private worldMatrixBuffer?: GPUBuffer
35
- private inverseBindMatrixBuffer?: GPUBuffer
36
- private skinMatrixComputePipeline?: GPUComputePipeline
37
- private boneCountBuffer?: GPUBuffer
38
- private multisampleTexture!: GPUTexture
39
- private readonly sampleCount = 4 // MSAA 4x
40
- private renderPassDescriptor!: GPURenderPassDescriptor
41
- private currentModel: Model | null = null
42
- private modelDir: string = ""
43
- private physics: Physics | null = null
44
- private textureSampler!: GPUSampler
45
- private textureCache = new Map<string, GPUTexture>()
46
- private textureSizes = new Map<string, { width: number; height: number }>()
47
-
48
- private lastFpsUpdate = performance.now()
49
- private framesSinceLastUpdate = 0
50
- private frameTimeSamples: number[] = []
51
- private frameTimeSum: number = 0
52
- private drawCallCount: number = 0
53
- private lastFrameTime = performance.now()
54
- private stats: EngineStats = {
55
- fps: 0,
56
- frameTime: 0,
57
- gpuMemory: 0,
58
- }
59
- private animationFrameId: number | null = null
60
- private renderLoopCallback: (() => void) | null = null
61
-
62
- constructor(canvas: HTMLCanvasElement) {
63
- this.canvas = canvas
64
- }
65
-
66
- // Step 1: Get WebGPU device and context
67
- public async init() {
68
- const adapter = await navigator.gpu?.requestAdapter()
69
- const device = await adapter?.requestDevice()
70
- if (!device) {
71
- throw new Error("WebGPU is not supported in this browser.")
72
- }
73
- this.device = device
74
-
75
- const context = this.canvas.getContext("webgpu")
76
- if (!context) {
77
- throw new Error("Failed to get WebGPU context.")
78
- }
79
- this.context = context
80
-
81
- this.presentationFormat = navigator.gpu.getPreferredCanvasFormat()
82
-
83
- this.context.configure({
84
- device: this.device,
85
- format: this.presentationFormat,
86
- alphaMode: "premultiplied",
87
- })
88
-
89
- this.setupCamera()
90
- this.setupLighting()
91
- this.createPipelines()
92
- this.setupResize()
93
- }
94
-
95
- // Step 2: Create shaders and render pipelines
96
- private createPipelines() {
97
- this.textureSampler = this.device.createSampler({
98
- magFilter: "linear",
99
- minFilter: "linear",
100
- addressModeU: "repeat",
101
- addressModeV: "repeat",
102
- })
103
-
104
- const shaderModule = this.device.createShaderModule({
105
- label: "model shaders",
106
- code: /* wgsl */ `
107
- struct CameraUniforms {
108
- view: mat4x4f,
109
- projection: mat4x4f,
110
- viewPos: vec3f,
111
- _padding: f32,
112
- };
113
-
114
- struct Light {
115
- direction: vec3f,
116
- _padding1: f32,
117
- color: vec3f,
118
- intensity: f32,
119
- };
120
-
121
- struct LightUniforms {
122
- ambient: f32,
123
- lightCount: f32,
124
- _padding1: f32,
125
- _padding2: f32,
126
- lights: array<Light, 4>,
127
- };
128
-
129
- struct MaterialUniforms {
130
- alpha: f32,
131
- _padding1: f32,
132
- _padding2: f32,
133
- _padding3: f32,
134
- };
135
-
136
- struct VertexOutput {
137
- @builtin(position) position: vec4f,
138
- @location(0) normal: vec3f,
139
- @location(1) uv: vec2f,
140
- @location(2) worldPos: vec3f,
141
- };
142
-
143
- @group(0) @binding(0) var<uniform> camera: CameraUniforms;
144
- @group(0) @binding(1) var<uniform> light: LightUniforms;
145
- @group(0) @binding(2) var diffuseTexture: texture_2d<f32>;
146
- @group(0) @binding(3) var diffuseSampler: sampler;
147
- @group(0) @binding(4) var<storage, read> skinMats: array<mat4x4f>;
148
- @group(0) @binding(5) var toonTexture: texture_2d<f32>;
149
- @group(0) @binding(6) var toonSampler: sampler;
150
- @group(0) @binding(7) var<uniform> material: MaterialUniforms;
151
-
152
- @vertex fn vs(
153
- @location(0) position: vec3f,
154
- @location(1) normal: vec3f,
155
- @location(2) uv: vec2f,
156
- @location(3) joints0: vec4<u32>,
157
- @location(4) weights0: vec4<f32>
158
- ) -> VertexOutput {
159
- var output: VertexOutput;
160
- let pos4 = vec4f(position, 1.0);
161
-
162
- // Normalize weights to ensure they sum to 1.0 (handles floating-point precision issues)
163
- let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
164
- var normalizedWeights: vec4f;
165
- if (weightSum > 0.0001) {
166
- normalizedWeights = weights0 / weightSum;
167
- } else {
168
- normalizedWeights = vec4f(1.0, 0.0, 0.0, 0.0);
169
- }
170
-
171
- var skinnedPos = vec4f(0.0, 0.0, 0.0, 0.0);
172
- var skinnedNrm = vec3f(0.0, 0.0, 0.0);
173
- for (var i = 0u; i < 4u; i++) {
174
- let j = joints0[i];
175
- let w = normalizedWeights[i];
176
- let m = skinMats[j];
177
- skinnedPos += (m * pos4) * w;
178
- let r3 = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
179
- skinnedNrm += (r3 * normal) * w;
180
- }
181
- let worldPos = skinnedPos.xyz;
182
- output.position = camera.projection * camera.view * vec4f(worldPos, 1.0);
183
- output.normal = normalize(skinnedNrm);
184
- output.uv = uv;
185
- output.worldPos = worldPos;
186
- return output;
187
- }
188
-
189
- @fragment fn fs(input: VertexOutput) -> @location(0) vec4f {
190
- let n = normalize(input.normal);
191
- let albedo = textureSample(diffuseTexture, diffuseSampler, input.uv).rgb;
192
-
193
- var lightAccum = vec3f(light.ambient);
194
- let numLights = u32(light.lightCount);
195
- for (var i = 0u; i < numLights; i++) {
196
- let l = -light.lights[i].direction;
197
- let nDotL = max(dot(n, l), 0.0);
198
- let toonUV = vec2f(nDotL, 0.5);
199
- let toonFactor = textureSample(toonTexture, toonSampler, toonUV).rgb;
200
- let radiance = light.lights[i].color * light.lights[i].intensity;
201
- lightAccum += toonFactor * radiance * nDotL;
202
- }
203
-
204
- let color = albedo * lightAccum;
205
- let finalAlpha = material.alpha;
206
- if (finalAlpha < 0.001) {
207
- discard;
208
- }
209
-
210
- return vec4f(clamp(color, vec3f(0.0), vec3f(1.0)), finalAlpha);
211
- }
212
- `,
213
- })
214
-
215
- // Single pipeline for all materials with alpha blending
216
- this.pipeline = this.device.createRenderPipeline({
217
- label: "model pipeline",
218
- layout: "auto",
219
- vertex: {
220
- module: shaderModule,
221
- buffers: [
222
- {
223
- arrayStride: 8 * 4,
224
- attributes: [
225
- { shaderLocation: 0, offset: 0, format: "float32x3" as GPUVertexFormat },
226
- { shaderLocation: 1, offset: 3 * 4, format: "float32x3" as GPUVertexFormat },
227
- { shaderLocation: 2, offset: 6 * 4, format: "float32x2" as GPUVertexFormat },
228
- ],
229
- },
230
- {
231
- arrayStride: 4 * 2,
232
- attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
233
- },
234
- {
235
- arrayStride: 4,
236
- attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
237
- },
238
- ],
239
- },
240
- fragment: {
241
- module: shaderModule,
242
- targets: [
243
- {
244
- format: this.presentationFormat,
245
- blend: {
246
- color: {
247
- srcFactor: "src-alpha",
248
- dstFactor: "one-minus-src-alpha",
249
- operation: "add",
250
- },
251
- alpha: {
252
- srcFactor: "one",
253
- dstFactor: "one-minus-src-alpha",
254
- operation: "add",
255
- },
256
- },
257
- },
258
- ],
259
- },
260
- primitive: { cullMode: "none" },
261
- depthStencil: {
262
- format: "depth24plus",
263
- depthWriteEnabled: true,
264
- depthCompare: "less",
265
- },
266
- multisample: {
267
- count: this.sampleCount,
268
- },
269
- })
270
-
271
- const outlineShaderModule = this.device.createShaderModule({
272
- label: "outline shaders",
273
- code: /* wgsl */ `
274
- struct CameraUniforms {
275
- view: mat4x4f,
276
- projection: mat4x4f,
277
- viewPos: vec3f,
278
- _padding: f32,
279
- };
280
-
281
- struct MaterialUniforms {
282
- edgeColor: vec4f,
283
- edgeSize: f32,
284
- _padding1: f32,
285
- _padding2: f32,
286
- _padding3: f32,
287
- };
288
-
289
- @group(0) @binding(0) var<uniform> camera: CameraUniforms;
290
- @group(0) @binding(1) var<uniform> material: MaterialUniforms;
291
- @group(0) @binding(2) var<storage, read> skinMats: array<mat4x4f>;
292
-
293
- struct VertexOutput {
294
- @builtin(position) position: vec4f,
295
- };
296
-
297
- @vertex fn vs(
298
- @location(0) position: vec3f,
299
- @location(1) normal: vec3f,
300
- @location(2) uv: vec2f,
301
- @location(3) joints0: vec4<u32>,
302
- @location(4) weights0: vec4<f32>
303
- ) -> VertexOutput {
304
- var output: VertexOutput;
305
- let pos4 = vec4f(position, 1.0);
306
-
307
- // Normalize weights to ensure they sum to 1.0 (handles floating-point precision issues)
308
- let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
309
- var normalizedWeights: vec4f;
310
- if (weightSum > 0.0001) {
311
- normalizedWeights = weights0 / weightSum;
312
- } else {
313
- normalizedWeights = vec4f(1.0, 0.0, 0.0, 0.0);
314
- }
315
-
316
- var skinnedPos = vec4f(0.0, 0.0, 0.0, 0.0);
317
- var skinnedNrm = vec3f(0.0, 0.0, 0.0);
318
- for (var i = 0u; i < 4u; i++) {
319
- let j = joints0[i];
320
- let w = normalizedWeights[i];
321
- let m = skinMats[j];
322
- skinnedPos += (m * pos4) * w;
323
- let r3 = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
324
- skinnedNrm += (r3 * normal) * w;
325
- }
326
- let worldPos = skinnedPos.xyz;
327
- let worldNormal = normalize(skinnedNrm);
328
-
329
- // MMD invert hull: expand vertices outward along normals
330
- let scaleFactor = 0.01;
331
- let expandedPos = worldPos + worldNormal * material.edgeSize * scaleFactor;
332
- output.position = camera.projection * camera.view * vec4f(expandedPos, 1.0);
333
- return output;
334
- }
335
-
336
- @fragment fn fs() -> @location(0) vec4f {
337
- return material.edgeColor;
338
- }
339
- `,
340
- })
341
-
342
- this.outlinePipeline = this.device.createRenderPipeline({
343
- label: "outline pipeline",
344
- layout: "auto",
345
- vertex: {
346
- module: outlineShaderModule,
347
- buffers: [
348
- {
349
- arrayStride: 8 * 4,
350
- attributes: [
351
- {
352
- shaderLocation: 0,
353
- offset: 0,
354
- format: "float32x3" as GPUVertexFormat,
355
- },
356
- {
357
- shaderLocation: 1,
358
- offset: 3 * 4,
359
- format: "float32x3" as GPUVertexFormat,
360
- },
361
- {
362
- shaderLocation: 2,
363
- offset: 6 * 4,
364
- format: "float32x2" as GPUVertexFormat,
365
- },
366
- ],
367
- },
368
- {
369
- arrayStride: 4 * 2,
370
- attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
371
- },
372
- {
373
- arrayStride: 4,
374
- attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
375
- },
376
- ],
377
- },
378
- fragment: {
379
- module: outlineShaderModule,
380
- targets: [
381
- {
382
- format: this.presentationFormat,
383
- blend: {
384
- color: {
385
- srcFactor: "src-alpha",
386
- dstFactor: "one-minus-src-alpha",
387
- operation: "add",
388
- },
389
- alpha: {
390
- srcFactor: "one",
391
- dstFactor: "one-minus-src-alpha",
392
- operation: "add",
393
- },
394
- },
395
- },
396
- ],
397
- },
398
- primitive: {
399
- cullMode: "back",
400
- },
401
- depthStencil: {
402
- format: "depth24plus",
403
- depthWriteEnabled: true,
404
- depthCompare: "less",
405
- },
406
- multisample: {
407
- count: this.sampleCount,
408
- },
409
- })
410
- }
411
-
412
- // Create compute shader for skin matrix computation
413
- private createSkinMatrixComputePipeline() {
414
- const computeShader = this.device.createShaderModule({
415
- label: "skin matrix compute",
416
- code: /* wgsl */ `
417
- struct BoneCountUniform {
418
- count: u32,
419
- _padding1: u32,
420
- _padding2: u32,
421
- _padding3: u32,
422
- _padding4: vec4<u32>,
423
- };
424
-
425
- @group(0) @binding(0) var<uniform> boneCount: BoneCountUniform;
426
- @group(0) @binding(1) var<storage, read> worldMatrices: array<mat4x4f>;
427
- @group(0) @binding(2) var<storage, read> inverseBindMatrices: array<mat4x4f>;
428
- @group(0) @binding(3) var<storage, read_write> skinMatrices: array<mat4x4f>;
429
-
430
- @compute @workgroup_size(64)
431
- fn main(@builtin(global_invocation_id) globalId: vec3<u32>) {
432
- let boneIndex = globalId.x;
433
- // Bounds check: we dispatch workgroups (64 threads each), so some threads may be out of range
434
- if (boneIndex >= boneCount.count) {
435
- return;
436
- }
437
- let worldMat = worldMatrices[boneIndex];
438
- let invBindMat = inverseBindMatrices[boneIndex];
439
- skinMatrices[boneIndex] = worldMat * invBindMat;
440
- }
441
- `,
442
- })
443
-
444
- this.skinMatrixComputePipeline = this.device.createComputePipeline({
445
- label: "skin matrix compute pipeline",
446
- layout: "auto",
447
- compute: {
448
- module: computeShader,
449
- },
450
- })
451
- }
452
-
453
- // Step 3: Setup canvas resize handling
454
- private setupResize() {
455
- this.resizeObserver = new ResizeObserver(() => this.handleResize())
456
- this.resizeObserver.observe(this.canvas)
457
- this.handleResize()
458
- }
459
-
460
- private handleResize() {
461
- const displayWidth = this.canvas.clientWidth
462
- const displayHeight = this.canvas.clientHeight
463
-
464
- const dpr = window.devicePixelRatio || 1
465
- const width = Math.floor(displayWidth * dpr)
466
- const height = Math.floor(displayHeight * dpr)
467
-
468
- if (!this.multisampleTexture || this.canvas.width !== width || this.canvas.height !== height) {
469
- this.canvas.width = width
470
- this.canvas.height = height
471
-
472
- this.multisampleTexture = this.device.createTexture({
473
- label: "multisample render target",
474
- size: [width, height],
475
- sampleCount: this.sampleCount,
476
- format: this.presentationFormat,
477
- usage: GPUTextureUsage.RENDER_ATTACHMENT,
478
- })
479
-
480
- this.depthTexture = this.device.createTexture({
481
- label: "depth texture",
482
- size: [width, height],
483
- sampleCount: this.sampleCount,
484
- format: "depth24plus",
485
- usage: GPUTextureUsage.RENDER_ATTACHMENT,
486
- })
487
-
488
- const depthTextureView = this.depthTexture.createView()
489
-
490
- const colorAttachment: GPURenderPassColorAttachment =
491
- this.sampleCount > 1
492
- ? {
493
- view: this.multisampleTexture.createView(),
494
- resolveTarget: this.context.getCurrentTexture().createView(),
495
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
496
- loadOp: "clear",
497
- storeOp: "store",
498
- }
499
- : {
500
- view: this.context.getCurrentTexture().createView(),
501
- clearValue: { r: 0, g: 0, b: 0, a: 0 },
502
- loadOp: "clear",
503
- storeOp: "store",
504
- }
505
-
506
- this.renderPassDescriptor = {
507
- label: "renderPass",
508
- colorAttachments: [colorAttachment],
509
- depthStencilAttachment: {
510
- view: depthTextureView,
511
- depthClearValue: 1.0,
512
- depthLoadOp: "clear",
513
- depthStoreOp: "store",
514
- },
515
- }
516
-
517
- this.camera.aspect = width / height
518
- }
519
- }
520
-
521
- // Step 4: Create camera and uniform buffer
522
- private setupCamera() {
523
- this.cameraUniformBuffer = this.device.createBuffer({
524
- label: "camera uniforms",
525
- size: 40 * 4,
526
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
527
- })
528
-
529
- this.camera = new Camera(Math.PI, Math.PI / 2.5, 26.6, new Vec3(0, 12.5, 0))
530
-
531
- this.camera.aspect = this.canvas.width / this.canvas.height
532
- this.camera.attachControl(this.canvas)
533
- }
534
-
535
- // Step 5: Create lighting buffers
536
- private setupLighting() {
537
- this.lightUniformBuffer = this.device.createBuffer({
538
- label: "light uniforms",
539
- size: 64 * 4,
540
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
541
- })
542
-
543
- this.lightCount = 0
544
-
545
- this.setAmbient(0.96)
546
- this.addLight(new Vec3(-0.5, -0.8, 0.5).normalize(), new Vec3(1.0, 0.95, 0.9), 0.12)
547
- this.addLight(new Vec3(0.7, -0.5, 0.3).normalize(), new Vec3(0.8, 0.85, 1.0), 0.1)
548
- this.addLight(new Vec3(0.3, -0.5, -1.0).normalize(), new Vec3(0.9, 0.9, 1.0), 0.08)
549
- this.device.queue.writeBuffer(this.lightUniformBuffer, 0, this.lightData)
550
- }
551
-
552
- public addLight(direction: Vec3, color: Vec3, intensity: number = 1.0): boolean {
553
- if (this.lightCount >= 4) return false
554
-
555
- const normalized = direction.normalize()
556
- const baseIndex = 4 + this.lightCount * 8
557
- this.lightData[baseIndex] = normalized.x
558
- this.lightData[baseIndex + 1] = normalized.y
559
- this.lightData[baseIndex + 2] = normalized.z
560
- this.lightData[baseIndex + 3] = 0
561
- this.lightData[baseIndex + 4] = color.x
562
- this.lightData[baseIndex + 5] = color.y
563
- this.lightData[baseIndex + 6] = color.z
564
- this.lightData[baseIndex + 7] = intensity
565
-
566
- this.lightCount++
567
- this.lightData[1] = this.lightCount
568
- return true
569
- }
570
-
571
- public setAmbient(intensity: number) {
572
- this.lightData[0] = intensity
573
- }
574
-
575
- public getStats(): EngineStats {
576
- return { ...this.stats }
577
- }
578
-
579
- public runRenderLoop(callback?: () => void) {
580
- this.renderLoopCallback = callback || null
581
-
582
- const loop = () => {
583
- this.render()
584
-
585
- if (this.renderLoopCallback) {
586
- this.renderLoopCallback()
587
- }
588
-
589
- this.animationFrameId = requestAnimationFrame(loop)
590
- }
591
-
592
- this.animationFrameId = requestAnimationFrame(loop)
593
- }
594
-
595
- public stopRenderLoop() {
596
- if (this.animationFrameId !== null) {
597
- cancelAnimationFrame(this.animationFrameId)
598
- this.animationFrameId = null
599
- }
600
- this.renderLoopCallback = null
601
- }
602
-
603
- public dispose() {
604
- this.stopRenderLoop()
605
- if (this.camera) this.camera.detachControl()
606
- if (this.resizeObserver) {
607
- this.resizeObserver.disconnect()
608
- this.resizeObserver = null
609
- }
610
- }
611
-
612
- // Step 6: Load PMX model file
613
- public async loadModel(path: string) {
614
- const pathParts = path.split("/")
615
- pathParts.pop()
616
- const dir = pathParts.join("/") + "/"
617
- this.modelDir = dir
618
-
619
- const model = await PmxLoader.load(path)
620
- this.physics = new Physics(model.getRigidbodies(), model.getJoints())
621
- await this.setupModelBuffers(model)
622
- }
623
-
624
- public rotateBones(bones: string[], rotations: Quat[], durationMs?: number) {
625
- this.currentModel?.rotateBones(bones, rotations, durationMs)
626
- }
627
-
628
- // Step 7: Create vertex, index, and joint buffers
629
- private async setupModelBuffers(model: Model) {
630
- this.currentModel = model
631
- const vertices = model.getVertices()
632
- const skinning = model.getSkinning()
633
- const skeleton = model.getSkeleton()
634
-
635
- this.vertexBuffer = this.device.createBuffer({
636
- label: "model vertex buffer",
637
- size: vertices.byteLength,
638
- usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
639
- })
640
- this.device.queue.writeBuffer(this.vertexBuffer, 0, vertices)
641
- this.vertexCount = model.getVertexCount()
642
-
643
- this.jointsBuffer = this.device.createBuffer({
644
- label: "joints buffer",
645
- size: skinning.joints.byteLength,
646
- usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
647
- })
648
- this.device.queue.writeBuffer(
649
- this.jointsBuffer,
650
- 0,
651
- skinning.joints.buffer,
652
- skinning.joints.byteOffset,
653
- skinning.joints.byteLength
654
- )
655
-
656
- this.weightsBuffer = this.device.createBuffer({
657
- label: "weights buffer",
658
- size: skinning.weights.byteLength,
659
- usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
660
- })
661
- this.device.queue.writeBuffer(
662
- this.weightsBuffer,
663
- 0,
664
- skinning.weights.buffer,
665
- skinning.weights.byteOffset,
666
- skinning.weights.byteLength
667
- )
668
-
669
- const boneCount = skeleton.bones.length
670
- const matrixSize = boneCount * 16 * 4
671
-
672
- this.skinMatrixBuffer = this.device.createBuffer({
673
- label: "skin matrices",
674
- size: Math.max(256, matrixSize),
675
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX,
676
- })
677
-
678
- this.worldMatrixBuffer = this.device.createBuffer({
679
- label: "world matrices",
680
- size: Math.max(256, matrixSize),
681
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
682
- })
683
-
684
- this.inverseBindMatrixBuffer = this.device.createBuffer({
685
- label: "inverse bind matrices",
686
- size: Math.max(256, matrixSize),
687
- usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
688
- })
689
-
690
- const invBindMatrices = skeleton.inverseBindMatrices
691
- this.device.queue.writeBuffer(
692
- this.inverseBindMatrixBuffer,
693
- 0,
694
- invBindMatrices.buffer,
695
- invBindMatrices.byteOffset,
696
- invBindMatrices.byteLength
697
- )
698
-
699
- this.boneCountBuffer = this.device.createBuffer({
700
- label: "bone count uniform",
701
- size: 32, // Minimum uniform buffer size is 32 bytes
702
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
703
- })
704
- const boneCountData = new Uint32Array(8) // 32 bytes total
705
- boneCountData[0] = boneCount
706
- this.device.queue.writeBuffer(this.boneCountBuffer, 0, boneCountData)
707
-
708
- this.createSkinMatrixComputePipeline()
709
-
710
- const indices = model.getIndices()
711
- if (indices) {
712
- this.indexBuffer = this.device.createBuffer({
713
- label: "model index buffer",
714
- size: indices.byteLength,
715
- usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
716
- })
717
- this.device.queue.writeBuffer(this.indexBuffer, 0, indices)
718
- } else {
719
- throw new Error("Model has no index buffer")
720
- }
721
-
722
- await this.setupMaterials(model)
723
- }
724
-
725
- private materialDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] = []
726
- private outlineDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] = []
727
-
728
- // Step 8: Load textures and create material bind groups
729
- private async setupMaterials(model: Model) {
730
- const materials = model.getMaterials()
731
- if (materials.length === 0) {
732
- throw new Error("Model has no materials")
733
- }
734
-
735
- const textures = model.getTextures()
736
-
737
- const loadTextureByIndex = async (texIndex: number): Promise<GPUTexture | null> => {
738
- if (texIndex < 0 || texIndex >= textures.length) {
739
- return null
740
- }
741
-
742
- const path = this.modelDir + textures[texIndex].path
743
- const texture = await this.createTextureFromPath(path)
744
- return texture
745
- }
746
-
747
- const loadToonTexture = async (toonTextureIndex: number): Promise<GPUTexture> => {
748
- const texture = await loadTextureByIndex(toonTextureIndex)
749
- if (texture) return texture
750
-
751
- // Default toon texture fallback - cache it
752
- const defaultToonPath = "__default_toon__"
753
- const cached = this.textureCache.get(defaultToonPath)
754
- if (cached) return cached
755
-
756
- const defaultToonData = new Uint8Array(256 * 2 * 4)
757
- for (let i = 0; i < 256; i++) {
758
- const factor = i / 255.0
759
- const gray = Math.floor(128 + factor * 127)
760
- defaultToonData[i * 4] = gray
761
- defaultToonData[i * 4 + 1] = gray
762
- defaultToonData[i * 4 + 2] = gray
763
- defaultToonData[i * 4 + 3] = 255
764
- defaultToonData[(256 + i) * 4] = gray
765
- defaultToonData[(256 + i) * 4 + 1] = gray
766
- defaultToonData[(256 + i) * 4 + 2] = gray
767
- defaultToonData[(256 + i) * 4 + 3] = 255
768
- }
769
- const defaultToonTexture = this.device.createTexture({
770
- label: "default toon texture",
771
- size: [256, 2],
772
- format: "rgba8unorm",
773
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
774
- })
775
- this.device.queue.writeTexture(
776
- { texture: defaultToonTexture },
777
- defaultToonData,
778
- { bytesPerRow: 256 * 4 },
779
- [256, 2]
780
- )
781
- this.textureCache.set(defaultToonPath, defaultToonTexture)
782
- this.textureSizes.set(defaultToonPath, { width: 256, height: 2 })
783
- return defaultToonTexture
784
- }
785
-
786
- this.materialDraws = []
787
- this.outlineDraws = []
788
- const outlineBindGroupLayout = this.outlinePipeline.getBindGroupLayout(0)
789
- let runningFirstIndex = 0
790
-
791
- for (const mat of materials) {
792
- const matCount = mat.vertexCount | 0
793
- if (matCount === 0) continue
794
-
795
- const diffuseTexture = await loadTextureByIndex(mat.diffuseTextureIndex)
796
- if (!diffuseTexture) throw new Error(`Material "${mat.name}" has no diffuse texture`)
797
-
798
- const toonTexture = await loadToonTexture(mat.toonTextureIndex)
799
-
800
- const materialAlpha = mat.diffuse[3]
801
- const EPSILON = 0.001
802
- const isTransparent = materialAlpha < 1.0 - EPSILON
803
-
804
- const materialUniformData = new Float32Array(4)
805
- materialUniformData[0] = materialAlpha
806
- materialUniformData[1] = 0.0
807
- materialUniformData[2] = 0.0
808
- materialUniformData[3] = 0.0
809
-
810
- const materialUniformBuffer = this.device.createBuffer({
811
- label: `material uniform: ${mat.name}`,
812
- size: materialUniformData.byteLength,
813
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
814
- })
815
- this.device.queue.writeBuffer(materialUniformBuffer, 0, materialUniformData)
816
-
817
- const bindGroup = this.device.createBindGroup({
818
- label: `material bind group: ${mat.name}`,
819
- layout: this.pipeline.getBindGroupLayout(0),
820
- entries: [
821
- { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
822
- { binding: 1, resource: { buffer: this.lightUniformBuffer } },
823
- { binding: 2, resource: diffuseTexture.createView() },
824
- { binding: 3, resource: this.textureSampler },
825
- { binding: 4, resource: { buffer: this.skinMatrixBuffer! } },
826
- { binding: 5, resource: toonTexture.createView() },
827
- { binding: 6, resource: this.textureSampler },
828
- { binding: 7, resource: { buffer: materialUniformBuffer } },
829
- ],
830
- })
831
-
832
- // All materials use the same pipeline
833
- this.materialDraws.push({
834
- count: matCount,
835
- firstIndex: runningFirstIndex,
836
- bindGroup,
837
- isTransparent,
838
- })
839
-
840
- // Outline for all materials (including transparent)
841
- // Edge flag is at bit 4 (0x10) in PMX format, not bit 0 (0x01)
842
- if ((mat.edgeFlag & 0x10) !== 0 && mat.edgeSize > 0) {
843
- const materialUniformData = new Float32Array(8)
844
- materialUniformData[0] = mat.edgeColor[0]
845
- materialUniformData[1] = mat.edgeColor[1]
846
- materialUniformData[2] = mat.edgeColor[2]
847
- materialUniformData[3] = mat.edgeColor[3]
848
- materialUniformData[4] = mat.edgeSize
849
-
850
- const materialUniformBuffer = this.device.createBuffer({
851
- label: `outline material uniform: ${mat.name}`,
852
- size: materialUniformData.byteLength,
853
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
854
- })
855
- this.device.queue.writeBuffer(materialUniformBuffer, 0, materialUniformData)
856
-
857
- const outlineBindGroup = this.device.createBindGroup({
858
- label: `outline bind group: ${mat.name}`,
859
- layout: outlineBindGroupLayout,
860
- entries: [
861
- { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
862
- { binding: 1, resource: { buffer: materialUniformBuffer } },
863
- { binding: 2, resource: { buffer: this.skinMatrixBuffer! } },
864
- ],
865
- })
866
-
867
- // All outlines use the same pipeline
868
- this.outlineDraws.push({
869
- count: matCount,
870
- firstIndex: runningFirstIndex,
871
- bindGroup: outlineBindGroup,
872
- isTransparent,
873
- })
874
- }
875
-
876
- runningFirstIndex += matCount
877
- }
878
- }
879
-
880
- // Helper: Load texture from file path with optional max size limit
881
- private async createTextureFromPath(path: string, maxSize: number = 2048): Promise<GPUTexture | null> {
882
- const cached = this.textureCache.get(path)
883
- if (cached) {
884
- return cached
885
- }
886
-
887
- try {
888
- const response = await fetch(path)
889
- if (!response.ok) {
890
- throw new Error(`HTTP ${response.status}: ${response.statusText}`)
891
- }
892
- let imageBitmap = await createImageBitmap(await response.blob(), {
893
- premultiplyAlpha: "none",
894
- colorSpaceConversion: "none",
895
- })
896
-
897
- // Downscale if texture is too large
898
- let finalWidth = imageBitmap.width
899
- let finalHeight = imageBitmap.height
900
- if (finalWidth > maxSize || finalHeight > maxSize) {
901
- const scale = Math.min(maxSize / finalWidth, maxSize / finalHeight)
902
- finalWidth = Math.floor(finalWidth * scale)
903
- finalHeight = Math.floor(finalHeight * scale)
904
-
905
- // Create canvas to downscale
906
- const canvas = new OffscreenCanvas(finalWidth, finalHeight)
907
- const ctx = canvas.getContext("2d")
908
- if (ctx) {
909
- ctx.drawImage(imageBitmap, 0, 0, finalWidth, finalHeight)
910
- imageBitmap = await createImageBitmap(canvas)
911
- }
912
- }
913
-
914
- const texture = this.device.createTexture({
915
- label: `texture: ${path}`,
916
- size: [finalWidth, finalHeight],
917
- format: "rgba8unorm",
918
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
919
- })
920
- this.device.queue.copyExternalImageToTexture({ source: imageBitmap }, { texture }, [finalWidth, finalHeight])
921
-
922
- this.textureCache.set(path, texture)
923
- this.textureSizes.set(path, { width: finalWidth, height: finalHeight })
924
- return texture
925
- } catch {
926
- return null
927
- }
928
- }
929
-
930
- // Step 9: Render one frame
931
- public render() {
932
- if (this.multisampleTexture && this.camera && this.device && this.currentModel) {
933
- const currentTime = performance.now()
934
- const deltaTime = this.lastFrameTime > 0 ? (currentTime - this.lastFrameTime) / 1000 : 0.016
935
- this.lastFrameTime = currentTime
936
-
937
- this.updateCameraUniforms()
938
- this.updateRenderTarget()
939
-
940
- this.updateModelPose(deltaTime)
941
-
942
- const encoder = this.device.createCommandEncoder()
943
- const pass = encoder.beginRenderPass(this.renderPassDescriptor)
944
-
945
- pass.setVertexBuffer(0, this.vertexBuffer)
946
- pass.setVertexBuffer(1, this.jointsBuffer)
947
- pass.setVertexBuffer(2, this.weightsBuffer)
948
- pass.setIndexBuffer(this.indexBuffer!, "uint32")
949
-
950
- this.drawCallCount = 0
951
- this.drawOutlines(pass, false)
952
- this.drawModel(pass, false)
953
- this.drawModel(pass, true)
954
- this.drawOutlines(pass, true)
955
-
956
- pass.end()
957
- this.device.queue.submit([encoder.finish()])
958
- this.updateStats(performance.now() - currentTime)
959
- }
960
- }
961
-
962
- // Update camera uniform buffer each frame
963
- private updateCameraUniforms() {
964
- const viewMatrix = this.camera.getViewMatrix()
965
- const projectionMatrix = this.camera.getProjectionMatrix()
966
- const cameraPos = this.camera.getPosition()
967
- this.cameraMatrixData.set(viewMatrix.values, 0)
968
- this.cameraMatrixData.set(projectionMatrix.values, 16)
969
- this.cameraMatrixData[32] = cameraPos.x
970
- this.cameraMatrixData[33] = cameraPos.y
971
- this.cameraMatrixData[34] = cameraPos.z
972
- this.device.queue.writeBuffer(this.cameraUniformBuffer, 0, this.cameraMatrixData)
973
- }
974
-
975
- // Update render target texture view
976
- private updateRenderTarget() {
977
- const colorAttachment = (this.renderPassDescriptor.colorAttachments as GPURenderPassColorAttachment[])[0]
978
- if (this.sampleCount > 1) {
979
- colorAttachment.resolveTarget = this.context.getCurrentTexture().createView()
980
- } else {
981
- colorAttachment.view = this.context.getCurrentTexture().createView()
982
- }
983
- }
984
-
985
- // Update model pose and physics
986
- private updateModelPose(deltaTime: number) {
987
- this.currentModel!.evaluatePose()
988
-
989
- // Upload world matrices to GPU
990
- const worldMats = this.currentModel!.getBoneWorldMatrices()
991
- this.device.queue.writeBuffer(
992
- this.worldMatrixBuffer!,
993
- 0,
994
- worldMats.buffer,
995
- worldMats.byteOffset,
996
- worldMats.byteLength
997
- )
998
-
999
- if (this.physics) {
1000
- this.physics.step(deltaTime, worldMats, this.currentModel!.getBoneInverseBindMatrices())
1001
- // Re-upload world matrices after physics (physics may have updated bones)
1002
- this.device.queue.writeBuffer(
1003
- this.worldMatrixBuffer!,
1004
- 0,
1005
- worldMats.buffer,
1006
- worldMats.byteOffset,
1007
- worldMats.byteLength
1008
- )
1009
- }
1010
-
1011
- // Compute skin matrices on GPU
1012
- this.computeSkinMatrices()
1013
- }
1014
-
1015
- // Compute skin matrices on GPU
1016
- private computeSkinMatrices() {
1017
- const boneCount = this.currentModel!.getSkeleton().bones.length
1018
- const workgroupSize = 64
1019
- // Dispatch exactly enough threads for all bones (no bounds check needed)
1020
- const workgroupCount = Math.ceil(boneCount / workgroupSize)
1021
-
1022
- // Update bone count uniform
1023
- const boneCountData = new Uint32Array(8) // 32 bytes total
1024
- boneCountData[0] = boneCount
1025
- this.device.queue.writeBuffer(this.boneCountBuffer!, 0, boneCountData)
1026
-
1027
- const bindGroup = this.device.createBindGroup({
1028
- label: "skin matrix compute bind group",
1029
- layout: this.skinMatrixComputePipeline!.getBindGroupLayout(0),
1030
- entries: [
1031
- { binding: 0, resource: { buffer: this.boneCountBuffer! } },
1032
- { binding: 1, resource: { buffer: this.worldMatrixBuffer! } },
1033
- { binding: 2, resource: { buffer: this.inverseBindMatrixBuffer! } },
1034
- { binding: 3, resource: { buffer: this.skinMatrixBuffer! } },
1035
- ],
1036
- })
1037
-
1038
- const encoder = this.device.createCommandEncoder()
1039
- const pass = encoder.beginComputePass()
1040
- pass.setPipeline(this.skinMatrixComputePipeline!)
1041
- pass.setBindGroup(0, bindGroup)
1042
- pass.dispatchWorkgroups(workgroupCount)
1043
- pass.end()
1044
- this.device.queue.submit([encoder.finish()])
1045
- }
1046
-
1047
- // Draw outlines (opaque or transparent)
1048
- private drawOutlines(pass: GPURenderPassEncoder, transparent: boolean) {
1049
- if (this.outlineDraws.length === 0) return
1050
- pass.setPipeline(this.outlinePipeline)
1051
- for (const draw of this.outlineDraws) {
1052
- if (draw.count > 0 && draw.isTransparent === transparent) {
1053
- pass.setBindGroup(0, draw.bindGroup)
1054
- pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1055
- }
1056
- }
1057
- }
1058
-
1059
- // Draw model materials (opaque or transparent)
1060
- private drawModel(pass: GPURenderPassEncoder, transparent: boolean) {
1061
- pass.setPipeline(this.pipeline)
1062
- for (const draw of this.materialDraws) {
1063
- if (draw.count > 0 && draw.isTransparent === transparent) {
1064
- pass.setBindGroup(0, draw.bindGroup)
1065
- pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1066
- this.drawCallCount++
1067
- }
1068
- }
1069
- }
1070
-
1071
- private updateStats(frameTime: number) {
1072
- const maxSamples = 60
1073
- this.frameTimeSamples.push(frameTime)
1074
- this.frameTimeSum += frameTime
1075
- if (this.frameTimeSamples.length > maxSamples) {
1076
- const removed = this.frameTimeSamples.shift()!
1077
- this.frameTimeSum -= removed
1078
- }
1079
- const avgFrameTime = this.frameTimeSum / this.frameTimeSamples.length
1080
- this.stats.frameTime = Math.round(avgFrameTime * 100) / 100
1081
-
1082
- const now = performance.now()
1083
- this.framesSinceLastUpdate++
1084
- const elapsed = now - this.lastFpsUpdate
1085
-
1086
- if (elapsed >= 1000) {
1087
- this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) * 1000)
1088
- this.framesSinceLastUpdate = 0
1089
- this.lastFpsUpdate = now
1090
- }
1091
-
1092
- // Calculate GPU memory: textures + buffers + render targets
1093
- let textureMemoryBytes = 0
1094
- for (const [path, size] of this.textureSizes.entries()) {
1095
- if (this.textureCache.has(path)) {
1096
- textureMemoryBytes += size.width * size.height * 4 // RGBA8 = 4 bytes per pixel
1097
- }
1098
- }
1099
-
1100
- let bufferMemoryBytes = 0
1101
- if (this.vertexBuffer) {
1102
- const vertices = this.currentModel?.getVertices()
1103
- if (vertices) bufferMemoryBytes += vertices.byteLength
1104
- }
1105
- if (this.indexBuffer) {
1106
- const indices = this.currentModel?.getIndices()
1107
- if (indices) bufferMemoryBytes += indices.byteLength
1108
- }
1109
- if (this.jointsBuffer) {
1110
- const skinning = this.currentModel?.getSkinning()
1111
- if (skinning) bufferMemoryBytes += skinning.joints.byteLength
1112
- }
1113
- if (this.weightsBuffer) {
1114
- const skinning = this.currentModel?.getSkinning()
1115
- if (skinning) bufferMemoryBytes += skinning.weights.byteLength
1116
- }
1117
- if (this.skinMatrixBuffer) {
1118
- const skeleton = this.currentModel?.getSkeleton()
1119
- if (skeleton) bufferMemoryBytes += Math.max(256, skeleton.bones.length * 16 * 4)
1120
- }
1121
- bufferMemoryBytes += 40 * 4 // cameraUniformBuffer
1122
- bufferMemoryBytes += 64 * 4 // lightUniformBuffer
1123
- bufferMemoryBytes += this.materialDraws.length * 4 // Material uniform buffers
1124
-
1125
- let renderTargetMemoryBytes = 0
1126
- if (this.multisampleTexture) {
1127
- const width = this.canvas.width
1128
- const height = this.canvas.height
1129
- renderTargetMemoryBytes += width * height * 4 * this.sampleCount // multisample color
1130
- renderTargetMemoryBytes += width * height * 4 // depth
1131
- }
1132
-
1133
- const totalGPUMemoryBytes = textureMemoryBytes + bufferMemoryBytes + renderTargetMemoryBytes
1134
- this.stats.gpuMemory = Math.round((totalGPUMemoryBytes / 1024 / 1024) * 100) / 100
1135
- }
1136
- }
1
+ import { Camera } from "./camera"
2
+ import { Quat, Vec3 } from "./math"
3
+ import { Model } from "./model"
4
+ import { PmxLoader } from "./pmx-loader"
5
+ import { Physics } from "./physics"
6
+
7
+ export interface EngineStats {
8
+ fps: number
9
+ frameTime: number // ms
10
+ gpuMemory: number // MB (estimated total GPU memory)
11
+ }
12
+
13
+ export class Engine {
14
+ private canvas: HTMLCanvasElement
15
+ private device!: GPUDevice
16
+ private context!: GPUCanvasContext
17
+ private presentationFormat!: GPUTextureFormat
18
+ public camera!: Camera
19
+ private cameraUniformBuffer!: GPUBuffer
20
+ private cameraMatrixData = new Float32Array(36)
21
+ private lightUniformBuffer!: GPUBuffer
22
+ private lightData = new Float32Array(64)
23
+ private lightCount = 0
24
+ private vertexBuffer!: GPUBuffer
25
+ private vertexCount: number = 0
26
+ private indexBuffer?: GPUBuffer
27
+ private resizeObserver: ResizeObserver | null = null
28
+ private depthTexture!: GPUTexture
29
+ private pipeline!: GPURenderPipeline
30
+ private outlinePipeline!: GPURenderPipeline
31
+ private hairOutlinePipeline!: GPURenderPipeline
32
+ private hairOutlineOverEyesPipeline!: GPURenderPipeline
33
+ private hairMultiplyPipeline!: GPURenderPipeline
34
+ private hairOpaquePipeline!: GPURenderPipeline
35
+ private eyePipeline!: GPURenderPipeline
36
+ private hairBindGroupLayout!: GPUBindGroupLayout
37
+ private outlineBindGroupLayout!: GPUBindGroupLayout
38
+ private jointsBuffer!: GPUBuffer
39
+ private weightsBuffer!: GPUBuffer
40
+ private skinMatrixBuffer?: GPUBuffer
41
+ private worldMatrixBuffer?: GPUBuffer
42
+ private inverseBindMatrixBuffer?: GPUBuffer
43
+ private skinMatrixComputePipeline?: GPUComputePipeline
44
+ private boneCountBuffer?: GPUBuffer
45
+ private multisampleTexture!: GPUTexture
46
+ private readonly sampleCount = 4 // MSAA 4x
47
+ private renderPassDescriptor!: GPURenderPassDescriptor
48
+ private currentModel: Model | null = null
49
+ private modelDir: string = ""
50
+ private physics: Physics | null = null
51
+ private textureSampler!: GPUSampler
52
+ private textureCache = new Map<string, GPUTexture>()
53
+ private textureSizes = new Map<string, { width: number; height: number }>()
54
+
55
+ private lastFpsUpdate = performance.now()
56
+ private framesSinceLastUpdate = 0
57
+ private frameTimeSamples: number[] = []
58
+ private frameTimeSum: number = 0
59
+ private drawCallCount: number = 0
60
+ private lastFrameTime = performance.now()
61
+ private stats: EngineStats = {
62
+ fps: 0,
63
+ frameTime: 0,
64
+ gpuMemory: 0,
65
+ }
66
+ private animationFrameId: number | null = null
67
+ private renderLoopCallback: (() => void) | null = null
68
+
69
+ constructor(canvas: HTMLCanvasElement) {
70
+ this.canvas = canvas
71
+ }
72
+
73
+ // Step 1: Get WebGPU device and context
74
+ public async init() {
75
+ const adapter = await navigator.gpu?.requestAdapter()
76
+ const device = await adapter?.requestDevice()
77
+ if (!device) {
78
+ throw new Error("WebGPU is not supported in this browser.")
79
+ }
80
+ this.device = device
81
+
82
+ const context = this.canvas.getContext("webgpu")
83
+ if (!context) {
84
+ throw new Error("Failed to get WebGPU context.")
85
+ }
86
+ this.context = context
87
+
88
+ this.presentationFormat = navigator.gpu.getPreferredCanvasFormat()
89
+
90
+ this.context.configure({
91
+ device: this.device,
92
+ format: this.presentationFormat,
93
+ alphaMode: "premultiplied",
94
+ })
95
+
96
+ this.setupCamera()
97
+ this.setupLighting()
98
+ this.createPipelines()
99
+ this.setupResize()
100
+ }
101
+
102
+ // Step 2: Create shaders and render pipelines
103
+ private createPipelines() {
104
+ this.textureSampler = this.device.createSampler({
105
+ magFilter: "linear",
106
+ minFilter: "linear",
107
+ addressModeU: "repeat",
108
+ addressModeV: "repeat",
109
+ })
110
+
111
+ const shaderModule = this.device.createShaderModule({
112
+ label: "model shaders",
113
+ code: /* wgsl */ `
114
+ struct CameraUniforms {
115
+ view: mat4x4f,
116
+ projection: mat4x4f,
117
+ viewPos: vec3f,
118
+ _padding: f32,
119
+ };
120
+
121
+ struct Light {
122
+ direction: vec3f,
123
+ _padding1: f32,
124
+ color: vec3f,
125
+ intensity: f32,
126
+ };
127
+
128
+ struct LightUniforms {
129
+ ambient: f32,
130
+ lightCount: f32,
131
+ _padding1: f32,
132
+ _padding2: f32,
133
+ lights: array<Light, 4>,
134
+ };
135
+
136
+ struct MaterialUniforms {
137
+ alpha: f32,
138
+ _padding1: f32,
139
+ _padding2: f32,
140
+ _padding3: f32,
141
+ };
142
+
143
+ struct VertexOutput {
144
+ @builtin(position) position: vec4f,
145
+ @location(0) normal: vec3f,
146
+ @location(1) uv: vec2f,
147
+ @location(2) worldPos: vec3f,
148
+ };
149
+
150
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
151
+ @group(0) @binding(1) var<uniform> light: LightUniforms;
152
+ @group(0) @binding(2) var diffuseTexture: texture_2d<f32>;
153
+ @group(0) @binding(3) var diffuseSampler: sampler;
154
+ @group(0) @binding(4) var<storage, read> skinMats: array<mat4x4f>;
155
+ @group(0) @binding(5) var toonTexture: texture_2d<f32>;
156
+ @group(0) @binding(6) var toonSampler: sampler;
157
+ @group(0) @binding(7) var<uniform> material: MaterialUniforms;
158
+
159
+ @vertex fn vs(
160
+ @location(0) position: vec3f,
161
+ @location(1) normal: vec3f,
162
+ @location(2) uv: vec2f,
163
+ @location(3) joints0: vec4<u32>,
164
+ @location(4) weights0: vec4<f32>
165
+ ) -> VertexOutput {
166
+ var output: VertexOutput;
167
+ let pos4 = vec4f(position, 1.0);
168
+
169
+ // Normalize weights to ensure they sum to 1.0 (handles floating-point precision issues)
170
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
171
+ var normalizedWeights: vec4f;
172
+ if (weightSum > 0.0001) {
173
+ normalizedWeights = weights0 / weightSum;
174
+ } else {
175
+ normalizedWeights = vec4f(1.0, 0.0, 0.0, 0.0);
176
+ }
177
+
178
+ var skinnedPos = vec4f(0.0, 0.0, 0.0, 0.0);
179
+ var skinnedNrm = vec3f(0.0, 0.0, 0.0);
180
+ for (var i = 0u; i < 4u; i++) {
181
+ let j = joints0[i];
182
+ let w = normalizedWeights[i];
183
+ let m = skinMats[j];
184
+ skinnedPos += (m * pos4) * w;
185
+ let r3 = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
186
+ skinnedNrm += (r3 * normal) * w;
187
+ }
188
+ let worldPos = skinnedPos.xyz;
189
+ output.position = camera.projection * camera.view * vec4f(worldPos, 1.0);
190
+ output.normal = normalize(skinnedNrm);
191
+ output.uv = uv;
192
+ output.worldPos = worldPos;
193
+ return output;
194
+ }
195
+
196
+ @fragment fn fs(input: VertexOutput) -> @location(0) vec4f {
197
+ let n = normalize(input.normal);
198
+ let albedo = textureSample(diffuseTexture, diffuseSampler, input.uv).rgb;
199
+
200
+ var lightAccum = vec3f(light.ambient);
201
+ let numLights = u32(light.lightCount);
202
+ for (var i = 0u; i < numLights; i++) {
203
+ let l = -light.lights[i].direction;
204
+ let nDotL = max(dot(n, l), 0.0);
205
+ let toonUV = vec2f(nDotL, 0.5);
206
+ let toonFactor = textureSample(toonTexture, toonSampler, toonUV).rgb;
207
+ let radiance = light.lights[i].color * light.lights[i].intensity;
208
+ lightAccum += toonFactor * radiance * nDotL;
209
+ }
210
+
211
+ let color = albedo * lightAccum;
212
+ let finalAlpha = material.alpha;
213
+ if (finalAlpha < 0.001) {
214
+ discard;
215
+ }
216
+
217
+ return vec4f(clamp(color, vec3f(0.0), vec3f(1.0)), finalAlpha);
218
+ }
219
+ `,
220
+ })
221
+
222
+ // Create a separate shader for hair-over-eyes that outputs pre-multiplied color for darkening effect
223
+ const hairMultiplyShaderModule = this.device.createShaderModule({
224
+ label: "hair multiply shaders",
225
+ code: /* wgsl */ `
226
+ struct CameraUniforms {
227
+ view: mat4x4f,
228
+ projection: mat4x4f,
229
+ viewPos: vec3f,
230
+ _padding: f32,
231
+ };
232
+
233
+ struct Light {
234
+ direction: vec3f,
235
+ _padding1: f32,
236
+ color: vec3f,
237
+ intensity: f32,
238
+ };
239
+
240
+ struct LightUniforms {
241
+ ambient: f32,
242
+ lightCount: f32,
243
+ _padding1: f32,
244
+ _padding2: f32,
245
+ lights: array<Light, 4>,
246
+ };
247
+
248
+ struct MaterialUniforms {
249
+ alpha: f32,
250
+ _padding1: f32,
251
+ _padding2: f32,
252
+ _padding3: f32,
253
+ };
254
+
255
+ struct VertexOutput {
256
+ @builtin(position) position: vec4f,
257
+ @location(0) normal: vec3f,
258
+ @location(1) uv: vec2f,
259
+ @location(2) worldPos: vec3f,
260
+ };
261
+
262
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
263
+ @group(0) @binding(1) var<uniform> light: LightUniforms;
264
+ @group(0) @binding(2) var diffuseTexture: texture_2d<f32>;
265
+ @group(0) @binding(3) var diffuseSampler: sampler;
266
+ @group(0) @binding(4) var<storage, read> skinMats: array<mat4x4f>;
267
+ @group(0) @binding(5) var toonTexture: texture_2d<f32>;
268
+ @group(0) @binding(6) var toonSampler: sampler;
269
+ @group(0) @binding(7) var<uniform> material: MaterialUniforms;
270
+
271
+ @vertex fn vs(
272
+ @location(0) position: vec3f,
273
+ @location(1) normal: vec3f,
274
+ @location(2) uv: vec2f,
275
+ @location(3) joints0: vec4<u32>,
276
+ @location(4) weights0: vec4<f32>
277
+ ) -> VertexOutput {
278
+ var output: VertexOutput;
279
+ let pos4 = vec4f(position, 1.0);
280
+
281
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
282
+ var normalizedWeights: vec4f;
283
+ if (weightSum > 0.0001) {
284
+ normalizedWeights = weights0 / weightSum;
285
+ } else {
286
+ normalizedWeights = vec4f(1.0, 0.0, 0.0, 0.0);
287
+ }
288
+
289
+ var skinnedPos = vec4f(0.0, 0.0, 0.0, 0.0);
290
+ var skinnedNrm = vec3f(0.0, 0.0, 0.0);
291
+ for (var i = 0u; i < 4u; i++) {
292
+ let j = joints0[i];
293
+ let w = normalizedWeights[i];
294
+ let m = skinMats[j];
295
+ skinnedPos += (m * pos4) * w;
296
+ let r3 = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
297
+ skinnedNrm += (r3 * normal) * w;
298
+ }
299
+ let worldPos = skinnedPos.xyz;
300
+ output.position = camera.projection * camera.view * vec4f(worldPos, 1.0);
301
+ output.normal = normalize(skinnedNrm);
302
+ output.uv = uv;
303
+ output.worldPos = worldPos;
304
+ return output;
305
+ }
306
+
307
+ @fragment fn fs(input: VertexOutput) -> @location(0) vec4f {
308
+ let n = normalize(input.normal);
309
+ let albedo = textureSample(diffuseTexture, diffuseSampler, input.uv).rgb;
310
+
311
+ var lightAccum = vec3f(light.ambient);
312
+ let numLights = u32(light.lightCount);
313
+ for (var i = 0u; i < numLights; i++) {
314
+ let l = -light.lights[i].direction;
315
+ let nDotL = max(dot(n, l), 0.0);
316
+ let toonUV = vec2f(nDotL, 0.5);
317
+ let toonFactor = textureSample(toonTexture, toonSampler, toonUV).rgb;
318
+ let radiance = light.lights[i].color * light.lights[i].intensity;
319
+ lightAccum += toonFactor * radiance * nDotL;
320
+ }
321
+
322
+ let color = albedo * lightAccum;
323
+ let finalAlpha = material.alpha;
324
+ if (finalAlpha < 0.001) {
325
+ discard;
326
+ }
327
+
328
+ // For hair-over-eyes effect: simple half-transparent overlay
329
+ // Use 60% opacity to create a semi-transparent hair color overlay
330
+ let overlayAlpha = finalAlpha * 0.6;
331
+
332
+ return vec4f(clamp(color, vec3f(0.0), vec3f(1.0)), overlayAlpha);
333
+ }
334
+ `,
335
+ })
336
+
337
+ // Create explicit bind group layout for all pipelines using the main shader
338
+ // This ensures compatibility across all pipelines (main, eye, hair multiply, hair opaque)
339
+ this.hairBindGroupLayout = this.device.createBindGroupLayout({
340
+ label: "shared material bind group layout",
341
+ entries: [
342
+ { binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } }, // camera
343
+ { binding: 1, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } }, // light
344
+ { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: {} }, // diffuseTexture
345
+ { binding: 3, visibility: GPUShaderStage.FRAGMENT, sampler: {} }, // diffuseSampler
346
+ { binding: 4, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } }, // skinMats
347
+ { binding: 5, visibility: GPUShaderStage.FRAGMENT, texture: {} }, // toonTexture
348
+ { binding: 6, visibility: GPUShaderStage.FRAGMENT, sampler: {} }, // toonSampler
349
+ { binding: 7, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } }, // material
350
+ ],
351
+ })
352
+
353
+ const sharedPipelineLayout = this.device.createPipelineLayout({
354
+ label: "shared pipeline layout",
355
+ bindGroupLayouts: [this.hairBindGroupLayout],
356
+ })
357
+
358
+ // Single pipeline for all materials with alpha blending
359
+ this.pipeline = this.device.createRenderPipeline({
360
+ label: "model pipeline",
361
+ layout: sharedPipelineLayout,
362
+ vertex: {
363
+ module: shaderModule,
364
+ buffers: [
365
+ {
366
+ arrayStride: 8 * 4,
367
+ attributes: [
368
+ { shaderLocation: 0, offset: 0, format: "float32x3" as GPUVertexFormat },
369
+ { shaderLocation: 1, offset: 3 * 4, format: "float32x3" as GPUVertexFormat },
370
+ { shaderLocation: 2, offset: 6 * 4, format: "float32x2" as GPUVertexFormat },
371
+ ],
372
+ },
373
+ {
374
+ arrayStride: 4 * 2,
375
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
376
+ },
377
+ {
378
+ arrayStride: 4,
379
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
380
+ },
381
+ ],
382
+ },
383
+ fragment: {
384
+ module: shaderModule,
385
+ targets: [
386
+ {
387
+ format: this.presentationFormat,
388
+ blend: {
389
+ color: {
390
+ srcFactor: "src-alpha",
391
+ dstFactor: "one-minus-src-alpha",
392
+ operation: "add",
393
+ },
394
+ alpha: {
395
+ srcFactor: "one",
396
+ dstFactor: "one-minus-src-alpha",
397
+ operation: "add",
398
+ },
399
+ },
400
+ },
401
+ ],
402
+ },
403
+ primitive: { cullMode: "none" },
404
+ depthStencil: {
405
+ format: "depth24plus-stencil8",
406
+ depthWriteEnabled: true,
407
+ depthCompare: "less",
408
+ },
409
+ multisample: {
410
+ count: this.sampleCount,
411
+ },
412
+ })
413
+
414
+ // Create bind group layout for outline pipelines
415
+ this.outlineBindGroupLayout = this.device.createBindGroupLayout({
416
+ label: "outline bind group layout",
417
+ entries: [
418
+ { binding: 0, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } }, // camera
419
+ { binding: 1, visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } }, // material
420
+ { binding: 2, visibility: GPUShaderStage.VERTEX, buffer: { type: "read-only-storage" } }, // skinMats
421
+ ],
422
+ })
423
+
424
+ const outlinePipelineLayout = this.device.createPipelineLayout({
425
+ label: "outline pipeline layout",
426
+ bindGroupLayouts: [this.outlineBindGroupLayout],
427
+ })
428
+
429
+ const outlineShaderModule = this.device.createShaderModule({
430
+ label: "outline shaders",
431
+ code: /* wgsl */ `
432
+ struct CameraUniforms {
433
+ view: mat4x4f,
434
+ projection: mat4x4f,
435
+ viewPos: vec3f,
436
+ _padding: f32,
437
+ };
438
+
439
+ struct MaterialUniforms {
440
+ edgeColor: vec4f,
441
+ edgeSize: f32,
442
+ _padding1: f32,
443
+ _padding2: f32,
444
+ _padding3: f32,
445
+ };
446
+
447
+ @group(0) @binding(0) var<uniform> camera: CameraUniforms;
448
+ @group(0) @binding(1) var<uniform> material: MaterialUniforms;
449
+ @group(0) @binding(2) var<storage, read> skinMats: array<mat4x4f>;
450
+
451
+ struct VertexOutput {
452
+ @builtin(position) position: vec4f,
453
+ };
454
+
455
+ @vertex fn vs(
456
+ @location(0) position: vec3f,
457
+ @location(1) normal: vec3f,
458
+ @location(2) uv: vec2f,
459
+ @location(3) joints0: vec4<u32>,
460
+ @location(4) weights0: vec4<f32>
461
+ ) -> VertexOutput {
462
+ var output: VertexOutput;
463
+ let pos4 = vec4f(position, 1.0);
464
+
465
+ // Normalize weights to ensure they sum to 1.0 (handles floating-point precision issues)
466
+ let weightSum = weights0.x + weights0.y + weights0.z + weights0.w;
467
+ var normalizedWeights: vec4f;
468
+ if (weightSum > 0.0001) {
469
+ normalizedWeights = weights0 / weightSum;
470
+ } else {
471
+ normalizedWeights = vec4f(1.0, 0.0, 0.0, 0.0);
472
+ }
473
+
474
+ var skinnedPos = vec4f(0.0, 0.0, 0.0, 0.0);
475
+ var skinnedNrm = vec3f(0.0, 0.0, 0.0);
476
+ for (var i = 0u; i < 4u; i++) {
477
+ let j = joints0[i];
478
+ let w = normalizedWeights[i];
479
+ let m = skinMats[j];
480
+ skinnedPos += (m * pos4) * w;
481
+ let r3 = mat3x3f(m[0].xyz, m[1].xyz, m[2].xyz);
482
+ skinnedNrm += (r3 * normal) * w;
483
+ }
484
+ let worldPos = skinnedPos.xyz;
485
+ let worldNormal = normalize(skinnedNrm);
486
+
487
+ // MMD invert hull: expand vertices outward along normals
488
+ let scaleFactor = 0.01;
489
+ let expandedPos = worldPos + worldNormal * material.edgeSize * scaleFactor;
490
+ output.position = camera.projection * camera.view * vec4f(expandedPos, 1.0);
491
+ return output;
492
+ }
493
+
494
+ @fragment fn fs() -> @location(0) vec4f {
495
+ return material.edgeColor;
496
+ }
497
+ `,
498
+ })
499
+
500
+ this.outlinePipeline = this.device.createRenderPipeline({
501
+ label: "outline pipeline",
502
+ layout: outlinePipelineLayout,
503
+ vertex: {
504
+ module: outlineShaderModule,
505
+ buffers: [
506
+ {
507
+ arrayStride: 8 * 4,
508
+ attributes: [
509
+ {
510
+ shaderLocation: 0,
511
+ offset: 0,
512
+ format: "float32x3" as GPUVertexFormat,
513
+ },
514
+ {
515
+ shaderLocation: 1,
516
+ offset: 3 * 4,
517
+ format: "float32x3" as GPUVertexFormat,
518
+ },
519
+ {
520
+ shaderLocation: 2,
521
+ offset: 6 * 4,
522
+ format: "float32x2" as GPUVertexFormat,
523
+ },
524
+ ],
525
+ },
526
+ {
527
+ arrayStride: 4 * 2,
528
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
529
+ },
530
+ {
531
+ arrayStride: 4,
532
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
533
+ },
534
+ ],
535
+ },
536
+ fragment: {
537
+ module: outlineShaderModule,
538
+ targets: [
539
+ {
540
+ format: this.presentationFormat,
541
+ blend: {
542
+ color: {
543
+ srcFactor: "src-alpha",
544
+ dstFactor: "one-minus-src-alpha",
545
+ operation: "add",
546
+ },
547
+ alpha: {
548
+ srcFactor: "one",
549
+ dstFactor: "one-minus-src-alpha",
550
+ operation: "add",
551
+ },
552
+ },
553
+ },
554
+ ],
555
+ },
556
+ primitive: {
557
+ cullMode: "back",
558
+ },
559
+ depthStencil: {
560
+ format: "depth24plus-stencil8",
561
+ depthWriteEnabled: true,
562
+ depthCompare: "less",
563
+ },
564
+ multisample: {
565
+ count: this.sampleCount,
566
+ },
567
+ })
568
+
569
+ // Hair outline pipeline: draws hair outlines over non-eyes (stencil != 1)
570
+ // Drawn after hair geometry, so depth testing ensures outlines only appear where hair exists
571
+ this.hairOutlinePipeline = this.device.createRenderPipeline({
572
+ label: "hair outline pipeline",
573
+ layout: outlinePipelineLayout,
574
+ vertex: {
575
+ module: outlineShaderModule,
576
+ buffers: [
577
+ {
578
+ arrayStride: 8 * 4,
579
+ attributes: [
580
+ {
581
+ shaderLocation: 0,
582
+ offset: 0,
583
+ format: "float32x3" as GPUVertexFormat,
584
+ },
585
+ {
586
+ shaderLocation: 1,
587
+ offset: 3 * 4,
588
+ format: "float32x3" as GPUVertexFormat,
589
+ },
590
+ {
591
+ shaderLocation: 2,
592
+ offset: 6 * 4,
593
+ format: "float32x2" as GPUVertexFormat,
594
+ },
595
+ ],
596
+ },
597
+ {
598
+ arrayStride: 4 * 2,
599
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
600
+ },
601
+ {
602
+ arrayStride: 4,
603
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
604
+ },
605
+ ],
606
+ },
607
+ fragment: {
608
+ module: outlineShaderModule,
609
+ targets: [
610
+ {
611
+ format: this.presentationFormat,
612
+ blend: {
613
+ color: {
614
+ srcFactor: "src-alpha",
615
+ dstFactor: "one-minus-src-alpha",
616
+ operation: "add",
617
+ },
618
+ alpha: {
619
+ srcFactor: "one",
620
+ dstFactor: "one-minus-src-alpha",
621
+ operation: "add",
622
+ },
623
+ },
624
+ },
625
+ ],
626
+ },
627
+ primitive: {
628
+ cullMode: "back",
629
+ },
630
+ depthStencil: {
631
+ format: "depth24plus-stencil8",
632
+ depthWriteEnabled: false, // Don't write depth - let hair geometry control depth
633
+ depthCompare: "less-equal", // Only draw where hair depth exists
634
+ stencilFront: {
635
+ compare: "not-equal", // Only render where stencil != 1 (not over eyes)
636
+ failOp: "keep",
637
+ depthFailOp: "keep",
638
+ passOp: "keep",
639
+ },
640
+ stencilBack: {
641
+ compare: "not-equal",
642
+ failOp: "keep",
643
+ depthFailOp: "keep",
644
+ passOp: "keep",
645
+ },
646
+ },
647
+ multisample: {
648
+ count: this.sampleCount,
649
+ },
650
+ })
651
+
652
+ // Hair outline pipeline for over eyes: draws where stencil == 1, but only where hair depth exists
653
+ // Uses depth compare "equal" with a small bias to only appear where hair geometry exists
654
+ this.hairOutlineOverEyesPipeline = this.device.createRenderPipeline({
655
+ label: "hair outline over eyes pipeline",
656
+ layout: outlinePipelineLayout,
657
+ vertex: {
658
+ module: outlineShaderModule,
659
+ buffers: [
660
+ {
661
+ arrayStride: 8 * 4,
662
+ attributes: [
663
+ {
664
+ shaderLocation: 0,
665
+ offset: 0,
666
+ format: "float32x3" as GPUVertexFormat,
667
+ },
668
+ {
669
+ shaderLocation: 1,
670
+ offset: 3 * 4,
671
+ format: "float32x3" as GPUVertexFormat,
672
+ },
673
+ {
674
+ shaderLocation: 2,
675
+ offset: 6 * 4,
676
+ format: "float32x2" as GPUVertexFormat,
677
+ },
678
+ ],
679
+ },
680
+ {
681
+ arrayStride: 4 * 2,
682
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
683
+ },
684
+ {
685
+ arrayStride: 4,
686
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
687
+ },
688
+ ],
689
+ },
690
+ fragment: {
691
+ module: outlineShaderModule,
692
+ targets: [
693
+ {
694
+ format: this.presentationFormat,
695
+ blend: {
696
+ color: {
697
+ srcFactor: "src-alpha",
698
+ dstFactor: "one-minus-src-alpha",
699
+ operation: "add",
700
+ },
701
+ alpha: {
702
+ srcFactor: "one",
703
+ dstFactor: "one-minus-src-alpha",
704
+ operation: "add",
705
+ },
706
+ },
707
+ },
708
+ ],
709
+ },
710
+ primitive: {
711
+ cullMode: "back",
712
+ },
713
+ depthStencil: {
714
+ format: "depth24plus-stencil8",
715
+ depthWriteEnabled: false, // Don't write depth
716
+
717
+ depthCompare: "less-equal", // Draw where outline depth <= existing depth (hair depth)
718
+ depthBias: -0.0001, // Small negative bias to bring outline slightly closer for depth test
719
+ depthBiasSlopeScale: 0.0,
720
+ depthBiasClamp: 0.0,
721
+ stencilFront: {
722
+ compare: "equal", // Only render where stencil == 1 (over eyes)
723
+ failOp: "keep",
724
+ depthFailOp: "keep",
725
+ passOp: "keep",
726
+ },
727
+ stencilBack: {
728
+ compare: "equal",
729
+ failOp: "keep",
730
+ depthFailOp: "keep",
731
+ passOp: "keep",
732
+ },
733
+ },
734
+ multisample: {
735
+ count: this.sampleCount,
736
+ },
737
+ })
738
+
739
+ // Hair pipeline with multiplicative blending (for hair over eyes)
740
+ this.hairMultiplyPipeline = this.device.createRenderPipeline({
741
+ label: "hair multiply pipeline",
742
+ layout: sharedPipelineLayout,
743
+ vertex: {
744
+ module: hairMultiplyShaderModule,
745
+ buffers: [
746
+ {
747
+ arrayStride: 8 * 4,
748
+ attributes: [
749
+ { shaderLocation: 0, offset: 0, format: "float32x3" as GPUVertexFormat },
750
+ { shaderLocation: 1, offset: 3 * 4, format: "float32x3" as GPUVertexFormat },
751
+ { shaderLocation: 2, offset: 6 * 4, format: "float32x2" as GPUVertexFormat },
752
+ ],
753
+ },
754
+ {
755
+ arrayStride: 4 * 2,
756
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
757
+ },
758
+ {
759
+ arrayStride: 4,
760
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
761
+ },
762
+ ],
763
+ },
764
+ fragment: {
765
+ module: hairMultiplyShaderModule,
766
+ targets: [
767
+ {
768
+ format: this.presentationFormat,
769
+ blend: {
770
+ color: {
771
+ // Simple half-transparent overlay effect
772
+ // Blend: hairColor * overlayAlpha + eyeColor * (1 - overlayAlpha)
773
+ srcFactor: "src-alpha",
774
+ dstFactor: "one-minus-src-alpha",
775
+ operation: "add",
776
+ },
777
+ alpha: {
778
+ srcFactor: "one",
779
+ dstFactor: "one-minus-src-alpha",
780
+ operation: "add",
781
+ },
782
+ },
783
+ },
784
+ ],
785
+ },
786
+ primitive: { cullMode: "none" },
787
+ depthStencil: {
788
+ format: "depth24plus-stencil8",
789
+ depthWriteEnabled: true, // Write depth so outlines can test against it
790
+ depthCompare: "less",
791
+ stencilFront: {
792
+ compare: "equal", // Only render where stencil == 1
793
+ failOp: "keep",
794
+ depthFailOp: "keep",
795
+ passOp: "keep",
796
+ },
797
+ stencilBack: {
798
+ compare: "equal",
799
+ failOp: "keep",
800
+ depthFailOp: "keep",
801
+ passOp: "keep",
802
+ },
803
+ },
804
+ multisample: { count: this.sampleCount },
805
+ })
806
+
807
+ // Hair pipeline for opaque rendering (hair over non-eyes)
808
+ this.hairOpaquePipeline = this.device.createRenderPipeline({
809
+ label: "hair opaque pipeline",
810
+ layout: sharedPipelineLayout,
811
+ vertex: {
812
+ module: shaderModule,
813
+ buffers: [
814
+ {
815
+ arrayStride: 8 * 4,
816
+ attributes: [
817
+ { shaderLocation: 0, offset: 0, format: "float32x3" as GPUVertexFormat },
818
+ { shaderLocation: 1, offset: 3 * 4, format: "float32x3" as GPUVertexFormat },
819
+ { shaderLocation: 2, offset: 6 * 4, format: "float32x2" as GPUVertexFormat },
820
+ ],
821
+ },
822
+ {
823
+ arrayStride: 4 * 2,
824
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
825
+ },
826
+ {
827
+ arrayStride: 4,
828
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
829
+ },
830
+ ],
831
+ },
832
+ fragment: {
833
+ module: shaderModule,
834
+ targets: [
835
+ {
836
+ format: this.presentationFormat,
837
+ blend: {
838
+ color: {
839
+ srcFactor: "src-alpha",
840
+ dstFactor: "one-minus-src-alpha",
841
+ operation: "add",
842
+ },
843
+ alpha: {
844
+ srcFactor: "one",
845
+ dstFactor: "one-minus-src-alpha",
846
+ operation: "add",
847
+ },
848
+ },
849
+ },
850
+ ],
851
+ },
852
+ primitive: { cullMode: "none" },
853
+ depthStencil: {
854
+ format: "depth24plus-stencil8",
855
+ depthWriteEnabled: true,
856
+ depthCompare: "less",
857
+ stencilFront: {
858
+ compare: "not-equal", // Only render where stencil != 1
859
+ failOp: "keep",
860
+ depthFailOp: "keep",
861
+ passOp: "keep",
862
+ },
863
+ stencilBack: {
864
+ compare: "not-equal",
865
+ failOp: "keep",
866
+ depthFailOp: "keep",
867
+ passOp: "keep",
868
+ },
869
+ },
870
+ multisample: { count: this.sampleCount },
871
+ })
872
+
873
+ // Eye overlay pipeline (renders after opaque, writes stencil)
874
+ this.eyePipeline = this.device.createRenderPipeline({
875
+ label: "eye overlay pipeline",
876
+ layout: sharedPipelineLayout,
877
+ vertex: {
878
+ module: shaderModule,
879
+ buffers: [
880
+ {
881
+ arrayStride: 8 * 4,
882
+ attributes: [
883
+ { shaderLocation: 0, offset: 0, format: "float32x3" as GPUVertexFormat },
884
+ { shaderLocation: 1, offset: 3 * 4, format: "float32x3" as GPUVertexFormat },
885
+ { shaderLocation: 2, offset: 6 * 4, format: "float32x2" as GPUVertexFormat },
886
+ ],
887
+ },
888
+ {
889
+ arrayStride: 4 * 2,
890
+ attributes: [{ shaderLocation: 3, offset: 0, format: "uint16x4" as GPUVertexFormat }],
891
+ },
892
+ {
893
+ arrayStride: 4,
894
+ attributes: [{ shaderLocation: 4, offset: 0, format: "unorm8x4" as GPUVertexFormat }],
895
+ },
896
+ ],
897
+ },
898
+ fragment: {
899
+ module: shaderModule,
900
+ targets: [
901
+ {
902
+ format: this.presentationFormat,
903
+ blend: {
904
+ color: {
905
+ srcFactor: "src-alpha",
906
+ dstFactor: "one-minus-src-alpha",
907
+ operation: "add",
908
+ },
909
+ alpha: {
910
+ srcFactor: "one",
911
+ dstFactor: "one-minus-src-alpha",
912
+ operation: "add",
913
+ },
914
+ },
915
+ },
916
+ ],
917
+ },
918
+ primitive: { cullMode: "none" },
919
+ depthStencil: {
920
+ format: "depth24plus-stencil8",
921
+ depthWriteEnabled: false, // Don't write depth
922
+ depthCompare: "less", // Respect existing depth
923
+ stencilFront: {
924
+ compare: "always",
925
+ failOp: "keep",
926
+ depthFailOp: "keep",
927
+ passOp: "replace", // Write stencil value 1
928
+ },
929
+ stencilBack: {
930
+ compare: "always",
931
+ failOp: "keep",
932
+ depthFailOp: "keep",
933
+ passOp: "replace",
934
+ },
935
+ },
936
+ multisample: { count: this.sampleCount },
937
+ })
938
+ }
939
+
940
+ // Create compute shader for skin matrix computation
941
+ private createSkinMatrixComputePipeline() {
942
+ const computeShader = this.device.createShaderModule({
943
+ label: "skin matrix compute",
944
+ code: /* wgsl */ `
945
+ struct BoneCountUniform {
946
+ count: u32,
947
+ _padding1: u32,
948
+ _padding2: u32,
949
+ _padding3: u32,
950
+ _padding4: vec4<u32>,
951
+ };
952
+
953
+ @group(0) @binding(0) var<uniform> boneCount: BoneCountUniform;
954
+ @group(0) @binding(1) var<storage, read> worldMatrices: array<mat4x4f>;
955
+ @group(0) @binding(2) var<storage, read> inverseBindMatrices: array<mat4x4f>;
956
+ @group(0) @binding(3) var<storage, read_write> skinMatrices: array<mat4x4f>;
957
+
958
+ @compute @workgroup_size(64)
959
+ fn main(@builtin(global_invocation_id) globalId: vec3<u32>) {
960
+ let boneIndex = globalId.x;
961
+ // Bounds check: we dispatch workgroups (64 threads each), so some threads may be out of range
962
+ if (boneIndex >= boneCount.count) {
963
+ return;
964
+ }
965
+ let worldMat = worldMatrices[boneIndex];
966
+ let invBindMat = inverseBindMatrices[boneIndex];
967
+ skinMatrices[boneIndex] = worldMat * invBindMat;
968
+ }
969
+ `,
970
+ })
971
+
972
+ this.skinMatrixComputePipeline = this.device.createComputePipeline({
973
+ label: "skin matrix compute pipeline",
974
+ layout: "auto",
975
+ compute: {
976
+ module: computeShader,
977
+ },
978
+ })
979
+ }
980
+
981
+ // Step 3: Setup canvas resize handling
982
+ private setupResize() {
983
+ this.resizeObserver = new ResizeObserver(() => this.handleResize())
984
+ this.resizeObserver.observe(this.canvas)
985
+ this.handleResize()
986
+ }
987
+
988
+ private handleResize() {
989
+ const displayWidth = this.canvas.clientWidth
990
+ const displayHeight = this.canvas.clientHeight
991
+
992
+ const dpr = window.devicePixelRatio || 1
993
+ const width = Math.floor(displayWidth * dpr)
994
+ const height = Math.floor(displayHeight * dpr)
995
+
996
+ if (!this.multisampleTexture || this.canvas.width !== width || this.canvas.height !== height) {
997
+ this.canvas.width = width
998
+ this.canvas.height = height
999
+
1000
+ this.multisampleTexture = this.device.createTexture({
1001
+ label: "multisample render target",
1002
+ size: [width, height],
1003
+ sampleCount: this.sampleCount,
1004
+ format: this.presentationFormat,
1005
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
1006
+ })
1007
+
1008
+ this.depthTexture = this.device.createTexture({
1009
+ label: "depth texture",
1010
+ size: [width, height],
1011
+ sampleCount: this.sampleCount,
1012
+ format: "depth24plus-stencil8",
1013
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
1014
+ })
1015
+
1016
+ const depthTextureView = this.depthTexture.createView()
1017
+
1018
+ const colorAttachment: GPURenderPassColorAttachment =
1019
+ this.sampleCount > 1
1020
+ ? {
1021
+ view: this.multisampleTexture.createView(),
1022
+ resolveTarget: this.context.getCurrentTexture().createView(),
1023
+ clearValue: { r: 0, g: 0, b: 0, a: 0 },
1024
+ loadOp: "clear",
1025
+ storeOp: "store",
1026
+ }
1027
+ : {
1028
+ view: this.context.getCurrentTexture().createView(),
1029
+ clearValue: { r: 0, g: 0, b: 0, a: 0 },
1030
+ loadOp: "clear",
1031
+ storeOp: "store",
1032
+ }
1033
+
1034
+ this.renderPassDescriptor = {
1035
+ label: "renderPass",
1036
+ colorAttachments: [colorAttachment],
1037
+ depthStencilAttachment: {
1038
+ view: depthTextureView,
1039
+ depthClearValue: 1.0,
1040
+ depthLoadOp: "clear",
1041
+ depthStoreOp: "store",
1042
+ stencilClearValue: 0, // New: clear stencil to 0
1043
+ stencilLoadOp: "clear", // New: clear stencil each frame
1044
+ stencilStoreOp: "store", // New: store stencil
1045
+ },
1046
+ }
1047
+
1048
+ this.camera.aspect = width / height
1049
+ }
1050
+ }
1051
+
1052
+ // Step 4: Create camera and uniform buffer
1053
+ private setupCamera() {
1054
+ this.cameraUniformBuffer = this.device.createBuffer({
1055
+ label: "camera uniforms",
1056
+ size: 40 * 4,
1057
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1058
+ })
1059
+
1060
+ this.camera = new Camera(Math.PI, Math.PI / 2.5, 26.6, new Vec3(0, 12.5, 0))
1061
+
1062
+ this.camera.aspect = this.canvas.width / this.canvas.height
1063
+ this.camera.attachControl(this.canvas)
1064
+ }
1065
+
1066
+ // Step 5: Create lighting buffers
1067
+ private setupLighting() {
1068
+ this.lightUniformBuffer = this.device.createBuffer({
1069
+ label: "light uniforms",
1070
+ size: 64 * 4,
1071
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1072
+ })
1073
+
1074
+ this.lightCount = 0
1075
+
1076
+ this.setAmbient(0.96)
1077
+ this.addLight(new Vec3(-0.5, -0.8, 0.5).normalize(), new Vec3(1.0, 0.95, 0.9), 0.12)
1078
+ this.addLight(new Vec3(0.7, -0.5, 0.3).normalize(), new Vec3(0.8, 0.85, 1.0), 0.1)
1079
+ this.addLight(new Vec3(0.3, -0.5, -1.0).normalize(), new Vec3(0.9, 0.9, 1.0), 0.08)
1080
+ this.device.queue.writeBuffer(this.lightUniformBuffer, 0, this.lightData)
1081
+ }
1082
+
1083
+ public addLight(direction: Vec3, color: Vec3, intensity: number = 1.0): boolean {
1084
+ if (this.lightCount >= 4) return false
1085
+
1086
+ const normalized = direction.normalize()
1087
+ const baseIndex = 4 + this.lightCount * 8
1088
+ this.lightData[baseIndex] = normalized.x
1089
+ this.lightData[baseIndex + 1] = normalized.y
1090
+ this.lightData[baseIndex + 2] = normalized.z
1091
+ this.lightData[baseIndex + 3] = 0
1092
+ this.lightData[baseIndex + 4] = color.x
1093
+ this.lightData[baseIndex + 5] = color.y
1094
+ this.lightData[baseIndex + 6] = color.z
1095
+ this.lightData[baseIndex + 7] = intensity
1096
+
1097
+ this.lightCount++
1098
+ this.lightData[1] = this.lightCount
1099
+ return true
1100
+ }
1101
+
1102
+ public setAmbient(intensity: number) {
1103
+ this.lightData[0] = intensity
1104
+ }
1105
+
1106
+ public getStats(): EngineStats {
1107
+ return { ...this.stats }
1108
+ }
1109
+
1110
+ public runRenderLoop(callback?: () => void) {
1111
+ this.renderLoopCallback = callback || null
1112
+
1113
+ const loop = () => {
1114
+ this.render()
1115
+
1116
+ if (this.renderLoopCallback) {
1117
+ this.renderLoopCallback()
1118
+ }
1119
+
1120
+ this.animationFrameId = requestAnimationFrame(loop)
1121
+ }
1122
+
1123
+ this.animationFrameId = requestAnimationFrame(loop)
1124
+ }
1125
+
1126
+ public stopRenderLoop() {
1127
+ if (this.animationFrameId !== null) {
1128
+ cancelAnimationFrame(this.animationFrameId)
1129
+ this.animationFrameId = null
1130
+ }
1131
+ this.renderLoopCallback = null
1132
+ }
1133
+
1134
+ public dispose() {
1135
+ this.stopRenderLoop()
1136
+ if (this.camera) this.camera.detachControl()
1137
+ if (this.resizeObserver) {
1138
+ this.resizeObserver.disconnect()
1139
+ this.resizeObserver = null
1140
+ }
1141
+ }
1142
+
1143
+ // Step 6: Load PMX model file
1144
+ public async loadModel(path: string) {
1145
+ const pathParts = path.split("/")
1146
+ pathParts.pop()
1147
+ const dir = pathParts.join("/") + "/"
1148
+ this.modelDir = dir
1149
+
1150
+ const model = await PmxLoader.load(path)
1151
+ this.physics = new Physics(model.getRigidbodies(), model.getJoints())
1152
+ await this.setupModelBuffers(model)
1153
+ }
1154
+
1155
+ public rotateBones(bones: string[], rotations: Quat[], durationMs?: number) {
1156
+ this.currentModel?.rotateBones(bones, rotations, durationMs)
1157
+ }
1158
+
1159
+ // Step 7: Create vertex, index, and joint buffers
1160
+ private async setupModelBuffers(model: Model) {
1161
+ this.currentModel = model
1162
+ const vertices = model.getVertices()
1163
+ const skinning = model.getSkinning()
1164
+ const skeleton = model.getSkeleton()
1165
+
1166
+ this.vertexBuffer = this.device.createBuffer({
1167
+ label: "model vertex buffer",
1168
+ size: vertices.byteLength,
1169
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
1170
+ })
1171
+ this.device.queue.writeBuffer(this.vertexBuffer, 0, vertices)
1172
+ this.vertexCount = model.getVertexCount()
1173
+
1174
+ this.jointsBuffer = this.device.createBuffer({
1175
+ label: "joints buffer",
1176
+ size: skinning.joints.byteLength,
1177
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
1178
+ })
1179
+ this.device.queue.writeBuffer(
1180
+ this.jointsBuffer,
1181
+ 0,
1182
+ skinning.joints.buffer,
1183
+ skinning.joints.byteOffset,
1184
+ skinning.joints.byteLength
1185
+ )
1186
+
1187
+ this.weightsBuffer = this.device.createBuffer({
1188
+ label: "weights buffer",
1189
+ size: skinning.weights.byteLength,
1190
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
1191
+ })
1192
+ this.device.queue.writeBuffer(
1193
+ this.weightsBuffer,
1194
+ 0,
1195
+ skinning.weights.buffer,
1196
+ skinning.weights.byteOffset,
1197
+ skinning.weights.byteLength
1198
+ )
1199
+
1200
+ const boneCount = skeleton.bones.length
1201
+ const matrixSize = boneCount * 16 * 4
1202
+
1203
+ this.skinMatrixBuffer = this.device.createBuffer({
1204
+ label: "skin matrices",
1205
+ size: Math.max(256, matrixSize),
1206
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX,
1207
+ })
1208
+
1209
+ this.worldMatrixBuffer = this.device.createBuffer({
1210
+ label: "world matrices",
1211
+ size: Math.max(256, matrixSize),
1212
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1213
+ })
1214
+
1215
+ this.inverseBindMatrixBuffer = this.device.createBuffer({
1216
+ label: "inverse bind matrices",
1217
+ size: Math.max(256, matrixSize),
1218
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
1219
+ })
1220
+
1221
+ const invBindMatrices = skeleton.inverseBindMatrices
1222
+ this.device.queue.writeBuffer(
1223
+ this.inverseBindMatrixBuffer,
1224
+ 0,
1225
+ invBindMatrices.buffer,
1226
+ invBindMatrices.byteOffset,
1227
+ invBindMatrices.byteLength
1228
+ )
1229
+
1230
+ this.boneCountBuffer = this.device.createBuffer({
1231
+ label: "bone count uniform",
1232
+ size: 32, // Minimum uniform buffer size is 32 bytes
1233
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1234
+ })
1235
+ const boneCountData = new Uint32Array(8) // 32 bytes total
1236
+ boneCountData[0] = boneCount
1237
+ this.device.queue.writeBuffer(this.boneCountBuffer, 0, boneCountData)
1238
+
1239
+ this.createSkinMatrixComputePipeline()
1240
+
1241
+ const indices = model.getIndices()
1242
+ if (indices) {
1243
+ this.indexBuffer = this.device.createBuffer({
1244
+ label: "model index buffer",
1245
+ size: indices.byteLength,
1246
+ usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
1247
+ })
1248
+ this.device.queue.writeBuffer(this.indexBuffer, 0, indices)
1249
+ } else {
1250
+ throw new Error("Model has no index buffer")
1251
+ }
1252
+
1253
+ await this.setupMaterials(model)
1254
+ }
1255
+
1256
+ private opaqueNonEyeNonHairDraws: {
1257
+ count: number
1258
+ firstIndex: number
1259
+ bindGroup: GPUBindGroup
1260
+ isTransparent: boolean
1261
+ }[] = []
1262
+ private eyeDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] = []
1263
+ private hairDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] = []
1264
+ private transparentNonEyeNonHairDraws: {
1265
+ count: number
1266
+ firstIndex: number
1267
+ bindGroup: GPUBindGroup
1268
+ isTransparent: boolean
1269
+ }[] = []
1270
+ private opaqueNonEyeNonHairOutlineDraws: {
1271
+ count: number
1272
+ firstIndex: number
1273
+ bindGroup: GPUBindGroup
1274
+ isTransparent: boolean
1275
+ }[] = []
1276
+ private eyeOutlineDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] = []
1277
+ private hairOutlineDraws: { count: number; firstIndex: number; bindGroup: GPUBindGroup; isTransparent: boolean }[] =
1278
+ []
1279
+ private transparentNonEyeNonHairOutlineDraws: {
1280
+ count: number
1281
+ firstIndex: number
1282
+ bindGroup: GPUBindGroup
1283
+ isTransparent: boolean
1284
+ }[] = []
1285
+
1286
+ // Step 8: Load textures and create material bind groups
1287
+ private async setupMaterials(model: Model) {
1288
+ const materials = model.getMaterials()
1289
+ if (materials.length === 0) {
1290
+ throw new Error("Model has no materials")
1291
+ }
1292
+
1293
+ const textures = model.getTextures()
1294
+
1295
+ const loadTextureByIndex = async (texIndex: number): Promise<GPUTexture | null> => {
1296
+ if (texIndex < 0 || texIndex >= textures.length) {
1297
+ return null
1298
+ }
1299
+
1300
+ const path = this.modelDir + textures[texIndex].path
1301
+ const texture = await this.createTextureFromPath(path)
1302
+ return texture
1303
+ }
1304
+
1305
+ const loadToonTexture = async (toonTextureIndex: number): Promise<GPUTexture> => {
1306
+ const texture = await loadTextureByIndex(toonTextureIndex)
1307
+ if (texture) return texture
1308
+
1309
+ // Default toon texture fallback - cache it
1310
+ const defaultToonPath = "__default_toon__"
1311
+ const cached = this.textureCache.get(defaultToonPath)
1312
+ if (cached) return cached
1313
+
1314
+ const defaultToonData = new Uint8Array(256 * 2 * 4)
1315
+ for (let i = 0; i < 256; i++) {
1316
+ const factor = i / 255.0
1317
+ const gray = Math.floor(128 + factor * 127)
1318
+ defaultToonData[i * 4] = gray
1319
+ defaultToonData[i * 4 + 1] = gray
1320
+ defaultToonData[i * 4 + 2] = gray
1321
+ defaultToonData[i * 4 + 3] = 255
1322
+ defaultToonData[(256 + i) * 4] = gray
1323
+ defaultToonData[(256 + i) * 4 + 1] = gray
1324
+ defaultToonData[(256 + i) * 4 + 2] = gray
1325
+ defaultToonData[(256 + i) * 4 + 3] = 255
1326
+ }
1327
+ const defaultToonTexture = this.device.createTexture({
1328
+ label: "default toon texture",
1329
+ size: [256, 2],
1330
+ format: "rgba8unorm",
1331
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
1332
+ })
1333
+ this.device.queue.writeTexture(
1334
+ { texture: defaultToonTexture },
1335
+ defaultToonData,
1336
+ { bytesPerRow: 256 * 4 },
1337
+ [256, 2]
1338
+ )
1339
+ this.textureCache.set(defaultToonPath, defaultToonTexture)
1340
+ this.textureSizes.set(defaultToonPath, { width: 256, height: 2 })
1341
+ return defaultToonTexture
1342
+ }
1343
+
1344
+ this.opaqueNonEyeNonHairDraws = []
1345
+ this.eyeDraws = []
1346
+ this.hairDraws = []
1347
+ this.transparentNonEyeNonHairDraws = []
1348
+ this.opaqueNonEyeNonHairOutlineDraws = []
1349
+ this.eyeOutlineDraws = []
1350
+ this.hairOutlineDraws = []
1351
+ this.transparentNonEyeNonHairOutlineDraws = []
1352
+ let runningFirstIndex = 0
1353
+
1354
+ for (const mat of materials) {
1355
+ const matCount = mat.vertexCount | 0
1356
+ if (matCount === 0) continue
1357
+
1358
+ const diffuseTexture = await loadTextureByIndex(mat.diffuseTextureIndex)
1359
+ if (!diffuseTexture) throw new Error(`Material "${mat.name}" has no diffuse texture`)
1360
+
1361
+ const toonTexture = await loadToonTexture(mat.toonTextureIndex)
1362
+
1363
+ const materialAlpha = mat.diffuse[3]
1364
+ const EPSILON = 0.001
1365
+ const isTransparent = materialAlpha < 1.0 - EPSILON
1366
+
1367
+ const materialUniformData = new Float32Array(4)
1368
+ materialUniformData[0] = materialAlpha
1369
+ materialUniformData[1] = 0.0
1370
+ materialUniformData[2] = 0.0
1371
+ materialUniformData[3] = 0.0
1372
+
1373
+ const materialUniformBuffer = this.device.createBuffer({
1374
+ label: `material uniform: ${mat.name}`,
1375
+ size: materialUniformData.byteLength,
1376
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1377
+ })
1378
+ this.device.queue.writeBuffer(materialUniformBuffer, 0, materialUniformData)
1379
+
1380
+ // Create bind groups using the shared bind group layout
1381
+ // All pipelines (main, eye, hair multiply, hair opaque) use the same shader and layout
1382
+ const bindGroup = this.device.createBindGroup({
1383
+ label: `material bind group: ${mat.name}`,
1384
+ layout: this.hairBindGroupLayout,
1385
+ entries: [
1386
+ { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
1387
+ { binding: 1, resource: { buffer: this.lightUniformBuffer } },
1388
+ { binding: 2, resource: diffuseTexture.createView() },
1389
+ { binding: 3, resource: this.textureSampler },
1390
+ { binding: 4, resource: { buffer: this.skinMatrixBuffer! } },
1391
+ { binding: 5, resource: toonTexture.createView() },
1392
+ { binding: 6, resource: this.textureSampler },
1393
+ { binding: 7, resource: { buffer: materialUniformBuffer } },
1394
+ ],
1395
+ })
1396
+
1397
+ // Classify materials into appropriate draw lists
1398
+ if (mat.isEye) {
1399
+ this.eyeDraws.push({
1400
+ count: matCount,
1401
+ firstIndex: runningFirstIndex,
1402
+ bindGroup,
1403
+ isTransparent,
1404
+ })
1405
+ } else if (mat.isHair) {
1406
+ this.hairDraws.push({
1407
+ count: matCount,
1408
+ firstIndex: runningFirstIndex,
1409
+ bindGroup,
1410
+ isTransparent,
1411
+ })
1412
+ } else if (isTransparent) {
1413
+ this.transparentNonEyeNonHairDraws.push({
1414
+ count: matCount,
1415
+ firstIndex: runningFirstIndex,
1416
+ bindGroup,
1417
+ isTransparent,
1418
+ })
1419
+ } else {
1420
+ this.opaqueNonEyeNonHairDraws.push({
1421
+ count: matCount,
1422
+ firstIndex: runningFirstIndex,
1423
+ bindGroup,
1424
+ isTransparent,
1425
+ })
1426
+ }
1427
+
1428
+ // Outline for all materials (including transparent)
1429
+ // Edge flag is at bit 4 (0x10) in PMX format, not bit 0 (0x01)
1430
+ if ((mat.edgeFlag & 0x10) !== 0 && mat.edgeSize > 0) {
1431
+ const materialUniformData = new Float32Array(8)
1432
+ materialUniformData[0] = mat.edgeColor[0]
1433
+ materialUniformData[1] = mat.edgeColor[1]
1434
+ materialUniformData[2] = mat.edgeColor[2]
1435
+ materialUniformData[3] = mat.edgeColor[3]
1436
+ materialUniformData[4] = mat.edgeSize
1437
+
1438
+ const materialUniformBuffer = this.device.createBuffer({
1439
+ label: `outline material uniform: ${mat.name}`,
1440
+ size: materialUniformData.byteLength,
1441
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1442
+ })
1443
+ this.device.queue.writeBuffer(materialUniformBuffer, 0, materialUniformData)
1444
+
1445
+ const outlineBindGroup = this.device.createBindGroup({
1446
+ label: `outline bind group: ${mat.name}`,
1447
+ layout: this.outlineBindGroupLayout,
1448
+ entries: [
1449
+ { binding: 0, resource: { buffer: this.cameraUniformBuffer } },
1450
+ { binding: 1, resource: { buffer: materialUniformBuffer } },
1451
+ { binding: 2, resource: { buffer: this.skinMatrixBuffer! } },
1452
+ ],
1453
+ })
1454
+
1455
+ // Classify outlines into appropriate draw lists
1456
+ if (mat.isEye) {
1457
+ this.eyeOutlineDraws.push({
1458
+ count: matCount,
1459
+ firstIndex: runningFirstIndex,
1460
+ bindGroup: outlineBindGroup,
1461
+ isTransparent,
1462
+ })
1463
+ } else if (mat.isHair) {
1464
+ this.hairOutlineDraws.push({
1465
+ count: matCount,
1466
+ firstIndex: runningFirstIndex,
1467
+ bindGroup: outlineBindGroup,
1468
+ isTransparent,
1469
+ })
1470
+ } else if (isTransparent) {
1471
+ this.transparentNonEyeNonHairOutlineDraws.push({
1472
+ count: matCount,
1473
+ firstIndex: runningFirstIndex,
1474
+ bindGroup: outlineBindGroup,
1475
+ isTransparent,
1476
+ })
1477
+ } else {
1478
+ this.opaqueNonEyeNonHairOutlineDraws.push({
1479
+ count: matCount,
1480
+ firstIndex: runningFirstIndex,
1481
+ bindGroup: outlineBindGroup,
1482
+ isTransparent,
1483
+ })
1484
+ }
1485
+ }
1486
+
1487
+ runningFirstIndex += matCount
1488
+ }
1489
+ }
1490
+
1491
+ // Helper: Load texture from file path with optional max size limit
1492
+ private async createTextureFromPath(path: string, maxSize: number = 2048): Promise<GPUTexture | null> {
1493
+ const cached = this.textureCache.get(path)
1494
+ if (cached) {
1495
+ return cached
1496
+ }
1497
+
1498
+ try {
1499
+ const response = await fetch(path)
1500
+ if (!response.ok) {
1501
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`)
1502
+ }
1503
+ let imageBitmap = await createImageBitmap(await response.blob(), {
1504
+ premultiplyAlpha: "none",
1505
+ colorSpaceConversion: "none",
1506
+ })
1507
+
1508
+ // Downscale if texture is too large
1509
+ let finalWidth = imageBitmap.width
1510
+ let finalHeight = imageBitmap.height
1511
+ if (finalWidth > maxSize || finalHeight > maxSize) {
1512
+ const scale = Math.min(maxSize / finalWidth, maxSize / finalHeight)
1513
+ finalWidth = Math.floor(finalWidth * scale)
1514
+ finalHeight = Math.floor(finalHeight * scale)
1515
+
1516
+ // Create canvas to downscale
1517
+ const canvas = new OffscreenCanvas(finalWidth, finalHeight)
1518
+ const ctx = canvas.getContext("2d")
1519
+ if (ctx) {
1520
+ ctx.drawImage(imageBitmap, 0, 0, finalWidth, finalHeight)
1521
+ imageBitmap = await createImageBitmap(canvas)
1522
+ }
1523
+ }
1524
+
1525
+ const texture = this.device.createTexture({
1526
+ label: `texture: ${path}`,
1527
+ size: [finalWidth, finalHeight],
1528
+ format: "rgba8unorm",
1529
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
1530
+ })
1531
+ this.device.queue.copyExternalImageToTexture({ source: imageBitmap }, { texture }, [finalWidth, finalHeight])
1532
+
1533
+ this.textureCache.set(path, texture)
1534
+ this.textureSizes.set(path, { width: finalWidth, height: finalHeight })
1535
+ return texture
1536
+ } catch {
1537
+ return null
1538
+ }
1539
+ }
1540
+
1541
+ // Step 9: Render one frame
1542
+ public render() {
1543
+ if (this.multisampleTexture && this.camera && this.device && this.currentModel) {
1544
+ const currentTime = performance.now()
1545
+ const deltaTime = this.lastFrameTime > 0 ? (currentTime - this.lastFrameTime) / 1000 : 0.016
1546
+ this.lastFrameTime = currentTime
1547
+
1548
+ this.updateCameraUniforms()
1549
+ this.updateRenderTarget()
1550
+
1551
+ this.updateModelPose(deltaTime)
1552
+
1553
+ const encoder = this.device.createCommandEncoder()
1554
+ const pass = encoder.beginRenderPass(this.renderPassDescriptor)
1555
+
1556
+ pass.setVertexBuffer(0, this.vertexBuffer)
1557
+ pass.setVertexBuffer(1, this.jointsBuffer)
1558
+ pass.setVertexBuffer(2, this.weightsBuffer)
1559
+ pass.setIndexBuffer(this.indexBuffer!, "uint32")
1560
+
1561
+ this.drawCallCount = 0
1562
+
1563
+ // === PASS 1: Opaque non-eye, non-hair (face, body, etc) ===
1564
+ this.drawOutlines(pass, false) // Opaque outlines
1565
+ pass.setPipeline(this.pipeline)
1566
+ for (const draw of this.opaqueNonEyeNonHairDraws) {
1567
+ if (draw.count > 0) {
1568
+ pass.setBindGroup(0, draw.bindGroup)
1569
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1570
+ this.drawCallCount++
1571
+ }
1572
+ }
1573
+
1574
+ // === PASS 2: Eyes (writes stencil = 1) ===
1575
+ pass.setPipeline(this.eyePipeline)
1576
+ pass.setStencilReference(1) // Set stencil reference value to 1
1577
+ for (const draw of this.eyeDraws) {
1578
+ if (draw.count > 0) {
1579
+ pass.setBindGroup(0, draw.bindGroup)
1580
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1581
+ this.drawCallCount++
1582
+ }
1583
+ }
1584
+
1585
+ // === PASS 3a: Hair over eyes (stencil == 1, multiply blend) ===
1586
+ // Draw hair geometry first to establish depth
1587
+ pass.setPipeline(this.hairMultiplyPipeline)
1588
+ pass.setStencilReference(1) // Check against stencil value 1
1589
+ for (const draw of this.hairDraws) {
1590
+ if (draw.count > 0) {
1591
+ pass.setBindGroup(0, draw.bindGroup)
1592
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1593
+ this.drawCallCount++
1594
+ }
1595
+ }
1596
+
1597
+ // === PASS 3a.5: Hair outlines over eyes (stencil == 1, depth test to only draw near hair) ===
1598
+ // Use depth compare "less-equal" with the hair depth to only draw outline where hair exists
1599
+ // The outline is expanded outward, so we need to ensure it only appears near the hair edge
1600
+ pass.setPipeline(this.hairOutlineOverEyesPipeline)
1601
+ pass.setStencilReference(1) // Check against stencil value 1 (with equal test)
1602
+ for (const draw of this.hairOutlineDraws) {
1603
+ if (draw.count > 0) {
1604
+ pass.setBindGroup(0, draw.bindGroup)
1605
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1606
+ }
1607
+ }
1608
+
1609
+ // === PASS 3b: Hair over non-eyes (stencil != 1, opaque) ===
1610
+ pass.setPipeline(this.hairOpaquePipeline)
1611
+ pass.setStencilReference(1) // Check against stencil value 1 (with not-equal test)
1612
+ for (const draw of this.hairDraws) {
1613
+ if (draw.count > 0) {
1614
+ pass.setBindGroup(0, draw.bindGroup)
1615
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1616
+ this.drawCallCount++
1617
+ }
1618
+ }
1619
+
1620
+ // === PASS 3b.5: Hair outlines over non-eyes (stencil != 1) ===
1621
+ // Draw hair outlines after hair geometry, so they only appear where hair exists
1622
+ pass.setPipeline(this.hairOutlinePipeline)
1623
+ pass.setStencilReference(1) // Check against stencil value 1 (with not-equal test)
1624
+ for (const draw of this.hairOutlineDraws) {
1625
+ if (draw.count > 0) {
1626
+ pass.setBindGroup(0, draw.bindGroup)
1627
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1628
+ }
1629
+ }
1630
+
1631
+ // === PASS 4: Transparent non-eye, non-hair ===
1632
+ pass.setPipeline(this.pipeline)
1633
+ for (const draw of this.transparentNonEyeNonHairDraws) {
1634
+ if (draw.count > 0) {
1635
+ pass.setBindGroup(0, draw.bindGroup)
1636
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1637
+ this.drawCallCount++
1638
+ }
1639
+ }
1640
+
1641
+ this.drawOutlines(pass, true) // Transparent outlines
1642
+
1643
+ pass.end()
1644
+ this.device.queue.submit([encoder.finish()])
1645
+ this.updateStats(performance.now() - currentTime)
1646
+ }
1647
+ }
1648
+
1649
+ // Update camera uniform buffer each frame
1650
+ private updateCameraUniforms() {
1651
+ const viewMatrix = this.camera.getViewMatrix()
1652
+ const projectionMatrix = this.camera.getProjectionMatrix()
1653
+ const cameraPos = this.camera.getPosition()
1654
+ this.cameraMatrixData.set(viewMatrix.values, 0)
1655
+ this.cameraMatrixData.set(projectionMatrix.values, 16)
1656
+ this.cameraMatrixData[32] = cameraPos.x
1657
+ this.cameraMatrixData[33] = cameraPos.y
1658
+ this.cameraMatrixData[34] = cameraPos.z
1659
+ this.device.queue.writeBuffer(this.cameraUniformBuffer, 0, this.cameraMatrixData)
1660
+ }
1661
+
1662
+ // Update render target texture view
1663
+ private updateRenderTarget() {
1664
+ const colorAttachment = (this.renderPassDescriptor.colorAttachments as GPURenderPassColorAttachment[])[0]
1665
+ if (this.sampleCount > 1) {
1666
+ colorAttachment.resolveTarget = this.context.getCurrentTexture().createView()
1667
+ } else {
1668
+ colorAttachment.view = this.context.getCurrentTexture().createView()
1669
+ }
1670
+ }
1671
+
1672
+ // Update model pose and physics
1673
+ private updateModelPose(deltaTime: number) {
1674
+ this.currentModel!.evaluatePose()
1675
+
1676
+ // Upload world matrices to GPU
1677
+ const worldMats = this.currentModel!.getBoneWorldMatrices()
1678
+ this.device.queue.writeBuffer(
1679
+ this.worldMatrixBuffer!,
1680
+ 0,
1681
+ worldMats.buffer,
1682
+ worldMats.byteOffset,
1683
+ worldMats.byteLength
1684
+ )
1685
+
1686
+ if (this.physics) {
1687
+ this.physics.step(deltaTime, worldMats, this.currentModel!.getBoneInverseBindMatrices())
1688
+ // Re-upload world matrices after physics (physics may have updated bones)
1689
+ this.device.queue.writeBuffer(
1690
+ this.worldMatrixBuffer!,
1691
+ 0,
1692
+ worldMats.buffer,
1693
+ worldMats.byteOffset,
1694
+ worldMats.byteLength
1695
+ )
1696
+ }
1697
+
1698
+ // Compute skin matrices on GPU
1699
+ this.computeSkinMatrices()
1700
+ }
1701
+
1702
+ // Compute skin matrices on GPU
1703
+ private computeSkinMatrices() {
1704
+ const boneCount = this.currentModel!.getSkeleton().bones.length
1705
+ const workgroupSize = 64
1706
+ // Dispatch exactly enough threads for all bones (no bounds check needed)
1707
+ const workgroupCount = Math.ceil(boneCount / workgroupSize)
1708
+
1709
+ // Update bone count uniform
1710
+ const boneCountData = new Uint32Array(8) // 32 bytes total
1711
+ boneCountData[0] = boneCount
1712
+ this.device.queue.writeBuffer(this.boneCountBuffer!, 0, boneCountData)
1713
+
1714
+ const bindGroup = this.device.createBindGroup({
1715
+ label: "skin matrix compute bind group",
1716
+ layout: this.skinMatrixComputePipeline!.getBindGroupLayout(0),
1717
+ entries: [
1718
+ { binding: 0, resource: { buffer: this.boneCountBuffer! } },
1719
+ { binding: 1, resource: { buffer: this.worldMatrixBuffer! } },
1720
+ { binding: 2, resource: { buffer: this.inverseBindMatrixBuffer! } },
1721
+ { binding: 3, resource: { buffer: this.skinMatrixBuffer! } },
1722
+ ],
1723
+ })
1724
+
1725
+ const encoder = this.device.createCommandEncoder()
1726
+ const pass = encoder.beginComputePass()
1727
+ pass.setPipeline(this.skinMatrixComputePipeline!)
1728
+ pass.setBindGroup(0, bindGroup)
1729
+ pass.dispatchWorkgroups(workgroupCount)
1730
+ pass.end()
1731
+ this.device.queue.submit([encoder.finish()])
1732
+ }
1733
+
1734
+ // Draw outlines (opaque or transparent)
1735
+ private drawOutlines(pass: GPURenderPassEncoder, transparent: boolean) {
1736
+ pass.setPipeline(this.outlinePipeline)
1737
+ if (transparent) {
1738
+ // Draw transparent outlines (if any)
1739
+ for (const draw of this.transparentNonEyeNonHairOutlineDraws) {
1740
+ if (draw.count > 0) {
1741
+ pass.setBindGroup(0, draw.bindGroup)
1742
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1743
+ }
1744
+ }
1745
+ } else {
1746
+ // Draw opaque outlines before main geometry
1747
+ for (const draw of this.opaqueNonEyeNonHairOutlineDraws) {
1748
+ if (draw.count > 0) {
1749
+ pass.setBindGroup(0, draw.bindGroup)
1750
+ pass.drawIndexed(draw.count, 1, draw.firstIndex, 0, 0)
1751
+ }
1752
+ }
1753
+ }
1754
+ }
1755
+
1756
+ private updateStats(frameTime: number) {
1757
+ const maxSamples = 60
1758
+ this.frameTimeSamples.push(frameTime)
1759
+ this.frameTimeSum += frameTime
1760
+ if (this.frameTimeSamples.length > maxSamples) {
1761
+ const removed = this.frameTimeSamples.shift()!
1762
+ this.frameTimeSum -= removed
1763
+ }
1764
+ const avgFrameTime = this.frameTimeSum / this.frameTimeSamples.length
1765
+ this.stats.frameTime = Math.round(avgFrameTime * 100) / 100
1766
+
1767
+ const now = performance.now()
1768
+ this.framesSinceLastUpdate++
1769
+ const elapsed = now - this.lastFpsUpdate
1770
+
1771
+ if (elapsed >= 1000) {
1772
+ this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) * 1000)
1773
+ this.framesSinceLastUpdate = 0
1774
+ this.lastFpsUpdate = now
1775
+ }
1776
+
1777
+ // Calculate GPU memory: textures + buffers + render targets
1778
+ let textureMemoryBytes = 0
1779
+ for (const [path, size] of this.textureSizes.entries()) {
1780
+ if (this.textureCache.has(path)) {
1781
+ textureMemoryBytes += size.width * size.height * 4 // RGBA8 = 4 bytes per pixel
1782
+ }
1783
+ }
1784
+
1785
+ let bufferMemoryBytes = 0
1786
+ if (this.vertexBuffer) {
1787
+ const vertices = this.currentModel?.getVertices()
1788
+ if (vertices) bufferMemoryBytes += vertices.byteLength
1789
+ }
1790
+ if (this.indexBuffer) {
1791
+ const indices = this.currentModel?.getIndices()
1792
+ if (indices) bufferMemoryBytes += indices.byteLength
1793
+ }
1794
+ if (this.jointsBuffer) {
1795
+ const skinning = this.currentModel?.getSkinning()
1796
+ if (skinning) bufferMemoryBytes += skinning.joints.byteLength
1797
+ }
1798
+ if (this.weightsBuffer) {
1799
+ const skinning = this.currentModel?.getSkinning()
1800
+ if (skinning) bufferMemoryBytes += skinning.weights.byteLength
1801
+ }
1802
+ if (this.skinMatrixBuffer) {
1803
+ const skeleton = this.currentModel?.getSkeleton()
1804
+ if (skeleton) bufferMemoryBytes += Math.max(256, skeleton.bones.length * 16 * 4)
1805
+ }
1806
+ bufferMemoryBytes += 40 * 4 // cameraUniformBuffer
1807
+ bufferMemoryBytes += 64 * 4 // lightUniformBuffer
1808
+ const totalMaterialDraws =
1809
+ this.opaqueNonEyeNonHairDraws.length +
1810
+ this.eyeDraws.length +
1811
+ this.hairDraws.length +
1812
+ this.transparentNonEyeNonHairDraws.length
1813
+ bufferMemoryBytes += totalMaterialDraws * 4 // Material uniform buffers
1814
+
1815
+ let renderTargetMemoryBytes = 0
1816
+ if (this.multisampleTexture) {
1817
+ const width = this.canvas.width
1818
+ const height = this.canvas.height
1819
+ renderTargetMemoryBytes += width * height * 4 * this.sampleCount // multisample color
1820
+ renderTargetMemoryBytes += width * height * 4 // depth
1821
+ }
1822
+
1823
+ const totalGPUMemoryBytes = textureMemoryBytes + bufferMemoryBytes + renderTargetMemoryBytes
1824
+ this.stats.gpuMemory = Math.round((totalGPUMemoryBytes / 1024 / 1024) * 100) / 100
1825
+ }
1826
+ }