@zephyr3d/scene 0.8.1 → 0.9.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.
- package/dist/animation/animationset.js +563 -128
- package/dist/animation/animationset.js.map +1 -1
- package/dist/animation/cloth/gpu_cloth_system.js +2516 -0
- package/dist/animation/cloth/gpu_cloth_system.js.map +1 -0
- package/dist/animation/fixed_geometry_cache_track.js +138 -0
- package/dist/animation/fixed_geometry_cache_track.js.map +1 -0
- package/dist/animation/geometry_cache_utils.js +120 -0
- package/dist/animation/geometry_cache_utils.js.map +1 -0
- package/dist/animation/ik_modifier.js +9 -1
- package/dist/animation/ik_modifier.js.map +1 -1
- package/dist/animation/joint_dynamics/collision.js +469 -0
- package/dist/animation/joint_dynamics/collision.js.map +1 -0
- package/dist/animation/joint_dynamics/constraints.js +329 -0
- package/dist/animation/joint_dynamics/constraints.js.map +1 -0
- package/dist/animation/joint_dynamics/controller.js +699 -0
- package/dist/animation/joint_dynamics/controller.js.map +1 -0
- package/dist/animation/joint_dynamics/joint_dynamics_system.js +393 -0
- package/dist/animation/joint_dynamics/joint_dynamics_system.js.map +1 -0
- package/dist/animation/joint_dynamics/solver.js +794 -0
- package/dist/animation/joint_dynamics/solver.js.map +1 -0
- package/dist/animation/joint_dynamics/types.js +19 -0
- package/dist/animation/joint_dynamics/types.js.map +1 -0
- package/dist/animation/joint_dynamics/vrm_spring_bone.js +46 -0
- package/dist/animation/joint_dynamics/vrm_spring_bone.js.map +1 -0
- package/dist/animation/joint_dynamics_modifier.js +51 -0
- package/dist/animation/joint_dynamics_modifier.js.map +1 -0
- package/dist/animation/pca_geometry_cache_track.js +222 -0
- package/dist/animation/pca_geometry_cache_track.js.map +1 -0
- package/dist/animation/skeleton.js +1907 -3
- package/dist/animation/skeleton.js.map +1 -1
- package/dist/animation/skeleton_modifier.js +3 -5
- package/dist/animation/skeleton_modifier.js.map +1 -1
- package/dist/animation/spring/multi_chain_spring_system.js +2 -0
- package/dist/animation/spring/multi_chain_spring_system.js.map +1 -1
- package/dist/animation/spring/spring_chain.js +3 -2
- package/dist/animation/spring/spring_chain.js.map +1 -1
- package/dist/animation/spring/spring_collider.js +35 -5
- package/dist/animation/spring/spring_collider.js.map +1 -1
- package/dist/animation/spring/spring_particle.js +1 -0
- package/dist/animation/spring/spring_particle.js.map +1 -1
- package/dist/animation/spring/spring_system.js +325 -28
- package/dist/animation/spring/spring_system.js.map +1 -1
- package/dist/animation/spring2/collision.js +469 -0
- package/dist/animation/spring2/collision.js.map +1 -0
- package/dist/animation/spring2/constraints.js +329 -0
- package/dist/animation/spring2/constraints.js.map +1 -0
- package/dist/animation/spring2/controller.js +434 -0
- package/dist/animation/spring2/controller.js.map +1 -0
- package/dist/animation/spring2/math.js +16 -0
- package/dist/animation/spring2/math.js.map +1 -0
- package/dist/animation/spring2/solver.js +624 -0
- package/dist/animation/spring2/solver.js.map +1 -0
- package/dist/animation/spring2/spring_system.js +118 -0
- package/dist/animation/spring2/spring_system.js.map +1 -0
- package/dist/animation/spring2/types.js +19 -0
- package/dist/animation/spring2/types.js.map +1 -0
- package/dist/animation/spring_modifier.js +17 -1
- package/dist/animation/spring_modifier.js.map +1 -1
- package/dist/app/engine.js +42 -15
- package/dist/app/engine.js.map +1 -1
- package/dist/app/runtimescript.js +132 -1
- package/dist/app/runtimescript.js.map +1 -1
- package/dist/app/screen.js +3 -3
- package/dist/app/screen.js.map +1 -1
- package/dist/app/scriptingsystem.js +69 -37
- package/dist/app/scriptingsystem.js.map +1 -1
- package/dist/app/scriptregistry.js +157 -146
- package/dist/app/scriptregistry.js.map +1 -1
- package/dist/asset/assetmanager.js +2 -1
- package/dist/asset/assetmanager.js.map +1 -1
- package/dist/asset/loaders/zabc/zabc_loader.js +570 -0
- package/dist/asset/loaders/zabc/zabc_loader.js.map +1 -0
- package/dist/asset/model.js +192 -0
- package/dist/asset/model.js.map +1 -1
- package/dist/camera/base.js +26 -7
- package/dist/camera/base.js.map +1 -1
- package/dist/camera/camera.js +146 -24
- package/dist/camera/camera.js.map +1 -1
- package/dist/camera/fps.js +2 -2
- package/dist/camera/fps.js.map +1 -1
- package/dist/camera/orbit.js +2 -2
- package/dist/camera/orbit.js.map +1 -1
- package/dist/index.d.ts +11701 -8506
- package/dist/index.js +29 -8
- package/dist/index.js.map +1 -1
- package/dist/material/lambert.js +1 -1
- package/dist/material/lambert.js.map +1 -1
- package/dist/material/material.js +9 -3
- package/dist/material/material.js.map +1 -1
- package/dist/material/meshmaterial.js +54 -7
- package/dist/material/meshmaterial.js.map +1 -1
- package/dist/material/mixins/lightmodel/blinnphong.js +17 -7
- package/dist/material/mixins/lightmodel/blinnphong.js.map +1 -1
- package/dist/material/mixins/lightmodel/lambert.js +7 -2
- package/dist/material/mixins/lightmodel/lambert.js.map +1 -1
- package/dist/material/mixins/lightmodel/pbrblueprintmixin.js +108 -16
- package/dist/material/mixins/lightmodel/pbrblueprintmixin.js.map +1 -1
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js +222 -14
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -1
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js +24 -12
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -1
- package/dist/material/mixins/lit.js +11 -8
- package/dist/material/mixins/lit.js.map +1 -1
- package/dist/material/mixins/pbr/common.js +72 -5
- package/dist/material/mixins/pbr/common.js.map +1 -1
- package/dist/material/pbrblueprint.js +148 -1
- package/dist/material/pbrblueprint.js.map +1 -1
- package/dist/material/pbrmr.js +125 -2
- package/dist/material/pbrmr.js.map +1 -1
- package/dist/material/shader/helper.js +10 -4
- package/dist/material/shader/helper.js.map +1 -1
- package/dist/material/water.js +2 -2
- package/dist/material/water.js.map +1 -1
- package/dist/posteffect/coloradjust.js +145 -0
- package/dist/posteffect/coloradjust.js.map +1 -0
- package/dist/posteffect/taa.js +68 -32
- package/dist/posteffect/taa.js.map +1 -1
- package/dist/render/cluster_light.js +17 -12
- package/dist/render/cluster_light.js.map +1 -1
- package/dist/render/deferredlightpass.js +510 -0
- package/dist/render/deferredlightpass.js.map +1 -0
- package/dist/render/deferredshadowlightpass.js +428 -0
- package/dist/render/deferredshadowlightpass.js.map +1 -0
- package/dist/render/drawable.js.map +1 -1
- package/dist/render/drawable_mixin.js +21 -12
- package/dist/render/drawable_mixin.js.map +1 -1
- package/dist/render/envlight.js +20 -22
- package/dist/render/envlight.js.map +1 -1
- package/dist/render/gbufferpass.js +50 -0
- package/dist/render/gbufferpass.js.map +1 -0
- package/dist/render/lightpass.js +1 -2
- package/dist/render/lightpass.js.map +1 -1
- package/dist/render/primitive.js +1 -1
- package/dist/render/primitive.js.map +1 -1
- package/dist/render/render_queue.js +3 -2
- package/dist/render/render_queue.js.map +1 -1
- package/dist/render/renderer.js +5 -358
- package/dist/render/renderer.js.map +1 -1
- package/dist/render/rendergraph/executor.js +15 -12
- package/dist/render/rendergraph/executor.js.map +1 -1
- package/dist/render/rendergraph/forward_plus_builder.js +144 -88
- package/dist/render/rendergraph/forward_plus_builder.js.map +1 -1
- package/dist/render/rendergraph/history_resource_manager.js +178 -0
- package/dist/render/rendergraph/history_resource_manager.js.map +1 -0
- package/dist/render/rendergraph/rendergraph.js +12 -1
- package/dist/render/rendergraph/rendergraph.js.map +1 -1
- package/dist/render/rendergraph/types.js +2 -2
- package/dist/render/rendergraph/types.js.map +1 -1
- package/dist/render/sky.js +26 -9
- package/dist/render/sky.js.map +1 -1
- package/dist/scene/basesprite.js +4 -3
- package/dist/scene/basesprite.js.map +1 -1
- package/dist/scene/batchgroup.js +4 -4
- package/dist/scene/environment.js +7 -4
- package/dist/scene/environment.js.map +1 -1
- package/dist/scene/light.js +184 -18
- package/dist/scene/light.js.map +1 -1
- package/dist/scene/mesh.js +30 -0
- package/dist/scene/mesh.js.map +1 -1
- package/dist/scene/particlesys.js +3 -2
- package/dist/scene/particlesys.js.map +1 -1
- package/dist/scene/raycast_visitor.js +29 -3
- package/dist/scene/raycast_visitor.js.map +1 -1
- package/dist/scene/scene.js +86 -19
- package/dist/scene/scene.js.map +1 -1
- package/dist/scene/scene_node.js +87 -31
- package/dist/scene/scene_node.js.map +1 -1
- package/dist/scene/script_attachment.js +59 -0
- package/dist/scene/script_attachment.js.map +1 -0
- package/dist/scene/terrain-cm/grass.js +3 -2
- package/dist/scene/terrain-cm/grass.js.map +1 -1
- package/dist/scene/terrain-cm/terrain-cm.js +4 -3
- package/dist/scene/terrain-cm/terrain-cm.js.map +1 -1
- package/dist/scene/water.js +3 -2
- package/dist/scene/water.js.map +1 -1
- package/dist/shaders/shadow.js +2 -2
- package/dist/shaders/shadow.js.map +1 -1
- package/dist/shadow/shadowmapper.js +74 -4
- package/dist/shadow/shadowmapper.js.map +1 -1
- package/dist/shapes/box.js +163 -0
- package/dist/shapes/box.js.map +1 -1
- package/dist/shapes/capsule.js +216 -0
- package/dist/shapes/capsule.js.map +1 -0
- package/dist/shapes/cylinder.js +91 -0
- package/dist/shapes/cylinder.js.map +1 -1
- package/dist/shapes/plane.js +32 -0
- package/dist/shapes/plane.js.map +1 -1
- package/dist/shapes/tetrahedron.js +211 -0
- package/dist/shapes/tetrahedron.js.map +1 -1
- package/dist/shapes/torus.js +173 -0
- package/dist/shapes/torus.js.map +1 -1
- package/dist/utility/blueprint/common/math.js +111 -1
- package/dist/utility/blueprint/common/math.js.map +1 -1
- package/dist/utility/blueprint/material/inputs.js +235 -1
- package/dist/utility/blueprint/material/inputs.js.map +1 -1
- package/dist/utility/blueprint/material/ir.js +154 -10
- package/dist/utility/blueprint/material/ir.js.map +1 -1
- package/dist/utility/blueprint/material/texture.js +273 -19
- package/dist/utility/blueprint/material/texture.js.map +1 -1
- package/dist/utility/serialization/json.js +12 -2
- package/dist/utility/serialization/json.js.map +1 -1
- package/dist/utility/serialization/manager.js +30 -11
- package/dist/utility/serialization/manager.js.map +1 -1
- package/dist/utility/serialization/scene/animation.js +250 -7
- package/dist/utility/serialization/scene/animation.js.map +1 -1
- package/dist/utility/serialization/scene/batch.js +4 -2
- package/dist/utility/serialization/scene/batch.js.map +1 -1
- package/dist/utility/serialization/scene/camera.js +154 -2
- package/dist/utility/serialization/scene/camera.js.map +1 -1
- package/dist/utility/serialization/scene/cloth_script.js +834 -0
- package/dist/utility/serialization/scene/cloth_script.js.map +1 -0
- package/dist/utility/serialization/scene/common.js +7 -0
- package/dist/utility/serialization/scene/common.js.map +1 -1
- package/dist/utility/serialization/scene/light.js +120 -4
- package/dist/utility/serialization/scene/light.js.map +1 -1
- package/dist/utility/serialization/scene/material.js +453 -1
- package/dist/utility/serialization/scene/material.js.map +1 -1
- package/dist/utility/serialization/scene/mesh.js +4 -2
- package/dist/utility/serialization/scene/mesh.js.map +1 -1
- package/dist/utility/serialization/scene/node.js +298 -10
- package/dist/utility/serialization/scene/node.js.map +1 -1
- package/dist/utility/serialization/scene/particle.js +4 -2
- package/dist/utility/serialization/scene/particle.js.map +1 -1
- package/dist/utility/serialization/scene/primitive.js +100 -3
- package/dist/utility/serialization/scene/primitive.js.map +1 -1
- package/dist/utility/serialization/scene/scene.js +130 -3
- package/dist/utility/serialization/scene/scene.js.map +1 -1
- package/dist/utility/serialization/scene/script.js +66 -0
- package/dist/utility/serialization/scene/script.js.map +1 -0
- package/dist/utility/serialization/scene/spring_script.js +596 -0
- package/dist/utility/serialization/scene/spring_script.js.map +1 -0
- package/dist/utility/serialization/scene/sprite.js +5 -3
- package/dist/utility/serialization/scene/sprite.js.map +1 -1
- package/dist/utility/serialization/scene/terrain.js +4 -2
- package/dist/utility/serialization/scene/terrain.js.map +1 -1
- package/dist/utility/serialization/scene/water.js +4 -2
- package/dist/utility/serialization/scene/water.js.map +1 -1
- package/dist/utility/serialization/types.js.map +1 -1
- package/dist/values.js +5 -1
- package/dist/values.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
import { EPSILON } from './types.js';
|
|
2
|
+
import { buildConstraints, buildSurfaceFaces } from './constraints.js';
|
|
3
|
+
import { simulate, applyAngleLimits, applyResult } from './solver.js';
|
|
4
|
+
import { Vector3, Quaternion, Matrix4x4, clamp01 } from '@zephyr3d/base';
|
|
5
|
+
|
|
6
|
+
// High-level orchestrator — SPCRJointDynamicsController API
|
|
7
|
+
class JointDynamicsSystemController {
|
|
8
|
+
_config;
|
|
9
|
+
_pointsR = [];
|
|
10
|
+
_pointsRW = [];
|
|
11
|
+
_constraints = [];
|
|
12
|
+
_collidersR = [];
|
|
13
|
+
_collidersRW = [];
|
|
14
|
+
_grabbersR = [];
|
|
15
|
+
_grabbersRW = [];
|
|
16
|
+
_movableLimitTargets = [];
|
|
17
|
+
_flatPlanes = [];
|
|
18
|
+
_flatPlaneEnabled = [];
|
|
19
|
+
_flatPlaneAll = [];
|
|
20
|
+
_surfaceConstraints = [];
|
|
21
|
+
_positionsToTransform = [];
|
|
22
|
+
_fakeWaveCounter = 0;
|
|
23
|
+
_previousRootPosition = Vector3.zero();
|
|
24
|
+
_previousRootRotation = Quaternion.identity();
|
|
25
|
+
_rootTransform = null;
|
|
26
|
+
_pointTransforms = [];
|
|
27
|
+
_colliderTransforms = [];
|
|
28
|
+
_grabberTransforms = [];
|
|
29
|
+
_colliderHandleIds = [];
|
|
30
|
+
_flatPlaneHandleIds = [];
|
|
31
|
+
_grabberHandleIds = [];
|
|
32
|
+
_colliderHandleToIndex = new Map();
|
|
33
|
+
_flatPlaneHandleToIndex = new Map();
|
|
34
|
+
_grabberHandleToIndex = new Map();
|
|
35
|
+
_nextColliderHandleId = 1;
|
|
36
|
+
_nextFlatPlaneHandleId = 1;
|
|
37
|
+
_nextGrabberHandleId = 1;
|
|
38
|
+
_initialized = false;
|
|
39
|
+
_isPaused = false;
|
|
40
|
+
_fadeState = 'none';
|
|
41
|
+
_fadeTimer = 0;
|
|
42
|
+
_fadeDuration = 0;
|
|
43
|
+
constructor(config){
|
|
44
|
+
this._config = config;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 初始化物理系统
|
|
48
|
+
* @param rootTransform 根骨骼的 transform(用于检测根运动)
|
|
49
|
+
* @param rootPoints 骨骼层级的根节点数组(可以有多个根,如裙子的多条链)
|
|
50
|
+
* @param pointTransforms 所有物理点的 transform 数组(顺序必须与 BoneNode.index 对应)
|
|
51
|
+
* @param colliders 碰撞器数组(球体/胶囊体)
|
|
52
|
+
* @param grabbers 抓取器数组(用于鼠标交互等)
|
|
53
|
+
* @param flatPlanes 平面限制器数组(如地板,防止穿透)
|
|
54
|
+
*/ initialize(rootTransform, rootPoints, pointTransforms, colliders, grabbers, flatPlanes) {
|
|
55
|
+
this._rootTransform = rootTransform;
|
|
56
|
+
this._pointTransforms = pointTransforms;
|
|
57
|
+
this._colliderTransforms = colliders.map((c)=>c.transform);
|
|
58
|
+
this._grabberTransforms = grabbers.map((g)=>g.transform);
|
|
59
|
+
// Build constraints
|
|
60
|
+
this._constraints = buildConstraints(rootPoints, this._config.constraintOptions);
|
|
61
|
+
this._surfaceConstraints = buildSurfaceFaces(rootPoints, this._config.constraintOptions.isLoop);
|
|
62
|
+
// Flatten bone hierarchy to point list, build parent map
|
|
63
|
+
const allPoints = [];
|
|
64
|
+
const parentMap = new Map(); // child index → parent index
|
|
65
|
+
function walk(node) {
|
|
66
|
+
allPoints.push(node);
|
|
67
|
+
for (const c of node.children){
|
|
68
|
+
parentMap.set(c.index, node.index);
|
|
69
|
+
walk(c);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
for (const r of rootPoints){
|
|
73
|
+
walk(r);
|
|
74
|
+
}
|
|
75
|
+
const maxDepth = Math.max(...allPoints.map((p)=>p.depth));
|
|
76
|
+
// Compute boneAxis from transforms: local-space direction to first child
|
|
77
|
+
// This is critical for correct bone rotation in skinned meshes
|
|
78
|
+
const indexToNode = new Map();
|
|
79
|
+
for (const p of allPoints){
|
|
80
|
+
indexToNode.set(p.index, p);
|
|
81
|
+
}
|
|
82
|
+
for (const p of allPoints){
|
|
83
|
+
if (!p.boneAxis && p.children.length > 0) {
|
|
84
|
+
const childPos = pointTransforms[p.children[0].index].getWorldPosition();
|
|
85
|
+
const parentPos = pointTransforms[p.index].getWorldPosition();
|
|
86
|
+
// InverseTransformPoint: convert child world pos to parent local space
|
|
87
|
+
const parentRot = pointTransforms[p.index].getWorldRotation();
|
|
88
|
+
const parentScale = pointTransforms[p.index].getLocalScale();
|
|
89
|
+
const diff = Vector3.sub(childPos, parentPos);
|
|
90
|
+
const invRot = Quaternion.inverse(parentRot);
|
|
91
|
+
const localDir = invRot.transform(diff);
|
|
92
|
+
// Apply inverse scale
|
|
93
|
+
const unscaled = new Vector3(parentScale.x !== 0 ? localDir.x / parentScale.x : 0, parentScale.y !== 0 ? localDir.y / parentScale.y : 0, parentScale.z !== 0 ? localDir.z / parentScale.z : 0);
|
|
94
|
+
p.boneAxis = Vector3.normalize(unscaled);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Build PointR/RW arrays
|
|
98
|
+
this._pointsR = allPoints.map((p)=>this._createPointR(p, maxDepth, parentMap, pointTransforms));
|
|
99
|
+
this._pointsRW = allPoints.map(()=>this._createPointRW());
|
|
100
|
+
this._positionsToTransform = new Array(allPoints.length).fill(Vector3.zero());
|
|
101
|
+
// Initialize colliders
|
|
102
|
+
this._collidersR = colliders.map((c)=>c.r);
|
|
103
|
+
this._collidersRW = colliders.map((c)=>this._createColliderRW(c.transform));
|
|
104
|
+
this._colliderHandleIds = this._collidersR.map(()=>this._nextColliderHandleId++);
|
|
105
|
+
this._rebuildColliderHandleMap();
|
|
106
|
+
// Initialize grabbers
|
|
107
|
+
this._grabbersR = grabbers.map((g)=>g.r);
|
|
108
|
+
this._grabbersRW = grabbers.map((g)=>({
|
|
109
|
+
enabled: g.enabled ? 1 : 0,
|
|
110
|
+
position: g.transform.getWorldPosition()
|
|
111
|
+
}));
|
|
112
|
+
this._grabberHandleIds = this._grabbersR.map(()=>this._nextGrabberHandleId++);
|
|
113
|
+
this._rebuildGrabberHandleMap();
|
|
114
|
+
// Flat planes
|
|
115
|
+
this._flatPlanes = flatPlanes.map((fp)=>({
|
|
116
|
+
normal: Vector3.normalize(fp.up),
|
|
117
|
+
distance: -Vector3.dot(Vector3.normalize(fp.up), fp.position)
|
|
118
|
+
}));
|
|
119
|
+
this._flatPlaneAll = [
|
|
120
|
+
...this._flatPlanes
|
|
121
|
+
];
|
|
122
|
+
this._flatPlaneEnabled = this._flatPlaneAll.map(()=>true);
|
|
123
|
+
this._flatPlaneHandleIds = this._flatPlaneAll.map(()=>this._nextFlatPlaneHandleId++);
|
|
124
|
+
this._rebuildFlatPlaneHandleMap();
|
|
125
|
+
// Capture initial state
|
|
126
|
+
this._previousRootPosition = rootTransform.getWorldPosition();
|
|
127
|
+
this._previousRootRotation = rootTransform.getWorldRotation();
|
|
128
|
+
for(let i = 0; i < this._pointsRW.length; i++){
|
|
129
|
+
const pos = pointTransforms[i].getWorldPosition();
|
|
130
|
+
this._pointsRW[i].positionCurrent = pos.clone();
|
|
131
|
+
this._pointsRW[i].positionPrevious = pos.clone();
|
|
132
|
+
this._pointsRW[i].positionCurrentTransform = pos.clone();
|
|
133
|
+
this._pointsRW[i].positionPreviousTransform = pos.clone();
|
|
134
|
+
this._pointsRW[i].positionToTransform = pos.clone();
|
|
135
|
+
this._pointsRW[i].directionPrevious = Vector3.axisPZ();
|
|
136
|
+
this._pointsRW[i].fakeWindDirection = Vector3.axisPZ();
|
|
137
|
+
this._pointsRW[i].grabberIndex = -1;
|
|
138
|
+
this._pointsRW[i].grabberDistance = 0;
|
|
139
|
+
this._pointsRW[i].friction = 0;
|
|
140
|
+
}
|
|
141
|
+
this._initialized = true;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 执行一帧物理模拟
|
|
145
|
+
* 内部流程:读取当前 transform → 运行 Verlet 积分和约束求解 → 写回 transform
|
|
146
|
+
* @param deltaTime 帧间隔时间(秒)。内部会被 subSteps 细分
|
|
147
|
+
*/ step(deltaTime) {
|
|
148
|
+
if (!this._initialized || !this._rootTransform) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
// Update fade
|
|
152
|
+
if (this._fadeState !== 'none') {
|
|
153
|
+
this._fadeTimer += deltaTime;
|
|
154
|
+
if (this._fadeTimer >= this._fadeDuration) {
|
|
155
|
+
this._fadeState = 'none';
|
|
156
|
+
this._fadeTimer = 0;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const blendRatio = this._computeBlendRatio();
|
|
160
|
+
// Capture current transforms
|
|
161
|
+
const rootPos = this._rootTransform.getWorldPosition();
|
|
162
|
+
const rootRot = this._rootTransform.getWorldRotation();
|
|
163
|
+
for(let i = 0; i < this._pointsRW.length; i++){
|
|
164
|
+
this._pointsRW[i].positionPreviousTransform = this._pointsRW[i].positionCurrentTransform.clone();
|
|
165
|
+
this._pointsRW[i].positionCurrentTransform = this._pointTransforms[i].getWorldPosition();
|
|
166
|
+
}
|
|
167
|
+
for(let i = 0; i < this._collidersRW.length; i++){
|
|
168
|
+
const t = this._colliderTransforms[i];
|
|
169
|
+
this._collidersRW[i].positionPreviousTransform = this._collidersRW[i].positionCurrentTransform.clone();
|
|
170
|
+
this._collidersRW[i].directionPreviousTransform = this._collidersRW[i].directionCurrentTransform.clone();
|
|
171
|
+
this._collidersRW[i].positionCurrentTransform = t.getWorldPosition();
|
|
172
|
+
this._collidersRW[i].directionCurrentTransform = t.getWorldRotation();
|
|
173
|
+
this._collidersRW[i].worldScale = t.getWorldScale();
|
|
174
|
+
this._collidersRW[i].worldToLocal = Matrix4x4.compose(this._collidersRW[i].worldScale, this._collidersRW[i].directionCurrentTransform, this._collidersRW[i].positionCurrentTransform).inplaceInvertAffine();
|
|
175
|
+
}
|
|
176
|
+
for(let i = 0; i < this._grabbersRW.length; i++){
|
|
177
|
+
this._grabbersRW[i].position = this._grabberTransforms[i].getWorldPosition();
|
|
178
|
+
}
|
|
179
|
+
// Run simulation
|
|
180
|
+
const params = {
|
|
181
|
+
isPaused: this._isPaused,
|
|
182
|
+
stepTime: deltaTime,
|
|
183
|
+
subSteps: this._config.subSteps,
|
|
184
|
+
rootPosition: rootPos,
|
|
185
|
+
previousRootPosition: this._previousRootPosition,
|
|
186
|
+
rootSlideLimit: this._config.rootSlideLimit,
|
|
187
|
+
rootRotation: rootRot,
|
|
188
|
+
previousRootRotation: this._previousRootRotation,
|
|
189
|
+
rootRotateLimit: this._config.rootRotateLimit,
|
|
190
|
+
windForce: this._config.windForce,
|
|
191
|
+
enableSurfaceCollision: this._config.enableSurfaceCollision,
|
|
192
|
+
surfaceConstraints: this._surfaceConstraints,
|
|
193
|
+
relaxation: this._config.relaxation,
|
|
194
|
+
constraintShrinkLimit: this._config.constraintShrinkLimit,
|
|
195
|
+
blendRatio,
|
|
196
|
+
isFakeWave: this._config.isFakeWave,
|
|
197
|
+
fakeWaveSpeed: this._config.fakeWaveSpeed,
|
|
198
|
+
fakeWavePower: this._config.fakeWavePower,
|
|
199
|
+
fakeWaveCounter: this._fakeWaveCounter,
|
|
200
|
+
collisionScale: 1.0,
|
|
201
|
+
enableBroadPhase: this._config.enableBroadPhase
|
|
202
|
+
};
|
|
203
|
+
const result = simulate(params, this._pointsR, this._pointsRW, this._constraints, this._collidersR, this._collidersRW, this._grabbersR, this._grabbersRW, this._movableLimitTargets, this._flatPlanes);
|
|
204
|
+
this._positionsToTransform = result.positionsToTransform;
|
|
205
|
+
this._fakeWaveCounter = result.fakeWaveCounter;
|
|
206
|
+
// Angle limits
|
|
207
|
+
if (this._config.angleLimitConfig.angleLimit >= 0) {
|
|
208
|
+
applyAngleLimits(this._pointsR, this._pointsRW, this._config.angleLimitConfig);
|
|
209
|
+
}
|
|
210
|
+
// Apply results
|
|
211
|
+
const transformRots = this._pointTransforms.map((t)=>t.getWorldRotation());
|
|
212
|
+
const transformLocalRots = this._pointTransforms.map((t)=>t.getLocalRotation());
|
|
213
|
+
const outputs = applyResult(this._pointsR, this._pointsRW, this._positionsToTransform, blendRatio, transformRots, transformLocalRots, this._config.preserveTwist);
|
|
214
|
+
for(let i = 0; i < outputs.length; i++){
|
|
215
|
+
this._pointTransforms[i].setWorldPosition(outputs[i].position);
|
|
216
|
+
this._pointTransforms[i].setWorldRotation(outputs[i].rotation);
|
|
217
|
+
}
|
|
218
|
+
this._previousRootPosition = rootPos;
|
|
219
|
+
this._previousRootRotation = rootRot;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* 获取所有点的模拟结果(世界坐标位置和旋转)
|
|
223
|
+
* 通常不需要手动调用——step() 已经自动写回 transform。
|
|
224
|
+
* 适用于需要在不写回 transform 的情况下读取物理结果的场景。
|
|
225
|
+
*/ getResults() {
|
|
226
|
+
const transformRots = this._pointTransforms.map((t)=>t.getWorldRotation());
|
|
227
|
+
const transformLocalRots = this._pointTransforms.map((t)=>t.getLocalRotation());
|
|
228
|
+
const outputs = applyResult(this._pointsR, this._pointsRW, this._positionsToTransform, this._config.blendRatio, transformRots, transformLocalRots, this._config.preserveTwist);
|
|
229
|
+
return outputs.map((o)=>({
|
|
230
|
+
position: o.position,
|
|
231
|
+
rotation: o.rotation
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* 瞬移补偿——在角色传送/瞬移后调用
|
|
236
|
+
* 将上一帧的根骨骼位置和旋转设为当前值,
|
|
237
|
+
* 使下一帧的根运动增量为零,避免物理点因巨大位移而弹射。
|
|
238
|
+
*/ warp() {
|
|
239
|
+
if (!this._rootTransform) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this._previousRootPosition = this._rootTransform.getWorldPosition();
|
|
243
|
+
this._previousRootRotation = this._rootTransform.getWorldRotation();
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* 重置所有物理状态
|
|
247
|
+
* 将所有点的模拟位置重置为当前 transform 位置,清除速度和抓取状态。
|
|
248
|
+
* 适用于场景切换或需要消除所有物理运动的场景。
|
|
249
|
+
*/ reset() {
|
|
250
|
+
for(let i = 0; i < this._pointsRW.length; i++){
|
|
251
|
+
const pos = this._pointTransforms[i].getWorldPosition();
|
|
252
|
+
this._pointsRW[i].positionCurrent = pos.clone();
|
|
253
|
+
this._pointsRW[i].positionPrevious = pos.clone();
|
|
254
|
+
this._pointsRW[i].grabberIndex = -1;
|
|
255
|
+
}
|
|
256
|
+
this._fakeWaveCounter = 0;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* 释放固定点为动态点(如脱衣、布料脱落)
|
|
260
|
+
* 将 weight 从 0 改为 1,同时重置该点的模拟状态避免瞬间弹射。
|
|
261
|
+
* @param index 点的索引
|
|
262
|
+
*/ releasePoint(index) {
|
|
263
|
+
if (index < 0 || index >= this._pointsR.length) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
this._pointsR[index].weight = 1;
|
|
267
|
+
const pos = this._pointTransforms[index].getWorldPosition();
|
|
268
|
+
this._pointsRW[index].positionCurrent = pos.clone();
|
|
269
|
+
this._pointsRW[index].positionPrevious = pos.clone();
|
|
270
|
+
this._pointsRW[index].grabberIndex = -1;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 将动态点重新固定为跟随动画(如重新穿戴)
|
|
274
|
+
* 将 weight 从 1 改为 0,清除抓取状态。
|
|
275
|
+
* @param index 点的索引
|
|
276
|
+
*/ fixPoint(index) {
|
|
277
|
+
if (index < 0 || index >= this._pointsR.length) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
this._pointsR[index].weight = 0;
|
|
281
|
+
this._pointsRW[index].grabberIndex = -1;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* 查询某个点是否为固定状态(weight=0,跟随动画)
|
|
285
|
+
* @param index 点的索引
|
|
286
|
+
*/ isPointFixed(index) {
|
|
287
|
+
if (index < 0 || index >= this._pointsR.length) {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
return this._pointsR[index].weight <= EPSILON;
|
|
291
|
+
}
|
|
292
|
+
/** 获取物理点总数 */ get pointCount() {
|
|
293
|
+
return this._pointsR.length;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 渐入物理——从动画姿态平滑过渡到物理模拟
|
|
297
|
+
* blendRatio 从 1(纯动画)渐变到 0(纯物理)
|
|
298
|
+
* @param seconds 过渡时长(秒)
|
|
299
|
+
*/ fadeIn(seconds) {
|
|
300
|
+
this._fadeState = 'in';
|
|
301
|
+
this._fadeTimer = 0;
|
|
302
|
+
this._fadeDuration = seconds;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* 渐出物理——从物理模拟平滑过渡回动画姿态
|
|
306
|
+
* blendRatio 从 0(纯物理)渐变到 1(纯动画)
|
|
307
|
+
* @param seconds 过渡时长(秒)
|
|
308
|
+
*/ fadeOut(seconds) {
|
|
309
|
+
this._fadeState = 'out';
|
|
310
|
+
this._fadeTimer = 0;
|
|
311
|
+
this._fadeDuration = seconds;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* 设置全局风力向量
|
|
315
|
+
* 风力会按每个点的 windForceScale 和质量进行缩放
|
|
316
|
+
* @param wind 风力向量(世界坐标)
|
|
317
|
+
*/ setWindForce(wind) {
|
|
318
|
+
this._config.windForce = wind;
|
|
319
|
+
}
|
|
320
|
+
/** Enable/disable broad-phase pruning for runtime performance comparison */ setBroadPhaseEnabled(enabled) {
|
|
321
|
+
this._config.enableBroadPhase = enabled;
|
|
322
|
+
}
|
|
323
|
+
/** The blend ratio for the physics simulation */ get blendRatio() {
|
|
324
|
+
return this._config.blendRatio;
|
|
325
|
+
}
|
|
326
|
+
set blendRatio(value) {
|
|
327
|
+
this._config.blendRatio = clamp01(value);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* 暂停/恢复物理模拟
|
|
331
|
+
* 暂停时仍会跟随根骨骼运动,但不进行力学积分和约束求解
|
|
332
|
+
* @param paused true=暂停, false=恢复
|
|
333
|
+
*/ setPaused(paused) {
|
|
334
|
+
this._isPaused = paused;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Enable or disable a collider by current array index.
|
|
338
|
+
* Transform state is still read from the collider's TransformAccess each frame.
|
|
339
|
+
* Prefer setColliderEnabled(handle, enabled) for runtime-owned colliders.
|
|
340
|
+
*/ setColliderEnabledAt(index, enabled) {
|
|
341
|
+
if (index >= 0 && index < this._collidersRW.length) {
|
|
342
|
+
this._collidersRW[index].enabled = enabled ? 1 : 0;
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Enable or disable a runtime collider by stable handle.
|
|
349
|
+
* @returns true if the handle is still valid.
|
|
350
|
+
*/ setColliderEnabled(handle, enabled) {
|
|
351
|
+
const index = this._getColliderIndex(handle);
|
|
352
|
+
if (index === -1) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
return this.setColliderEnabledAt(index, enabled);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Add a collider at runtime.
|
|
359
|
+
* @returns A stable handle that remains valid until this collider is removed.
|
|
360
|
+
*/ addCollider(r, transform) {
|
|
361
|
+
const id = this._nextColliderHandleId++;
|
|
362
|
+
this._collidersR.push(r);
|
|
363
|
+
this._colliderTransforms.push(transform);
|
|
364
|
+
this._collidersRW.push(this._createColliderRW(transform));
|
|
365
|
+
this._colliderHandleIds.push(id);
|
|
366
|
+
this._colliderHandleToIndex.set(id, this._collidersR.length - 1);
|
|
367
|
+
return {
|
|
368
|
+
type: 'collider',
|
|
369
|
+
id
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Remove a collider by stable handle.
|
|
374
|
+
* @returns true if the collider existed and was removed.
|
|
375
|
+
*/ removeCollider(handle) {
|
|
376
|
+
const index = this._getColliderIndex(handle);
|
|
377
|
+
if (index === -1) {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
return this.removeColliderAt(index);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Remove a collider by current array index.
|
|
384
|
+
* Prefer removeCollider(handle) for runtime-owned colliders.
|
|
385
|
+
*/ removeColliderAt(index) {
|
|
386
|
+
if (index < 0 || index >= this._collidersR.length) {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
this._collidersR.splice(index, 1);
|
|
390
|
+
this._colliderTransforms.splice(index, 1);
|
|
391
|
+
this._collidersRW.splice(index, 1);
|
|
392
|
+
this._colliderHandleIds.splice(index, 1);
|
|
393
|
+
this._rebuildColliderHandleMap();
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Enable or disable a flat plane by current array index.
|
|
398
|
+
* Prefer setFlatPlaneEnabled(handle, enabled) for runtime-owned flat planes.
|
|
399
|
+
*/ setFlatPlaneEnabledAt(index, enabled) {
|
|
400
|
+
if (index < 0 || index >= this._flatPlaneAll.length) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
this._flatPlaneEnabled[index] = enabled;
|
|
404
|
+
this._rebuildActiveFlatPlanes();
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Enable or disable a runtime flat plane by stable handle.
|
|
409
|
+
* @returns true if the handle is still valid.
|
|
410
|
+
*/ setFlatPlaneEnabled(handle, enabled) {
|
|
411
|
+
const index = this._getFlatPlaneIndex(handle);
|
|
412
|
+
if (index === -1) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
return this.setFlatPlaneEnabledAt(index, enabled);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Add a flat plane at runtime.
|
|
419
|
+
* @returns A stable handle that remains valid until this flat plane is removed.
|
|
420
|
+
*/ addFlatPlane(up, position) {
|
|
421
|
+
const id = this._nextFlatPlaneHandleId++;
|
|
422
|
+
const normal = Vector3.normalize(up);
|
|
423
|
+
this._flatPlaneAll.push({
|
|
424
|
+
normal,
|
|
425
|
+
distance: -Vector3.dot(normal, position)
|
|
426
|
+
});
|
|
427
|
+
this._flatPlaneEnabled.push(true);
|
|
428
|
+
this._flatPlaneHandleIds.push(id);
|
|
429
|
+
this._flatPlaneHandleToIndex.set(id, this._flatPlaneAll.length - 1);
|
|
430
|
+
this._rebuildActiveFlatPlanes();
|
|
431
|
+
return {
|
|
432
|
+
type: 'flatPlane',
|
|
433
|
+
id
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Remove a flat plane by stable handle.
|
|
438
|
+
* @returns true if the flat plane existed and was removed.
|
|
439
|
+
*/ removeFlatPlane(handle) {
|
|
440
|
+
const index = this._getFlatPlaneIndex(handle);
|
|
441
|
+
if (index === -1) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
return this.removeFlatPlaneAt(index);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Remove a flat plane by current array index.
|
|
448
|
+
* Prefer removeFlatPlane(handle) for runtime-owned flat planes.
|
|
449
|
+
*/ removeFlatPlaneAt(index) {
|
|
450
|
+
if (index < 0 || index >= this._flatPlaneAll.length) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
this._flatPlaneAll.splice(index, 1);
|
|
454
|
+
this._flatPlaneEnabled.splice(index, 1);
|
|
455
|
+
this._flatPlaneHandleIds.splice(index, 1);
|
|
456
|
+
this._rebuildFlatPlaneHandleMap();
|
|
457
|
+
this._rebuildActiveFlatPlanes();
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Enable or disable a grabber by current array index.
|
|
462
|
+
* Transform state is still read from the grabber's TransformAccess each frame.
|
|
463
|
+
* Prefer setGrabberEnabled(handle, enabled) for runtime-owned grabbers.
|
|
464
|
+
*/ setGrabberEnabledAt(index, enabled) {
|
|
465
|
+
if (index >= 0 && index < this._grabbersRW.length) {
|
|
466
|
+
this._grabbersRW[index].enabled = enabled ? 1 : 0;
|
|
467
|
+
if (!enabled) {
|
|
468
|
+
this._releaseGrabber(index);
|
|
469
|
+
}
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Enable or disable a runtime grabber by stable handle.
|
|
476
|
+
* @returns true if the handle is still valid.
|
|
477
|
+
*/ setGrabberEnabled(handle, enabled) {
|
|
478
|
+
const index = this._getGrabberIndex(handle);
|
|
479
|
+
if (index === -1) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
return this.setGrabberEnabledAt(index, enabled);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Add a grabber at runtime.
|
|
486
|
+
* @returns A stable handle that remains valid until this grabber is removed.
|
|
487
|
+
*/ addGrabber(r, transform, enabled = false) {
|
|
488
|
+
const id = this._nextGrabberHandleId++;
|
|
489
|
+
this._grabbersR.push(r);
|
|
490
|
+
this._grabberTransforms.push(transform);
|
|
491
|
+
this._grabbersRW.push({
|
|
492
|
+
enabled: enabled ? 1 : 0,
|
|
493
|
+
position: transform.getWorldPosition()
|
|
494
|
+
});
|
|
495
|
+
this._grabberHandleIds.push(id);
|
|
496
|
+
this._grabberHandleToIndex.set(id, this._grabbersR.length - 1);
|
|
497
|
+
return {
|
|
498
|
+
type: 'grabber',
|
|
499
|
+
id
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Remove a grabber by stable handle.
|
|
504
|
+
* @returns true if the grabber existed and was removed.
|
|
505
|
+
*/ removeGrabber(handle) {
|
|
506
|
+
const index = this._getGrabberIndex(handle);
|
|
507
|
+
if (index === -1) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
return this.removeGrabberAt(index);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Remove a grabber by current array index.
|
|
514
|
+
* Prefer removeGrabber(handle) for runtime-owned grabbers.
|
|
515
|
+
*/ removeGrabberAt(index) {
|
|
516
|
+
if (index < 0 || index >= this._grabbersR.length) {
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
this._grabbersR.splice(index, 1);
|
|
520
|
+
this._grabberTransforms.splice(index, 1);
|
|
521
|
+
this._grabbersRW.splice(index, 1);
|
|
522
|
+
this._grabberHandleIds.splice(index, 1);
|
|
523
|
+
this._rebuildGrabberHandleMap();
|
|
524
|
+
this._releaseGrabber(index);
|
|
525
|
+
for (const ptRW of this._pointsRW){
|
|
526
|
+
if (ptRW.grabberIndex > index) {
|
|
527
|
+
ptRW.grabberIndex--;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
/** Gets the number of runtime colliders. */ get colliderCount() {
|
|
533
|
+
return this._collidersR.length;
|
|
534
|
+
}
|
|
535
|
+
/** Gets the number of runtime flat planes. */ get flatPlaneCount() {
|
|
536
|
+
return this._flatPlaneAll.length;
|
|
537
|
+
}
|
|
538
|
+
/** Gets the number of runtime grabbers. */ get grabberCount() {
|
|
539
|
+
return this._grabbersR.length;
|
|
540
|
+
}
|
|
541
|
+
_computeBlendRatio() {
|
|
542
|
+
if (this._fadeState === 'in') {
|
|
543
|
+
return 1.0 - clamp01(this._fadeTimer / this._fadeDuration);
|
|
544
|
+
}
|
|
545
|
+
if (this._fadeState === 'out') {
|
|
546
|
+
return clamp01(this._fadeTimer / this._fadeDuration);
|
|
547
|
+
}
|
|
548
|
+
return this._config.blendRatio;
|
|
549
|
+
}
|
|
550
|
+
_getColliderIndex(handle) {
|
|
551
|
+
if (handle.type !== 'collider') {
|
|
552
|
+
return -1;
|
|
553
|
+
}
|
|
554
|
+
return this._colliderHandleToIndex.get(handle.id) ?? -1;
|
|
555
|
+
}
|
|
556
|
+
_getGrabberIndex(handle) {
|
|
557
|
+
if (handle.type !== 'grabber') {
|
|
558
|
+
return -1;
|
|
559
|
+
}
|
|
560
|
+
return this._grabberHandleToIndex.get(handle.id) ?? -1;
|
|
561
|
+
}
|
|
562
|
+
_getFlatPlaneIndex(handle) {
|
|
563
|
+
if (handle.type !== 'flatPlane') {
|
|
564
|
+
return -1;
|
|
565
|
+
}
|
|
566
|
+
return this._flatPlaneHandleToIndex.get(handle.id) ?? -1;
|
|
567
|
+
}
|
|
568
|
+
_releaseGrabber(index) {
|
|
569
|
+
for (const ptRW of this._pointsRW){
|
|
570
|
+
if (ptRW.grabberIndex === index) {
|
|
571
|
+
ptRW.grabberIndex = -1;
|
|
572
|
+
ptRW.grabberDistance = 0;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
_rebuildColliderHandleMap() {
|
|
577
|
+
this._colliderHandleToIndex.clear();
|
|
578
|
+
for(let i = 0; i < this._colliderHandleIds.length; i++){
|
|
579
|
+
this._colliderHandleToIndex.set(this._colliderHandleIds[i], i);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
_rebuildFlatPlaneHandleMap() {
|
|
583
|
+
this._flatPlaneHandleToIndex.clear();
|
|
584
|
+
for(let i = 0; i < this._flatPlaneHandleIds.length; i++){
|
|
585
|
+
this._flatPlaneHandleToIndex.set(this._flatPlaneHandleIds[i], i);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
_rebuildActiveFlatPlanes() {
|
|
589
|
+
this._flatPlanes = [];
|
|
590
|
+
for(let i = 0; i < this._flatPlaneAll.length; i++){
|
|
591
|
+
if (this._flatPlaneEnabled[i]) {
|
|
592
|
+
this._flatPlanes.push(this._flatPlaneAll[i]);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
_rebuildGrabberHandleMap() {
|
|
597
|
+
this._grabberHandleToIndex.clear();
|
|
598
|
+
for(let i = 0; i < this._grabberHandleIds.length; i++){
|
|
599
|
+
this._grabberHandleToIndex.set(this._grabberHandleIds[i], i);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
_createPointR(node, maxDepth, parentMap, transforms) {
|
|
603
|
+
const rate = maxDepth > 0 ? node.depth / maxDepth : 0;
|
|
604
|
+
const c = this._config.curves;
|
|
605
|
+
const parentIdx = parentMap.get(node.index) ?? -1;
|
|
606
|
+
const childIdx = node.children.length > 0 ? node.children[0].index : -1;
|
|
607
|
+
// Capture initial local transform for rotation blending
|
|
608
|
+
const t = transforms[node.index];
|
|
609
|
+
const initLocalPos = t.getLocalPosition();
|
|
610
|
+
const initLocalRot = t.getLocalRotation();
|
|
611
|
+
const initLocalScale = t.getLocalScale();
|
|
612
|
+
// BoneAxis: local-space direction to first child (computed in initialize)
|
|
613
|
+
const boneAxis = node.boneAxis ?? new Vector3(0, -1, 0);
|
|
614
|
+
// ParentLength: distance to parent
|
|
615
|
+
let parentLength = 0;
|
|
616
|
+
if (parentIdx !== -1) {
|
|
617
|
+
const pPos = transforms[parentIdx].getWorldPosition();
|
|
618
|
+
const cPos = transforms[node.index].getWorldPosition();
|
|
619
|
+
parentLength = Vector3.distance(pPos, cPos);
|
|
620
|
+
}
|
|
621
|
+
return {
|
|
622
|
+
parent: parentIdx,
|
|
623
|
+
child: childIdx,
|
|
624
|
+
applyInvertCollision: 0,
|
|
625
|
+
movableLimitIndex: -1,
|
|
626
|
+
movableLimitRadius: 0,
|
|
627
|
+
weight: node.isFixed ? 0 : 1,
|
|
628
|
+
mass: c.massScale.evaluate(rate),
|
|
629
|
+
resistance: clamp01(c.resistance.evaluate(rate)),
|
|
630
|
+
hardness: clamp01(c.hardness.evaluate(rate)),
|
|
631
|
+
frictionScale: c.friction.evaluate(rate),
|
|
632
|
+
sliderJointLength: c.sliderJointLength.evaluate(rate),
|
|
633
|
+
parentLength,
|
|
634
|
+
structuralShrinkVertical: c.structuralShrinkVertical.evaluate(rate) * 0.5,
|
|
635
|
+
structuralStretchVertical: c.structuralStretchVertical.evaluate(rate) * 0.5,
|
|
636
|
+
structuralShrinkHorizontal: c.structuralShrinkHorizontal.evaluate(rate) * 0.5,
|
|
637
|
+
structuralStretchHorizontal: c.structuralStretchHorizontal.evaluate(rate) * 0.5,
|
|
638
|
+
shearShrink: c.shearShrink.evaluate(rate) * 0.5,
|
|
639
|
+
shearStretch: c.shearStretch.evaluate(rate) * 0.5,
|
|
640
|
+
bendingShrinkVertical: c.bendingShrinkVertical.evaluate(rate) * 0.5,
|
|
641
|
+
bendingStretchVertical: c.bendingStretchVertical.evaluate(rate) * 0.5,
|
|
642
|
+
bendingShrinkHorizontal: c.bendingShrinkHorizontal.evaluate(rate) * 0.5,
|
|
643
|
+
bendingStretchHorizontal: c.bendingStretchHorizontal.evaluate(rate) * 0.5,
|
|
644
|
+
windForceScale: c.windForceScale.evaluate(rate) * rate,
|
|
645
|
+
fakeWavePower: c.fakeWavePower.evaluate(rate),
|
|
646
|
+
fakeWaveFreq: c.fakeWaveFreq.evaluate(rate),
|
|
647
|
+
forceFadeRatio: 0,
|
|
648
|
+
pointRadius: Math.max(0, c.pointRadius.evaluate(rate)),
|
|
649
|
+
gravity: Vector3.scale(this._config.gravity, c.gravityScale.evaluate(rate)),
|
|
650
|
+
boneAxis,
|
|
651
|
+
initialLocalScale: initLocalScale,
|
|
652
|
+
initialLocalRotation: initLocalRot,
|
|
653
|
+
initialLocalTwist: (()=>{
|
|
654
|
+
const tw = new Quaternion();
|
|
655
|
+
initLocalRot.decomposeSwingTwist(boneAxis, undefined, tw);
|
|
656
|
+
return tw;
|
|
657
|
+
})(),
|
|
658
|
+
initialLocalPosition: initLocalPos
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
_createPointRW() {
|
|
662
|
+
return {
|
|
663
|
+
positionToTransform: Vector3.zero(),
|
|
664
|
+
positionCurrentTransform: Vector3.zero(),
|
|
665
|
+
positionPreviousTransform: Vector3.zero(),
|
|
666
|
+
positionCurrent: Vector3.zero(),
|
|
667
|
+
positionPrevious: Vector3.zero(),
|
|
668
|
+
directionPrevious: Vector3.axisPZ(),
|
|
669
|
+
fakeWindDirection: Vector3.axisPZ(),
|
|
670
|
+
grabberIndex: -1,
|
|
671
|
+
grabberDistance: 0,
|
|
672
|
+
friction: 0
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
_createColliderRW(transform) {
|
|
676
|
+
const pos = transform.getWorldPosition();
|
|
677
|
+
const rot = transform.getWorldRotation();
|
|
678
|
+
const scale = transform.getWorldScale();
|
|
679
|
+
return {
|
|
680
|
+
positionCurrent: pos.clone(),
|
|
681
|
+
directionCurrent: Vector3.zero(),
|
|
682
|
+
boundsCenter: pos.clone(),
|
|
683
|
+
boundsRadius: 0,
|
|
684
|
+
positionCurrentTransform: pos.clone(),
|
|
685
|
+
positionPreviousTransform: pos.clone(),
|
|
686
|
+
directionCurrentTransform: rot.clone(),
|
|
687
|
+
directionPreviousTransform: rot.clone(),
|
|
688
|
+
worldToLocal: Matrix4x4.identity(),
|
|
689
|
+
worldScale: scale.clone(),
|
|
690
|
+
localBoundsMin: Vector3.zero(),
|
|
691
|
+
localBoundsMax: Vector3.zero(),
|
|
692
|
+
radius: 0,
|
|
693
|
+
enabled: 1
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
export { JointDynamicsSystemController };
|
|
699
|
+
//# sourceMappingURL=controller.js.map
|