reze-engine 0.3.11 → 0.3.13
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/dist/bezier-interpolate.d.ts +15 -0
- package/dist/bezier-interpolate.d.ts.map +1 -0
- package/dist/bezier-interpolate.js +40 -0
- package/dist/engine.d.ts +3 -8
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +23 -34
- package/dist/ik.d.ts +32 -0
- package/dist/ik.d.ts.map +1 -0
- package/dist/ik.js +337 -0
- package/dist/pool-scene.d.ts +52 -0
- package/dist/pool-scene.d.ts.map +1 -0
- package/dist/pool-scene.js +1122 -0
- package/dist/pool.d.ts +38 -0
- package/dist/pool.d.ts.map +1 -0
- package/dist/pool.js +422 -0
- package/dist/rzm-converter.d.ts +12 -0
- package/dist/rzm-converter.d.ts.map +1 -0
- package/dist/rzm-converter.js +40 -0
- package/dist/rzm-loader.d.ts +24 -0
- package/dist/rzm-loader.d.ts.map +1 -0
- package/dist/rzm-loader.js +488 -0
- package/dist/rzm-writer.d.ts +27 -0
- package/dist/rzm-writer.d.ts.map +1 -0
- package/dist/rzm-writer.js +701 -0
- package/package.json +1 -1
- package/src/engine.ts +32 -32
- package/src/player.ts +0 -290
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bezier interpolation for VMD animations
|
|
3
|
+
* Based on the reference implementation from babylon-mmd
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Bezier interpolation function
|
|
7
|
+
* @param x1 First control point X (0-127, normalized to 0-1)
|
|
8
|
+
* @param x2 Second control point X (0-127, normalized to 0-1)
|
|
9
|
+
* @param y1 First control point Y (0-127, normalized to 0-1)
|
|
10
|
+
* @param y2 Second control point Y (0-127, normalized to 0-1)
|
|
11
|
+
* @param t Interpolation parameter (0-1)
|
|
12
|
+
* @returns Interpolated value (0-1)
|
|
13
|
+
*/
|
|
14
|
+
export declare function bezierInterpolate(x1: number, x2: number, y1: number, y2: number, t: number): number;
|
|
15
|
+
//# sourceMappingURL=bezier-interpolate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bezier-interpolate.d.ts","sourceRoot":"","sources":["../src/bezier-interpolate.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAgCnG"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bezier interpolation for VMD animations
|
|
3
|
+
* Based on the reference implementation from babylon-mmd
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Bezier interpolation function
|
|
7
|
+
* @param x1 First control point X (0-127, normalized to 0-1)
|
|
8
|
+
* @param x2 Second control point X (0-127, normalized to 0-1)
|
|
9
|
+
* @param y1 First control point Y (0-127, normalized to 0-1)
|
|
10
|
+
* @param y2 Second control point Y (0-127, normalized to 0-1)
|
|
11
|
+
* @param t Interpolation parameter (0-1)
|
|
12
|
+
* @returns Interpolated value (0-1)
|
|
13
|
+
*/
|
|
14
|
+
export function bezierInterpolate(x1, x2, y1, y2, t) {
|
|
15
|
+
// Clamp t to [0, 1]
|
|
16
|
+
t = Math.max(0, Math.min(1, t));
|
|
17
|
+
// Binary search for the t value that gives us the desired x
|
|
18
|
+
// We're solving for t in the Bezier curve: x(t) = 3*(1-t)^2*t*x1 + 3*(1-t)*t^2*x2 + t^3
|
|
19
|
+
let start = 0;
|
|
20
|
+
let end = 1;
|
|
21
|
+
let mid = 0.5;
|
|
22
|
+
// Iterate until we find the t value that gives us the desired x
|
|
23
|
+
for (let i = 0; i < 15; i++) {
|
|
24
|
+
// Evaluate Bezier curve at mid point
|
|
25
|
+
const x = 3 * (1 - mid) * (1 - mid) * mid * x1 + 3 * (1 - mid) * mid * mid * x2 + mid * mid * mid;
|
|
26
|
+
if (Math.abs(x - t) < 0.0001) {
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
if (x < t) {
|
|
30
|
+
start = mid;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
end = mid;
|
|
34
|
+
}
|
|
35
|
+
mid = (start + end) / 2;
|
|
36
|
+
}
|
|
37
|
+
// Now evaluate the y value at this t
|
|
38
|
+
const y = 3 * (1 - mid) * (1 - mid) * mid * y1 + 3 * (1 - mid) * mid * mid * y2 + mid * mid * mid;
|
|
39
|
+
return y;
|
|
40
|
+
}
|
package/dist/engine.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export type EngineOptions = {
|
|
|
5
5
|
rimLightIntensity?: number;
|
|
6
6
|
cameraDistance?: number;
|
|
7
7
|
cameraTarget?: Vec3;
|
|
8
|
+
cameraFov?: number;
|
|
8
9
|
};
|
|
10
|
+
export declare const DEFAULT_ENGINE_OPTIONS: Required<EngineOptions>;
|
|
9
11
|
export interface EngineStats {
|
|
10
12
|
fps: number;
|
|
11
13
|
frameTime: number;
|
|
@@ -20,6 +22,7 @@ export declare class Engine {
|
|
|
20
22
|
private cameraMatrixData;
|
|
21
23
|
private cameraDistance;
|
|
22
24
|
private cameraTarget;
|
|
25
|
+
private cameraFov;
|
|
23
26
|
private lightUniformBuffer;
|
|
24
27
|
private lightData;
|
|
25
28
|
private vertexBuffer;
|
|
@@ -44,14 +47,6 @@ export declare class Engine {
|
|
|
44
47
|
private renderPassDescriptor;
|
|
45
48
|
private readonly STENCIL_EYE_VALUE;
|
|
46
49
|
private readonly BLOOM_DOWNSCALE_FACTOR;
|
|
47
|
-
private static readonly DEFAULT_BLOOM_THRESHOLD;
|
|
48
|
-
private static readonly DEFAULT_BLOOM_INTENSITY;
|
|
49
|
-
private static readonly DEFAULT_RIM_LIGHT_INTENSITY;
|
|
50
|
-
private static readonly DEFAULT_CAMERA_DISTANCE;
|
|
51
|
-
private static readonly DEFAULT_CAMERA_TARGET;
|
|
52
|
-
private static readonly TRANSPARENCY_EPSILON;
|
|
53
|
-
private static readonly STATS_FPS_UPDATE_INTERVAL_MS;
|
|
54
|
-
private static readonly STATS_FRAME_TIME_ROUNDING;
|
|
55
50
|
private ambientColor;
|
|
56
51
|
private sceneRenderTexture;
|
|
57
52
|
private sceneRenderTextureView;
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAInC,MAAM,MAAM,aAAa,GAAG;IAC1B,YAAY,CAAC,EAAE,IAAI,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,YAAY,CAAC,EAAE,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAInC,MAAM,MAAM,aAAa,GAAG;IAC1B,YAAY,CAAC,EAAE,IAAI,CAAA;IACnB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,YAAY,CAAC,EAAE,IAAI,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,aAAa,CAO1D,CAAA;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAA;IACX,SAAS,EAAE,MAAM,CAAA;CAClB;AAoBD,qBAAa,MAAM;IACjB,OAAO,CAAC,M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S = {
|
|
5
|
+
ambientColor: new Vec3(1.0, 1.0, 1.0),
|
|
6
|
+
bloomIntensity: 0.12,
|
|
7
|
+
rimLightIntensity: 0.45,
|
|
8
|
+
cameraDistance: 26.6,
|
|
9
|
+
cameraTarget: new Vec3(0, 12.5, 0),
|
|
10
|
+
cameraFov: Math.PI / 4,
|
|
11
|
+
};
|
|
4
12
|
export class Engine {
|
|
5
13
|
constructor(canvas, options) {
|
|
6
14
|
this.cameraMatrixData = new Float32Array(36);
|
|
7
|
-
this.cameraDistance = 26.6;
|
|
8
|
-
this.cameraTarget = new Vec3(0, 12.5, 0);
|
|
9
15
|
this.lightData = new Float32Array(4);
|
|
10
16
|
this.resizeObserver = null;
|
|
11
17
|
this.sampleCount = 4;
|
|
12
18
|
// Constants
|
|
13
19
|
this.STENCIL_EYE_VALUE = 1;
|
|
14
20
|
this.BLOOM_DOWNSCALE_FACTOR = 2;
|
|
15
|
-
// Ambient light settings
|
|
16
|
-
this.ambientColor = new Vec3(1.0, 1.0, 1.0);
|
|
17
|
-
// Bloom settings
|
|
18
|
-
this.bloomThreshold = Engine.DEFAULT_BLOOM_THRESHOLD;
|
|
19
|
-
this.bloomIntensity = Engine.DEFAULT_BLOOM_INTENSITY;
|
|
20
|
-
// Rim light settings
|
|
21
|
-
this.rimLightIntensity = Engine.DEFAULT_RIM_LIGHT_INTENSITY;
|
|
22
21
|
this.currentModel = null;
|
|
23
22
|
this.modelDir = "";
|
|
24
23
|
this.textureCache = new Map();
|
|
@@ -38,11 +37,12 @@ export class Engine {
|
|
|
38
37
|
this.renderLoopCallback = null;
|
|
39
38
|
this.canvas = canvas;
|
|
40
39
|
if (options) {
|
|
41
|
-
this.ambientColor = options.ambientColor ??
|
|
42
|
-
this.bloomIntensity = options.bloomIntensity ??
|
|
43
|
-
this.rimLightIntensity = options.rimLightIntensity ??
|
|
44
|
-
this.cameraDistance = options.cameraDistance ??
|
|
45
|
-
this.cameraTarget = options.cameraTarget ??
|
|
40
|
+
this.ambientColor = options.ambientColor ?? DEFAULT_ENGINE_OPTIONS.ambientColor;
|
|
41
|
+
this.bloomIntensity = options.bloomIntensity ?? DEFAULT_ENGINE_OPTIONS.bloomIntensity;
|
|
42
|
+
this.rimLightIntensity = options.rimLightIntensity ?? DEFAULT_ENGINE_OPTIONS.rimLightIntensity;
|
|
43
|
+
this.cameraDistance = options.cameraDistance ?? DEFAULT_ENGINE_OPTIONS.cameraDistance;
|
|
44
|
+
this.cameraTarget = options.cameraTarget ?? DEFAULT_ENGINE_OPTIONS.cameraTarget;
|
|
45
|
+
this.cameraFov = options.cameraFov ?? DEFAULT_ENGINE_OPTIONS.cameraFov;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
// Step 1: Get WebGPU device and context
|
|
@@ -250,12 +250,12 @@ export class Engine {
|
|
|
250
250
|
|
|
251
251
|
let lightAccum = light.ambientColor;
|
|
252
252
|
|
|
253
|
-
// Rim light calculation
|
|
253
|
+
// Rim light calculation - proper Fresnel for edge-only highlights
|
|
254
254
|
let viewDir = normalize(camera.viewPos - input.worldPos);
|
|
255
|
-
|
|
256
|
-
rimFactor =
|
|
255
|
+
let fresnel = 1.0 - abs(dot(n, viewDir));
|
|
256
|
+
let rimFactor = pow(fresnel, 4.0); // Higher power for sharper edge-only effect
|
|
257
257
|
let rimLight = material.rimColor * material.rimIntensity * rimFactor;
|
|
258
|
-
|
|
258
|
+
|
|
259
259
|
let color = albedo * lightAccum + rimLight;
|
|
260
260
|
|
|
261
261
|
return vec4f(color, finalAlpha);
|
|
@@ -890,7 +890,7 @@ export class Engine {
|
|
|
890
890
|
size: 40 * 4,
|
|
891
891
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
892
892
|
});
|
|
893
|
-
this.camera = new Camera(Math.PI, Math.PI / 2.5, this.cameraDistance, this.cameraTarget);
|
|
893
|
+
this.camera = new Camera(Math.PI, Math.PI / 2.5, this.cameraDistance, this.cameraTarget, this.cameraFov);
|
|
894
894
|
this.camera.aspect = this.canvas.width / this.canvas.height;
|
|
895
895
|
this.camera.attachControl(this.canvas);
|
|
896
896
|
}
|
|
@@ -1070,7 +1070,7 @@ export class Engine {
|
|
|
1070
1070
|
if (!diffuseTexture)
|
|
1071
1071
|
throw new Error(`Material "${mat.name}" has no diffuse texture`);
|
|
1072
1072
|
const materialAlpha = mat.diffuse[3];
|
|
1073
|
-
const isTransparent = materialAlpha < 1.0 -
|
|
1073
|
+
const isTransparent = materialAlpha < 1.0 - 0.001;
|
|
1074
1074
|
const materialUniformBuffer = this.createMaterialUniformBuffer(mat.name, materialAlpha, 0.0);
|
|
1075
1075
|
// Create bind groups using the shared bind group layout - All pipelines (main, eye, hair multiply, hair opaque) use the same shader and layout
|
|
1076
1076
|
const bindGroup = this.device.createBindGroup({
|
|
@@ -1460,26 +1460,15 @@ export class Engine {
|
|
|
1460
1460
|
this.frameTimeSum -= avg;
|
|
1461
1461
|
this.frameTimeCount = maxSamples;
|
|
1462
1462
|
}
|
|
1463
|
-
this.stats.frameTime =
|
|
1464
|
-
Math.round((this.frameTimeSum / this.frameTimeCount) * Engine.STATS_FRAME_TIME_ROUNDING) /
|
|
1465
|
-
Engine.STATS_FRAME_TIME_ROUNDING;
|
|
1463
|
+
this.stats.frameTime = Math.round((this.frameTimeSum / this.frameTimeCount) * 100) / 100;
|
|
1466
1464
|
// FPS tracking
|
|
1467
1465
|
const now = performance.now();
|
|
1468
1466
|
this.framesSinceLastUpdate++;
|
|
1469
1467
|
const elapsed = now - this.lastFpsUpdate;
|
|
1470
|
-
if (elapsed >=
|
|
1471
|
-
this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) *
|
|
1468
|
+
if (elapsed >= 1000) {
|
|
1469
|
+
this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) * 1000);
|
|
1472
1470
|
this.framesSinceLastUpdate = 0;
|
|
1473
1471
|
this.lastFpsUpdate = now;
|
|
1474
1472
|
}
|
|
1475
1473
|
}
|
|
1476
1474
|
}
|
|
1477
|
-
// Default values
|
|
1478
|
-
Engine.DEFAULT_BLOOM_THRESHOLD = 0.01;
|
|
1479
|
-
Engine.DEFAULT_BLOOM_INTENSITY = 0.12;
|
|
1480
|
-
Engine.DEFAULT_RIM_LIGHT_INTENSITY = 0.45;
|
|
1481
|
-
Engine.DEFAULT_CAMERA_DISTANCE = 26.6;
|
|
1482
|
-
Engine.DEFAULT_CAMERA_TARGET = new Vec3(0, 12.5, 0);
|
|
1483
|
-
Engine.TRANSPARENCY_EPSILON = 0.001;
|
|
1484
|
-
Engine.STATS_FPS_UPDATE_INTERVAL_MS = 1000;
|
|
1485
|
-
Engine.STATS_FRAME_TIME_ROUNDING = 100;
|
package/dist/ik.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Vec3 } from "./math";
|
|
2
|
+
import type { Skeleton } from "./model";
|
|
3
|
+
export interface IKLink {
|
|
4
|
+
boneIndex: number;
|
|
5
|
+
hasLimit: boolean;
|
|
6
|
+
minAngle?: Vec3;
|
|
7
|
+
maxAngle?: Vec3;
|
|
8
|
+
}
|
|
9
|
+
export interface IKChain {
|
|
10
|
+
targetBoneIndex: number;
|
|
11
|
+
effectorBoneIndex: number;
|
|
12
|
+
iterationCount: number;
|
|
13
|
+
rotationConstraint: number;
|
|
14
|
+
links: IKLink[];
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare class IK {
|
|
18
|
+
private chains;
|
|
19
|
+
private computeWorldMatricesCallback?;
|
|
20
|
+
private lastTargetPositions;
|
|
21
|
+
private convergedChains;
|
|
22
|
+
private ikRotations;
|
|
23
|
+
constructor(chains?: IKChain[]);
|
|
24
|
+
setComputeWorldMatricesCallback(callback: () => void): void;
|
|
25
|
+
getChains(): IKChain[];
|
|
26
|
+
enableChain(targetBoneName: string, enabled: boolean): void;
|
|
27
|
+
solve(skeleton: Skeleton, localRotations: Float32Array, localTranslations: Float32Array, worldMatrices: Float32Array): void;
|
|
28
|
+
private solveCCD;
|
|
29
|
+
private applyAngleConstraints;
|
|
30
|
+
private computeWorldMatrices;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=ik.d.ts.map
|
package/dist/ik.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ik.d.ts","sourceRoot":"","sources":["../src/ik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,IAAI,EAAQ,MAAM,QAAQ,CAAA;AACzC,OAAO,KAAK,EAAQ,QAAQ,EAAE,MAAM,SAAS,CAAA;AAE7C,MAAM,WAAW,MAAM;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,IAAI,CAAA;IACf,QAAQ,CAAC,EAAE,IAAI,CAAA;CAChB;AAED,MAAM,WAAW,OAAO;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,iBAAiB,EAAE,MAAM,CAAA;IACzB,cAAc,EAAE,MAAM,CAAA;IACtB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,qBAAa,EAAE;IACb,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,4BAA4B,CAAC,CAAY;IAEjD,OAAO,CAAC,mBAAmB,CAA+B;IAE1D,OAAO,CAAC,eAAe,CAAyB;IAEhD,OAAO,CAAC,WAAW,CAA+B;gBAEtC,MAAM,GAAE,OAAO,EAAO;IAMlC,+BAA+B,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI;IAI3D,SAAS,IAAI,OAAO,EAAE;IAKtB,WAAW,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAU3D,KAAK,CACH,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,YAAY,EAC5B,iBAAiB,EAAE,YAAY,EAC/B,aAAa,EAAE,YAAY,GAC1B,IAAI;IA2FP,OAAO,CAAC,QAAQ;IAqLhB,OAAO,CAAC,qBAAqB;IAe7B,OAAO,CAAC,oBAAoB;CAsG7B"}
|
package/dist/ik.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { Quat, Vec3, Mat4 } from "./math";
|
|
2
|
+
export class IK {
|
|
3
|
+
constructor(chains = []) {
|
|
4
|
+
// Track last target positions to detect movement
|
|
5
|
+
this.lastTargetPositions = new Map();
|
|
6
|
+
// Track convergence state for each chain (true = converged, skip solving)
|
|
7
|
+
this.convergedChains = new Set();
|
|
8
|
+
// Accumulated IK rotations for each bone (boneIndex -> quaternion)
|
|
9
|
+
this.ikRotations = new Map();
|
|
10
|
+
this.chains = chains;
|
|
11
|
+
}
|
|
12
|
+
// Set the callback to use Model's computeWorldMatrices
|
|
13
|
+
// The callback doesn't need parameters since Model uses its own runtime state
|
|
14
|
+
setComputeWorldMatricesCallback(callback) {
|
|
15
|
+
this.computeWorldMatricesCallback = callback;
|
|
16
|
+
}
|
|
17
|
+
getChains() {
|
|
18
|
+
return this.chains;
|
|
19
|
+
}
|
|
20
|
+
// Enable/disable IK chain by target bone name
|
|
21
|
+
enableChain(targetBoneName, enabled) {
|
|
22
|
+
// Chains are identified by target bone index, but we need to find by name
|
|
23
|
+
// This will be called from Model which has bone name lookup
|
|
24
|
+
for (const chain of this.chains) {
|
|
25
|
+
// We'll need to pass bone names or use indices - for now, this is a placeholder
|
|
26
|
+
// The actual implementation will be in Model class
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Main IK solve method - modifies bone local rotations in-place
|
|
30
|
+
solve(skeleton, localRotations, localTranslations, worldMatrices) {
|
|
31
|
+
if (this.chains.length === 0)
|
|
32
|
+
return;
|
|
33
|
+
const boneCount = skeleton.bones.length;
|
|
34
|
+
// Reset accumulated IK rotations for all chain bones (as per reference)
|
|
35
|
+
for (const chain of this.chains) {
|
|
36
|
+
for (const link of chain.links) {
|
|
37
|
+
const boneIdx = link.boneIndex;
|
|
38
|
+
if (boneIdx >= 0 && boneIdx < boneCount) {
|
|
39
|
+
this.ikRotations.set(boneIdx, new Quat(0, 0, 0, 1));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Use Model's computeWorldMatrices if available (it uses the same arrays)
|
|
44
|
+
// Otherwise fall back to simplified version
|
|
45
|
+
if (this.computeWorldMatricesCallback) {
|
|
46
|
+
this.computeWorldMatricesCallback();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Fallback to simplified version (shouldn't happen in normal usage)
|
|
50
|
+
this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
|
|
51
|
+
}
|
|
52
|
+
// Solve each IK chain
|
|
53
|
+
for (const chain of this.chains) {
|
|
54
|
+
if (!chain.enabled)
|
|
55
|
+
continue;
|
|
56
|
+
const targetBoneIdx = chain.targetBoneIndex;
|
|
57
|
+
const effectorBoneIdx = chain.effectorBoneIndex;
|
|
58
|
+
if (targetBoneIdx < 0 || targetBoneIdx >= boneCount || effectorBoneIdx < 0 || effectorBoneIdx >= boneCount) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
// Get target position (world position of target bone)
|
|
62
|
+
// In MMD, the target bone is the bone that should reach a specific position
|
|
63
|
+
// The target bone's current world position is where we want the effector to reach
|
|
64
|
+
const targetWorldMatIdx = targetBoneIdx * 16;
|
|
65
|
+
const targetWorldMat = new Mat4(worldMatrices.subarray(targetWorldMatIdx, targetWorldMatIdx + 16));
|
|
66
|
+
const targetPos = targetWorldMat.getPosition();
|
|
67
|
+
// Check if target has moved (detect any movement, even small)
|
|
68
|
+
const lastTargetPos = this.lastTargetPositions.get(targetBoneIdx);
|
|
69
|
+
let targetMoved = false;
|
|
70
|
+
if (!lastTargetPos) {
|
|
71
|
+
// First time seeing this target, initialize position and always solve
|
|
72
|
+
this.lastTargetPositions.set(targetBoneIdx, new Vec3(targetPos.x, targetPos.y, targetPos.z));
|
|
73
|
+
targetMoved = true;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const targetMoveDistance = targetPos.subtract(lastTargetPos).length();
|
|
77
|
+
targetMoved = targetMoveDistance > 0.001; // Detect any movement > 0.001 units (0.1mm)
|
|
78
|
+
}
|
|
79
|
+
// Get current effector position
|
|
80
|
+
const effectorWorldMatIdx = effectorBoneIdx * 16;
|
|
81
|
+
const effectorWorldMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
|
|
82
|
+
const effectorPos = effectorWorldMat.getPosition();
|
|
83
|
+
// Check distance to target
|
|
84
|
+
const distanceToTarget = effectorPos.subtract(targetPos).length();
|
|
85
|
+
// If target moved, always clear convergence and solve
|
|
86
|
+
if (targetMoved) {
|
|
87
|
+
this.convergedChains.delete(targetBoneIdx);
|
|
88
|
+
this.lastTargetPositions.set(targetBoneIdx, new Vec3(targetPos.x, targetPos.y, targetPos.z));
|
|
89
|
+
// Always solve when target moves, regardless of distance
|
|
90
|
+
}
|
|
91
|
+
else if (distanceToTarget < 0.1) {
|
|
92
|
+
// Target hasn't moved and we're already close, skip solving
|
|
93
|
+
if (!this.convergedChains.has(targetBoneIdx)) {
|
|
94
|
+
this.convergedChains.add(targetBoneIdx);
|
|
95
|
+
}
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// Otherwise, solve (target hasn't moved but effector is far from target)
|
|
99
|
+
// Solve using CCD
|
|
100
|
+
// Note: In PMX, links are stored from effector toward root
|
|
101
|
+
// So links[0] is the effector, links[links.length-1] is closest to root
|
|
102
|
+
this.solveCCD(chain, skeleton, localRotations, localTranslations, worldMatrices, targetPos);
|
|
103
|
+
// Recompute world matrices after IK adjustments
|
|
104
|
+
if (this.computeWorldMatricesCallback) {
|
|
105
|
+
this.computeWorldMatricesCallback();
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Cyclic Coordinate Descent IK solver (based on saba MMD implementation)
|
|
113
|
+
solveCCD(chain, skeleton, localRotations, localTranslations, worldMatrices, targetPos) {
|
|
114
|
+
const bones = skeleton.bones;
|
|
115
|
+
const iterationCount = chain.iterationCount;
|
|
116
|
+
const rotationConstraint = chain.rotationConstraint;
|
|
117
|
+
const links = chain.links;
|
|
118
|
+
if (links.length === 0)
|
|
119
|
+
return;
|
|
120
|
+
const effectorBoneIdx = chain.effectorBoneIndex;
|
|
121
|
+
// Get effector position
|
|
122
|
+
const effectorWorldMatIdx = effectorBoneIdx * 16;
|
|
123
|
+
const effectorWorldMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
|
|
124
|
+
let effectorPos = effectorWorldMat.getPosition();
|
|
125
|
+
// Check initial distance - only skip if extremely close (numerical precision threshold)
|
|
126
|
+
const initialDistanceSq = effectorPos.subtract(targetPos).lengthSquared();
|
|
127
|
+
if (initialDistanceSq < 1.0e-10) {
|
|
128
|
+
this.convergedChains.add(chain.targetBoneIndex);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const halfIteration = iterationCount >> 1;
|
|
132
|
+
for (let iter = 0; iter < iterationCount; iter++) {
|
|
133
|
+
const useAxis = iter < halfIteration;
|
|
134
|
+
for (let linkIdx = 0; linkIdx < links.length; linkIdx++) {
|
|
135
|
+
const link = links[linkIdx];
|
|
136
|
+
const jointBoneIdx = link.boneIndex;
|
|
137
|
+
if (jointBoneIdx < 0 || jointBoneIdx >= bones.length)
|
|
138
|
+
continue;
|
|
139
|
+
const bone = bones[jointBoneIdx];
|
|
140
|
+
// Get joint world position
|
|
141
|
+
const jointWorldMatIdx = jointBoneIdx * 16;
|
|
142
|
+
const jointWorldMat = new Mat4(worldMatrices.subarray(jointWorldMatIdx, jointWorldMatIdx + 16));
|
|
143
|
+
const jointPos = jointWorldMat.getPosition();
|
|
144
|
+
// Vectors: from joint to target and effector (REVERSED from typical CCD!)
|
|
145
|
+
// This matches the reference implementation
|
|
146
|
+
const chainTargetVector = jointPos.subtract(targetPos).normalize();
|
|
147
|
+
const chainIkVector = jointPos.subtract(effectorPos).normalize();
|
|
148
|
+
// Rotation axis: cross product
|
|
149
|
+
const chainRotationAxis = chainTargetVector.cross(chainIkVector);
|
|
150
|
+
const axisLenSq = chainRotationAxis.lengthSquared();
|
|
151
|
+
// Skip if axis is too small (vectors are parallel)
|
|
152
|
+
if (axisLenSq < 1.0e-8)
|
|
153
|
+
continue;
|
|
154
|
+
const chainRotationAxisNorm = chainRotationAxis.normalize();
|
|
155
|
+
// Get parent's world rotation matrix (rotation part only)
|
|
156
|
+
let parentWorldRot;
|
|
157
|
+
if (bone.parentIndex >= 0 && bone.parentIndex < bones.length) {
|
|
158
|
+
const parentWorldMatIdx = bone.parentIndex * 16;
|
|
159
|
+
const parentWorldMat = new Mat4(worldMatrices.subarray(parentWorldMatIdx, parentWorldMatIdx + 16));
|
|
160
|
+
parentWorldRot = parentWorldMat.toQuat();
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
parentWorldRot = new Quat(0, 0, 0, 1);
|
|
164
|
+
}
|
|
165
|
+
// Transform rotation axis to parent's local space
|
|
166
|
+
// Invert parent rotation: parentWorldRot^-1
|
|
167
|
+
const parentWorldRotInv = parentWorldRot.conjugate();
|
|
168
|
+
// Transform axis: parentWorldRotInv * axis (as vector)
|
|
169
|
+
const localAxis = parentWorldRotInv.rotateVec(chainRotationAxisNorm).normalize();
|
|
170
|
+
// Calculate angle between vectors
|
|
171
|
+
const dot = Math.max(-1.0, Math.min(1.0, chainTargetVector.dot(chainIkVector)));
|
|
172
|
+
const angle = Math.min(rotationConstraint * (linkIdx + 1), Math.acos(dot));
|
|
173
|
+
// Create rotation quaternion from axis and angle
|
|
174
|
+
// q = (sin(angle/2) * axis, cos(angle/2))
|
|
175
|
+
const halfAngle = angle * 0.5;
|
|
176
|
+
const sinHalf = Math.sin(halfAngle);
|
|
177
|
+
const cosHalf = Math.cos(halfAngle);
|
|
178
|
+
const rotationFromAxis = new Quat(localAxis.x * sinHalf, localAxis.y * sinHalf, localAxis.z * sinHalf, cosHalf).normalize();
|
|
179
|
+
// Get accumulated ikRotation for this bone (or identity if first time)
|
|
180
|
+
let accumulatedIkRot = this.ikRotations.get(jointBoneIdx) || new Quat(0, 0, 0, 1);
|
|
181
|
+
// Accumulate rotation: ikRotation = rotationFromAxis * ikRotation
|
|
182
|
+
// Reference: ikRotation.multiplyToRef(chainBone.ikChainInfo!.ikRotation, chainBone.ikChainInfo!.ikRotation)
|
|
183
|
+
// This means: ikRotation = rotationFromAxis * accumulatedIkRot
|
|
184
|
+
accumulatedIkRot = rotationFromAxis.multiply(accumulatedIkRot);
|
|
185
|
+
this.ikRotations.set(jointBoneIdx, accumulatedIkRot);
|
|
186
|
+
// Get current local rotation
|
|
187
|
+
const qi = jointBoneIdx * 4;
|
|
188
|
+
const currentLocalRot = new Quat(localRotations[qi], localRotations[qi + 1], localRotations[qi + 2], localRotations[qi + 3]);
|
|
189
|
+
// Reference: ikRotation.multiplyToRef(chainBone.ikChainInfo!.localRotation, ikRotation)
|
|
190
|
+
// This means: tempRot = accumulatedIkRot * currentLocalRot
|
|
191
|
+
let tempRot = accumulatedIkRot.multiply(currentLocalRot);
|
|
192
|
+
// Apply angle constraints if specified (on the combined rotation)
|
|
193
|
+
if (link.hasLimit && link.minAngle && link.maxAngle) {
|
|
194
|
+
tempRot = this.applyAngleConstraints(tempRot, link.minAngle, link.maxAngle);
|
|
195
|
+
}
|
|
196
|
+
// Reference: ikRotation.multiplyToRef(invertedLocalRotation, ikRotation)
|
|
197
|
+
// This means: accumulatedIkRot = tempRot * currentLocalRot^-1
|
|
198
|
+
// But we need the new local rotation, not the accumulated IK rotation
|
|
199
|
+
// The new local rotation should be: newLocalRot such that accumulatedIkRot * newLocalRot = tempRot
|
|
200
|
+
// So: newLocalRot = accumulatedIkRot^-1 * tempRot
|
|
201
|
+
// But wait, the reference updates ikRotation, not localRotation directly...
|
|
202
|
+
// Actually, looking at the reference, it seems like ikRotation is used to compute the final rotation
|
|
203
|
+
// Let me try a different approach: the reference applies constraints to (ikRotation * localRotation)
|
|
204
|
+
// then extracts the new ikRotation, but we need the new localRotation
|
|
205
|
+
// Actually, I think the issue is that we should apply: newLocalRot = tempRot (the constrained result)
|
|
206
|
+
// But we need to extract what the new local rotation should be
|
|
207
|
+
// If tempRot = accumulatedIkRot * currentLocalRot (after constraints)
|
|
208
|
+
// Then: newLocalRot = accumulatedIkRot^-1 * tempRot
|
|
209
|
+
const accumulatedIkRotInv = accumulatedIkRot.conjugate();
|
|
210
|
+
let newLocalRot = accumulatedIkRotInv.multiply(tempRot);
|
|
211
|
+
// Update local rotation
|
|
212
|
+
const normalized = newLocalRot.normalize();
|
|
213
|
+
localRotations[qi] = normalized.x;
|
|
214
|
+
localRotations[qi + 1] = normalized.y;
|
|
215
|
+
localRotations[qi + 2] = normalized.z;
|
|
216
|
+
localRotations[qi + 3] = normalized.w;
|
|
217
|
+
// Update accumulated IK rotation as per reference
|
|
218
|
+
const localRotInv = currentLocalRot.conjugate();
|
|
219
|
+
accumulatedIkRot = tempRot.multiply(localRotInv);
|
|
220
|
+
this.ikRotations.set(jointBoneIdx, accumulatedIkRot);
|
|
221
|
+
// Update world matrices after this link adjustment (only once per link, not per bone)
|
|
222
|
+
if (this.computeWorldMatricesCallback) {
|
|
223
|
+
this.computeWorldMatricesCallback();
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
|
|
227
|
+
}
|
|
228
|
+
// Update effector position for next link
|
|
229
|
+
const updatedEffectorMat2 = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
|
|
230
|
+
effectorPos = updatedEffectorMat2.getPosition();
|
|
231
|
+
// Early exit if converged (check against original target position)
|
|
232
|
+
const currentDistanceSq = effectorPos.subtract(targetPos).lengthSquared();
|
|
233
|
+
if (currentDistanceSq < 1.0e-10) {
|
|
234
|
+
this.convergedChains.add(chain.targetBoneIndex);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// Check convergence at end of iteration
|
|
239
|
+
const finalEffectorMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
|
|
240
|
+
const finalEffectorPos = finalEffectorMat.getPosition();
|
|
241
|
+
const finalDistanceSq = finalEffectorPos.subtract(targetPos).lengthSquared();
|
|
242
|
+
if (finalDistanceSq < 1.0e-10) {
|
|
243
|
+
this.convergedChains.add(chain.targetBoneIndex);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Apply angle constraints to local rotation (Euler angle limits)
|
|
249
|
+
applyAngleConstraints(localRot, minAngle, maxAngle) {
|
|
250
|
+
// Convert quaternion to Euler angles
|
|
251
|
+
const euler = localRot.toEuler();
|
|
252
|
+
// Clamp each Euler angle component
|
|
253
|
+
let clampedX = Math.max(minAngle.x, Math.min(maxAngle.x, euler.x));
|
|
254
|
+
let clampedY = Math.max(minAngle.y, Math.min(maxAngle.y, euler.y));
|
|
255
|
+
let clampedZ = Math.max(minAngle.z, Math.min(maxAngle.z, euler.z));
|
|
256
|
+
// Convert back to quaternion (ZXY order, left-handed)
|
|
257
|
+
return Quat.fromEuler(clampedX, clampedY, clampedZ);
|
|
258
|
+
}
|
|
259
|
+
// Compute world matrices from local rotations and translations
|
|
260
|
+
// This matches Model.computeWorldMatrices logic (including append transforms)
|
|
261
|
+
computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices) {
|
|
262
|
+
const bones = skeleton.bones;
|
|
263
|
+
const boneCount = bones.length;
|
|
264
|
+
const computed = new Array(boneCount).fill(false);
|
|
265
|
+
const computeWorld = (i) => {
|
|
266
|
+
if (computed[i])
|
|
267
|
+
return;
|
|
268
|
+
const bone = bones[i];
|
|
269
|
+
if (bone.parentIndex >= boneCount) {
|
|
270
|
+
console.warn(`[IK] bone ${i} parent out of range: ${bone.parentIndex}`);
|
|
271
|
+
}
|
|
272
|
+
const qi = i * 4;
|
|
273
|
+
const ti = i * 3;
|
|
274
|
+
// Get local rotation
|
|
275
|
+
let rotateM = Mat4.fromQuat(localRotations[qi], localRotations[qi + 1], localRotations[qi + 2], localRotations[qi + 3]);
|
|
276
|
+
let addLocalTx = 0, addLocalTy = 0, addLocalTz = 0;
|
|
277
|
+
// Handle append transforms (same as Model.computeWorldMatrices)
|
|
278
|
+
const appendParentIdx = bone.appendParentIndex;
|
|
279
|
+
const hasAppend = bone.appendRotate && appendParentIdx !== undefined && appendParentIdx >= 0 && appendParentIdx < boneCount;
|
|
280
|
+
if (hasAppend) {
|
|
281
|
+
const ratio = bone.appendRatio === undefined ? 1 : Math.max(-1, Math.min(1, bone.appendRatio));
|
|
282
|
+
const hasRatio = Math.abs(ratio) > 1e-6;
|
|
283
|
+
if (hasRatio) {
|
|
284
|
+
const apQi = appendParentIdx * 4;
|
|
285
|
+
const apTi = appendParentIdx * 3;
|
|
286
|
+
if (bone.appendRotate) {
|
|
287
|
+
let ax = localRotations[apQi];
|
|
288
|
+
let ay = localRotations[apQi + 1];
|
|
289
|
+
let az = localRotations[apQi + 2];
|
|
290
|
+
const aw = localRotations[apQi + 3];
|
|
291
|
+
const absRatio = ratio < 0 ? -ratio : ratio;
|
|
292
|
+
if (ratio < 0) {
|
|
293
|
+
ax = -ax;
|
|
294
|
+
ay = -ay;
|
|
295
|
+
az = -az;
|
|
296
|
+
}
|
|
297
|
+
const identityQuat = new Quat(0, 0, 0, 1);
|
|
298
|
+
const appendQuat = new Quat(ax, ay, az, aw);
|
|
299
|
+
const result = Quat.slerp(identityQuat, appendQuat, absRatio);
|
|
300
|
+
rotateM = Mat4.fromQuat(result.x, result.y, result.z, result.w).multiply(rotateM);
|
|
301
|
+
}
|
|
302
|
+
if (bone.appendMove) {
|
|
303
|
+
const appendRatio = bone.appendRatio ?? 1;
|
|
304
|
+
addLocalTx = localTranslations[apTi] * appendRatio;
|
|
305
|
+
addLocalTy = localTranslations[apTi + 1] * appendRatio;
|
|
306
|
+
addLocalTz = localTranslations[apTi + 2] * appendRatio;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// Get bone's own translation
|
|
311
|
+
const boneTx = localTranslations[ti];
|
|
312
|
+
const boneTy = localTranslations[ti + 1];
|
|
313
|
+
const boneTz = localTranslations[ti + 2];
|
|
314
|
+
// Build local matrix: bindTranslation + rotation + (bone translation + append translation)
|
|
315
|
+
const localM = Mat4.identity().translateInPlace(bone.bindTranslation[0], bone.bindTranslation[1], bone.bindTranslation[2]);
|
|
316
|
+
const transM = Mat4.identity().translateInPlace(boneTx + addLocalTx, boneTy + addLocalTy, boneTz + addLocalTz);
|
|
317
|
+
const localMatrix = localM.multiply(rotateM).multiply(transM);
|
|
318
|
+
const worldOffset = i * 16;
|
|
319
|
+
if (bone.parentIndex >= 0) {
|
|
320
|
+
const p = bone.parentIndex;
|
|
321
|
+
if (!computed[p])
|
|
322
|
+
computeWorld(p);
|
|
323
|
+
const parentOffset = p * 16;
|
|
324
|
+
const parentWorldMat = new Mat4(worldMatrices.subarray(parentOffset, parentOffset + 16));
|
|
325
|
+
const worldMat = parentWorldMat.multiply(localMatrix);
|
|
326
|
+
worldMatrices.set(worldMat.values, worldOffset);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
worldMatrices.set(localMatrix.values, worldOffset);
|
|
330
|
+
}
|
|
331
|
+
computed[i] = true;
|
|
332
|
+
};
|
|
333
|
+
// Process all bones
|
|
334
|
+
for (let i = 0; i < boneCount; i++)
|
|
335
|
+
computeWorld(i);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface PoolSceneConfig {
|
|
2
|
+
cloudSharpness?: number;
|
|
3
|
+
windSpeed?: [number, number];
|
|
4
|
+
bumpFactor?: number;
|
|
5
|
+
bumpDistance?: number;
|
|
6
|
+
skyColor?: [number, number, number];
|
|
7
|
+
moonlightColor?: [number, number, number];
|
|
8
|
+
skyByMoonlightColor?: [number, number, number];
|
|
9
|
+
waterColor?: [number, number, number];
|
|
10
|
+
exposure?: number;
|
|
11
|
+
epsilon?: number;
|
|
12
|
+
marchSteps?: number;
|
|
13
|
+
moonPosition?: [number, number, number];
|
|
14
|
+
}
|
|
15
|
+
export declare class PoolScene {
|
|
16
|
+
private device;
|
|
17
|
+
private pipeline;
|
|
18
|
+
private computePipeline;
|
|
19
|
+
private uniformBuffer;
|
|
20
|
+
private computeUniformBuffer;
|
|
21
|
+
private bindGroup;
|
|
22
|
+
private computeBindGroup;
|
|
23
|
+
private startTime;
|
|
24
|
+
private config;
|
|
25
|
+
private format;
|
|
26
|
+
private sampleCount;
|
|
27
|
+
private cameraUniformBuffer;
|
|
28
|
+
private waterHeightTexture;
|
|
29
|
+
private waterHeightTextureView;
|
|
30
|
+
private waterHeightSampler;
|
|
31
|
+
private waterHeightTextureSize;
|
|
32
|
+
private waterPlanePipeline;
|
|
33
|
+
private waterPlaneVertexBuffer;
|
|
34
|
+
private waterPlaneIndexBuffer;
|
|
35
|
+
private waterPlaneBindGroup;
|
|
36
|
+
private waterPlaneSize;
|
|
37
|
+
private waterPlaneSubdivisions;
|
|
38
|
+
private moonPosition;
|
|
39
|
+
private moonRadius;
|
|
40
|
+
constructor(device: GPUDevice, format: GPUTextureFormat, sampleCount: number, cameraUniformBuffer: GPUBuffer, config?: PoolSceneConfig);
|
|
41
|
+
private createWaterHeightTexture;
|
|
42
|
+
private createComputePipeline;
|
|
43
|
+
private createPipeline;
|
|
44
|
+
private createUniformBuffer;
|
|
45
|
+
dispatchCompute(encoder: GPUCommandEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
|
|
46
|
+
private createWaterPlane;
|
|
47
|
+
private createWaterPlanePipeline;
|
|
48
|
+
renderWaterPlane(pass: GPURenderPassEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
|
|
49
|
+
render(pass: GPURenderPassEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
|
|
50
|
+
getSkyColor(): [number, number, number];
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=pool-scene.d.ts.map
|