-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_waterfall.patch
More file actions
67 lines (58 loc) · 2.92 KB
/
Copy pathpatch_waterfall.patch
File metadata and controls
67 lines (58 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--- src/foliage/waterfall-batcher.ts
+++ src/foliage/waterfall-batcher.ts
@@ -47,6 +47,7 @@
// Splash Attributes
private splashOrigin: THREE.InstancedBufferAttribute | null = null;
private splashVelocity: THREE.InstancedBufferAttribute | null = null; // Stores random velocity params
+ private baseThickness: Float32Array | null = null;
private constructor() {}
@@ -107,6 +108,7 @@
this.mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
this.mesh.count = 0;
this.mesh.frustumCulled = false;
+ this.baseThickness = new Float32Array(MAX_WATERFALLS);
// 2. Setup Splash Particles
// Using 1 triangle for splash drops to save geometry (billboarded in shader)
@@ -265,6 +267,7 @@
this.idToIndex.set(id, index);
this.indexToId[index] = id;
+ this.baseThickness![index] = width;
// 1. Setup Column
// Cylinder Base: R=1, H=1.
@@ -384,33 +387,17 @@
if (!this.initialized || !this.idToIndex.has(id)) return;
- // Note: We need to know the original scale to modify it.
- // But we didn't store it.
- // We can get it from matrix.
const index = this.idToIndex.get(id)!;
// ⚡ OPTIMIZATION: Bypassed .getMatrixAt() and .decompose() overhead.
- // Scale is encoded in the lengths of the column vectors of the 4x4 matrix.
// We need to change the Z scale. We can modify the 3rd column directly.
const matrixArray = this.mesh!.instanceMatrix.array as Float32Array;
const offset = index * 16;
- // Assuming X and Z were equal initially (circular).
- // Current X scale is the magnitude of the 1st column.
- const m00 = matrixArray[offset + 0], m01 = matrixArray[offset + 1], m02 = matrixArray[offset + 2];
- const scaleX = Math.sqrt(m00 * m00 + m01 * m01 + m02 * m02);
-
- // Current Z scale is the magnitude of the 3rd column.
- const m20 = matrixArray[offset + 8], m21 = matrixArray[offset + 9], m22 = matrixArray[offset + 10];
- const currentScaleZ = Math.sqrt(m20 * m20 + m21 * m21 + m22 * m22);
-
- // Compute target scale
- const targetScaleZ = scaleX * thicknessScale;
-
- // Apply scaling factor to the 3rd column
- if (currentScaleZ > 0.0001) {
- const ratio = targetScaleZ / currentScaleZ;
- matrixArray[offset + 8] *= ratio;
- matrixArray[offset + 9] *= ratio;
- matrixArray[offset + 10] *= ratio;
- }
+ // ⚡ OPTIMIZATION: Tracked base thickness in Float32Array to avoid Math.sqrt() overhead.
+ // Since rotation is strictly identity at creation, Z scale corresponds exactly to m22 (offset + 10).
+ const targetScaleZ = this.baseThickness![index] * thicknessScale;
+
+ // Avoid math/ratios and overwrite Z-scale directly.
+ matrixArray[offset + 10] = targetScaleZ;
this.mesh!.instanceMatrix.needsUpdate = true;