reze-engine 0.14.0 → 0.15.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 (93) hide show
  1. package/README.md +81 -108
  2. package/dist/engine.d.ts +1 -7
  3. package/dist/engine.d.ts.map +1 -1
  4. package/dist/engine.js +4 -7
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/physics/body.d.ts +30 -0
  9. package/dist/physics/body.d.ts.map +1 -0
  10. package/dist/physics/body.js +215 -0
  11. package/dist/physics/constraint.d.ts +17 -0
  12. package/dist/physics/constraint.d.ts.map +1 -0
  13. package/dist/physics/constraint.js +102 -0
  14. package/dist/physics/contact.d.ts +32 -0
  15. package/dist/physics/contact.d.ts.map +1 -0
  16. package/dist/physics/contact.js +728 -0
  17. package/dist/physics/index.d.ts +4 -0
  18. package/dist/physics/index.d.ts.map +1 -0
  19. package/dist/physics/index.js +3 -0
  20. package/dist/physics/physics.d.ts +31 -0
  21. package/dist/physics/physics.d.ts.map +1 -0
  22. package/dist/physics/physics.js +211 -0
  23. package/dist/physics/solver.d.ts +5 -0
  24. package/dist/physics/solver.d.ts.map +1 -0
  25. package/dist/physics/solver.js +416 -0
  26. package/dist/physics/types.d.ts +46 -0
  27. package/dist/physics/types.d.ts.map +1 -0
  28. package/dist/physics/types.js +12 -0
  29. package/dist/physics/world.d.ts +12 -0
  30. package/dist/physics/world.d.ts.map +1 -0
  31. package/dist/physics/world.js +146 -0
  32. package/dist/physics-debug.d.ts +30 -0
  33. package/dist/physics-debug.d.ts.map +1 -0
  34. package/dist/physics-debug.js +526 -0
  35. package/dist/shaders/materials/hair.d.ts +1 -1
  36. package/dist/shaders/materials/hair.d.ts.map +1 -1
  37. package/dist/shaders/materials/hair.js +2 -2
  38. package/dist/shaders/passes/physics-debug.d.ts +2 -0
  39. package/dist/shaders/passes/physics-debug.d.ts.map +1 -0
  40. package/dist/shaders/passes/physics-debug.js +69 -0
  41. package/package.json +3 -6
  42. package/src/engine.ts +5 -9
  43. package/src/index.ts +1 -1
  44. package/src/physics/body.ts +305 -0
  45. package/src/physics/constraint.ts +151 -0
  46. package/src/physics/contact.ts +983 -0
  47. package/src/physics/index.ts +8 -0
  48. package/src/physics/physics.ts +255 -0
  49. package/src/physics/solver.ts +430 -0
  50. package/src/physics/types.ts +50 -0
  51. package/src/physics/world.ts +152 -0
  52. package/src/shaders/materials/hair.ts +2 -2
  53. package/dist/ammo-loader.d.ts +0 -3
  54. package/dist/ammo-loader.d.ts.map +0 -1
  55. package/dist/ammo-loader.js +0 -26
  56. package/dist/physics.d.ts +0 -86
  57. package/dist/physics.d.ts.map +0 -1
  58. package/dist/physics.js +0 -527
  59. package/dist/shaders/body.d.ts +0 -2
  60. package/dist/shaders/body.d.ts.map +0 -1
  61. package/dist/shaders/body.js +0 -199
  62. package/dist/shaders/classify.d.ts +0 -4
  63. package/dist/shaders/classify.d.ts.map +0 -1
  64. package/dist/shaders/classify.js +0 -12
  65. package/dist/shaders/cloth_rough.d.ts +0 -2
  66. package/dist/shaders/cloth_rough.d.ts.map +0 -1
  67. package/dist/shaders/cloth_rough.js +0 -178
  68. package/dist/shaders/cloth_smooth.d.ts +0 -2
  69. package/dist/shaders/cloth_smooth.d.ts.map +0 -1
  70. package/dist/shaders/cloth_smooth.js +0 -174
  71. package/dist/shaders/default.d.ts +0 -2
  72. package/dist/shaders/default.d.ts.map +0 -1
  73. package/dist/shaders/default.js +0 -171
  74. package/dist/shaders/eye.d.ts +0 -2
  75. package/dist/shaders/eye.d.ts.map +0 -1
  76. package/dist/shaders/eye.js +0 -146
  77. package/dist/shaders/face.d.ts +0 -2
  78. package/dist/shaders/face.d.ts.map +0 -1
  79. package/dist/shaders/face.js +0 -199
  80. package/dist/shaders/hair.d.ts +0 -2
  81. package/dist/shaders/hair.d.ts.map +0 -1
  82. package/dist/shaders/hair.js +0 -176
  83. package/dist/shaders/metal.d.ts +0 -2
  84. package/dist/shaders/metal.d.ts.map +0 -1
  85. package/dist/shaders/metal.js +0 -174
  86. package/dist/shaders/nodes.d.ts +0 -2
  87. package/dist/shaders/nodes.d.ts.map +0 -1
  88. package/dist/shaders/nodes.js +0 -456
  89. package/dist/shaders/stockings.d.ts +0 -2
  90. package/dist/shaders/stockings.d.ts.map +0 -1
  91. package/dist/shaders/stockings.js +0 -244
  92. package/src/ammo-loader.ts +0 -31
  93. package/src/physics.ts +0 -706
