@objectifthunes/react-three-book 0.1.0

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.
Files changed (3) hide show
  1. package/dist/index.d.ts +1266 -0
  2. package/dist/index.js +2880 -0
  3. package/package.json +35 -0
package/dist/index.js ADDED
@@ -0,0 +1,2880 @@
1
+ import { jsxs as se, jsx as ie } from "react/jsx-runtime";
2
+ import { useRef as ut, useState as $t, useEffect as Tt, createContext as ne, useContext as Yt, forwardRef as re, useImperativeHandle as oe, useCallback as rt, useMemo as ae } from "react";
3
+ import { useFrame as Kt, useThree as he } from "@react-three/fiber";
4
+ import * as c from "three";
5
+ class pt {
6
+ constructor() {
7
+ this.m_PositionX = 0, this.m_PositionZ = 0, this.m_DirectionX = 0, this.m_DirectionZ = 0, this.m_EulerY = 0, this.m_Radius = 0;
8
+ }
9
+ // ── Properties ────────────────────────────────────────────────────────
10
+ get position() {
11
+ return new c.Vector3(this.m_PositionX, 0, this.m_PositionZ);
12
+ }
13
+ set position(t) {
14
+ this.m_PositionX = t.x, this.m_PositionZ = t.z;
15
+ }
16
+ set direction(t) {
17
+ this.m_DirectionX = t.x, this.m_DirectionZ = t.z, this.m_EulerY = Math.atan2(this.m_DirectionX, this.m_DirectionZ) * (180 / Math.PI);
18
+ }
19
+ set radius(t) {
20
+ this.m_Radius = t;
21
+ }
22
+ // ── Public API ────────────────────────────────────────────────────────
23
+ rollPoint(t) {
24
+ return this.roll(t.clone()).sub(this.getOffset(t.clone()));
25
+ }
26
+ // ── Private helpers ───────────────────────────────────────────────────
27
+ /**
28
+ * Apply Quaternion.Euler(0, eulerY, z) * Vector3(x, 0, 0) manually.
29
+ *
30
+ * Unity Euler convention (ZXY intrinsic, i.e. applied Z then X then Y):
31
+ * 1. Rotate (x, 0, 0) by Z degrees around Z-axis.
32
+ * 2. Rotate by X degrees around X-axis (X = 0 here, so skip).
33
+ * 3. Rotate by Y degrees around Y-axis.
34
+ */
35
+ eulerRotateVector(t, e, s) {
36
+ const i = s * Math.PI / 180, r = e * Math.PI / 180, o = Math.cos(i), a = Math.sin(i), h = t * o, u = t * a, l = 0, d = Math.cos(r), p = Math.sin(r), g = h * d + l * p, m = u, _ = -h * p + l * d;
37
+ return new c.Vector3(g, m, _);
38
+ }
39
+ roll(t) {
40
+ if (this.getSide(t) >= 0) return t;
41
+ const e = this.getClosestPoint(t.clone());
42
+ let s = t.distanceTo(e);
43
+ if (s > Math.PI * this.m_Radius) {
44
+ s = s - Math.PI * this.m_Radius;
45
+ const i = this.eulerRotateVector(-s, this.m_EulerY, 0);
46
+ t.copy(i.add(e)), t.y += this.m_Radius * 2;
47
+ } else {
48
+ const i = 180 / Math.PI * (s / this.m_Radius) - 90, r = this.eulerRotateVector(this.m_Radius, this.m_EulerY, i);
49
+ t.copy(r.add(e)), t.y += this.m_Radius;
50
+ }
51
+ return t;
52
+ }
53
+ getOffset(t) {
54
+ t.x = 0;
55
+ const e = this.roll(t.clone());
56
+ return e.z -= t.z, e;
57
+ }
58
+ getClosestPoint(t) {
59
+ const e = t.x - this.m_PositionX, s = t.z - this.m_PositionZ, i = e * this.m_DirectionX + s * this.m_DirectionZ;
60
+ return t.x = this.m_PositionX + this.m_DirectionX * i, t.z = this.m_PositionZ + this.m_DirectionZ * i, t;
61
+ }
62
+ getSide(t) {
63
+ const e = t.x - this.m_PositionX;
64
+ return (t.z - this.m_PositionZ) * this.m_DirectionX - e * this.m_DirectionZ;
65
+ }
66
+ // ── Value-type clone ──────────────────────────────────────────────────
67
+ clone() {
68
+ const t = new pt();
69
+ return t.m_PositionX = this.m_PositionX, t.m_PositionZ = this.m_PositionZ, t.m_DirectionX = this.m_DirectionX, t.m_DirectionZ = this.m_DirectionZ, t.m_EulerY = this.m_EulerY, t.m_Radius = this.m_Radius, t;
70
+ }
71
+ }
72
+ function Pt(n, t, e, s, i = 1 / 0, r = 1 / 60) {
73
+ s = Math.max(s, 1e-4);
74
+ const o = 2 / s, a = o * r, h = 1 / (1 + a + 0.48 * a * a + 0.235 * a * a * a);
75
+ let u = n - t;
76
+ const l = t, d = i * s;
77
+ u = c.MathUtils.clamp(u, -d, d), t = n - u;
78
+ const p = (e + o * u) * r;
79
+ e = (e - o * p) * h;
80
+ let g = t + (u + p) * h;
81
+ return l - n > 0 == g > l && (g = l, e = (g - l) / r), { value: g, velocity: e };
82
+ }
83
+ function le(n, t, e, s, i = 1 / 0, r = 1 / 60) {
84
+ const o = Pt(
85
+ n.x,
86
+ t.x,
87
+ e.x,
88
+ s.x,
89
+ i,
90
+ r
91
+ ), a = Pt(
92
+ n.y,
93
+ t.y,
94
+ e.y,
95
+ s.y,
96
+ i,
97
+ r
98
+ ), h = Pt(
99
+ n.z,
100
+ t.z,
101
+ e.z,
102
+ s.z,
103
+ i,
104
+ r
105
+ );
106
+ return e.set(o.velocity, a.velocity, h.velocity), new c.Vector3(o.value, a.value, h.value);
107
+ }
108
+ function ce(n) {
109
+ return new c.Vector3(n.x, 0, n.y);
110
+ }
111
+ function ue(n) {
112
+ return new c.Vector3(n.x, n.z, 0);
113
+ }
114
+ function Bt(n, t, e) {
115
+ const s = t * t - 4 * n * e;
116
+ if (s < 0)
117
+ return { rootCount: 0, root0: 0, root1: 0 };
118
+ if (s === 0)
119
+ return { rootCount: 1, root0: -t / 2 * n, root1: 0 };
120
+ {
121
+ const i = Math.sqrt(s);
122
+ return {
123
+ rootCount: 2,
124
+ root0: (-t + i) / (2 * n),
125
+ root1: (-t - i) / (2 * n)
126
+ };
127
+ }
128
+ }
129
+ function bt(n, t, e) {
130
+ return me(n, t, e) ? n.clone() : de(
131
+ new c.Vector2(n.x, t.y),
132
+ n,
133
+ t,
134
+ e
135
+ );
136
+ }
137
+ function me(n, t, e) {
138
+ const s = n.x - t.x;
139
+ let i = n.y - t.y;
140
+ return i *= e.x / e.y, Math.sqrt(s * s + i * i) < e.x;
141
+ }
142
+ function de(n, t, e, s) {
143
+ const i = e.x, r = e.y, o = n.x, a = n.y, h = t.x, u = t.y, l = s.x, d = s.y, p = 1 / (l * l), g = 1 / (d * d);
144
+ if (Math.abs(o - h) < 1e-5) {
145
+ if (o >= i - l && o <= i + l) {
146
+ const m = Bt(
147
+ g,
148
+ -2 * g * r,
149
+ p * (o * o - 2 * i * o + i * i) + g * r * r - 1
150
+ );
151
+ if (m.rootCount === 1)
152
+ return new c.Vector2(o, m.root0);
153
+ if (m.rootCount === 2) {
154
+ let _ = m.root0, f = m.root1;
155
+ return a < u && ([_, f] = [f, _]), new c.Vector2(o, f);
156
+ }
157
+ }
158
+ } else {
159
+ const m = (u - a) / (h - o), _ = a - m * o, f = _ - r, T = Bt(
160
+ p + g * m * m,
161
+ 2 * f * m * g - 2 * i * p,
162
+ i * i * p + f * f * g - 1
163
+ );
164
+ if (T.rootCount === 1)
165
+ return new c.Vector2(T.root0, m * T.root0 + _);
166
+ if (T.rootCount === 2) {
167
+ let b = T.root0, x = T.root1;
168
+ return o < h && ([b, x] = [x, b]), new c.Vector2(x, m * x + _);
169
+ }
170
+ }
171
+ return n.clone();
172
+ }
173
+ function It(n, t) {
174
+ const e = n.origin.clone().applyMatrix4(t), s = n.origin.clone().add(n.direction).applyMatrix4(t);
175
+ return new c.Ray(e, s.sub(e));
176
+ }
177
+ function _e(n) {
178
+ return new c.Vector4(-n.x, n.y, n.z + n.x, n.w);
179
+ }
180
+ function ge(n) {
181
+ return new c.Vector4(n.x, -n.y, n.z, n.w + n.y);
182
+ }
183
+ var X = /* @__PURE__ */ ((n) => (n[n.LeftToRight = 0] = "LeftToRight", n[n.RightToLeft = 1] = "RightToLeft", n[n.UpToDown = 2] = "UpToDown", n[n.DownToUp = 3] = "DownToUp", n))(X || {});
184
+ const Rt = Math.PI / 180, Vt = 180 / Math.PI, pe = 1192093e-13;
185
+ function ft(n) {
186
+ return n < 0 ? 0 : n > 1 ? 1 : n;
187
+ }
188
+ function zt(n, t, e) {
189
+ return n < t ? t : n > e ? e : n;
190
+ }
191
+ function gt(n, t, e) {
192
+ return n !== t ? ft((e - n) / (t - n)) : 0;
193
+ }
194
+ function Dt(n, t, e) {
195
+ return n + (t - n) * e;
196
+ }
197
+ function At(n, t, e) {
198
+ return n + (t - n) * e;
199
+ }
200
+ function Ft(n, t, e) {
201
+ return e = ft((e - n) / (t - n)), e * e * (3 - 2 * e);
202
+ }
203
+ class fe {
204
+ // ---- Constructor ----
205
+ constructor(t, e, s, i) {
206
+ this.m_UseBackContentForSides = !1, this.m_Prev = null, this.m_Next = null, this.m_NoHole = !1, this.m_Size = new c.Vector2(), this.m_Thickness = 0, this.m_Stiffness = 0, this.m_Margin = 0, this.m_MeshDataType = 1, this.m_Cylinder = new pt(), this.m_IsRolling = !1, this.m_IsAutoTurning = !1, this.m_WorldPlane = new c.Plane(), this.m_StartHandle = new c.Vector3(), this.m_CurrentHandle = new c.Vector3(), this.m_EndHandle = new c.Vector3(), this.m_PrevHandle = new c.Vector3(), this.m_HandleOffset = new c.Vector3(), this.m_HandleVelocity = new c.Vector3(), this.m_HandleVelocities = [], this.m_SubMeshCount = -1, this.m_MinTurningRadius = 0, this.m_TurningRadius = 0, this.m_FallDuration = 0, this.m_FallTime = 0.2, this.m_XTime = 0, this.m_ZTime = 0, this.m_IsTurning = !1, this.m_IsFalling = !1, this.m_IsFallingLeft = !1, this.m_isMeshChanged = !1, this.sizeXOffset = 0, this.m_IsCover = t, this.m_Book = s, this.m_Index = e, this.m_Renderer = i, this.m_Transform = i.transform;
207
+ }
208
+ // ---- Properties (mirrors C#) ----
209
+ get isMeshChanged() {
210
+ return this.m_isMeshChanged;
211
+ }
212
+ set isMeshChanged(t) {
213
+ this.m_isMeshChanged = t;
214
+ }
215
+ get isCover() {
216
+ return this.m_IsCover;
217
+ }
218
+ get index() {
219
+ return this.m_Index;
220
+ }
221
+ get transform() {
222
+ return this.m_Transform;
223
+ }
224
+ get renderer() {
225
+ return this.m_Renderer;
226
+ }
227
+ get meshData() {
228
+ return this.m_MeshData;
229
+ }
230
+ get size() {
231
+ return this.m_Size;
232
+ }
233
+ set size(t) {
234
+ this.m_Size.copy(t);
235
+ }
236
+ get thickness() {
237
+ return this.m_Thickness;
238
+ }
239
+ get margin() {
240
+ return this.m_Margin;
241
+ }
242
+ get zTime() {
243
+ return this.m_IsFalling || this.m_IsTurning ? this.m_Transform.scale.x === -1 ? 1 - this.m_ZTime : this.m_ZTime : this.m_Transform.scale.x === -1 ? 1 : 0;
244
+ }
245
+ get direction() {
246
+ const e = this.zTime * 180 * Rt, s = Math.cos(e), i = Math.sin(e);
247
+ return new c.Vector3(-s, -i, 0);
248
+ }
249
+ get isTurning() {
250
+ return this.m_IsTurning;
251
+ }
252
+ get isFalling() {
253
+ return this.m_IsFalling;
254
+ }
255
+ get isFlipped() {
256
+ return this.m_Transform.scale.x === -1;
257
+ }
258
+ get isOnRightStack() {
259
+ return this.m_IsFalling ? this.m_Transform.scale.x === -1 ? this.m_IsFallingLeft : !this.m_IsFallingLeft : this.m_Transform.scale.x !== -1;
260
+ }
261
+ get frontContent() {
262
+ return this.m_FrontContent;
263
+ }
264
+ get backContent() {
265
+ return this.m_BackContent;
266
+ }
267
+ get currentContent() {
268
+ return this.isOnRightStack ? this.m_FrontContent : this.m_BackContent;
269
+ }
270
+ get needHole() {
271
+ if (this.m_NoHole || this.m_Prev === null || this.m_Next === null || this.m_IsTurning || this.m_IsAutoTurning || this.m_IsFalling || this.m_Prev.isCover || this.m_Next.isCover) return !1;
272
+ const t = this.m_Prev.isOnRightStack, e = this.isOnRightStack, s = this.m_Next.isOnRightStack;
273
+ return t === e && e === s;
274
+ }
275
+ set prev(t) {
276
+ this.m_Prev = t;
277
+ }
278
+ set next(t) {
279
+ this.m_Next = t;
280
+ }
281
+ set noHole(t) {
282
+ this.m_NoHole = t;
283
+ }
284
+ get isIdle() {
285
+ return !this.m_IsFalling && !this.m_IsTurning;
286
+ }
287
+ get isInMiddelOfStack() {
288
+ return this.m_Prev === null || this.m_Next === null ? !1 : this.isIdle && this.m_Prev.isIdle && this.m_Next.isIdle && this.m_Prev.isOnRightStack === this.m_Next.isOnRightStack;
289
+ }
290
+ // ---- Methods (faithful port, same order as C#) ----
291
+ setTime(t) {
292
+ this.m_XTime = t, this.m_ZTime = t;
293
+ }
294
+ setMeshData(t, e, s) {
295
+ this.m_MeshDataType = this.needHole && e != null ? 2 : 1, this.m_MeshData = this.m_MeshDataType === 2 ? e : t, this.m_LowpolyMeshData = t, this.m_LowpolyHoleMeshData = e, this.m_HighpolyMeshDataPool = s, this.m_Renderer.mesh = this.m_MeshData.geometry, this.m_Renderer.castShadows = this.m_Book.castShadows && !this.m_Book.reduceShadows, this.updateMaterials(), this.m_isMeshChanged = !0;
296
+ }
297
+ restState(t) {
298
+ this.m_IsTurning = !1, this.m_IsFalling = !1, this.m_IsAutoTurning = !1, this.m_Transform.scale.set(t ? 1 : -1, 1, 1), this.m_Book.bound.resetPaperPosition(this);
299
+ }
300
+ restMesh() {
301
+ this.switchMeshData(
302
+ this.needHole ? 2 : 1
303
+ /* Lowpoly */
304
+ ), this.updateMaterials();
305
+ }
306
+ setMaterialData(t) {
307
+ this.m_MaterialData = t;
308
+ }
309
+ setPaperSetup(t) {
310
+ this.m_Size.set(t.size.x, t.size.y), this.m_Thickness = t.thickness, this.m_Stiffness = t.stiffness, this.m_Margin = t.margin, this.m_UVMargin = t.uvMargin;
311
+ }
312
+ setContentData(t, e, s = !1) {
313
+ this.m_FrontContent = t, this.m_BackContent = e, this.m_UseBackContentForSides = s;
314
+ }
315
+ setMinTurningRadius(t) {
316
+ this.m_MinTurningRadius = t;
317
+ }
318
+ updateTurningRadius(t = 1) {
319
+ let e = Math.max(this.m_Stiffness, 1 - ft(t));
320
+ e <= 0.5 ? this.m_TurningRadius = gt(0, 0.5, e) * this.m_Size.x / Math.PI : this.m_TurningRadius = this.m_Size.x / (Math.max(180 * (1 - gt(0.5, 1, e)), 5) * Rt), this.m_TurningRadius = Math.max(
321
+ this.m_TurningRadius,
322
+ this.m_MinTurningRadius
323
+ );
324
+ }
325
+ startTurning(t) {
326
+ var r, o;
327
+ const e = t.clone();
328
+ t = It(
329
+ t,
330
+ this.m_Transform.matrixWorld.clone().invert()
331
+ ), this.m_WorldPlane.setFromNormalAndCoplanarPoint(
332
+ new c.Vector3(0, 1, 0).applyQuaternion(this.m_Transform.quaternion),
333
+ this.m_Transform.position
334
+ );
335
+ const s = new c.Plane(new c.Vector3(0, 1, 0), 0), i = new c.Vector3();
336
+ if (t.intersectPlane(s, i) !== null) {
337
+ const a = i;
338
+ if (a.x > 0 && a.x < this.m_Size.x && a.z > 0 && a.z < this.m_Size.y) {
339
+ if (this.m_IsRolling = !0, this.m_IsTurning = !0, this.m_IsFalling = !1, this.m_HandleOffset.set(0, 0, 0), this.m_StartHandle.copy(a), this.m_StartHandle.x = this.m_Size.x, this.m_CurrentHandle.copy(this.m_StartHandle), a.x < this.m_Size.x * 0.9) {
340
+ this.m_HandleOffset.set(a.x - this.m_Size.x, 0, 0);
341
+ const h = this.m_Transform.scale;
342
+ h.x *= -1;
343
+ }
344
+ return this.m_HandleVelocity.set(0, 0, 0), this.m_PrevHandle.copy(this.m_CurrentHandle), this.m_HandleVelocities.length = 0, this.switchMeshData(
345
+ 0
346
+ /* Highpoly */
347
+ ), (r = this.m_Prev) == null || r.trySwitchMeshData(1), (o = this.m_Next) == null || o.trySwitchMeshData(1), this.updateTurning(e), this.clampHandle(), this.updateCylinder(), !0;
348
+ }
349
+ }
350
+ return !1;
351
+ }
352
+ stopTurning() {
353
+ this.clampHandle(), this.m_IsTurning = !1, this.m_IsFalling = !0;
354
+ const t = new c.Vector3(0, 0, 0);
355
+ for (const e of this.m_HandleVelocities)
356
+ t.add(e);
357
+ t.divideScalar(this.m_HandleVelocities.length), t.length() > 0.1 ? this.m_IsFallingLeft = t.x < 0 : this.m_IsFallingLeft = this.m_XTime > 0.5 && this.m_ZTime > 0.1, this.m_IsFallingLeft ? this.m_FallTime = 1 - this.m_XTime : this.m_FallTime = this.m_XTime, this.m_FallTime = Dt(0.1, 0.2, this.m_FallTime), this.m_EndHandle.copy(this.m_StartHandle), this.m_IsFallingLeft && (this.m_EndHandle.x = -this.m_Size.x);
358
+ }
359
+ /**
360
+ * Called each frame while the user is dragging.
361
+ * `dt` replaces Time.deltaTime.
362
+ */
363
+ updateTurning(t, e = 1 / 60) {
364
+ const s = new c.Vector3();
365
+ if (t.intersectPlane(this.m_WorldPlane, s) !== null) {
366
+ const i = s;
367
+ this.m_Book.bound.resetPaperPosition(this), this.m_CurrentHandle.copy(
368
+ this.m_Transform.worldToLocal(i.clone())
369
+ ), this.m_CurrentHandle.y = 0, this.m_CurrentHandle.add(this.m_HandleOffset), this.m_HandleVelocity.copy(this.m_CurrentHandle).sub(this.m_PrevHandle).divideScalar(e), this.m_HandleVelocities.length === 5 && this.m_HandleVelocities.shift(), this.m_HandleVelocities.push(this.m_HandleVelocity.clone()), this.m_PrevHandle.copy(this.m_CurrentHandle);
370
+ }
371
+ this.updateBaseVertices();
372
+ }
373
+ /**
374
+ * Called each frame while the page is falling back into place.
375
+ * `dt` replaces Time.deltaTime.
376
+ */
377
+ updateFalling(t = 1 / 60) {
378
+ var s, i;
379
+ let e = !1;
380
+ if (this.m_IsAutoTurning) {
381
+ let r = ft(this.m_FallTime / this.m_FallDuration);
382
+ r = Ft(0, 1, r), r = Ft(0, 1, r), this.m_CurrentHandle.lerpVectors(
383
+ this.m_StartHandle,
384
+ this.m_EndHandle,
385
+ this.m_IsFallingLeft ? r : 1 - r
386
+ ), this.m_FallTime += t, e = Math.abs(r - 1) < pe;
387
+ } else {
388
+ const r = new c.Vector3(
389
+ this.m_FallTime,
390
+ 0,
391
+ this.m_FallTime * 0.75
392
+ );
393
+ this.m_CurrentHandle = le(
394
+ this.m_CurrentHandle,
395
+ this.m_EndHandle,
396
+ this.m_HandleVelocity,
397
+ r,
398
+ 1 / 0,
399
+ t
400
+ ), e = Math.abs(this.m_EndHandle.x - this.m_CurrentHandle.x) < 1e-4;
401
+ }
402
+ if (e) {
403
+ if (this.m_IsFallingLeft) {
404
+ const r = this.m_Transform.scale;
405
+ r.x *= -1, this.m_IsRolling = !1, this.m_IsFallingLeft = !1, this.m_IsFalling = !1, this.switchMeshData(
406
+ 1
407
+ /* Lowpoly */
408
+ ), this.m_ZTime = this.m_Transform.scale.x === -1 ? 1 : 0, this.m_Book.bound.updatePaperPosition(this), this.updateMaterials();
409
+ } else
410
+ this.m_ZTime = this.m_Transform.scale.x === -1 ? 1 : 0, this.m_IsRolling = !1, this.m_IsFallingLeft = !1, this.m_IsFalling = !1, this.switchMeshData(
411
+ 1
412
+ /* Lowpoly */
413
+ ), this.updateMaterials();
414
+ this.isOnRightStack ? (s = this.m_Next) == null || s.trySwitchMeshData(2) : (i = this.m_Prev) == null || i.trySwitchMeshData(2), this.m_IsAutoTurning && this.updateTurningRadius(), this.m_IsAutoTurning = !1;
415
+ return;
416
+ }
417
+ this.updateBaseVertices();
418
+ }
419
+ getTextureCoordinate(t) {
420
+ const e = this.raycastLocal(t, !0);
421
+ return e !== null ? this.hit2UV(e) : new c.Vector2(0, 0);
422
+ }
423
+ raycast(t) {
424
+ const e = {
425
+ point: new c.Vector3(),
426
+ textureCoordinate: new c.Vector2(),
427
+ pageContent: null,
428
+ paperIndex: 0,
429
+ pageIndex: 0
430
+ };
431
+ if (!this.isFalling && !this.isTurning) {
432
+ const s = this.raycastLocal(t);
433
+ if (s !== null)
434
+ return e.pageContent = this.isOnRightStack ? this.m_FrontContent : this.m_BackContent, e.point = this.m_Transform.localToWorld(s.clone()), e.textureCoordinate = this.hit2UV(s), e.paperIndex = this.index, { hit: !0, hitInfo: e };
435
+ }
436
+ return { hit: !1, hitInfo: e };
437
+ }
438
+ hit2UV(t) {
439
+ const e = new c.Vector2(
440
+ gt(-this.sizeXOffset, this.m_Size.x, t.x),
441
+ t.z / this.m_Size.y
442
+ );
443
+ this.m_UVMargin.fixUV(e);
444
+ const s = this.m_Book.direction;
445
+ if (s === X.UpToDown || s === X.DownToUp) {
446
+ const i = e.x;
447
+ e.x = e.y, e.y = i, this.isOnRightStack && (e.y = 1 - e.y);
448
+ } else
449
+ this.isOnRightStack || (e.x = 1 - e.x);
450
+ return e;
451
+ }
452
+ /**
453
+ * Internal raycast against the paper plane in local space.
454
+ * Returns the local-space hit point, or null if no hit / out of bounds.
455
+ */
456
+ raycastLocal(t, e = !1) {
457
+ t = It(
458
+ t,
459
+ this.m_Transform.matrixWorld.clone().invert()
460
+ );
461
+ const s = new c.Plane(new c.Vector3(0, 1, 0), 0), i = new c.Vector3();
462
+ if (t.intersectPlane(s, i) !== null) {
463
+ const r = i;
464
+ if (r.x > 0 && r.x < this.m_Size.x && r.z > 0 && r.z < this.m_Size.y || e)
465
+ return r;
466
+ }
467
+ return null;
468
+ }
469
+ trySwitchMeshData(t) {
470
+ this.m_IsFalling || this.m_IsTurning || (this.switchMeshData(t), this.updateMaterials());
471
+ }
472
+ switchMeshData(t) {
473
+ if (t === 2 && (this.m_LowpolyHoleMeshData == null || this.m_NoHole) && (t = 1), this.m_MeshDataType === t) return;
474
+ this.m_MeshDataType === 0 && this.m_HighpolyMeshDataPool.free(this.m_MeshData);
475
+ let e = this.m_Book.castShadows;
476
+ switch (this.m_Book.reduceShadows && t !== 0 && (e = !1), this.m_Renderer.castShadows = e, this.m_MeshDataType = t, t) {
477
+ case 0:
478
+ this.m_MeshData = this.m_HighpolyMeshDataPool.get();
479
+ break;
480
+ case 1:
481
+ this.m_MeshData = this.m_LowpolyMeshData;
482
+ break;
483
+ case 2:
484
+ this.m_MeshData = this.m_LowpolyHoleMeshData;
485
+ break;
486
+ }
487
+ this.m_Renderer.mesh = this.m_MeshData.geometry, this.m_isMeshChanged = !0;
488
+ }
489
+ updateMaterials() {
490
+ const t = this.m_MeshData.pattern.subMeshCount;
491
+ this.m_SubMeshCount !== t && (this.m_Renderer.setMaterials(
492
+ t === 1 ? this.m_MaterialData.materials1 : this.m_MaterialData.materials3
493
+ ), this.m_SubMeshCount = t);
494
+ const e = this.m_FrontContent.textureST.clone();
495
+ let s = this.m_BackContent.textureST.clone();
496
+ this.m_Book.direction > 1 ? s = ge(s) : s = _e(s);
497
+ const i = this.m_FrontContent.texture, r = this.m_BackContent.texture;
498
+ if (t === 3) {
499
+ let o = 0, a = 1;
500
+ this.m_Transform.scale.x === -1 && (o = 1, a = 0), this.m_MaterialData.updatePropertyBlock(i, e), this.m_Renderer.setPropertyBlock(
501
+ this.m_MaterialData.propertyBlock,
502
+ o
503
+ ), this.m_UseBackContentForSides || this.m_Renderer.setPropertyBlock(
504
+ this.m_MaterialData.propertyBlock,
505
+ 2
506
+ ), this.m_MaterialData.updatePropertyBlock(r, s), this.m_Renderer.setPropertyBlock(
507
+ this.m_MaterialData.propertyBlock,
508
+ a
509
+ ), this.m_UseBackContentForSides && this.m_Renderer.setPropertyBlock(
510
+ this.m_MaterialData.propertyBlock,
511
+ 2
512
+ );
513
+ } else
514
+ this.m_Transform.scale.x === -1 ? this.m_MaterialData.updatePropertyBlock(r, s) : this.m_MaterialData.updatePropertyBlock(i, e), this.m_Renderer.setPropertyBlock(
515
+ this.m_MaterialData.propertyBlock,
516
+ 0
517
+ );
518
+ }
519
+ updateBaseVertices() {
520
+ if (this.clampHandle(), this.updateCylinder(), this.updateTime(), !this.m_IsRolling) return;
521
+ this.m_MeshData.updateBaseVertices();
522
+ const t = this.m_MeshData.baseVertices, e = this.m_Cylinder, s = t.length;
523
+ for (let i = 0; i < s; i++)
524
+ t[i] = e.rollPoint(t[i]);
525
+ }
526
+ updateMesh() {
527
+ this.updateMaterials(), this.m_MeshData.updateMesh(), this.m_isMeshChanged = !0;
528
+ }
529
+ getDirection(t) {
530
+ let e = new c.Vector3(0, 0, t), s = new c.Vector3(0.1, 0, t);
531
+ return e = this.rollPoint(e), s = this.rollPoint(s), e = this.m_Transform.localToWorld(e), s = this.m_Transform.localToWorld(s), this.m_Transform.parent && (e = this.m_Transform.parent.worldToLocal(e), s = this.m_Transform.parent.worldToLocal(s)), e.sub(s).normalize();
532
+ }
533
+ updateTime() {
534
+ if (this.isTurning || this.isFalling) {
535
+ const t = this.findTime(new c.Vector3(this.m_Size.x, 0, 0)), e = this.findTime(
536
+ new c.Vector3(this.m_Size.x, 0, this.m_Size.y)
537
+ );
538
+ this.m_XTime = Dt(
539
+ Math.min(t, e),
540
+ Math.max(t, e),
541
+ 0.9
542
+ );
543
+ const s = this.m_MeshData.pattern.baseXArray, i = this.m_MeshData.pattern.baseZArray, r = this.rollPoint(new c.Vector3(s[1], 0, 0)), o = this.rollPoint(new c.Vector3(s[2], 0, 0)), a = this.rollPoint(
544
+ new c.Vector3(s[1], 0, i[i.length - 1])
545
+ ), h = this.rollPoint(
546
+ new c.Vector3(s[2], 0, i[i.length - 1])
547
+ ), u = o.clone().sub(r).normalize(), l = h.clone().sub(a).normalize(), d = Vt * Math.atan2(u.y, u.x), p = Vt * Math.atan2(l.y, l.x), g = (d + p) / 2;
548
+ this.m_ZTime = g / 180;
549
+ } else
550
+ this.m_XTime = 0, this.m_ZTime = 0;
551
+ }
552
+ findTime(t) {
553
+ return t = this.rollPoint(t), gt(this.m_Size.x, -this.m_Size.x, t.x);
554
+ }
555
+ clampHandle() {
556
+ this.m_StartHandle.y = 0, this.m_CurrentHandle.y = 0;
557
+ const t = this.m_CurrentHandle.clone(), e = new c.Vector3(0, 0, 0), s = new c.Vector3(0, 0, this.m_Size.y), i = e.distanceTo(this.m_StartHandle), r = s.distanceTo(this.m_StartHandle), o = Math.max(i - this.m_TurningRadius, 0.01), a = Math.max(r - this.m_TurningRadius, 0.01), h = this.m_StartHandle.z, u = new c.Vector2(
558
+ 0,
559
+ h + (e.z - h) * (o / i)
560
+ ), l = new c.Vector2(
561
+ 0,
562
+ h + (s.z - h) * (a / r)
563
+ ), d = new c.Vector2(i, o), p = new c.Vector2(r, a);
564
+ t.x = zt(t.x, -this.m_Size.x, this.m_Size.x);
565
+ const g = ue(t), m = bt(
566
+ new c.Vector2(g.x, g.y),
567
+ u,
568
+ d
569
+ ), _ = bt(
570
+ new c.Vector2(m.x, m.y),
571
+ l,
572
+ p
573
+ ), f = ce(
574
+ new c.Vector3(_.x, _.y, 0)
575
+ );
576
+ this.m_CurrentHandle.copy(f);
577
+ }
578
+ updateCylinder() {
579
+ const t = this.m_StartHandle.clone(), e = this.m_CurrentHandle.clone(), s = t.clone().sub(e).normalize();
580
+ s.length() === 0 && s.set(1, 0, 0);
581
+ const i = t.clone().sub(
582
+ s.clone().multiplyScalar(
583
+ this.m_Size.x * 2 + this.m_TurningRadius * Math.PI
584
+ )
585
+ ), r = t.clone(), o = new pt();
586
+ o.radius = this.m_TurningRadius, o.direction = new c.Vector3(
587
+ -s.z,
588
+ 0,
589
+ s.x
590
+ );
591
+ for (let a = 0; a < 100; a++) {
592
+ o.position = i.clone().add(r).multiplyScalar(0.5), this.m_Cylinder = o, this.m_Book.bound.updatePaperPosition(this);
593
+ const h = o.rollPoint(t.clone());
594
+ if (Math.abs(e.x - h.x) < 1e-4) break;
595
+ h.x > e.x ? r.copy(o.position) : i.copy(o.position);
596
+ }
597
+ }
598
+ rollPoint(t) {
599
+ return this.m_IsRolling ? this.m_Cylinder.rollPoint(t) : t;
600
+ }
601
+ drawWireframe(t) {
602
+ }
603
+ startAutoTurning(t, e, s, i) {
604
+ var u, l;
605
+ if (this.updateTurningRadius(s), this.m_PrevHandle.copy(this.m_CurrentHandle), this.m_IsRolling = !0, this.m_HandleOffset.set(0, 0, 0), t === 0) {
606
+ const d = this.m_Transform.scale;
607
+ d.x *= -1;
608
+ }
609
+ this.switchMeshData(
610
+ 0
611
+ /* Highpoly */
612
+ ), (u = this.m_Prev) == null || u.trySwitchMeshData(1), (l = this.m_Next) == null || l.trySwitchMeshData(1), this.m_IsFallingLeft = t === 1, this.m_IsTurning = !1, this.m_IsFalling = !0, this.m_FallTime = 0, this.m_FallDuration = i;
613
+ const r = this.m_Size.x, o = this.m_Size.y;
614
+ e = zt(e, -0.99, 0.99);
615
+ const a = At(0.5, 1, e), h = At(0.5, 0, e);
616
+ this.m_StartHandle.set(r, 0, o * a), this.m_EndHandle.set(-r, 0, o * h), this.m_IsAutoTurning = !0;
617
+ }
618
+ // ---- Public getter for the cylinder (used by BookBound etc.) ----
619
+ get cylinder() {
620
+ return this.m_Cylinder;
621
+ }
622
+ }
623
+ const Y = class Y {
624
+ constructor() {
625
+ this.m_Left = 0, this.m_Right = 0, this.m_Down = 0, this.m_Up = 0;
626
+ }
627
+ // ── Properties ────────────────────────────────────────────────────────
628
+ get left() {
629
+ return this.m_Left;
630
+ }
631
+ set left(t) {
632
+ this.m_Left = this.clamp(t);
633
+ }
634
+ get right() {
635
+ return this.m_Right;
636
+ }
637
+ set right(t) {
638
+ this.m_Right = this.clamp(t);
639
+ }
640
+ get down() {
641
+ return this.m_Down;
642
+ }
643
+ set down(t) {
644
+ this.m_Down = this.clamp(t);
645
+ }
646
+ get up() {
647
+ return this.m_Up;
648
+ }
649
+ set up(t) {
650
+ this.m_Up = this.clamp(t);
651
+ }
652
+ // ── Helpers ───────────────────────────────────────────────────────────
653
+ clamp(t) {
654
+ return Math.max(Y.kMin, Math.min(t, Y.kMax));
655
+ }
656
+ // ── Transform ─────────────────────────────────────────────────────────
657
+ /**
658
+ * Returns a new PaperUVMargin with margins remapped according to the
659
+ * book direction.
660
+ */
661
+ transform(t) {
662
+ const e = new Y();
663
+ switch (t) {
664
+ case X.LeftToRight:
665
+ e.m_Left = this.m_Left, e.m_Right = this.m_Right, e.m_Down = this.m_Down, e.m_Up = this.m_Up;
666
+ break;
667
+ case X.RightToLeft:
668
+ e.m_Left = this.m_Right, e.m_Right = this.m_Left, e.m_Down = this.m_Down, e.m_Up = this.m_Up;
669
+ break;
670
+ case X.UpToDown:
671
+ e.m_Left = this.m_Up, e.m_Right = this.m_Down, e.m_Down = this.m_Left, e.m_Up = this.m_Right;
672
+ break;
673
+ case X.DownToUp:
674
+ default:
675
+ e.m_Left = this.m_Down, e.m_Right = this.m_Up, e.m_Down = this.m_Left, e.m_Up = this.m_Right;
676
+ break;
677
+ }
678
+ return e;
679
+ }
680
+ // ── FixUV ─────────────────────────────────────────────────────────────
681
+ /**
682
+ * Remap a UV coordinate so that (0,0) and (1,1) correspond to the
683
+ * content area inside the margins.
684
+ *
685
+ * Unity's `Mathf.InverseLerp(a, b, v)` = `(v - a) / (b - a)` clamped to [0,1].
686
+ */
687
+ fixUV(t) {
688
+ return t.x = Y.inverseLerp(this.m_Left, 1 - this.m_Right, t.x), t.y = Y.inverseLerp(this.m_Down, 1 - this.m_Up, t.y), t;
689
+ }
690
+ static inverseLerp(t, e, s) {
691
+ if (t === e) return 0;
692
+ const i = (s - t) / (e - t);
693
+ return Math.max(0, Math.min(1, i));
694
+ }
695
+ // ── Value-type clone ──────────────────────────────────────────────────
696
+ clone() {
697
+ const t = new Y();
698
+ return t.m_Left = this.m_Left, t.m_Right = this.m_Right, t.m_Down = this.m_Down, t.m_Up = this.m_Up, t;
699
+ }
700
+ };
701
+ Y.kMin = 0, Y.kMax = 0.25;
702
+ let yt = Y;
703
+ const U = class U {
704
+ constructor(t) {
705
+ this.m_Material = null, this.m_Color = new c.Color(1, 1, 1), this.m_Width = 0, this.m_Height = 0, this.m_Thickness = 0, this.m_Stiffness = 0, this.m_Quality = 0, this.m_UVMargin = new yt(), this.margin = 0, this.bookDirection = X.LeftToRight, this.color = (t == null ? void 0 : t.color) ?? new c.Color(1, 1, 1), this.width = (t == null ? void 0 : t.width) ?? U.kMinSize * 2, this.height = (t == null ? void 0 : t.height) ?? U.kMinSize * 2, this.thickness = (t == null ? void 0 : t.thickness) ?? U.kMinThickness * 2, this.stiffness = (t == null ? void 0 : t.stiffness) ?? 0.1, this.quality = (t == null ? void 0 : t.quality) ?? 3, (t == null ? void 0 : t.material) !== void 0 && (this.m_Material = t.material);
706
+ }
707
+ // ── Properties ────────────────────────────────────────────────────────
708
+ get material() {
709
+ return this.m_Material;
710
+ }
711
+ set material(t) {
712
+ this.m_Material = t;
713
+ }
714
+ get color() {
715
+ return this.m_Color;
716
+ }
717
+ set color(t) {
718
+ this.m_Color = t;
719
+ }
720
+ get width() {
721
+ return this.bookDirection > 1 ? this.m_Height : this.m_Width;
722
+ }
723
+ set width(t) {
724
+ this.m_Width = Math.max(t, U.kMinSize);
725
+ }
726
+ get height() {
727
+ return this.bookDirection > 1 ? this.m_Width : this.m_Height;
728
+ }
729
+ set height(t) {
730
+ this.m_Height = Math.max(t, U.kMinSize);
731
+ }
732
+ get thickness() {
733
+ return this.m_Thickness;
734
+ }
735
+ set thickness(t) {
736
+ this.m_Thickness = Math.max(t, U.kMinThickness);
737
+ }
738
+ get stiffness() {
739
+ return this.m_Stiffness;
740
+ }
741
+ set stiffness(t) {
742
+ this.m_Stiffness = Math.max(0, Math.min(1, t));
743
+ }
744
+ get quality() {
745
+ return this.m_Quality;
746
+ }
747
+ set quality(t) {
748
+ this.m_Quality = Math.max(
749
+ U.kMinQuality,
750
+ Math.min(t, U.kMaxQuality)
751
+ );
752
+ }
753
+ get uvMargin() {
754
+ return this.m_UVMargin.transform(this.bookDirection);
755
+ }
756
+ set uvMargin(t) {
757
+ this.m_UVMargin = t;
758
+ }
759
+ get size() {
760
+ return new c.Vector2(this.width, this.height);
761
+ }
762
+ };
763
+ U.kMinSize = 1, U.kMinThickness = 1e-4, U.kMinQuality = 1, U.kMaxQuality = 5;
764
+ let lt = U;
765
+ class Ht {
766
+ constructor(t) {
767
+ this.m_Texture = null, this.m_TextureST = new c.Vector4(1, 1, 0, 0);
768
+ const e = t.material ?? new c.MeshStandardMaterial();
769
+ this.m_Materials1 = [e.clone()], this.m_Materials3 = [e.clone(), e.clone(), e.clone()], this.m_Color = t.color.clone();
770
+ }
771
+ get materials1() {
772
+ return this.m_Materials1;
773
+ }
774
+ get materials3() {
775
+ return this.m_Materials3;
776
+ }
777
+ get color() {
778
+ return this.m_Color;
779
+ }
780
+ get texture() {
781
+ return this.m_Texture;
782
+ }
783
+ get textureST() {
784
+ return this.m_TextureST;
785
+ }
786
+ /**
787
+ * Mirrors Unity's MaterialPropertyBlock: stores color, texture, and textureST.
788
+ * Paper.ts reads this after calling updatePropertyBlock.
789
+ */
790
+ get propertyBlock() {
791
+ return {
792
+ color: this.m_Color.clone(),
793
+ map: this.m_Texture,
794
+ textureST: this.m_TextureST.clone()
795
+ };
796
+ }
797
+ updatePropertyBlock(t, e) {
798
+ this.m_Texture = t, this.m_TextureST.copy(e);
799
+ }
800
+ }
801
+ function Ot(n, t, e, s = new c.Vector3()) {
802
+ let i = t.x - n.x, r = t.y - n.y, o = t.z - n.z, a = 1 / Math.sqrt(i * i + r * r + o * o);
803
+ i *= a, r *= a, o *= a;
804
+ let h = e.x - n.x, u = e.y - n.y, l = e.z - n.z;
805
+ a = 1 / Math.sqrt(h * h + u * u + l * l), h *= a, u *= a, l *= a;
806
+ const d = r * l - o * u, p = o * h - i * l, g = i * u - r * h;
807
+ return a = 1 / Math.sqrt(d * d + p * p + g * g), s.set(d * a, p * a, g * a);
808
+ }
809
+ class it {
810
+ constructor(t, e = !1, s = !1) {
811
+ this.prev = null, this.next = null, this.index = 0, this.value = t, this.hole = e, this.seam = s;
812
+ }
813
+ // ── Navigation properties ─────────────────────────────────────────────
814
+ get prevNoneSeam() {
815
+ return this.prev.seam ? this.prev.prevNoneSeam : this.prev;
816
+ }
817
+ get nextNoneSeam() {
818
+ return this.next.seam ? this.next.nextNoneSeam : this.next;
819
+ }
820
+ get prevNoneHole() {
821
+ return this.prev.hole ? this.prev.prevNoneHole : this.prev;
822
+ }
823
+ get nextNoneHole() {
824
+ return this.next.hole ? this.next.nextNoneHole : this.next;
825
+ }
826
+ // ── Factory helpers ───────────────────────────────────────────────────
827
+ createNext(t, e = !1, s = !1) {
828
+ const i = new it(t, e, s);
829
+ return i.prev = this, this.next = i, i;
830
+ }
831
+ // C# had a double overload — in TS numbers are already IEEE 754 doubles.
832
+ // Keeping a separate method for parity; it simply casts to number.
833
+ createNextFromDouble(t, e = !1, s = !1) {
834
+ const i = new it(t, e, s);
835
+ return i.prev = this, this.next = i, i;
836
+ }
837
+ // ── Insertion (sorted linked-list insert) ─────────────────────────────
838
+ insert(t) {
839
+ return this.value >= t.value || this.next === null ? !1 : this.next.value > t.value ? Math.abs(this.value - t.value) < 1e-4 || Math.abs(this.next.value - t.value) < 1e-4 ? !1 : (this.next.prev = t, t.next = this.next, t.prev = this, this.next = t, t.hole = this.hole, !0) : this.next.insert(t);
840
+ }
841
+ // ── Index assignment (recursive) ──────────────────────────────────────
842
+ updateIndex(t) {
843
+ this.index = t, this.next !== null && this.next.updateIndex(t + 1);
844
+ }
845
+ getValues(t) {
846
+ if (t === void 0) {
847
+ const e = [];
848
+ return this._getValuesRecursive(e), e;
849
+ }
850
+ this._getValuesRecursive(t);
851
+ }
852
+ _getValuesRecursive(t) {
853
+ t.push(this.value), this.next !== null && this.next._getValuesRecursive(t);
854
+ }
855
+ getHoles(t) {
856
+ if (t === void 0) {
857
+ const e = [];
858
+ return this._getHolesRecursive(e), e;
859
+ }
860
+ this._getHolesRecursive(t);
861
+ }
862
+ _getHolesRecursive(t) {
863
+ t.push(this.hole), this.next !== null && this.next._getHolesRecursive(t);
864
+ }
865
+ }
866
+ class wt {
867
+ constructor(t, e, s, i) {
868
+ this.active = !0, this.prevIndex = t, this.index = e, this.nextIndex = s, this.time = i;
869
+ }
870
+ clone() {
871
+ const t = new wt(this.prevIndex, this.index, this.nextIndex, this.time);
872
+ return t.active = this.active, t;
873
+ }
874
+ }
875
+ class St {
876
+ constructor(t, e, s, i, r, o = !0) {
877
+ this.startX = t, this.startZ = e, this.endX = s, this.endZ = i, this.flip = r, this.left = o;
878
+ }
879
+ clone() {
880
+ return new St(
881
+ this.startX,
882
+ this.startZ,
883
+ this.endX,
884
+ this.endZ,
885
+ this.flip,
886
+ this.left
887
+ );
888
+ }
889
+ }
890
+ class Me {
891
+ constructor(t, e, s) {
892
+ const i = t.size;
893
+ this.leftNode = new it(e.left * i.x, s, !0), this.rightNode = new it((1 - e.right) * i.x, s, !0), this.downNode = new it(e.down * i.y, s, !0), this.upNode = new it((1 - e.up) * i.y, s, !0);
894
+ }
895
+ insert(t, e, s, i) {
896
+ t.insert(this.leftNode) && s.push(this.leftNode), t.insert(this.rightNode) && s.push(this.rightNode), e.insert(this.downNode) && i.push(this.downNode), e.insert(this.upNode) && i.push(this.upNode);
897
+ }
898
+ }
899
+ function mt(n, t, e) {
900
+ if (n === t) return 0;
901
+ const s = (e - n) / (t - n);
902
+ return Math.max(0, Math.min(1, s));
903
+ }
904
+ function Zt(n, t) {
905
+ for (const e of n) {
906
+ const s = e.prevNoneSeam, i = e.nextNoneSeam, r = mt(s.value, i.value, e.value), o = new wt(s.index, e.index, i.index, r);
907
+ t.push(o);
908
+ }
909
+ }
910
+ function Nt(n, t, e, s, i) {
911
+ for (const r of n) {
912
+ if (!r.active) return;
913
+ for (let o = 0; o < s; o++) {
914
+ const a = r.prevIndex, h = r.nextIndex, u = t[o * e + a], l = t[o * e + h], d = t[o * e + r.index];
915
+ i ? Jt(u, l, r.time, d) : d.copy(u).lerp(l, r.time);
916
+ }
917
+ }
918
+ }
919
+ function Lt(n, t, e, s, i) {
920
+ for (const r of n) {
921
+ if (!r.active) return;
922
+ for (let o = 0; o < e; o++) {
923
+ const a = r.prevIndex, h = r.nextIndex, u = t[a * e + o], l = t[h * e + o], d = t[r.index * e + o];
924
+ i ? Jt(u, l, r.time, d) : d.copy(u).lerp(l, r.time);
925
+ }
926
+ }
927
+ }
928
+ function Jt(n, t, e, s = new c.Vector3()) {
929
+ const i = Math.sqrt(n.x * n.x + n.y * n.y + n.z * n.z), r = Math.sqrt(t.x * t.x + t.y * t.y + t.z * t.z);
930
+ if (i === 0 || r === 0)
931
+ return s.copy(n).lerp(t, e);
932
+ const o = n.x / i, a = n.y / i, h = n.z / i, u = t.x / r, l = t.y / r, d = t.z / r;
933
+ let p = o * u + a * l + h * d;
934
+ p = Math.max(-1, Math.min(1, p));
935
+ const g = Math.acos(p);
936
+ if (g < 1e-6)
937
+ return s.copy(n).lerp(t, e);
938
+ const m = Math.sin(g), _ = Math.sin((1 - e) * g) / m, f = Math.sin(e * g) / m, T = i + (r - i) * e;
939
+ return s.set(
940
+ (o * _ + u * f) * T,
941
+ (a * _ + l * f) * T,
942
+ (h * _ + d * f) * T
943
+ );
944
+ }
945
+ function xe(n, t, e, s, i) {
946
+ for (const r of n) {
947
+ const o = (r.endX - r.startX + 1) * 2, a = (r.endZ - r.startZ + 1) * 2, h = e.length;
948
+ for (let l = 0, d = r.endX - r.startX; l < d; l++) {
949
+ const p = h + l * 2 + 0, g = h + l * 2 + 1, m = h + l * 2 + 2, _ = h + l * 2 + 3;
950
+ r.flip ? Et(t, p, g, m, _, o) : Ut(t, p, g, m, _, o);
951
+ }
952
+ if (r.left)
953
+ for (let l = 0, d = r.endZ - r.startZ; l < d; l++) {
954
+ const p = h + l * 2 + 0 + o * 2, g = h + l * 2 + 1 + o * 2, m = h + l * 2 + 2 + o * 2, _ = h + l * 2 + 3 + o * 2;
955
+ r.flip ? Et(t, p, g, m, _, a) : Ut(t, p, g, m, _, a);
956
+ }
957
+ else
958
+ for (let l = 0, d = r.endZ - r.startZ; l < d; l++) {
959
+ const p = h + l * 2 + 0 + o * 2 + a, g = h + l * 2 + 1 + o * 2 + a, m = h + l * 2 + 2 + o * 2 + a, _ = h + l * 2 + 3 + o * 2 + a;
960
+ r.flip ? xt(t, p, g, m, _) : Mt(t, p, g, m, _);
961
+ }
962
+ const u = s * i;
963
+ for (let l = r.startX; l <= r.endX; l++) {
964
+ const d = r.startZ * s + l;
965
+ e.push(e[d].clone()), e.push(e[d + u].clone());
966
+ }
967
+ for (let l = r.startX; l <= r.endX; l++) {
968
+ const d = l + r.endZ * s;
969
+ e.push(e[d + u].clone()), e.push(e[d].clone());
970
+ }
971
+ for (let l = r.startZ; l <= r.endZ; l++) {
972
+ const d = r.startX + l * s;
973
+ e.push(e[d + u].clone()), e.push(e[d].clone());
974
+ }
975
+ for (let l = r.startZ; l <= r.endZ; l++) {
976
+ const d = l * s + r.endX;
977
+ e.push(e[d].clone()), e.push(e[d + u].clone());
978
+ }
979
+ }
980
+ }
981
+ function Ce(n, t, e, s, i) {
982
+ const r = s * i;
983
+ let o = r * 2;
984
+ const a = new c.Vector3();
985
+ for (const h of n) {
986
+ for (let u = h.startX; u <= h.endX; u++) {
987
+ const l = u + h.startZ * s, d = t[l], p = t[l + s];
988
+ a.subVectors(d, p).normalize(), h.flip && a.multiplyScalar(-1), e[o].copy(a), t[o++].copy(d), e[o].copy(a), t[o++].copy(t[l + r]);
989
+ }
990
+ for (let u = h.startX; u <= h.endX; u++) {
991
+ const l = u + h.endZ * s, d = t[l + r], p = t[l + r - s];
992
+ a.subVectors(d, p).normalize(), h.flip && a.multiplyScalar(-1), e[o].copy(a), t[o++].copy(d), e[o].copy(a), t[o++].copy(t[l]);
993
+ }
994
+ for (let u = h.startZ; u <= h.endZ; u++) {
995
+ const l = u * s + h.startX, d = t[l + r], p = t[l + r + 1];
996
+ a.subVectors(d, p).normalize(), h.flip && a.multiplyScalar(-1), e[o].copy(a), t[o++].copy(d), e[o].copy(a), t[o++].copy(t[l]);
997
+ }
998
+ for (let u = h.startZ; u <= h.endZ; u++) {
999
+ const l = u * s + h.endX, d = t[l], p = t[l - 1];
1000
+ a.subVectors(d, p).normalize(), h.flip && a.multiplyScalar(-1), e[o].copy(a), t[o++].copy(d), e[o].copy(a), t[o++].copy(t[l + r]);
1001
+ }
1002
+ }
1003
+ }
1004
+ function Ut(n, t, e, s, i, r) {
1005
+ Mt(n, t, e, s, i), t += r, e += r, s += r, i += r, Mt(n, t, e, s, i);
1006
+ }
1007
+ function Et(n, t, e, s, i, r) {
1008
+ xt(n, t, e, s, i), t += r, e += r, s += r, i += r, xt(n, t, e, s, i);
1009
+ }
1010
+ function Pe(n, t, e, s, i, r, o) {
1011
+ Mt(n, e, s, i, r), e += o, s += o, i += o, r += o, xt(t, e, s, i, r);
1012
+ }
1013
+ function ye(n, t, e, s, i, r) {
1014
+ const o = i.left * s.x, a = (1 - i.right) * s.x, h = i.down * s.y, u = (1 - i.up) * s.y, l = t.length, d = e.length;
1015
+ if (r > 1)
1016
+ for (let g = 0; g < d; g++)
1017
+ for (let m = 0; m < l; m++) {
1018
+ const _ = mt(a, o, t[m]), f = mt(u, h, e[g]);
1019
+ n.push(new c.Vector2(f, _));
1020
+ }
1021
+ else
1022
+ for (let g = 0; g < d; g++)
1023
+ for (let m = 0; m < l; m++) {
1024
+ const _ = mt(o, a, t[m]), f = mt(u, h, e[g]);
1025
+ n.push(new c.Vector2(_, f));
1026
+ }
1027
+ const p = n.length;
1028
+ for (let g = 0; g < p; g++)
1029
+ n.push(n[g].clone());
1030
+ }
1031
+ function Mt(n, t, e, s, i) {
1032
+ n.push(t), n.push(s), n.push(e), n.push(e), n.push(s), n.push(i);
1033
+ }
1034
+ function xt(n, t, e, s, i) {
1035
+ n.push(t), n.push(e), n.push(s), n.push(e), n.push(i), n.push(s);
1036
+ }
1037
+ function Te(n, t, e, s) {
1038
+ const i = [];
1039
+ for (let r = 0; r < e - 1; r++)
1040
+ for (let o = 0; o < t - 1; o++) {
1041
+ let a = n[r * t + o].clone(), h = n[r * t + o + 1].clone(), u = n[(r + 1) * t + o].clone(), l = n[(r + 1) * t + o + 1].clone();
1042
+ a.applyMatrix4(s), h.applyMatrix4(s), u.applyMatrix4(s), l.applyMatrix4(s), i.push([a, h, u, l]);
1043
+ }
1044
+ return i;
1045
+ }
1046
+ class Xt {
1047
+ get geometry() {
1048
+ return this.m_Geometry;
1049
+ }
1050
+ get pattern() {
1051
+ return this.m_Pattern;
1052
+ }
1053
+ get baseVertices() {
1054
+ return this.m_BaseVertices;
1055
+ }
1056
+ constructor(t, e) {
1057
+ this.m_Pattern = e, this.m_BaseVertices = new Array(e.baseVertexCount);
1058
+ for (let o = 0; o < e.baseVertexCount; o++)
1059
+ this.m_BaseVertices[o] = new c.Vector3();
1060
+ this.m_Vertices = new Array(e.vertexCount);
1061
+ for (let o = 0; o < e.vertexCount; o++)
1062
+ this.m_Vertices[o] = new c.Vector3();
1063
+ this.m_Normals = new Array(e.vertexCount);
1064
+ for (let o = 0; o < e.vertexCount; o++)
1065
+ this.m_Normals[o] = new c.Vector3();
1066
+ if (this.m_Geometry = t, this.m_Pattern.subMeshCount === 1) {
1067
+ const o = new Uint32Array(e.triangles);
1068
+ this.m_Geometry.setIndex(new c.BufferAttribute(o, 1)), this.m_Geometry.addGroup(0, e.triangles.length, 0);
1069
+ } else {
1070
+ const o = e.frontTriangles.length + e.backTriangles.length + e.borderTriangles.length, a = new Uint32Array(o);
1071
+ let h = 0;
1072
+ a.set(e.frontTriangles, h), this.m_Geometry.addGroup(h, e.frontTriangles.length, 0), h += e.frontTriangles.length, a.set(e.backTriangles, h), this.m_Geometry.addGroup(h, e.backTriangles.length, 1), h += e.backTriangles.length, a.set(e.borderTriangles, h), this.m_Geometry.addGroup(h, e.borderTriangles.length, 2), this.m_Geometry.setIndex(new c.BufferAttribute(a, 1));
1073
+ }
1074
+ const s = new Float32Array(e.vertexCount * 3), i = new Float32Array(e.vertexCount * 3), r = new Float32Array(e.vertexCount * 2);
1075
+ for (let o = 0; o < e.texcoords.length; o++)
1076
+ r[o * 2] = e.texcoords[o].x, r[o * 2 + 1] = e.texcoords[o].y;
1077
+ this.m_Geometry.setAttribute(
1078
+ "position",
1079
+ new c.BufferAttribute(s, 3)
1080
+ ), this.m_Geometry.setAttribute(
1081
+ "normal",
1082
+ new c.BufferAttribute(i, 3)
1083
+ ), this.m_Geometry.setAttribute(
1084
+ "uv",
1085
+ new c.BufferAttribute(r, 2)
1086
+ ), this.updateBaseVertices();
1087
+ }
1088
+ // ── UpdateBaseVertices ──────────────────────────────────────────────────
1089
+ /**
1090
+ * Recalculates base vertex positions from the pattern's X/Z arrays
1091
+ * and offset.
1092
+ *
1093
+ * Ported from lines ~2167-2183.
1094
+ */
1095
+ updateBaseVertices() {
1096
+ const t = this.m_BaseVertices, e = this.m_Pattern.baseXArray, s = this.m_Pattern.baseZArray, i = this.m_Pattern.baseXOffset, r = e.length, o = s.length;
1097
+ let a = 0;
1098
+ for (let h = 0; h < o; h++)
1099
+ for (let u = 0; u < r; u++)
1100
+ t[a++].set(e[u] + i, 0, s[h]);
1101
+ }
1102
+ // ── UpdateMesh ─────────────────────────────────────────────────────────
1103
+ /**
1104
+ * Recomputes all vertex positions and normals.
1105
+ *
1106
+ * Algorithm:
1107
+ * 1. Clear normals for all base vertices.
1108
+ * 2. Interpolate seam positions in X and Z.
1109
+ * 3. Accumulate face normals per quad onto each vertex.
1110
+ * 4. Normalise by dividing by weight and re-normalising.
1111
+ * 5. Interpolate seam normals (with slerp).
1112
+ * 6. Offset front/back by +/- halfThickness along normal.
1113
+ * 7. Update border vertex positions and normals.
1114
+ * 8. Push data into BufferGeometry attributes.
1115
+ *
1116
+ * Ported from lines ~2185-2255.
1117
+ */
1118
+ updateMesh() {
1119
+ const t = this.m_BaseVertices, e = this.m_Vertices, s = this.m_Normals, i = this.m_Pattern.weights, r = this.m_Pattern.baseXArray.length, o = this.m_Pattern.baseZArray.length, a = this.m_Pattern.baseVertexCount, h = new c.Vector3(), u = new c.Vector3(), l = new c.Vector3();
1120
+ for (let m = 0; m < a; m++)
1121
+ s[m].set(0, 0, 0);
1122
+ Nt(this.m_Pattern.xSeams, t, r, o, !1), Lt(this.m_Pattern.zSeams, t, r, o, !1);
1123
+ for (let m = 0; m < o - 1; m++) {
1124
+ const _ = m + 1;
1125
+ for (let f = 0; f < r - 1; f++) {
1126
+ const T = f + 1, b = m * r + f, x = m * r + T, P = _ * r + f, S = _ * r + T, k = t[b], O = t[x], Z = t[P], z = t[S];
1127
+ Ot(k, Z, O, h), Ot(z, O, Z, u), l.addVectors(h, u), s[b].add(l), s[x].add(l), s[P].add(l), s[S].add(l);
1128
+ }
1129
+ }
1130
+ for (let m = 0; m < a; m++)
1131
+ s[m].divideScalar(i[m]).normalize();
1132
+ Nt(this.m_Pattern.xSeams, s, r, o, !0), Lt(this.m_Pattern.zSeams, s, r, o, !0);
1133
+ const d = this.m_Pattern.thickness / 2;
1134
+ for (let m = 0; m < a; m++) {
1135
+ const _ = s[m];
1136
+ s[m + a].copy(_).negate();
1137
+ const f = t[m];
1138
+ e[m].copy(f).addScaledVector(_, d), e[m + a].copy(f).addScaledVector(_, -d);
1139
+ }
1140
+ Ce(this.m_Pattern.borders, e, s, r, o);
1141
+ const p = this.m_Geometry.getAttribute(
1142
+ "position"
1143
+ ), g = this.m_Geometry.getAttribute(
1144
+ "normal"
1145
+ );
1146
+ for (let m = 0; m < e.length; m++) {
1147
+ const _ = e[m];
1148
+ p.setXYZ(m, _.x, _.y, _.z);
1149
+ const f = s[m];
1150
+ g.setXYZ(m, f.x, f.y, f.z);
1151
+ }
1152
+ p.needsUpdate = !0, g.needsUpdate = !0, this.m_Geometry.computeBoundingBox(), this.m_Geometry.computeBoundingSphere();
1153
+ }
1154
+ // ── DrawWireframe (debug) ──────────────────────────────────────────────
1155
+ /**
1156
+ * Returns wireframe quad data for debug visualisation.
1157
+ *
1158
+ * Ported from lines ~2257-2262.
1159
+ */
1160
+ drawWireframe(t) {
1161
+ const e = this.m_Pattern.baseXArray.length, s = this.m_Pattern.baseZArray.length;
1162
+ return Te(this.m_Vertices, e, s, t);
1163
+ }
1164
+ }
1165
+ class we {
1166
+ constructor() {
1167
+ this.m_UsedMeshs = [], this.m_FreeMeshs = [], this.m_Meshs = /* @__PURE__ */ new Set();
1168
+ }
1169
+ get() {
1170
+ let t;
1171
+ return this.m_FreeMeshs.length > 0 ? t = this.m_FreeMeshs.pop() : (t = new c.BufferGeometry(), this.m_Meshs.add(t)), this.m_UsedMeshs.push(t), t;
1172
+ }
1173
+ recycle() {
1174
+ for (const t of this.m_UsedMeshs)
1175
+ t.deleteAttribute("position"), t.deleteAttribute("normal"), t.deleteAttribute("uv"), t.setIndex(null), t.clearGroups(), this.m_FreeMeshs.push(t);
1176
+ this.m_UsedMeshs.length = 0;
1177
+ }
1178
+ destroy() {
1179
+ for (const t of this.m_Meshs)
1180
+ t.dispose();
1181
+ this.m_Meshs.clear();
1182
+ }
1183
+ }
1184
+ class Se {
1185
+ constructor(t) {
1186
+ this.m_CreateMeshCollider = !1, this.m_UsedRenderers = [], this.m_FreeRenderers = [], this.m_Renderers = /* @__PURE__ */ new Set(), this.m_Ids = [], this.m_Root = t;
1187
+ }
1188
+ set createColliders(t) {
1189
+ this.m_CreateMeshCollider = t;
1190
+ }
1191
+ get ids() {
1192
+ return [...this.m_Ids];
1193
+ }
1194
+ get(t) {
1195
+ let e;
1196
+ return this.m_FreeRenderers.length > 0 ? (e = this.m_FreeRenderers.pop(), e.createCollider = this.m_CreateMeshCollider, e.reset(t)) : (e = new ke(this.m_Root, t, this.m_CreateMeshCollider), this.m_Renderers.add(e)), this.m_UsedRenderers.push(e), this.m_Ids.push(e.id), e;
1197
+ }
1198
+ recycle() {
1199
+ for (const t of this.m_UsedRenderers)
1200
+ t.clear(), this.m_FreeRenderers.push(t);
1201
+ this.m_UsedRenderers.length = 0, this.m_Ids.length = 0;
1202
+ }
1203
+ destroy() {
1204
+ for (const t of this.m_Renderers)
1205
+ t.destroy();
1206
+ this.m_Renderers.clear();
1207
+ }
1208
+ getBounds() {
1209
+ const t = new c.Box3();
1210
+ let e = !0;
1211
+ for (const s of this.m_UsedRenderers)
1212
+ e ? (t.copy(s.bounds), e = !1) : t.union(s.bounds);
1213
+ return t;
1214
+ }
1215
+ }
1216
+ let ve = 1;
1217
+ class ke {
1218
+ constructor(t, e, s) {
1219
+ this.m_Visibility = !0, this.m_PropertyBlocks = /* @__PURE__ */ new Map(), this.m_MaterialTextures = /* @__PURE__ */ new Map(), this.m_Id = ve++, this.m_Object3D = new c.Object3D(), this.m_Object3D.name = e, this.m_Mesh = new c.Mesh(
1220
+ new c.BufferGeometry(),
1221
+ new c.MeshStandardMaterial()
1222
+ ), this.m_Object3D.add(this.m_Mesh), t.add(this.m_Object3D), this.m_CreateCollider = s;
1223
+ }
1224
+ get createCollider() {
1225
+ return this.m_CreateCollider;
1226
+ }
1227
+ set createCollider(t) {
1228
+ this.m_CreateCollider = t;
1229
+ }
1230
+ get bounds() {
1231
+ const t = new c.Box3();
1232
+ return this.m_Mesh.geometry && (this.m_Mesh.geometry.computeBoundingBox(), this.m_Mesh.geometry.boundingBox && (t.copy(this.m_Mesh.geometry.boundingBox), t.applyMatrix4(this.m_Mesh.matrixWorld))), t;
1233
+ }
1234
+ get id() {
1235
+ return this.m_Id;
1236
+ }
1237
+ get transform() {
1238
+ return this.m_Object3D;
1239
+ }
1240
+ get visibility() {
1241
+ return this.m_Visibility;
1242
+ }
1243
+ set castShadows(t) {
1244
+ this.m_Mesh.castShadow = t;
1245
+ }
1246
+ set mesh(t) {
1247
+ t && (this.m_Mesh.geometry = t);
1248
+ }
1249
+ get meshObject() {
1250
+ return this.m_Mesh;
1251
+ }
1252
+ setMaterials(t) {
1253
+ this.disposeManagedTextures(), this.disposeCurrentMaterials(), Array.isArray(t) ? t.length === 0 ? this.m_Mesh.material = new c.MeshStandardMaterial() : t.length === 1 ? this.m_Mesh.material = t[0].clone() : this.m_Mesh.material = t.map((e) => e.clone()) : this.m_Mesh.material = t.clone(), this.m_PropertyBlocks.clear();
1254
+ }
1255
+ setPropertyBlock(t, e) {
1256
+ this.m_PropertyBlocks.set(e, t);
1257
+ const s = this.getMaterial(e);
1258
+ s && (this.isMapCapable(s) && (t.map instanceof c.Texture ? this.applyTextureProperty(
1259
+ s,
1260
+ e,
1261
+ t.map,
1262
+ t.textureST
1263
+ ) : this.clearMaterialTexture(e, s)), this.isColorCapable(s) && t.color instanceof c.Color && s.color.copy(t.color));
1264
+ }
1265
+ reset(t) {
1266
+ this.m_Object3D.name = t, this.setVisibility(!0), this.m_Mesh.geometry = new c.BufferGeometry(), this.setMaterials([]);
1267
+ }
1268
+ clear() {
1269
+ this.m_Object3D.name = "", this.m_Mesh.geometry = new c.BufferGeometry(), this.m_PropertyBlocks.clear(), this.setVisibility(!1), this.m_Object3D.position.set(0, 0, 0), this.m_Object3D.rotation.set(0, 0, 0), this.m_Object3D.scale.set(1, 1, 1);
1270
+ }
1271
+ destroy() {
1272
+ this.disposeManagedTextures(), this.disposeCurrentMaterials(), this.m_Mesh.geometry && this.m_Mesh.geometry.dispose(), this.m_Object3D.parent && this.m_Object3D.parent.remove(this.m_Object3D);
1273
+ }
1274
+ setVisibility(t) {
1275
+ this.m_Visibility !== t && (this.m_Visibility = t, this.m_Object3D.visible = t);
1276
+ }
1277
+ updateCollider() {
1278
+ }
1279
+ getMaterial(t) {
1280
+ return Array.isArray(this.m_Mesh.material) ? this.m_Mesh.material[t] ?? null : this.m_Mesh.material;
1281
+ }
1282
+ isMapCapable(t) {
1283
+ return "map" in t;
1284
+ }
1285
+ isColorCapable(t) {
1286
+ return "color" in t && t.color instanceof c.Color;
1287
+ }
1288
+ getSTKey(t) {
1289
+ return t ? `${t.x.toFixed(6)},${t.y.toFixed(6)},${t.z.toFixed(6)},${t.w.toFixed(6)}` : "1,1,0,0";
1290
+ }
1291
+ applyTextureProperty(t, e, s, i) {
1292
+ const r = this.getSTKey(i), o = this.m_MaterialTextures.get(e);
1293
+ if (o && o.source === s && o.stKey === r) {
1294
+ t.map !== o.texture && (t.map = o.texture, t.needsUpdate = !0);
1295
+ return;
1296
+ }
1297
+ this.clearManagedTexture(e);
1298
+ const a = s.clone();
1299
+ a.needsUpdate = !0, i && (a.repeat.set(i.x, i.y), a.offset.set(i.z, i.w), a.wrapS = c.RepeatWrapping, a.wrapT = c.RepeatWrapping), t.map = a, t.needsUpdate = !0, this.m_MaterialTextures.set(e, {
1300
+ source: s,
1301
+ texture: a,
1302
+ stKey: r
1303
+ });
1304
+ }
1305
+ clearMaterialTexture(t, e) {
1306
+ this.clearManagedTexture(t), e.map !== null && (e.map = null, e.needsUpdate = !0);
1307
+ }
1308
+ clearManagedTexture(t) {
1309
+ const e = this.m_MaterialTextures.get(t);
1310
+ e && (e.texture.dispose(), this.m_MaterialTextures.delete(t));
1311
+ }
1312
+ disposeManagedTextures() {
1313
+ for (const t of this.m_MaterialTextures.values())
1314
+ t.texture.dispose();
1315
+ this.m_MaterialTextures.clear();
1316
+ }
1317
+ disposeCurrentMaterials() {
1318
+ const t = Array.isArray(this.m_Mesh.material) ? this.m_Mesh.material : [this.m_Mesh.material];
1319
+ for (const e of t)
1320
+ e.dispose();
1321
+ }
1322
+ }
1323
+ class Be {
1324
+ constructor(t, e, s = !1) {
1325
+ this.m_Stack = [], this.m_SharedData = null, this.m_MeshFactory = t, this.m_Pattern = e, (this.m_UseSharedData = s) && (this.m_SharedData = new Xt(this.m_MeshFactory.get(), this.m_Pattern), this.m_SharedData.updateMesh());
1326
+ }
1327
+ get() {
1328
+ return this.m_UseSharedData ? this.m_SharedData : this.m_Stack.length > 0 ? this.m_Stack.pop() : new Xt(this.m_MeshFactory.get(), this.m_Pattern);
1329
+ }
1330
+ free(t) {
1331
+ if (this.m_UseSharedData) throw new Error("Not implemented");
1332
+ this.m_Stack.push(t);
1333
+ }
1334
+ }
1335
+ var dt = /* @__PURE__ */ ((n) => (n[n.Next = 0] = "Next", n[n.Back = 1] = "Back", n))(dt || {}), be = /* @__PURE__ */ ((n) => (n[n.Surface = 0] = "Surface", n[n.Edge = 1] = "Edge", n))(be || {}), Ie = /* @__PURE__ */ ((n) => (n[n.Constant = 0] = "Constant", n[n.RandomBetweenTwoConstants = 1] = "RandomBetweenTwoConstants", n[n.Curve = 2] = "Curve", n[n.RandomBetweenTwoCurves = 3] = "RandomBetweenTwoCurves", n))(Ie || {}), Re = /* @__PURE__ */ ((n) => (n[n.PaperIndexTime = 0] = "PaperIndexTime", n[n.TurnIndexTime = 1] = "TurnIndexTime", n))(Re || {});
1336
+ class Ve {
1337
+ constructor(t) {
1338
+ this.keys = t ? t.map((e) => ({ time: e.time, value: e.value })) : [];
1339
+ }
1340
+ /**
1341
+ * Evaluate the curve at parameter `t`.
1342
+ * Linear interpolation between keyframes (Unity default is cubic,
1343
+ * but the exact tangent data is not available here).
1344
+ */
1345
+ evaluate(t) {
1346
+ const e = this.keys, s = e.length;
1347
+ if (s === 0) return 0;
1348
+ if (s === 1 || t <= e[0].time) return e[0].value;
1349
+ if (t >= e[s - 1].time) return e[s - 1].value;
1350
+ for (let i = 0; i < s - 1; i++)
1351
+ if (t >= e[i].time && t <= e[i + 1].time) {
1352
+ const r = (t - e[i].time) / (e[i + 1].time - e[i].time);
1353
+ return e[i].value + (e[i + 1].value - e[i].value) * r;
1354
+ }
1355
+ return e[s - 1].value;
1356
+ }
1357
+ }
1358
+ function ot(n, t, e) {
1359
+ return Math.max(t, Math.min(e, n));
1360
+ }
1361
+ function ze(n, t, e) {
1362
+ return n === t ? 0 : ot((e - n) / (t - n), 0, 1);
1363
+ }
1364
+ function jt(n, t) {
1365
+ return n + Math.random() * (t - n);
1366
+ }
1367
+ class at {
1368
+ constructor(t, e) {
1369
+ this.m_Mode = 0, this.m_Constant = 0, this.m_ConstantMin = 0, this.m_ConstantMax = 0, this.m_Curve = null, this.m_CurveMin = null, this.m_CurveMax = null, this.m_CurveTimeMode = 0, t !== void 0 && (e === void 0 ? (this.m_Constant = t, this.m_ConstantMin = t, this.m_ConstantMax = t, this.m_Curve = null, this.m_CurveMin = null, this.m_CurveMax = null, this.m_Mode = 0, this.m_CurveTimeMode = 0) : (this.m_Constant = 0, this.m_ConstantMin = t, this.m_ConstantMax = e, this.m_Curve = null, this.m_CurveMin = null, this.m_CurveMax = null, this.m_Mode = 1, this.m_CurveTimeMode = 0));
1370
+ }
1371
+ // ── Properties ──────────────────────────────────────────────────────────
1372
+ get mode() {
1373
+ return this.m_Mode;
1374
+ }
1375
+ set mode(t) {
1376
+ this.m_Mode = t;
1377
+ }
1378
+ get constant() {
1379
+ return this.m_Constant;
1380
+ }
1381
+ set constant(t) {
1382
+ this.m_Constant = t;
1383
+ }
1384
+ get constantMin() {
1385
+ return this.m_ConstantMin;
1386
+ }
1387
+ set constantMin(t) {
1388
+ this.m_ConstantMin = t;
1389
+ }
1390
+ get constantMax() {
1391
+ return this.m_ConstantMax;
1392
+ }
1393
+ set constantMax(t) {
1394
+ this.m_ConstantMax = t;
1395
+ }
1396
+ get curve() {
1397
+ return this.m_Curve;
1398
+ }
1399
+ set curve(t) {
1400
+ this.m_Curve = t;
1401
+ }
1402
+ get curveMin() {
1403
+ return this.m_CurveMin;
1404
+ }
1405
+ set curveMin(t) {
1406
+ this.m_CurveMin = t;
1407
+ }
1408
+ get curveMax() {
1409
+ return this.m_CurveMax;
1410
+ }
1411
+ set curveMax(t) {
1412
+ this.m_CurveMax = t;
1413
+ }
1414
+ get curveTimeMode() {
1415
+ return this.m_CurveTimeMode;
1416
+ }
1417
+ set curveTimeMode(t) {
1418
+ this.m_CurveTimeMode = t;
1419
+ }
1420
+ /** A value based on a curve. */
1421
+ static fromCurve(t, e) {
1422
+ const s = new at();
1423
+ return s.m_Constant = 0, s.m_ConstantMin = 0, s.m_ConstantMax = 0, s.m_Curve = t, s.m_CurveMin = null, s.m_CurveMax = null, s.m_Mode = 2, s.m_CurveTimeMode = e, s;
1424
+ }
1425
+ /** A random value generated between two curves. */
1426
+ static fromCurveRange(t, e, s) {
1427
+ const i = new at();
1428
+ return i.m_Constant = 0, i.m_ConstantMin = 0, i.m_ConstantMax = 0, i.m_Curve = null, i.m_CurveMin = t, i.m_CurveMax = e, i.m_Mode = 3, i.m_CurveTimeMode = s, i;
1429
+ }
1430
+ // ── Internal API ────────────────────────────────────────────────────────
1431
+ /** @internal */
1432
+ getValue(t, e) {
1433
+ if (this.m_Mode === 0) return this.m_Constant;
1434
+ if (this.m_Mode === 1)
1435
+ return jt(this.m_ConstantMin, this.m_ConstantMax);
1436
+ const s = this.m_CurveTimeMode === 0 ? t : e;
1437
+ if (this.m_Mode === 2)
1438
+ return this.m_Curve.evaluate(s);
1439
+ if (this.m_Mode === 3)
1440
+ return jt(
1441
+ this.m_CurveMin.evaluate(s),
1442
+ this.m_CurveMax.evaluate(s)
1443
+ );
1444
+ throw new Error("AutoTurnSettingMode not implemented");
1445
+ }
1446
+ /** @internal */
1447
+ clampValues(t, e) {
1448
+ return this.m_Constant = ot(this.m_Constant, t, e), this.m_ConstantMin = ot(this.m_ConstantMin, t, e), this.m_ConstantMax = ot(this.m_ConstantMax, t, e), this.m_Curve = this.clampCurve(this.m_Curve, t, e), this.m_CurveMin = this.clampCurve(this.m_CurveMin, t, e), this.m_CurveMax = this.clampCurve(this.m_CurveMax, t, e), this;
1449
+ }
1450
+ clampCurve(t, e, s) {
1451
+ if (this.m_Curve === null || t === null) return null;
1452
+ const i = t.keys.map((h) => ({ time: h.time, value: h.value })), r = i.length;
1453
+ let o = 1 / 0, a = -1 / 0;
1454
+ for (let h = 0; h < r; h++) {
1455
+ const u = i[h].time;
1456
+ o = Math.min(o, u), a = Math.max(a, u);
1457
+ }
1458
+ for (let h = 0; h < r; h++) {
1459
+ let u = i[h].time, l = i[h].value;
1460
+ u = ze(o, a, u), l = ot(l, e, s), i[h].time = u, i[h].value = l;
1461
+ }
1462
+ return new Ve(i);
1463
+ }
1464
+ // ── Value-type clone ────────────────────────────────────────────────────
1465
+ clone() {
1466
+ const t = new at();
1467
+ return t.m_Mode = this.m_Mode, t.m_Constant = this.m_Constant, t.m_ConstantMin = this.m_ConstantMin, t.m_ConstantMax = this.m_ConstantMax, t.m_Curve = this.m_Curve, t.m_CurveMin = this.m_CurveMin, t.m_CurveMax = this.m_CurveMax, t.m_CurveTimeMode = this.m_CurveTimeMode, t;
1468
+ }
1469
+ }
1470
+ const I = class I {
1471
+ constructor() {
1472
+ this.m_Mode = 0, this.m_Twist = new at(), this.m_Bend = new at(1), this.m_Duration = new at(0.5);
1473
+ }
1474
+ // ── Properties ──────────────────────────────────────────────────────────
1475
+ get mode() {
1476
+ return this.m_Mode;
1477
+ }
1478
+ set mode(t) {
1479
+ this.m_Mode = t;
1480
+ }
1481
+ get twist() {
1482
+ return this.m_Twist;
1483
+ }
1484
+ set twist(t) {
1485
+ this.m_Twist = t.clampValues(
1486
+ I.kMinTwist,
1487
+ I.kMaxTwist
1488
+ );
1489
+ }
1490
+ get bend() {
1491
+ return this.m_Bend;
1492
+ }
1493
+ set bend(t) {
1494
+ this.m_Bend = t.clampValues(
1495
+ I.kMinBend,
1496
+ I.kMaxBend
1497
+ );
1498
+ }
1499
+ get duration() {
1500
+ return this.m_Duration;
1501
+ }
1502
+ set duration(t) {
1503
+ this.m_Duration = t.clampValues(
1504
+ I.kMinDuration,
1505
+ I.kMaxDuration
1506
+ );
1507
+ }
1508
+ // ── Internal API ────────────────────────────────────────────────────────
1509
+ /** @internal */
1510
+ getModeValue() {
1511
+ return this.m_Mode;
1512
+ }
1513
+ /** @internal */
1514
+ getBendValue(t, e) {
1515
+ return ot(
1516
+ this.m_Bend.getValue(t, e),
1517
+ I.kMinBend,
1518
+ I.kMaxBend
1519
+ );
1520
+ }
1521
+ /** @internal */
1522
+ getDurationValue(t, e) {
1523
+ return ot(
1524
+ this.m_Duration.getValue(t, e),
1525
+ I.kMinDuration,
1526
+ I.kMaxDuration
1527
+ );
1528
+ }
1529
+ /** @internal */
1530
+ getTwistValue(t, e) {
1531
+ return ot(
1532
+ this.m_Twist.getValue(t, e),
1533
+ I.kMinTwist,
1534
+ I.kMaxTwist
1535
+ );
1536
+ }
1537
+ };
1538
+ I.kMinTwist = -1, I.kMaxTwist = 1, I.kMinBend = 0, I.kMaxBend = 1, I.kMinDuration = 0, I.kMaxDuration = 5;
1539
+ let ct = I, as = class extends Error {
1540
+ constructor() {
1541
+ super(
1542
+ "The book's height exceeds the maximum limit. Please consider using thinner paper, increasing the width of the paper, or reducing the number of pages."
1543
+ );
1544
+ }
1545
+ };
1546
+ var K;
1547
+ let De = (K = class extends c.Group {
1548
+ // ── Constructor ────────────────────────────────────────────────────────
1549
+ constructor(t) {
1550
+ super(), this.m_Content = null, this.m_Binding = null, this.m_InitialOpenProgress = 0, this.m_BuildOnAwake = !0, this.m_CastShadows = !0, this.m_AlignToGround = !1, this.m_HideBinder = !1, this.m_CreateColliders = !1, this.m_ReduceShadows = !1, this.m_ReduceSubMeshes = !1, this.m_ReduceOverdraw = !1, this.m_CoverPaperSetup = new lt({
1551
+ color: new c.Color(1, 0, 0),
1552
+ width: 2.1,
1553
+ height: 3.1,
1554
+ thickness: 0.04,
1555
+ stiffness: 0.5
1556
+ }), this.m_PagePaperSetup = new lt({
1557
+ color: new c.Color(1, 1, 1),
1558
+ width: 2,
1559
+ height: 3,
1560
+ thickness: 0.02,
1561
+ stiffness: 0.2
1562
+ }), this.m_Root = null, this.m_IsBuilt = !1, this.m_HasCover = !1, this.m_RendererFactory = null, this.m_MeshFactory = null, this.m_Bound = null, this.m_Papers = [], this.m_SelectedPaper = null, this.m_Direction = X.LeftToRight, this.m_AutoTurnQueue = [], this.m_AutoTurnTimer = 0, this.m_AutoTurningEndTime = -1, this.m_CurrentTime = 0, this.m_CoverPaperCount = 0, this.m_PagePaperCount = 0, this.m_TotalThickness = 0, this.m_MinPaperWidth = 0, this.m_MinPaperHeight = 0, this.m_MinPaperThickness = 0, this.m_MaxPaperThickness = 0, this.m_RendererIds = [], this.m_WasIdle = !1, t && (t.content && (this.m_Content = t.content), t.binding && (this.m_Binding = t.binding), t.initialOpenProgress !== void 0 && (this.m_InitialOpenProgress = c.MathUtils.clamp(t.initialOpenProgress, 0, 1)), t.buildOnAwake !== void 0 && (this.m_BuildOnAwake = t.buildOnAwake), t.castShadows !== void 0 && (this.m_CastShadows = t.castShadows), t.alignToGround !== void 0 && (this.m_AlignToGround = t.alignToGround), t.hideBinder !== void 0 && (this.m_HideBinder = t.hideBinder), t.createColliders !== void 0 && (this.m_CreateColliders = t.createColliders), t.reduceShadows !== void 0 && (this.m_ReduceShadows = t.reduceShadows), t.reduceSubMeshes !== void 0 && (this.m_ReduceSubMeshes = t.reduceSubMeshes), t.reduceOverdraw !== void 0 && (this.m_ReduceOverdraw = t.reduceOverdraw), t.coverPaperSetup && (this.m_CoverPaperSetup = new lt({
1563
+ color: t.coverPaperSetup.color ?? new c.Color(1, 0, 0),
1564
+ width: t.coverPaperSetup.width ?? 2.1,
1565
+ height: t.coverPaperSetup.height ?? 3.1,
1566
+ thickness: t.coverPaperSetup.thickness ?? 0.04,
1567
+ stiffness: t.coverPaperSetup.stiffness ?? 0.5,
1568
+ material: t.coverPaperSetup.material ?? null
1569
+ })), t.pagePaperSetup && (this.m_PagePaperSetup = new lt({
1570
+ color: t.pagePaperSetup.color ?? new c.Color(1, 1, 1),
1571
+ width: t.pagePaperSetup.width ?? 2,
1572
+ height: t.pagePaperSetup.height ?? 3,
1573
+ thickness: t.pagePaperSetup.thickness ?? 0.02,
1574
+ stiffness: t.pagePaperSetup.stiffness ?? 0.2,
1575
+ material: t.pagePaperSetup.material ?? null
1576
+ }))), K.s_Instances.add(this);
1577
+ }
1578
+ static get instances() {
1579
+ return [...K.s_Instances];
1580
+ }
1581
+ // ── Internal accessors ─────────────────────────────────────────────────
1582
+ get minPaperWidth() {
1583
+ return this.m_MinPaperWidth;
1584
+ }
1585
+ get minPaperHeight() {
1586
+ return this.m_MinPaperHeight;
1587
+ }
1588
+ get minPaperThickness() {
1589
+ return this.m_MinPaperThickness;
1590
+ }
1591
+ get maxPaperThickness() {
1592
+ return this.m_MaxPaperThickness;
1593
+ }
1594
+ get totalThickness() {
1595
+ return this.m_TotalThickness;
1596
+ }
1597
+ get hasCover() {
1598
+ return this.m_HasCover;
1599
+ }
1600
+ get castShadowsFlag() {
1601
+ return this.m_CastShadows;
1602
+ }
1603
+ /** Alias matching IBookOwner interface expected by Paper.ts */
1604
+ get castShadows() {
1605
+ return this.m_CastShadows;
1606
+ }
1607
+ get alignToGround() {
1608
+ return this.m_AlignToGround;
1609
+ }
1610
+ get reduceShadows() {
1611
+ return this.m_ReduceShadows;
1612
+ }
1613
+ get coverPaperSetup() {
1614
+ return this.m_CoverPaperSetup;
1615
+ }
1616
+ get pagePaperSetup() {
1617
+ return this.m_PagePaperSetup;
1618
+ }
1619
+ get bound() {
1620
+ return this.m_Bound;
1621
+ }
1622
+ get papers() {
1623
+ return this.m_Papers;
1624
+ }
1625
+ get rendererIds() {
1626
+ return this.m_RendererIds;
1627
+ }
1628
+ get direction() {
1629
+ return this.m_Direction;
1630
+ }
1631
+ // ── Public API (properties) ────────────────────────────────────────────
1632
+ get binding() {
1633
+ return this.m_Binding;
1634
+ }
1635
+ set binding(t) {
1636
+ this.m_Binding !== t && (this.m_Binding = t, this.clear());
1637
+ }
1638
+ get content() {
1639
+ return this.m_Content;
1640
+ }
1641
+ set content(t) {
1642
+ this.m_Content !== t && (this.m_Content = t, this.clear());
1643
+ }
1644
+ get initialOpenProgress() {
1645
+ return this.m_InitialOpenProgress;
1646
+ }
1647
+ set initialOpenProgress(t) {
1648
+ this.m_InitialOpenProgress = c.MathUtils.clamp(t, 0, 1);
1649
+ }
1650
+ get isBuilt() {
1651
+ return this.m_IsBuilt;
1652
+ }
1653
+ get paperCount() {
1654
+ return this.m_CoverPaperCount + this.m_PagePaperCount;
1655
+ }
1656
+ get coverPaperCount() {
1657
+ return this.m_CoverPaperCount;
1658
+ }
1659
+ get pagePaperCount() {
1660
+ return this.m_PagePaperCount;
1661
+ }
1662
+ get isTurning() {
1663
+ return this.m_SelectedPaper !== null && this.m_SelectedPaper.isTurning;
1664
+ }
1665
+ get isFalling() {
1666
+ for (const t of this.m_Papers)
1667
+ if (t.isFalling) return !0;
1668
+ return !1;
1669
+ }
1670
+ get isIdle() {
1671
+ return !this.isTurning && !this.isFalling && !this.isAutoTurning;
1672
+ }
1673
+ get isAutoTurning() {
1674
+ return this.m_AutoTurningEndTime > this.m_CurrentTime;
1675
+ }
1676
+ get hasPendingAutoTurns() {
1677
+ return this.m_AutoTurnQueue.length > 0;
1678
+ }
1679
+ get autoTurningEndTime() {
1680
+ return this.m_AutoTurningEndTime;
1681
+ }
1682
+ // ── Public API (methods) ───────────────────────────────────────────────
1683
+ startTurning(t) {
1684
+ if (!this.m_IsBuilt || this.isTurning) return !1;
1685
+ const e = this.getFrontPapers();
1686
+ for (const s of e) {
1687
+ const i = s.raycast(t);
1688
+ if (i.hit && i.hitInfo.pageContent.isPointOverUI(i.hitInfo.textureCoordinate))
1689
+ return !1;
1690
+ }
1691
+ for (const s of e)
1692
+ if (!s.isFalling && s.startTurning(t))
1693
+ return this.m_SelectedPaper = s, !0;
1694
+ return !1;
1695
+ }
1696
+ updateTurning(t) {
1697
+ this.m_IsBuilt && this.m_SelectedPaper !== null && this.m_SelectedPaper.updateTurning(t);
1698
+ }
1699
+ stopTurning() {
1700
+ this.m_IsBuilt && this.m_SelectedPaper !== null && (this.m_SelectedPaper.stopTurning(), this.m_SelectedPaper = null);
1701
+ }
1702
+ getActivePaperSideIndices(t) {
1703
+ if (t.length = 0, !this.m_IsBuilt) return;
1704
+ const e = this.m_Content.direction === X.RightToLeft, s = this.m_Papers.length, i = (r, o) => {
1705
+ let a = r * 2;
1706
+ o && a++, e && (a = s * 2 - a - 1), t.includes(a) || t.push(a);
1707
+ };
1708
+ for (let r = 0; r < s; r++) {
1709
+ const o = this.m_Papers[r];
1710
+ if (o.isFalling || o.isTurning)
1711
+ r > 0 && i(r - 1, !0), i(r, !1), i(r, !0), r < s - 1 && i(r + 1, !1);
1712
+ else if (o.isOnRightStack) {
1713
+ r > 0 && i(r - 1, !0), i(r, !1);
1714
+ break;
1715
+ }
1716
+ }
1717
+ t.length === 0 && i(this.m_Papers.length - 1, !0), e && t.reverse();
1718
+ }
1719
+ setOpenProgress(t) {
1720
+ this.cancelPendingAutoTurns(), this.m_Content.direction % 2 !== 0 && (t = 1 - t);
1721
+ const e = this.m_Papers.length, s = Math.round(c.MathUtils.lerp(0, e, t));
1722
+ for (let i = 0; i < e; i++) {
1723
+ const r = i >= s;
1724
+ this.m_Papers[i].restState(r);
1725
+ }
1726
+ for (let i = 0; i < e; i++)
1727
+ this.m_Papers[i].restMesh();
1728
+ this.m_WasIdle = !1;
1729
+ }
1730
+ setOpenProgressByIndex(t) {
1731
+ const e = this.m_Content.convertPaperSideIndexToOpenProgress(t);
1732
+ this.setOpenProgress(e);
1733
+ }
1734
+ // ── Auto Turning ──────────────────────────────────────────────────────
1735
+ startAutoTurning(t, e, s = 1, i = 0) {
1736
+ if (!this.m_IsBuilt) return !1;
1737
+ this.cancelPendingAutoTurns();
1738
+ const r = typeof i == "number" ? new at(i) : i;
1739
+ if (s = Math.min(s, this.getMaxAutoTurnCount(t)), s === 0) return !1;
1740
+ for (let o = 0; o < s && this.canAutoTurn(t); o++) {
1741
+ const a = o / (s - 1 || 1), h = this.getAutoTurnPaperIndexTime(t), u = o > 0 ? r.getValue(h, a) : 0, l = e.getModeValue(), d = e.getTwistValue(h, a), p = e.getBendValue(h, a), g = e.getDurationValue(h, a);
1742
+ this.m_AutoTurnQueue.push({ direction: t, mode: l, twist: d, bend: p, duration: g, delay: u });
1743
+ }
1744
+ return this.m_AutoTurnTimer = 0, !0;
1745
+ }
1746
+ cancelPendingAutoTurns() {
1747
+ this.m_AutoTurnQueue.length = 0;
1748
+ }
1749
+ processAutoTurnQueue(t) {
1750
+ if (this.m_AutoTurnQueue.length === 0 || (this.m_AutoTurnTimer -= t, this.m_AutoTurnTimer > 0)) return;
1751
+ const e = this.m_AutoTurnQueue[0];
1752
+ if (!this.canAutoTurn(e.direction)) {
1753
+ this.m_AutoTurnQueue.length = 0;
1754
+ return;
1755
+ }
1756
+ const s = this.getAutoTurnPaper(e.direction);
1757
+ if (s === null) {
1758
+ this.m_AutoTurnQueue.length = 0;
1759
+ return;
1760
+ }
1761
+ s.startAutoTurning(e.mode, e.twist, e.bend, e.duration), this.m_AutoTurningEndTime = Math.max(
1762
+ this.m_AutoTurningEndTime,
1763
+ this.m_CurrentTime + e.duration
1764
+ ), this.m_AutoTurnQueue.shift(), this.m_AutoTurnQueue.length > 0 && (this.m_AutoTurnTimer = this.m_AutoTurnQueue[0].delay);
1765
+ }
1766
+ getAutoTurnPaper(t) {
1767
+ if (this.isTurning) return null;
1768
+ const e = this.getFrontPapers();
1769
+ if (e.length > 0) {
1770
+ const s = t === dt.Next, i = e[s ? e.length - 1 : 0];
1771
+ if (!i.isTurning && !i.isFalling && s === i.isOnRightStack)
1772
+ return i;
1773
+ }
1774
+ return null;
1775
+ }
1776
+ getMaxAutoTurnCount(t) {
1777
+ const e = this.getAutoTurnPaper(t);
1778
+ if (e === null) return 0;
1779
+ let s = 1;
1780
+ return t === dt.Next ? s += this.m_Papers.length - e.index - 1 : s += e.index, s;
1781
+ }
1782
+ canAutoTurn(t) {
1783
+ return this.getAutoTurnPaper(t) !== null;
1784
+ }
1785
+ getAutoTurnPaperIndexTime(t) {
1786
+ const e = this.getAutoTurnPaper(t);
1787
+ return e === null ? 0 : e.index / (this.m_Papers.length - 1 || 1);
1788
+ }
1789
+ // ── Lifecycle ─────────────────────────────────────────────────────────
1790
+ /**
1791
+ * Initialize the book. Call this after adding to scene.
1792
+ * Equivalent to Unity's Start/Awake.
1793
+ */
1794
+ init() {
1795
+ K.s_Instances.add(this), this.hardClear(), this.m_BuildOnAwake && this.build();
1796
+ }
1797
+ /**
1798
+ * Call every frame with delta time.
1799
+ * Equivalent to Unity's LateUpdate.
1800
+ */
1801
+ update(t) {
1802
+ if (this.m_CurrentTime += t, !!this.m_IsBuilt && this.m_Papers.length !== 0 && this.m_Bound !== null && (this.processAutoTurnQueue(t), !(this.m_WasIdle && this.isIdle))) {
1803
+ this.m_WasIdle = this.isIdle;
1804
+ for (const e of this.m_Papers)
1805
+ e.isFalling && e.updateFalling(t);
1806
+ this.updateLivePages(), this.m_Bound.onLateUpdate();
1807
+ }
1808
+ }
1809
+ dispose() {
1810
+ K.s_Instances.delete(this), this.hardClear();
1811
+ }
1812
+ // ── Build ─────────────────────────────────────────────────────────────
1813
+ build() {
1814
+ if (this.clear(), this.m_Content === null || this.m_Content.isEmpty || this.m_Binding === null) return;
1815
+ if (this.m_MeshFactory === null && (this.m_MeshFactory = new we()), this.m_RendererFactory === null) {
1816
+ const C = new c.Object3D();
1817
+ C.name = "Root", this.add(C), this.m_Root = C, this.m_RendererFactory = new Se(this.m_Root);
1818
+ }
1819
+ this.m_RendererFactory.createColliders = this.m_CreateColliders, this.m_Direction = this.m_Content.direction;
1820
+ const t = this.m_Direction > 1 ? Math.PI / 2 : 0;
1821
+ this.m_Root.rotation.set(0, t, 0), this.m_CoverPaperSetup.bookDirection = this.m_Content.direction, this.m_PagePaperSetup.bookDirection = this.m_Content.direction, this.m_PagePaperSetup.height < this.m_CoverPaperSetup.height ? (this.m_CoverPaperSetup.margin = 0, this.m_PagePaperSetup.margin = (this.m_CoverPaperSetup.height - this.m_PagePaperSetup.height) / 2) : (this.m_CoverPaperSetup.margin = (this.m_PagePaperSetup.height - this.m_CoverPaperSetup.height) / 2, this.m_PagePaperSetup.margin = 0);
1822
+ const e = new Ht(this.m_CoverPaperSetup), s = new Ht(this.m_PagePaperSetup);
1823
+ this.m_Content.init(this);
1824
+ let i = this.m_Content.coverContents, r = this.m_Content.pageContents, o = this.m_InitialOpenProgress;
1825
+ this.m_Content.direction % 2 !== 0 && (i = [...i].reverse(), r = [...r].reverse(), o = 1 - o), this.m_HasCover = i.length > 0, this.m_CoverPaperCount = Math.floor(i.length / 2), this.m_PagePaperCount = Math.floor(r.length / 2);
1826
+ const a = this.m_CoverPaperCount + this.m_PagePaperCount, h = Math.floor(i.length / 4);
1827
+ let u = 0, l = 0, d = 0;
1828
+ this.m_Papers = new Array(a);
1829
+ for (let C = 0; C < a; C++) {
1830
+ const R = this.m_HasCover && (C < h || C >= a - h), L = this.m_RendererFactory.get("Paper"), v = this.m_Papers[C] = new fe(R, C, this, L);
1831
+ v.renderer.castShadows = this.m_CastShadows, C < Math.round(c.MathUtils.lerp(0, a, o)) && (v.transform.scale.set(-1, 1, 1), v.setTime(1)), R ? (v.setContentData(i[l++], i[l++], C > h), v.setMaterialData(e), v.setPaperSetup(this.m_CoverPaperSetup)) : (v.setContentData(r[u++], r[u++]), v.setMaterialData(s), v.setPaperSetup(this.m_PagePaperSetup)), d += v.thickness;
1832
+ }
1833
+ this.m_TotalThickness = d;
1834
+ const p = this.m_Papers[0].thickness, g = this.m_Papers[Math.floor(a / 2)].thickness;
1835
+ this.m_MinPaperThickness = Math.min(p, g), this.m_MaxPaperThickness = Math.max(p, g);
1836
+ const m = this.m_Papers[0].size.x, _ = this.m_Papers[Math.floor(a / 2)].size.x;
1837
+ this.m_MinPaperWidth = Math.min(m, _);
1838
+ const f = this.m_Papers[0].size.y, T = this.m_Papers[Math.floor(a / 2)].size.y;
1839
+ this.m_MinPaperHeight = Math.min(f, T), this.m_Bound = this.m_Binding.createBound(
1840
+ this,
1841
+ this.m_Root,
1842
+ this.m_RendererFactory,
1843
+ this.m_MeshFactory
1844
+ ), this.m_Bound.binderRenderer.setVisibility(!this.m_HideBinder);
1845
+ const b = this.m_ReduceOverdraw && r.length > 0, x = this.m_ReduceSubMeshes, P = this.createPaperMeshDataPool(this.m_PagePaperSetup, !0, x), S = b ? this.createPaperMeshDataPool(this.m_PagePaperSetup, !0, x, !0) : null, k = this.createPaperMeshDataPool(this.m_PagePaperSetup, !1), O = this.createPaperMeshDataPool(this.m_CoverPaperSetup, !0), Z = this.createPaperMeshDataPool(this.m_CoverPaperSetup, !1), z = Math.floor(a / 2) - 1, N = z + 1, D = this.m_Bound.constructor.name.includes("Staple");
1846
+ for (let C = 0; C < a; C++) {
1847
+ const R = this.m_Papers[C];
1848
+ C !== 0 && (R.prev = this.m_Papers[C - 1]), C !== a - 1 && (R.next = this.m_Papers[C + 1]), R.noHole = D && (C === z || C === N), R.isCover ? R.setMeshData(O.get(), null, Z) : R.setMeshData(
1849
+ P.get(),
1850
+ (S == null ? void 0 : S.get()) ?? null,
1851
+ k
1852
+ );
1853
+ }
1854
+ this.m_IsBuilt = !0, this.m_RendererIds = this.m_RendererFactory.ids, this.update(0), this.m_Bound.binderRenderer.updateCollider(), this.m_CoverPaperSetup.bookDirection = X.LeftToRight, this.m_PagePaperSetup.bookDirection = X.LeftToRight;
1855
+ }
1856
+ clear() {
1857
+ return this.m_CoverPaperCount = this.m_PagePaperCount = 0, this.m_IsBuilt = !1, this.m_WasIdle = !1, this.m_Bound = null, this.m_MeshFactory !== null && this.m_MeshFactory.recycle(), this.m_RendererFactory !== null && this.m_RendererFactory.recycle(), this;
1858
+ }
1859
+ hardClear() {
1860
+ this.m_CoverPaperCount = this.m_PagePaperCount = 0, this.m_IsBuilt = !1, this.m_WasIdle = !1, this.m_Bound = null, this.m_Root !== null && (this.remove(this.m_Root), this.m_Root = null), this.m_MeshFactory !== null && (this.m_MeshFactory.destroy(), this.m_MeshFactory = null), this.m_RendererFactory !== null && (this.m_RendererFactory.destroy(), this.m_RendererFactory = null);
1861
+ }
1862
+ createPaperMeshDataPool(t, e, s = !1, i = !1) {
1863
+ const r = e ? 0 : t.quality, o = this.m_Bound.createPaperPattern(
1864
+ r,
1865
+ t.size,
1866
+ t.thickness,
1867
+ t.uvMargin,
1868
+ i,
1869
+ s
1870
+ );
1871
+ return new Be(
1872
+ this.m_MeshFactory,
1873
+ o,
1874
+ e && this.m_Bound.useSharedMeshDataForLowpoly
1875
+ );
1876
+ }
1877
+ // ── Query methods ─────────────────────────────────────────────────────
1878
+ getFrontPapers() {
1879
+ const t = [];
1880
+ if (this.m_SelectedPaper !== null) {
1881
+ const e = this.m_SelectedPaper.index;
1882
+ e > 0 && t.push(this.m_Papers[e - 1]), t.push(this.m_SelectedPaper), e < this.m_Papers.length - 1 && t.push(this.m_Papers[e + 1]);
1883
+ } else {
1884
+ for (let e = 0; e < this.m_Papers.length; e++)
1885
+ if (this.m_Papers[e].isOnRightStack) {
1886
+ e > 0 && t.push(this.m_Papers[e - 1]), t.push(this.m_Papers[e]);
1887
+ break;
1888
+ }
1889
+ t.length === 0 && t.push(this.m_Papers[this.m_Papers.length - 1]);
1890
+ }
1891
+ return t;
1892
+ }
1893
+ getAverageTime() {
1894
+ const t = this.m_Papers.length;
1895
+ let e = 0;
1896
+ for (const s of this.m_Papers)
1897
+ s.updateTime(), e += s.zTime;
1898
+ return e /= t, e;
1899
+ }
1900
+ bookRaycast(t) {
1901
+ const e = this.getFrontPapers();
1902
+ for (const s of e) {
1903
+ const i = s.raycast(t);
1904
+ if (i.hit) return i.hitInfo;
1905
+ }
1906
+ return null;
1907
+ }
1908
+ // ── Live Pages ────────────────────────────────────────────────────────
1909
+ updateLivePages() {
1910
+ const t = /* @__PURE__ */ new Set(), e = this.m_Papers.length;
1911
+ for (let s = 0; s < e; s++) {
1912
+ const i = this.m_Papers[s];
1913
+ if (i.isFalling || i.isTurning)
1914
+ s > 0 && t.add(this.m_Papers[s - 1].backContent), t.add(i.frontContent), t.add(i.backContent), s < e - 1 && t.add(this.m_Papers[s + 1].frontContent);
1915
+ else if (i.isOnRightStack) {
1916
+ s > 0 && t.add(this.m_Papers[s - 1].backContent), t.add(i.frontContent);
1917
+ break;
1918
+ }
1919
+ }
1920
+ t.size === 0 && t.add(this.m_Papers[this.m_Papers.length - 1].backContent);
1921
+ for (const s of this.m_Papers)
1922
+ s.frontContent.setActive(t.has(s.frontContent)), s.backContent.setActive(t.has(s.backContent));
1923
+ }
1924
+ }, K.s_Instances = /* @__PURE__ */ new Set(), K);
1925
+ function Ae(n, t, e) {
1926
+ const s = ut(null), [i, r] = $t(!1);
1927
+ return Tt(() => {
1928
+ const o = new De(n);
1929
+ s.current = o;
1930
+ try {
1931
+ o.init(), r(!0), t == null || t(o);
1932
+ } catch (a) {
1933
+ e == null || e(a);
1934
+ }
1935
+ return () => {
1936
+ o.dispose(), s.current = null, r(!1);
1937
+ };
1938
+ }, []), Kt((o, a) => {
1939
+ var h;
1940
+ (h = s.current) == null || h.update(a);
1941
+ }), { bookRef: s, ready: i };
1942
+ }
1943
+ const vt = ne(null);
1944
+ function Ct() {
1945
+ return Yt(vt);
1946
+ }
1947
+ function ls() {
1948
+ const n = Yt(vt);
1949
+ if (!n)
1950
+ throw new Error("useRequiredBook() must be called inside a <Book> component.");
1951
+ return n;
1952
+ }
1953
+ const cs = re(function({ onBuilt: t, onError: e, children: s, ...i }, r) {
1954
+ const { bookRef: o, ready: a } = Ae(i, t, e);
1955
+ return oe(r, () => o.current, [a]), !a || !o.current ? null : /* @__PURE__ */ se(vt.Provider, { value: o.current, children: [
1956
+ /* @__PURE__ */ ie("primitive", { object: o.current }),
1957
+ s
1958
+ ] });
1959
+ });
1960
+ function Fe(n, { enabled: t = !0, orbitControlsRef: e } = {}) {
1961
+ const { gl: s, camera: i } = he(), r = ut(!1), o = ut(null), a = ut(t);
1962
+ a.current = t, Tt(() => {
1963
+ const h = s.domElement, u = new c.Raycaster(), l = new c.Vector2(), d = (_) => {
1964
+ const f = h.getBoundingClientRect();
1965
+ return l.set(
1966
+ (_.clientX - f.left) / f.width * 2 - 1,
1967
+ -((_.clientY - f.top) / f.height) * 2 + 1
1968
+ ), u.setFromCamera(l, i), u.ray;
1969
+ }, p = (_) => {
1970
+ if (!a.current || _.button !== 0) return;
1971
+ r.current = !0;
1972
+ const f = n.current;
1973
+ f && f.startTurning(d(_)) && (o.current = f, e != null && e.current && (e.current.enabled = !1));
1974
+ }, g = (_) => {
1975
+ !r.current || !o.current || o.current.updateTurning(d(_));
1976
+ }, m = () => {
1977
+ r.current = !1, o.current && (o.current.stopTurning(), o.current = null, e != null && e.current && (e.current.enabled = !0));
1978
+ };
1979
+ return h.addEventListener("pointerdown", p), h.addEventListener("pointermove", g), h.addEventListener("pointerup", m), () => {
1980
+ h.removeEventListener("pointerdown", p), h.removeEventListener("pointermove", g), h.removeEventListener("pointerup", m);
1981
+ };
1982
+ }, [s, i, n, e]);
1983
+ }
1984
+ function us({ bookRef: n, enabled: t = !0, orbitControlsRef: e }) {
1985
+ const s = Ct(), i = ut(null);
1986
+ return i.current = (n == null ? void 0 : n.current) ?? s, Fe(i, { enabled: t, orbitControlsRef: e }), null;
1987
+ }
1988
+ function ms(n) {
1989
+ const t = Ct(), e = rt(
1990
+ (r) => {
1991
+ var o;
1992
+ return (o = (n == null ? void 0 : n.current) ?? t) == null ? void 0 : o.setOpenProgress(r);
1993
+ },
1994
+ [n, t]
1995
+ ), s = rt(
1996
+ (r) => {
1997
+ var o;
1998
+ return (o = (n == null ? void 0 : n.current) ?? t) == null ? void 0 : o.setOpenProgressByIndex(r);
1999
+ },
2000
+ [n, t]
2001
+ ), i = rt(
2002
+ () => {
2003
+ var r;
2004
+ return (r = (n == null ? void 0 : n.current) ?? t) == null ? void 0 : r.stopTurning();
2005
+ },
2006
+ [n, t]
2007
+ );
2008
+ return { setOpenProgress: e, setOpenProgressByIndex: s, stopTurning: i };
2009
+ }
2010
+ function ds(n) {
2011
+ const t = Ct(), e = () => (n == null ? void 0 : n.current) ?? t, s = rt(
2012
+ (h) => {
2013
+ var u;
2014
+ return ((u = e()) == null ? void 0 : u.startAutoTurning(dt.Next, h ?? new ct(), 1)) ?? !1;
2015
+ },
2016
+ [n, t]
2017
+ // eslint-disable-line react-hooks/exhaustive-deps
2018
+ ), i = rt(
2019
+ (h) => {
2020
+ var u;
2021
+ return ((u = e()) == null ? void 0 : u.startAutoTurning(dt.Back, h ?? new ct(), 1)) ?? !1;
2022
+ },
2023
+ [n, t]
2024
+ // eslint-disable-line react-hooks/exhaustive-deps
2025
+ ), r = rt(
2026
+ (h, u) => {
2027
+ const l = e();
2028
+ return l ? l.startAutoTurning(h, u ?? new ct(), l.paperCount) : !1;
2029
+ },
2030
+ [n, t]
2031
+ // eslint-disable-line react-hooks/exhaustive-deps
2032
+ ), o = rt(
2033
+ (h, u = new ct(), l = 1, d = 0) => {
2034
+ var p;
2035
+ return ((p = e()) == null ? void 0 : p.startAutoTurning(h, u, l, d)) ?? !1;
2036
+ },
2037
+ [n, t]
2038
+ // eslint-disable-line react-hooks/exhaustive-deps
2039
+ ), a = rt(
2040
+ () => {
2041
+ var h;
2042
+ return (h = e()) == null ? void 0 : h.cancelPendingAutoTurns();
2043
+ },
2044
+ [n, t]
2045
+ // eslint-disable-line react-hooks/exhaustive-deps
2046
+ );
2047
+ return { turnNext: s, turnPrev: i, turnAll: r, startAutoTurning: o, cancelPendingAutoTurns: a };
2048
+ }
2049
+ const He = {
2050
+ isBuilt: !1,
2051
+ isTurning: !1,
2052
+ isFalling: !1,
2053
+ isIdle: !0,
2054
+ isAutoTurning: !1,
2055
+ hasPendingAutoTurns: !1,
2056
+ paperCount: 0,
2057
+ coverPaperCount: 0,
2058
+ pagePaperCount: 0
2059
+ };
2060
+ function Oe(n) {
2061
+ return {
2062
+ isBuilt: n.isBuilt,
2063
+ isTurning: n.isTurning,
2064
+ isFalling: n.isFalling,
2065
+ isIdle: n.isIdle,
2066
+ isAutoTurning: n.isAutoTurning,
2067
+ hasPendingAutoTurns: n.hasPendingAutoTurns,
2068
+ paperCount: n.paperCount,
2069
+ coverPaperCount: n.coverPaperCount,
2070
+ pagePaperCount: n.pagePaperCount
2071
+ };
2072
+ }
2073
+ function Ze(n, t) {
2074
+ return n.isBuilt === t.isBuilt && n.isTurning === t.isTurning && n.isFalling === t.isFalling && n.isIdle === t.isIdle && n.isAutoTurning === t.isAutoTurning && n.hasPendingAutoTurns === t.hasPendingAutoTurns && n.paperCount === t.paperCount && n.coverPaperCount === t.coverPaperCount && n.pagePaperCount === t.pagePaperCount;
2075
+ }
2076
+ function _s(n) {
2077
+ const t = Ct(), [e, s] = $t(He);
2078
+ return Kt(() => {
2079
+ const i = (n == null ? void 0 : n.current) ?? t;
2080
+ if (!i) return;
2081
+ const r = Oe(i);
2082
+ s((o) => Ze(o, r) ? o : r);
2083
+ }), e;
2084
+ }
2085
+ function Ne(n) {
2086
+ return [...n.covers, ...n.pages].filter((e) => e instanceof c.Texture);
2087
+ }
2088
+ function gs(n, t) {
2089
+ const e = ut([]), s = ae(() => {
2090
+ for (const r of e.current) r.dispose();
2091
+ const i = n();
2092
+ return e.current = Ne(i), i;
2093
+ }, t);
2094
+ return Tt(() => () => {
2095
+ for (const i of e.current) i.dispose();
2096
+ }, []), s;
2097
+ }
2098
+ function Le(n, t, e, s, i, r, o) {
2099
+ const a = t.naturalWidth || t.width, h = t.naturalHeight || t.height;
2100
+ if (a <= 0 || h <= 0) return;
2101
+ if (o === "fill") {
2102
+ n.drawImage(t, e, s, i, r);
2103
+ return;
2104
+ }
2105
+ const u = o === "contain" ? Math.min(i / a, r / h) : Math.max(i / a, r / h), l = a * u, d = h * u;
2106
+ n.drawImage(t, e + (i - l) * 0.5, s + (r - d) * 0.5, l, d);
2107
+ }
2108
+ function ps(n, t, e, s, i) {
2109
+ const r = document.createElement("canvas");
2110
+ r.width = 512, r.height = 512;
2111
+ const o = r.getContext("2d");
2112
+ if (o.fillStyle = n, o.fillRect(0, 0, 512, 512), e) {
2113
+ const h = i ? 0 : 56;
2114
+ Le(o, e, h, h, 512 - h * 2, 512 - h * 2, s);
2115
+ } else
2116
+ o.fillStyle = "#333", o.font = "bold 48px Arial", o.textAlign = "center", o.textBaseline = "middle", o.fillText(t, 256, 256);
2117
+ const a = new c.CanvasTexture(r);
2118
+ return a.colorSpace = c.SRGBColorSpace, a;
2119
+ }
2120
+ async function fs(n) {
2121
+ if (!n) return null;
2122
+ const t = URL.createObjectURL(n), e = new Image();
2123
+ e.decoding = "async", e.src = t;
2124
+ try {
2125
+ return await new Promise((s, i) => {
2126
+ e.onload = () => s(), e.onerror = () => i(new Error(`Failed to decode image: ${n.name}`));
2127
+ }), { image: e, objectUrl: t };
2128
+ } catch (s) {
2129
+ return URL.revokeObjectURL(t), console.error(s), null;
2130
+ }
2131
+ }
2132
+ class Ms {
2133
+ constructor() {
2134
+ this.m_BookContent = null, this.m_IsActive = !1, this.onActiveChangedCallback = null;
2135
+ }
2136
+ get bookContent() {
2137
+ return this.m_BookContent;
2138
+ }
2139
+ get isActive() {
2140
+ return this.m_IsActive;
2141
+ }
2142
+ get textureST() {
2143
+ return new c.Vector4(1, 1, 0, 0);
2144
+ }
2145
+ get isShareable() {
2146
+ return !0;
2147
+ }
2148
+ onInit() {
2149
+ }
2150
+ onActiveChanged() {
2151
+ }
2152
+ onIsPointOverUI(t) {
2153
+ return !1;
2154
+ }
2155
+ isPointOverUI(t) {
2156
+ return this.onIsPointOverUI(t);
2157
+ }
2158
+ init(t) {
2159
+ var e;
2160
+ !this.isShareable && this.m_BookContent !== null && this.m_BookContent !== t && (console.error("The page content is already in use. It can only be assigned to one book content."), (e = this.m_BookContent.book) == null || e.clear()), this.m_BookContent = t, this.m_IsActive = !1, this.onInit();
2161
+ }
2162
+ setActive(t) {
2163
+ var e;
2164
+ this.m_IsActive !== t && (this.m_IsActive = t, this.onActiveChanged(), (e = this.onActiveChangedCallback) == null || e.call(this));
2165
+ }
2166
+ }
2167
+ class qt {
2168
+ constructor(t, e) {
2169
+ this.m_Texture = t, this.m_TextureST = e ?? new c.Vector4(1, 1, 0, 0);
2170
+ }
2171
+ get texture() {
2172
+ return this.m_Texture;
2173
+ }
2174
+ get textureST() {
2175
+ return this.m_Texture ? this.m_TextureST : new c.Vector4(1, 1, 0, 0);
2176
+ }
2177
+ isPointOverUI(t) {
2178
+ return !1;
2179
+ }
2180
+ init(t) {
2181
+ }
2182
+ setActive(t) {
2183
+ }
2184
+ }
2185
+ class xs {
2186
+ constructor() {
2187
+ this.m_Direction = X.LeftToRight, this.m_Covers = [null, null, null, null], this.m_Pages = [null, null, null, null, null, null, null, null], this.m_Book = null;
2188
+ }
2189
+ get coverCount4() {
2190
+ return this.nextMultipleOf4(this.m_Covers.length);
2191
+ }
2192
+ get pageCount4() {
2193
+ return this.nextMultipleOf4(this.m_Pages.length);
2194
+ }
2195
+ get book() {
2196
+ return this.m_Book;
2197
+ }
2198
+ get covers() {
2199
+ return this.m_Covers;
2200
+ }
2201
+ get pages() {
2202
+ return this.m_Pages;
2203
+ }
2204
+ get isEmpty() {
2205
+ return this.m_Covers.length === 0 && this.m_Pages.length === 0;
2206
+ }
2207
+ get direction() {
2208
+ return this.m_Direction;
2209
+ }
2210
+ set direction(t) {
2211
+ this.m_Direction = t;
2212
+ }
2213
+ get coverContents() {
2214
+ return this.getContents(this.m_Covers, !1);
2215
+ }
2216
+ get pageContents() {
2217
+ return this.getContents(this.m_Pages, !1);
2218
+ }
2219
+ getContents(t, e) {
2220
+ const s = t.length;
2221
+ let i = this.nextMultipleOf4(s);
2222
+ e && (i = Math.min(i, 4));
2223
+ const r = new Array(i);
2224
+ for (let o = 0; o < i; o++)
2225
+ r[o] = this.getContent(o < s ? t[o] : null);
2226
+ return r;
2227
+ }
2228
+ nextMultipleOf4(t) {
2229
+ return Math.ceil(t / 4) * 4;
2230
+ }
2231
+ getContent(t) {
2232
+ if (t !== null) {
2233
+ if (t instanceof c.Texture)
2234
+ return new qt(t);
2235
+ if (typeof t.init == "function")
2236
+ return t;
2237
+ }
2238
+ return new qt(null);
2239
+ }
2240
+ init(t) {
2241
+ this.m_Book = t;
2242
+ for (const e of this.coverContents)
2243
+ e.init(this);
2244
+ for (const e of this.pageContents)
2245
+ e.init(this);
2246
+ }
2247
+ // ── Index conversion methods ───────────────────────────────────────────
2248
+ convertCoverIndexToPaperSideIndex(t) {
2249
+ if (this.m_Covers.length === 0)
2250
+ throw new Error("Check covers.length > 0 before calling this method.");
2251
+ return this.m_Pages.length === 0 || t < this.coverCount4 / 2 ? t : t + this.pageCount4;
2252
+ }
2253
+ convertPageIndexToPaperSideIndex(t) {
2254
+ if (this.m_Pages.length === 0)
2255
+ throw new Error("Check pages.length > 0 before calling this method.");
2256
+ return this.m_Covers.length === 0 ? t : t + this.coverCount4 / 2;
2257
+ }
2258
+ convertPaperSideIndexToCoverIndex(t) {
2259
+ if (this.m_Covers.length === 0)
2260
+ throw new Error("Check covers.length > 0 before calling this method.");
2261
+ if (this.isPagePaperSideIndex(t))
2262
+ throw new Error("Check isCoverPaperSideIndex before calling this method.");
2263
+ return t > this.coverCount4 / 2 ? t - this.pageCount4 : t;
2264
+ }
2265
+ convertPaperSideIndexToPageIndex(t) {
2266
+ if (this.m_Pages.length === 0)
2267
+ throw new Error("Check pages.length > 0 before calling this method.");
2268
+ if (this.isCoverPaperSideIndex(t))
2269
+ throw new Error("Check isPagePaperSideIndex before calling this method.");
2270
+ return t - this.coverCount4 / 2;
2271
+ }
2272
+ convertPaperSideIndexToOpenProgress(t) {
2273
+ return t / (this.coverCount4 + this.pageCount4 - 1);
2274
+ }
2275
+ isCoverPaperSideIndex(t) {
2276
+ const e = this.coverCount4 / 2, s = this.pageCount4 / 2;
2277
+ if (e === 0) return !1;
2278
+ if (s === 0) return !0;
2279
+ const i = e, r = e + s * 2 - 1;
2280
+ return t < i || t > r;
2281
+ }
2282
+ isPagePaperSideIndex(t) {
2283
+ return !this.isCoverPaperSideIndex(t);
2284
+ }
2285
+ }
2286
+ class Ue {
2287
+ }
2288
+ class Cs {
2289
+ constructor(t, e) {
2290
+ this.m_Book = t, this.m_Root = e;
2291
+ }
2292
+ }
2293
+ function Gt(n, t, e, s) {
2294
+ const i = 1 - s;
2295
+ return new c.Vector3(
2296
+ i * i * n.x + 2 * i * s * t.x + s * s * e.x,
2297
+ i * i * n.y + 2 * i * s * t.y + s * s * e.y,
2298
+ i * i * n.z + 2 * i * s * t.z + s * s * e.z
2299
+ );
2300
+ }
2301
+ function Wt(n, t) {
2302
+ return n++, n === t ? 0 : n;
2303
+ }
2304
+ function Ee(n, t) {
2305
+ return n === 0 ? t - 1 : n - 1;
2306
+ }
2307
+ class Xe {
2308
+ constructor() {
2309
+ this.baseXArray = [], this.baseZArray = [], this.baseXOffset = 0, this.baseVertexCount = 0, this.xSeams = [], this.zSeams = [], this.xNoneSeamIndexes = [], this.borders = [], this.texcoords = [], this.weights = [], this.triangles = [], this.frontTriangles = [], this.backTriangles = [], this.borderTriangles = [], this.vertexCount = 0, this.subMeshCount = 0, this.size = new c.Vector2(), this.thickness = 0;
2310
+ }
2311
+ }
2312
+ const je = 0.01, qe = 0.1, Ge = 0.04, We = 0.4, Qe = 2, $e = 10, Ye = 0, Ke = 5;
2313
+ function st(n, t, e) {
2314
+ return Math.max(t, Math.min(e, n));
2315
+ }
2316
+ function te(n) {
2317
+ return Math.max(0, Math.min(1, n));
2318
+ }
2319
+ function H(n, t, e) {
2320
+ return n + (t - n) * e;
2321
+ }
2322
+ function Qt(n, t, e) {
2323
+ return n === t ? 0 : te((e - n) / (t - n));
2324
+ }
2325
+ function Je(n, t) {
2326
+ const e = n.clone().normalize();
2327
+ if (e.lengthSq() === 0) return new c.Quaternion();
2328
+ let s = new c.Vector3().crossVectors(t, e);
2329
+ if (s.lengthSq() === 0) {
2330
+ const o = Math.abs(e.y) < 0.999 ? new c.Vector3(0, 1, 0) : new c.Vector3(1, 0, 0);
2331
+ s = new c.Vector3().crossVectors(o, e);
2332
+ }
2333
+ s.normalize();
2334
+ const i = new c.Vector3().crossVectors(e, s).normalize(), r = new c.Matrix4().makeBasis(s, i, e);
2335
+ return new c.Quaternion().setFromRotationMatrix(r);
2336
+ }
2337
+ const et = Math.PI / 180, nt = 180 / Math.PI;
2338
+ class ts {
2339
+ constructor() {
2340
+ this._material = null, this._color = new c.Color(1, 1, 1), this._thickness = 0.05, this._crown = 0.2, this._margin = 0.1, this._count = 4, this._quality = 3;
2341
+ }
2342
+ get material() {
2343
+ return this._material;
2344
+ }
2345
+ set material(t) {
2346
+ this._material = t;
2347
+ }
2348
+ get color() {
2349
+ return this._color;
2350
+ }
2351
+ set color(t) {
2352
+ this._color = t;
2353
+ }
2354
+ get thickness() {
2355
+ return this._thickness;
2356
+ }
2357
+ set thickness(t) {
2358
+ this._thickness = st(t, je, qe);
2359
+ }
2360
+ get margin() {
2361
+ return this._margin;
2362
+ }
2363
+ set margin(t) {
2364
+ this._margin = te(t);
2365
+ }
2366
+ get crown() {
2367
+ return this._crown;
2368
+ }
2369
+ set crown(t) {
2370
+ this._crown = st(t, Ge, We);
2371
+ }
2372
+ get count() {
2373
+ return this._count;
2374
+ }
2375
+ set count(t) {
2376
+ this._count = st(t, Qe, $e);
2377
+ }
2378
+ get quality() {
2379
+ return this._quality;
2380
+ }
2381
+ set quality(t) {
2382
+ this._quality = st(t, Ye, Ke);
2383
+ }
2384
+ }
2385
+ class es extends Error {
2386
+ constructor() {
2387
+ super("Book height is too large relative to paper width."), this.name = "BookHeightException";
2388
+ }
2389
+ }
2390
+ class ss {
2391
+ // ── Constructor ────────────────────────────────────────────────────────
2392
+ constructor(t, e, s, i, r) {
2393
+ if (this.m_StapleMargin = 0, this.m_StapleThickness = 0, this.m_BindingRadius = 0, this.m_BindingMidSpace = 0, this.m_StackHeight = 0, this.m_BindingVertexCount = 0, this.useSharedMeshDataForLowpoly = !1, this._binderRendererAdapter = null, this.m_Book = s, this.m_Root = i, this.m_Quality = t, s.totalThickness * 1.25 > s.minPaperWidth)
2394
+ throw new es();
2395
+ const o = this.m_Book.coverPaperSetup, a = this.m_Book.pagePaperSetup, h = new c.BufferGeometry();
2396
+ this.updateStapleMesh(e, h);
2397
+ const u = r ?? new c.MeshStandardMaterial({
2398
+ color: e.color,
2399
+ metalness: 0.9,
2400
+ roughness: 0.3
2401
+ });
2402
+ this.stapleMesh = new c.Mesh(h, u), this.stapleMesh.name = "Staple", this.stapleMesh.castShadow = this.m_Book.castShadows, i.add(this.stapleMesh), this.stapleMesh.position.set(
2403
+ 0,
2404
+ 0,
2405
+ this.m_StapleMargin + a.margin + o.margin
2406
+ );
2407
+ let l = o.thickness;
2408
+ l = Math.max(this.m_BindingRadius, l);
2409
+ for (const d of this.m_Book.papers) {
2410
+ const p = d.size.clone();
2411
+ p.x -= this.m_BindingRadius, d.sizeXOffset = this.m_BindingRadius, d.size = p, d.setMinTurningRadius(l), d.updateTurningRadius(), this.updatePaperPosition(d);
2412
+ }
2413
+ this.updateRootPosition();
2414
+ }
2415
+ get binderRenderer() {
2416
+ return this._binderRendererAdapter || (this._binderRendererAdapter = new is(this.stapleMesh)), this._binderRendererAdapter;
2417
+ }
2418
+ // ── updateStapleMesh ──────────────────────────────────────────────────
2419
+ updateStapleMesh(t, e) {
2420
+ this.m_StapleThickness = t.thickness, this.m_StackHeight = this.m_Book.totalThickness, this.m_BindingMidSpace = this.m_StapleThickness * 1.75, this.m_BindingRadius = (this.m_StackHeight + this.m_BindingMidSpace) / 2 / Math.sin(45 * et), this.m_StackHeight += this.m_BindingMidSpace;
2421
+ let s = t.crown;
2422
+ s = Math.max(s, this.m_StapleThickness * 4);
2423
+ const i = this.m_StapleThickness * 0.5, r = Math.max(
2424
+ this.m_Book.minPaperHeight / 2 - s - i,
2425
+ i
2426
+ );
2427
+ this.m_StapleMargin = H(i, r, t.margin);
2428
+ const o = this.m_Book.minPaperHeight - this.m_StapleMargin * 2;
2429
+ let a = t.count, h = (o - s * a) / (a - 1);
2430
+ for (h = Math.max(h, 0); h < this.m_StapleThickness * 2 && a > 2; )
2431
+ a--, h = (o - s * a) / (a - 1), h = Math.max(h, 0);
2432
+ const u = t.quality / 5, l = Math.floor(H(4, 20, u)), d = Math.floor(H(4, 10, u)), p = Math.floor(H(3, 10, u)), g = this.m_StapleThickness / 2, m = g * 2.5, _ = 0.9, f = g * 1;
2433
+ let T = s / 2 * _;
2434
+ T = Math.max(T, f * 2);
2435
+ let b = 0;
2436
+ b += this.m_Book.totalThickness / 2, b += g;
2437
+ const x = -(this.m_Book.papers[0].thickness / 2 + g), P = [];
2438
+ {
2439
+ for (let V = 0; V < d; V++) {
2440
+ const E = V / (d - 1), W = H(-90, -180, E) * et, y = new c.Vector3(
2441
+ f * Math.sin(W),
2442
+ 0,
2443
+ f * Math.cos(W)
2444
+ );
2445
+ y.x += f, y.z += f, y.x += x, P.push(y);
2446
+ }
2447
+ const M = new c.Vector3(b, 0, 0), F = new c.Vector3(b + m * 0.75, 0, 0), w = new c.Vector3(b - g * 0.5, 0, T);
2448
+ for (let V = 0; V < p; V++) {
2449
+ const E = V / (p - 1), G = Gt(M, F, w, E);
2450
+ G.x += x, P.push(G);
2451
+ }
2452
+ P.reverse();
2453
+ }
2454
+ const S = new Array(l);
2455
+ for (let M = 0; M < l; M++) {
2456
+ const w = (90 - M * (360 / l)) * et;
2457
+ S[M] = new c.Vector3(Math.cos(w), Math.sin(w), 0), S[M].x *= 0.75;
2458
+ }
2459
+ const k = P.length, O = k * l, Z = new Array(O * 2), z = new Array(O * 2), N = new Array(
2460
+ (O * 2 - 1) * l * 2 * 3
2461
+ );
2462
+ for (let M = 0; M < O * 2; M++)
2463
+ Z[M] = new c.Vector3(), z[M] = new c.Vector3();
2464
+ let D = 0;
2465
+ for (let M = 0; M < k; M++) {
2466
+ const F = P[Ee(M, k)], w = P[M], V = P[Wt(M, k)];
2467
+ let E;
2468
+ if (M === 0)
2469
+ E = new c.Vector3(1, 0, -2).normalize();
2470
+ else if (M === k - 1)
2471
+ E = new c.Vector3(0, 0, 1);
2472
+ else {
2473
+ const J = w.clone().sub(F).normalize(), tt = V.clone().sub(w).normalize();
2474
+ E = J.add(tt).multiplyScalar(0.5);
2475
+ }
2476
+ const G = new c.Vector3(0, 1, 0), W = Je(E, G), y = D;
2477
+ let B = Qt(0, p / 4, M);
2478
+ B = H(0.1, 1, B), B = Math.sqrt(B);
2479
+ for (let J = 0; J < l; J++) {
2480
+ const tt = S[J].clone().applyQuaternion(W).multiplyScalar(B), _t = w.clone().add(tt.clone().multiplyScalar(g));
2481
+ Z[D] = _t, z[D] = tt.clone();
2482
+ const ht = w.clone().add(tt.clone().multiplyScalar(g));
2483
+ ht.z = s - ht.z;
2484
+ const ee = y + l - J - 1, kt = O * 2 - ee - 1;
2485
+ Z[kt] = ht, z[kt] = tt.clone(), D++;
2486
+ }
2487
+ }
2488
+ let C = 0;
2489
+ const R = P.length * 2;
2490
+ for (let M = 0; M < R - 1; M++) {
2491
+ const F = M * l, w = (M + 1) * l;
2492
+ for (let V = 0; V < l; V++) {
2493
+ const E = Wt(V, l), G = F + V, W = F + E, y = w + E, B = w + V;
2494
+ N[C++] = G, N[C++] = B, N[C++] = W, N[C++] = W, N[C++] = B, N[C++] = y;
2495
+ }
2496
+ }
2497
+ const L = [...Z], v = [...z], Q = [...N], q = Z.map((M) => M.clone()), $ = [...N];
2498
+ for (let M = 0; M < a - 1; M++) {
2499
+ const F = q.length;
2500
+ for (let w = 0; w < $.length; w++)
2501
+ $[w] += F;
2502
+ for (let w = 0; w < q.length; w++)
2503
+ q[w].z += h + s;
2504
+ L.push(...q.map((w) => w.clone())), v.push(...z.map((w) => w.clone())), Q.push(...$);
2505
+ }
2506
+ const j = new Float32Array(L.length * 3), A = new Float32Array(v.length * 3);
2507
+ for (let M = 0; M < L.length; M++)
2508
+ j[M * 3] = L[M].x, j[M * 3 + 1] = L[M].y, j[M * 3 + 2] = L[M].z, A[M * 3] = v[M].x, A[M * 3 + 1] = v[M].y, A[M * 3 + 2] = v[M].z;
2509
+ e.setAttribute(
2510
+ "position",
2511
+ new c.BufferAttribute(j, 3)
2512
+ ), e.setAttribute(
2513
+ "normal",
2514
+ new c.BufferAttribute(A, 3)
2515
+ ), e.setIndex(Q);
2516
+ }
2517
+ // ── createPaperPattern ────────────────────────────────────────────────
2518
+ createPaperPattern(t, e, s, i, r, o) {
2519
+ const a = new Xe();
2520
+ a.size = e, a.thickness = s;
2521
+ const h = this.m_BindingRadius;
2522
+ a.baseXOffset = -h;
2523
+ const u = this.m_Quality + 1;
2524
+ this.m_BindingVertexCount = u;
2525
+ const l = t / 5, d = Math.min(e.x, e.y) / 60, p = Math.floor(
2526
+ H(0, (e.x - h) / d, l)
2527
+ ), g = Math.floor(H(0, e.y / d, l));
2528
+ let m = 2 + p + 1 + u, _ = 2 + g;
2529
+ const f = new it(0);
2530
+ let T = f;
2531
+ const b = h / u;
2532
+ let x = 0;
2533
+ for (let y = 1; y < u + 1; y++)
2534
+ T = T.createNext(x), x += b;
2535
+ const P = (e.x - h) / (p + 1);
2536
+ for (let y = u + 1; y < m; y++)
2537
+ T = T.createNext(x), x += P;
2538
+ const S = new it(0);
2539
+ let k = S;
2540
+ const O = e.y / (_ - 1);
2541
+ let Z = 0;
2542
+ for (let y = 0; y < _ - 1; y++)
2543
+ Z += O, k = k.createNext(Z);
2544
+ const z = [], N = [], D = i.clone();
2545
+ if (r) {
2546
+ let y = 0;
2547
+ this.m_Book.hasCover && (y = Math.floor(this.m_Book.coverPaperCount / 2));
2548
+ const B = Math.max(0.01, s);
2549
+ D.left = 0, D.right = (this.getPX(y + 1, s) - this.getPX(y, s) + B) / e.x, D.down = B / e.y, D.up = B / e.y;
2550
+ }
2551
+ const C = new Me(a, D, !1);
2552
+ C.insert(f, S, z, N), f.updateIndex(0), S.updateIndex(0), Zt(z, a.xSeams), Zt(N, a.zSeams);
2553
+ const R = this.m_BindingVertexCount + 2, L = new Array(R);
2554
+ a.xNoneSeamIndexes = L, T = f;
2555
+ for (let y = 0; y < R; y++) {
2556
+ L[y] = T.index;
2557
+ do
2558
+ T = T.next;
2559
+ while (T.seam);
2560
+ }
2561
+ const v = f.getValues(), Q = S.getValues();
2562
+ m = v.length, _ = Q.length;
2563
+ const q = m * _, $ = [], j = new Array(q).fill(0), A = [], M = [], F = [], w = [];
2564
+ ye(
2565
+ $,
2566
+ v,
2567
+ Q,
2568
+ e,
2569
+ i,
2570
+ this.m_Book.direction
2571
+ );
2572
+ const V = C.leftNode.index, E = C.downNode.index;
2573
+ let G = C.rightNode.index, W = C.upNode.index;
2574
+ G === 0 && (G = m - 1), W === 0 && (W = _ - 1);
2575
+ for (let y = 0; y < _ - 1; y++)
2576
+ for (let B = 0; B < m - 1; B++) {
2577
+ if (r && y >= E && y < W && B >= V && B < G)
2578
+ continue;
2579
+ const J = y * m + B, tt = y * m + (B + 1), _t = (y + 1) * m + B, ht = (y + 1) * m + (B + 1);
2580
+ j[J] += 2, j[tt] += 2, j[_t] += 2, j[ht] += 2, Pe(
2581
+ A,
2582
+ M,
2583
+ J,
2584
+ tt,
2585
+ _t,
2586
+ ht,
2587
+ q
2588
+ );
2589
+ }
2590
+ if (w.push(new St(0, 0, m - 1, _ - 1, !1, !1)), xe(w, F, $, m, _), a.baseXArray = v, a.baseZArray = Q, a.baseVertexCount = q, a.vertexCount = $.length, a.texcoords = $, a.weights = j, o) {
2591
+ a.subMeshCount = 1;
2592
+ const y = [];
2593
+ y.push(...A), y.push(...F), y.push(...M), a.triangles = y;
2594
+ } else
2595
+ a.subMeshCount = 3, a.frontTriangles = A, a.backTriangles = M, a.borderTriangles = F;
2596
+ return a.borders = w, a;
2597
+ }
2598
+ // ── updateRootPosition ────────────────────────────────────────────────
2599
+ updateRootPosition() {
2600
+ const t = this.m_Book.papers, e = t[0].transform.position.y, s = t[t.length - 1].transform.position.y;
2601
+ let i = -Math.min(e, s);
2602
+ i += t[0].thickness / 2, this.m_Root.position.set(0, i, this.m_Root.position.z);
2603
+ }
2604
+ // ── resetPaperPosition ────────────────────────────────────────────────
2605
+ resetPaperPosition(t) {
2606
+ const e = this.m_Book.papers, s = e.length;
2607
+ let i = 0;
2608
+ const r = e.length / 2 - 1, o = e.length / 2;
2609
+ for (let x = 0; x < s; x++) {
2610
+ const P = e[x];
2611
+ P.updateTime();
2612
+ const S = P.zTime, k = P.thickness;
2613
+ i += S * k, x === r && (i += S * this.m_BindingMidSpace / 2), x === o && (i += S * this.m_BindingMidSpace / 2);
2614
+ }
2615
+ let a = this.getStackHeight(t.index) - t.thickness / 2;
2616
+ const h = this.getStackZ(a), u = 180 + h;
2617
+ let l = t.isFlipped ? 1 : 0, d = H(h, u, l);
2618
+ const p = d * et, g = new c.Vector3(
2619
+ Math.cos(p) * this.m_BindingRadius,
2620
+ Math.sin(p) * this.m_BindingRadius,
2621
+ 0
2622
+ );
2623
+ g.z = t.margin;
2624
+ let m = e[Math.floor(s / 2)].size.x, _ = this.m_BindingMidSpace, f = Math.sqrt(m * m - _ * _), T = Math.asin(f / m) * nt - 90;
2625
+ if (t.index < Math.floor(s / 2)) {
2626
+ let x = 0;
2627
+ if (this.m_Book.alignToGround) {
2628
+ m = e[0].size.x;
2629
+ const P = this.m_StackHeight / 2;
2630
+ _ = st(i, 0, P) - P, f = Math.sqrt(m * m - _ * _), x = (Math.asin(f / m) * nt - 90) * 2;
2631
+ }
2632
+ d = H(T, -x, l);
2633
+ } else {
2634
+ let x = 0;
2635
+ if (this.m_Book.alignToGround) {
2636
+ m = e[0].size.x;
2637
+ const P = this.m_StackHeight / 2;
2638
+ _ = P - st(i, P, P * 2), f = Math.sqrt(m * m - _ * _), x = (Math.asin(f / m) * nt - 90) * 2;
2639
+ }
2640
+ d = H(x, -T, l);
2641
+ }
2642
+ t.transform.position.copy(g), t.transform.quaternion.setFromEuler(
2643
+ new c.Euler(0, 0, d * et)
2644
+ );
2645
+ }
2646
+ // ── getPX (private helper) ───────────────────────────────────────────
2647
+ getPX(t, e) {
2648
+ const s = this.m_Book.papers, i = s.length;
2649
+ let r = 0;
2650
+ const o = s.length / 2 - 1, a = s.length / 2;
2651
+ for (let _ = 0; _ < i; _++)
2652
+ r += 0 * e, _ === o && (r += 0 * this.m_BindingMidSpace / 2), _ === a && (r += 0 * this.m_BindingMidSpace / 2);
2653
+ const h = this.getStackHeight(t) - e / 2, u = this.getStackZ(h), l = 180 + u, g = H(u, l, 0) * et;
2654
+ return new c.Vector3(
2655
+ Math.cos(g) * this.m_BindingRadius,
2656
+ Math.sin(g) * this.m_BindingRadius,
2657
+ 0
2658
+ ).x;
2659
+ }
2660
+ // ── updatePaperPosition ───────────────────────────────────────────────
2661
+ updatePaperPosition(t) {
2662
+ const e = this.m_Book.papers, s = e.length;
2663
+ let i = 0;
2664
+ const r = e.length / 2 - 1, o = e.length / 2;
2665
+ for (let x = 0; x < s; x++) {
2666
+ const P = e[x];
2667
+ P.updateTime();
2668
+ const S = P.zTime, k = P.thickness;
2669
+ i += S * k, x === r && (i += S * this.m_BindingMidSpace / 2), x === o && (i += S * this.m_BindingMidSpace / 2);
2670
+ }
2671
+ const a = this.getStackHeight(t.index) - t.thickness / 2, h = this.getStackZ(a + i), u = 180 + this.getStackZ(a + i - this.m_StackHeight);
2672
+ let l = t.zTime, d = H(h, u, l);
2673
+ const p = d * et, g = new c.Vector3(
2674
+ Math.cos(p) * this.m_BindingRadius,
2675
+ Math.sin(p) * this.m_BindingRadius,
2676
+ 0
2677
+ );
2678
+ g.z = t.margin;
2679
+ let m = e[Math.floor(s / 2)].size.x, _ = this.m_BindingMidSpace, f = Math.sqrt(m * m - _ * _), T = Math.asin(f / m) * nt - 90;
2680
+ if (t.index < Math.floor(s / 2)) {
2681
+ let x = 0;
2682
+ if (this.m_Book.alignToGround) {
2683
+ m = e[0].size.x;
2684
+ const P = this.m_StackHeight / 2;
2685
+ _ = st(i, 0, P) - P, f = Math.sqrt(m * m - _ * _), x = (Math.asin(f / m) * nt - 90) * 2;
2686
+ }
2687
+ d = H(T, -x, l);
2688
+ } else {
2689
+ let x = 0;
2690
+ if (this.m_Book.alignToGround) {
2691
+ m = e[0].size.x;
2692
+ const P = this.m_StackHeight / 2;
2693
+ _ = P - st(i, P, P * 2), f = Math.sqrt(m * m - _ * _), x = (Math.asin(f / m) * nt - 90) * 2;
2694
+ }
2695
+ d = H(x, -T, l);
2696
+ }
2697
+ t.transform.position.copy(g), t.transform.quaternion.setFromEuler(
2698
+ new c.Euler(0, 0, d * et)
2699
+ );
2700
+ }
2701
+ // ── getStackHeight ────────────────────────────────────────────────────
2702
+ getStackHeight(t) {
2703
+ const e = this.m_Book.papers;
2704
+ let s = 0;
2705
+ const i = e.length;
2706
+ for (let r = t; r < i; r++)
2707
+ s += e[r].thickness;
2708
+ return t < Math.floor(i / 2) && (s += this.m_BindingMidSpace), s;
2709
+ }
2710
+ // ── getStackZ ─────────────────────────────────────────────────────────
2711
+ getStackZ(t) {
2712
+ t = st(t, 0, this.m_StackHeight);
2713
+ const e = t - this.m_StackHeight * 0.5;
2714
+ return Math.asin(e / this.m_BindingRadius) * nt;
2715
+ }
2716
+ // ── onLateUpdate ──────────────────────────────────────────────────────
2717
+ onLateUpdate() {
2718
+ const t = this.m_Book.papers;
2719
+ for (const e of t)
2720
+ this.updatePaperPosition(e);
2721
+ this.m_Root.updateMatrixWorld(!0), this.updateBindingVertices();
2722
+ for (const e of t)
2723
+ e.updateMesh();
2724
+ this.updateRootPosition();
2725
+ }
2726
+ // ── updateBindingVertices ─────────────────────────────────────────────
2727
+ updateBindingVertices() {
2728
+ const t = this.m_Book.papers;
2729
+ let e = new c.Vector3(0, 0, 0), s = new c.Vector3(0, 0, 0), i = this.m_BindingRadius * 0.6;
2730
+ const r = this.m_StapleThickness * 0.5, o = t[0].thickness, a = this.m_Root.matrixWorld.clone(), h = t.length;
2731
+ for (let u = 0; u < h; u++) {
2732
+ const l = t[u], d = l.transform, p = l.meshData.pattern, g = l.meshData.baseVertices, m = p.baseXArray, _ = p.baseZArray, f = m.length, T = _.length, b = this.m_BindingVertexCount + 1, x = l.thickness, P = p.xNoneSeamIndexes, S = d.position.clone();
2733
+ let k = u, O = r;
2734
+ u >= Math.floor(h / 2) && (k = h - u - 1, O *= -1);
2735
+ const Z = (o + x) * 0.5 + x * (k - 1), z = d.matrixWorld.clone().invert().multiply(a), N = k / (h / 2);
2736
+ i = this.m_BindingRadius * H(0.45, 0.65, 1 - N);
2737
+ for (let D = 0; D < T; D++) {
2738
+ const C = _[D], R = new c.Vector3(0, 0, S.z + C), L = S.clone();
2739
+ L.z += C;
2740
+ const v = l.getDirection(C).multiplyScalar(i).add(L.clone());
2741
+ if (u === 0 && D === 0) {
2742
+ const A = t[h - 1], F = A.transform.position.clone().clone();
2743
+ F.z += C;
2744
+ const w = A.getDirection(C).multiplyScalar(i).add(F);
2745
+ s = v.clone().sub(w).normalize(), e = new c.Vector3(
2746
+ -s.y,
2747
+ s.x,
2748
+ 0
2749
+ ).normalize().negate();
2750
+ const V = Math.atan2(e.y, e.x) * nt;
2751
+ this.stapleMesh.rotation.set(0, 0, V * et);
2752
+ }
2753
+ k > 0 && R.add(e.clone().multiplyScalar(Z));
2754
+ const Q = D * f;
2755
+ g[Q] = R.clone().applyMatrix4(z), R.add(s.clone().multiplyScalar(O));
2756
+ const q = R.clone().applyMatrix4(z), $ = v.clone().applyMatrix4(z), j = L.clone().applyMatrix4(z);
2757
+ g[Q + P[1]] = q;
2758
+ for (let A = 2; A < b; A++) {
2759
+ const M = Qt(1, b, A);
2760
+ g[Q + P[A]] = Gt(
2761
+ q,
2762
+ $,
2763
+ j,
2764
+ M
2765
+ );
2766
+ }
2767
+ }
2768
+ }
2769
+ }
2770
+ // ── Getters for binding parameters (useful for external code) ─────────
2771
+ get bindingRadius() {
2772
+ return this.m_BindingRadius;
2773
+ }
2774
+ get bindingMidSpace() {
2775
+ return this.m_BindingMidSpace;
2776
+ }
2777
+ get stackHeight() {
2778
+ return this.m_StackHeight;
2779
+ }
2780
+ get bindingVertexCount() {
2781
+ return this.m_BindingVertexCount;
2782
+ }
2783
+ get quality() {
2784
+ return this.m_Quality;
2785
+ }
2786
+ get stapleMargin() {
2787
+ return this.m_StapleMargin;
2788
+ }
2789
+ get stapleThickness() {
2790
+ return this.m_StapleThickness;
2791
+ }
2792
+ }
2793
+ class is {
2794
+ constructor(t) {
2795
+ this.mesh = t;
2796
+ }
2797
+ setVisibility(t) {
2798
+ this.mesh.visible = t;
2799
+ }
2800
+ updateCollider() {
2801
+ }
2802
+ get castShadows() {
2803
+ return this.mesh.castShadow;
2804
+ }
2805
+ set castShadows(t) {
2806
+ this.mesh.castShadow = t;
2807
+ }
2808
+ get bounds() {
2809
+ const t = new c.Box3();
2810
+ return this.mesh.geometry && (this.mesh.geometry.computeBoundingBox(), this.mesh.geometry.boundingBox && (t.copy(this.mesh.geometry.boundingBox), t.applyMatrix4(this.mesh.matrixWorld))), t;
2811
+ }
2812
+ get transform() {
2813
+ return this.mesh;
2814
+ }
2815
+ get meshObject() {
2816
+ return this.mesh;
2817
+ }
2818
+ }
2819
+ class Ps extends Ue {
2820
+ constructor() {
2821
+ super(...arguments), this.quality = 3, this.stapleSetup = new ts();
2822
+ }
2823
+ createBound(t, e, s, i) {
2824
+ return new ss(
2825
+ this.quality,
2826
+ this.stapleSetup,
2827
+ t,
2828
+ e
2829
+ );
2830
+ }
2831
+ }
2832
+ export {
2833
+ Ve as AnimationCurve,
2834
+ dt as AutoTurnDirection,
2835
+ be as AutoTurnMode,
2836
+ at as AutoTurnSetting,
2837
+ Re as AutoTurnSettingCurveTimeMode,
2838
+ Ie as AutoTurnSettingMode,
2839
+ ct as AutoTurnSettings,
2840
+ cs as Book,
2841
+ Ue as BookBinding,
2842
+ Cs as BookBound,
2843
+ xs as BookContent,
2844
+ vt as BookContext,
2845
+ X as BookDirection,
2846
+ as as BookHeightException,
2847
+ us as BookInteraction,
2848
+ ke as BookRenderer,
2849
+ pt as Cylinder,
2850
+ we as MeshFactory,
2851
+ Ms as PageContent,
2852
+ fe as Paper,
2853
+ St as PaperBorder,
2854
+ Ht as PaperMaterialData,
2855
+ Xt as PaperMeshData,
2856
+ Be as PaperMeshDataPool,
2857
+ it as PaperNode,
2858
+ Me as PaperNodeMargin,
2859
+ Xe as PaperPattern,
2860
+ wt as PaperSeam,
2861
+ lt as PaperSetup,
2862
+ yt as PaperUVMargin,
2863
+ Se as RendererFactory,
2864
+ qt as SpritePageContent2,
2865
+ Ps as StapleBookBinding,
2866
+ ss as StapleBookBound,
2867
+ ts as StapleSetup,
2868
+ De as ThreeBook,
2869
+ ps as createPageTexture,
2870
+ Le as drawImageWithFit,
2871
+ fs as loadImage,
2872
+ ds as useAutoTurn,
2873
+ Ct as useBook,
2874
+ gs as useBookContent,
2875
+ ms as useBookControls,
2876
+ Ae as useBookRef,
2877
+ _s as useBookState,
2878
+ Fe as usePageTurning,
2879
+ ls as useRequiredBook
2880
+ };