@@ -0,0 +1,46 @@
1
+ import { Vec3, Mat4 } from "../math";
2
+ export declare enum RigidbodyShape {
3
+ Sphere = 0,
4
+ Box = 1,
5
+ Capsule = 2
6
+ }
7
+ export declare enum RigidbodyType {
8
+ Static = 0,
9
+ Dynamic = 1,
10
+ Kinematic = 2
11
+ }
12
+ export interface Rigidbody {
13
+ name: string;
14
+ englishName: string;
15
+ boneIndex: number;
16
+ group: number;
17
+ collisionMask: number;
18
+ shape: RigidbodyShape;
19
+ size: Vec3;
20
+ shapePosition: Vec3;
21
+ shapeRotation: Vec3;
22
+ mass: number;
23
+ linearDamping: number;
24
+ angularDamping: number;
25
+ restitution: number;
26
+ friction: number;
27
+ type: RigidbodyType;
28
+ bodyOffsetMatrixInverse: Mat4;
29
+ bodyOffsetMatrix?: Mat4;
30
+ }
31
+ export interface Joint {
32
+ name: string;
33
+ englishName: string;
34
+ type: number;
35
+ rigidbodyIndexA: number;
36
+ rigidbodyIndexB: number;
37
+ position: Vec3;
38
+ rotation: Vec3;
39
+ positionMin: Vec3;
40
+ positionMax: Vec3;
41
+ rotationMin: Vec3;
42
+ rotationMax: Vec3;
43
+ springPosition: Vec3;
44
+ springRotation: Vec3;
45
+ }
46
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/physics/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAEpC,oBAAY,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Shape[RigidbodyShape["Box"] = 1] = "Box";
5
+ RigidbodyShape[RigidbodyShape["Capsule"] = 2] = "Capsule";
6
+ })(RigidbodyShape || (RigidbodyShape = {}));
7
+ export var RigidbodyType;
8
+ (function (RigidbodyType) {
9
+ RigidbodyType[RigidbodyType["Static"] = 0] = "Static";
10
+ RigidbodyType[RigidbodyType["Dynamic"] = 1] = "Dynamic";
11
+ RigidbodyType[RigidbodyType["Kinematic"] = 2] = "Kinematic";
12
+ })(RigidbodyType || (RigidbodyType = {}));
@@ -0,0 +1,12 @@
1
+ import { Vec3 } from "../math";
2
+ import type { RigidBodyStore } from "./body";
3
+ import type { SixDofSpringConstraint } from "./constraint";
4
+ import { type ContactPool } from "./contact";
5
+ export declare class World {
6
+ readonly gravity: Vec3;
7
+ solverIterations: number;
8
+ constructor(gravity: Vec3);
9
+ setGravity(g: Vec3): void;
10
+ step(store: RigidBodyStore, constraints: SixDofSpringConstraint[], contacts: ContactPool, dt: number): void;
11
+ }
12
+ //# sourceMappingURL=world.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"world.d.ts","sourceRoot":"","sources":["../../src/physics/world.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAE5C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAE1D,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,WAAW,CAAA;AAO1D,qBAAa,KAAK;IAChB,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAA;IACtB,gBAAgB,SAAK;gBAET,OAAO,EAAE,IAAI;IAIzB,UAAU,CAAC,CAAC,EAAE,IAAI,GAAG,IAAI;IAMzB,IAAI,CAAC,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,sBAAsB,EAAE,EAAE,QAAQ,EAAE,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI;CA6H5G"}
@@ -0,0 +1,146 @@
1
+ import { Vec3 } from "../math";
2
+ import { RigidbodyType } from "./types";
3
+ import { solveConstraints } from "./solver";
4
+ import { findContacts } from "./contact";
5
+ // World step: predict velocities → collide → solve → position correction →
6
+ // integrate. Static and kinematic bodies are skipped during predict and
7
+ // integrate; the parent class syncs them from bones around the step. The
8
+ // solver pass runs on all bodies — kinematic ones have invMass = 0 and
9
+ // act as anchors.
10
+ export class World {
11
+ constructor(gravity) {
12
+ this.solverIterations = 10;
13
+ this.gravity = new Vec3(gravity.x, gravity.y, gravity.z);
14
+ }
15
+ setGravity(g) {
16
+ this.gravity.x = g.x;
17
+ this.gravity.y = g.y;
18
+ this.gravity.z = g.z;
19
+ }
20
+ step(store, constraints, contacts, dt) {
21
+ if (dt <= 0)
22
+ return;
23
+ const N = store.count;
24
+ const types = store.type;
25
+ const lv = store.linearVelocities;
26
+ const av = store.angularVelocities;
27
+ const pos = store.positions;
28
+ const ori = store.orientations;
29
+ const ldamp = store.linearDamping;
30
+ const adamp = store.angularDamping;
31
+ const invMass = store.invMass;
32
+ const gx = this.gravity.x;
33
+ const gy = this.gravity.y;
34
+ const gz = this.gravity.z;
35
+ // 1. Predict — gravity + damping. The pow form (vs the linear
36
+ // 1−damping·dt approximation) stays stable at high PMX damping
37
+ // values like 0.99.
38
+ for (let i = 0; i < N; i++) {
39
+ if (types[i] !== RigidbodyType.Dynamic || invMass[i] <= 0)
40
+ continue;
41
+ const i3 = i * 3;
42
+ lv[i3 + 0] += gx * dt;
43
+ lv[i3 + 1] += gy * dt;
44
+ lv[i3 + 2] += gz * dt;
45
+ const ld = Math.pow(Math.max(0, 1 - ldamp[i]), dt);
46
+ const ad = Math.pow(Math.max(0, 1 - adamp[i]), dt);
47
+ lv[i3 + 0] *= ld;
48
+ lv[i3 + 1] *= ld;
49
+ lv[i3 + 2] *= ld;
50
+ av[i3 + 0] *= ad;
51
+ av[i3 + 1] *= ad;
52
+ av[i3 + 2] *= ad;
53
+ }
54
+ // 2. Collide.
55
+ contacts.reset();
56
+ findContacts(store, contacts);
57
+ // 3. Solve joint + contact constraints (velocity-only).
58
+ if (constraints.length > 0 || contacts.count > 0) {
59
+ solveConstraints(store, constraints, contacts, dt, this.solverIterations);
60
+ }
61
+ // 4. Position correction (split impulse). Direct translation along the
62
+ // contact normal — joint constraints in the same SI loop can't undo
63
+ // it because it doesn't go through the velocity channel. Inverse-mass
64
+ // weighted so a kinematic body stays put and only the dynamic one
65
+ // translates.
66
+ const POS_CORRECTION_FACTOR = 0.4;
67
+ const POS_SLOP = 0.005;
68
+ for (let ci = 0; ci < contacts.count; ci++) {
69
+ const c = contacts.get(ci);
70
+ if (c.depth <= POS_SLOP)
71
+ continue;
72
+ const imA = invMass[c.bodyA];
73
+ const imB = invMass[c.bodyB];
74
+ const total = imA + imB;
75
+ if (total <= 0)
76
+ continue;
77
+ const correction = (c.depth - POS_SLOP) * POS_CORRECTION_FACTOR;
78
+ const dx = correction * c.nx;
79
+ const dy = correction * c.ny;
80
+ const dz = correction * c.nz;
81
+ const ai = c.bodyA * 3;
82
+ const bi = c.bodyB * 3;
83
+ if (imA > 0) {
84
+ const fA = imA / total;
85
+ pos[ai + 0] -= dx * fA;
86
+ pos[ai + 1] -= dy * fA;
87
+ pos[ai + 2] -= dz * fA;
88
+ }
89
+ if (imB > 0) {
90
+ const fB = imB / total;
91
+ pos[bi + 0] += dx * fB;
92
+ pos[bi + 1] += dy * fB;
93
+ pos[bi + 2] += dz * fB;
94
+ }
95
+ }
96
+ // 5. Integrate. Cap angular velocity at π/2 per step — a high-impulse
97
+ // contact spike on a low-inertia body would otherwise spin past π
98
+ // in one step and trash the quaternion integration.
99
+ const MAX_ANGVEL_DT = Math.PI * 0.5;
100
+ for (let i = 0; i < N; i++) {
101
+ if (types[i] !== RigidbodyType.Dynamic || invMass[i] <= 0)
102
+ continue;
103
+ const i3 = i * 3;
104
+ const i4 = i * 4;
105
+ pos[i3 + 0] += lv[i3 + 0] * dt;
106
+ pos[i3 + 1] += lv[i3 + 1] * dt;
107
+ pos[i3 + 2] += lv[i3 + 2] * dt;
108
+ let wx = av[i3 + 0];
109
+ let wy = av[i3 + 1];
110
+ let wz = av[i3 + 2];
111
+ const wmag = Math.sqrt(wx * wx + wy * wy + wz * wz);
112
+ if (wmag * dt > MAX_ANGVEL_DT) {
113
+ const scale = MAX_ANGVEL_DT / (wmag * dt);
114
+ wx *= scale;
115
+ wy *= scale;
116
+ wz *= scale;
117
+ av[i3 + 0] = wx;
118
+ av[i3 + 1] = wy;
119
+ av[i3 + 2] = wz;
120
+ }
121
+ if (wx !== 0 || wy !== 0 || wz !== 0) {
122
+ const qx = ori[i4 + 0];
123
+ const qy = ori[i4 + 1];
124
+ const qz = ori[i4 + 2];
125
+ const qw = ori[i4 + 3];
126
+ const dx = qw * wx + wy * qz - wz * qy;
127
+ const dy = qw * wy + wz * qx - wx * qz;
128
+ const dz = qw * wz + wx * qy - wy * qx;
129
+ const dw = -(wx * qx + wy * qy + wz * qz);
130
+ const half = 0.5 * dt;
131
+ const nx = qx + dx * half;
132
+ const ny = qy + dy * half;
133
+ const nz = qz + dz * half;
134
+ const nw = qw + dw * half;
135
+ const len2 = nx * nx + ny * ny + nz * nz + nw * nw;
136
+ if (len2 > 0) {
137
+ const inv = 1 / Math.sqrt(len2);
138
+ ori[i4 + 0] = nx * inv;
139
+ ori[i4 + 1] = ny * inv;
140
+ ori[i4 + 2] = nz * inv;
141
+ ori[i4 + 3] = nw * inv;
142
+ }
143
+ }
144
+ }
145
+ }
146
+ }
@@ -0,0 +1,30 @@
1
+ import type { RezePhysics } from "./physics";
2
+ export declare class PhysicsDebugRenderer {
3
+ private device;
4
+ private bindGroup;
5
+ private wirePipelineSphere;
6
+ private wirePipelineBox;
7
+ private wirePipelineCapsule;
8
+ private wireSphereBuffer;
9
+ private wireBoxBuffer;
10
+ private wireCapsuleBuffer;
11
+ private wireSphereCount;
12
+ private wireBoxCount;
13
+ private wireCapsuleCount;
14
+ private solidPipelineSphere;
15
+ private solidPipelineBox;
16
+ private solidPipelineCapsule;
17
+ private solidSphereBuffer;
18
+ private solidBoxBuffer;
19
+ private solidCapsuleBuffer;
20
+ private solidSphereCount;
21
+ private solidBoxCount;
22
+ private solidCapsuleCount;
23
+ private instanceBuffer;
24
+ private instanceData;
25
+ private instanceCapacity;
26
+ constructor(device: GPUDevice, cameraUniformBuffer: GPUBuffer, presentationFormat: GPUTextureFormat);
27
+ render(pass: GPURenderPassEncoder, physics: RezePhysics): void;
28
+ destroy(): void;
29
+ }
30
+ //# sourceMappingURL=physics-debug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"physics-debug.d.ts","sourceRoot":"","sources":["../src/physics-debug.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAmB5C,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,SAAS,CAAc;IAG/B,OAAO,CAAC,kBAAkB,CAAmB;IAC7C,OAAO,CAAC,eAAe,CAAmB;IAC1C,OAAO,CAAC,mBAAmB,CAAmB;IAC9C,OAAO,CAAC,gBAAgB,CAAW;IACnC,OAAO,CAAC,aAAa,CAAW;IAChC,OAAO,CAAC,iBAAiB,CAAW;IACpC,OAAO,CAAC,eAAe,CAAQ;IAC/B,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,gBAAgB,CAAQ;IAIhC,OAAO,CAAC,mBAAmB,CAAmB;IAC9C,OAAO,CAAC,gBAAgB,CAAmB;IAC3C,OAAO,CAAC,oBAAoB,CAAmB;IAC/C,OAAO,CAAC,iBAAiB,CAAW;IACpC,OAAO,CAAC,cAAc,CAAW;IACjC,OAAO,CAAC,kBAAkB,CAAW;IACrC,OAAO,CAAC,gBAAgB,CAAQ;IAChC,OAAO,CAAC,aAAa,CAAQ;IAC7B,OAAO,CAAC,iBAAiB,CAAQ;IAEjC,OAAO,CAAC,cAAc,CAAW;IACjC,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,gBAAgB,CAAQ;gBAG9B,MAAM,EAAE,SAAS,EACjB,mBAAmB,EAAE,SAAS,EAC9B,kBAAkB,EAAE,gBAAgB;IA2HtC,MAAM,CAAC,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,WAAW,GAAG,IAAI;IAmI9D,OAAO,IAAI,IAAI;CAShB"}