@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,624 @@
|
|
|
1
|
+
import { Vector3, Quaternion, degree2radian, Matrix4x4, radian2degree } from '@zephyr3d/base';
|
|
2
|
+
import { clamp01, smoothStep } from './math.js';
|
|
3
|
+
import { EPSILON, ConstraintType } from './types.js';
|
|
4
|
+
import { checkSurfaceCollision, pushInFromCapsule, pushoutFromCapsule, pushInFromSphere, pushoutFromSphere, collisionDetection } from './collision.js';
|
|
5
|
+
|
|
6
|
+
// Core physics simulation 閳?direct port of JobExecuteSimulation from SPCRJointDynamicsJob.cs
|
|
7
|
+
const AXIS_Y = new Vector3(0, 1, 0);
|
|
8
|
+
const VEC3_ONE = new Vector3(1, 1, 1);
|
|
9
|
+
const _computeCapsuleAxis = new Vector3();
|
|
10
|
+
const _computeCapsuleHalfDir = new Vector3();
|
|
11
|
+
const _applySystemTransformOffset = new Vector3();
|
|
12
|
+
const _applySystemTransformRotated = new Vector3();
|
|
13
|
+
const _broadPhaseSegmentDir = new Vector3();
|
|
14
|
+
const _broadPhaseToCenter = new Vector3();
|
|
15
|
+
const _broadPhaseClosestPoint = new Vector3();
|
|
16
|
+
const _broadPhaseDelta = new Vector3();
|
|
17
|
+
/**
|
|
18
|
+
* Run the full simulation step. Mutates pointsRW and collidersRW in place.
|
|
19
|
+
* Returns { positionsToTransform, fakeWaveCounter }.
|
|
20
|
+
*/ function simulate(params, pointsR, pointsRW, constraints, collidersR, collidersRW, grabbersR, grabbersRW, movableLimitTargets, flatPlanes) {
|
|
21
|
+
const pointCount = pointsR.length;
|
|
22
|
+
const positionsToTransform = new Array(pointCount);
|
|
23
|
+
let subSteps = params.subSteps;
|
|
24
|
+
if (params.isPaused) {
|
|
25
|
+
subSteps = 1;
|
|
26
|
+
}
|
|
27
|
+
let fakeWaveFreq = params.fakeWaveCounter;
|
|
28
|
+
// Root slide offset
|
|
29
|
+
let rootSlideOffset = Vector3.zero();
|
|
30
|
+
const rootDeltaSlide = Vector3.sub(params.rootPosition, params.previousRootPosition);
|
|
31
|
+
const rootDeltaSlideLen = rootDeltaSlide.magnitude;
|
|
32
|
+
if (params.rootSlideLimit >= 0 && rootDeltaSlideLen > params.rootSlideLimit) {
|
|
33
|
+
rootSlideOffset = Vector3.scale(rootDeltaSlide, (1.0 - params.rootSlideLimit / rootDeltaSlideLen) / subSteps);
|
|
34
|
+
}
|
|
35
|
+
// Root rotation offset
|
|
36
|
+
let rootRotationOffset = Quaternion.identity();
|
|
37
|
+
const rootDeltaRot = Quaternion.multiply(params.rootRotation, Quaternion.inverse(params.previousRootRotation));
|
|
38
|
+
let rotateAngle = Math.acos(Math.max(-1, Math.min(1, rootDeltaRot.w))) * 2.0 * (180 / Math.PI);
|
|
39
|
+
if (rotateAngle > 180) {
|
|
40
|
+
rotateAngle -= 360;
|
|
41
|
+
}
|
|
42
|
+
if (params.rootRotateLimit >= 0 && Math.abs(rotateAngle) > params.rootRotateLimit) {
|
|
43
|
+
let rotateAxis = new Vector3();
|
|
44
|
+
if (!isFinite(rotateAxis.x) || !isFinite(rotateAxis.y) || !isFinite(rotateAxis.z)) {
|
|
45
|
+
rotateAxis = Vector3.axisPY();
|
|
46
|
+
}
|
|
47
|
+
let angle = rotateAngle > 0 ? rotateAngle - params.rootRotateLimit : rotateAngle + params.rootRotateLimit;
|
|
48
|
+
angle /= subSteps;
|
|
49
|
+
rootRotationOffset = Quaternion.fromAxisAngle(rotateAxis, degree2radian(angle));
|
|
50
|
+
}
|
|
51
|
+
if (params.isPaused) {
|
|
52
|
+
rootSlideOffset = rootDeltaSlide;
|
|
53
|
+
rootRotationOffset = rootDeltaRot;
|
|
54
|
+
}
|
|
55
|
+
const stepTime = params.stepTime / subSteps;
|
|
56
|
+
for(let iSubStep = 1; iSubStep <= subSteps; iSubStep++){
|
|
57
|
+
const stepDelta = iSubStep / subSteps;
|
|
58
|
+
const stepTime_x2_half = stepTime * stepTime * 0.5;
|
|
59
|
+
colliderUpdate(collidersR, collidersRW, stepDelta, params.collisionScale);
|
|
60
|
+
pointUpdatePass1(pointsR, pointsRW, stepDelta, stepTime_x2_half, rootSlideOffset, rootRotationOffset, params.rootPosition, params.windForce, params.isPaused, grabbersR, grabbersRW, movableLimitTargets, flatPlanes);
|
|
61
|
+
if (!params.isPaused) {
|
|
62
|
+
fakeWaveFreq += params.fakeWaveSpeed * stepTime;
|
|
63
|
+
if (params.enableSurfaceCollision) {
|
|
64
|
+
surfaceCollision(pointsRW, collidersR, collidersRW, params.surfaceConstraints);
|
|
65
|
+
}
|
|
66
|
+
for(let iRelax = params.relaxation - 1; iRelax >= 0; --iRelax){
|
|
67
|
+
constraintUpdate(pointsR, pointsRW, constraints, collidersR, collidersRW, params.constraintShrinkLimit, params.enableBroadPhase);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
pointUpdatePass2(pointsR, pointsRW, positionsToTransform, stepDelta, fakeWaveFreq, params.blendRatio, params.isFakeWave, params.fakeWavePower);
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
positionsToTransform,
|
|
74
|
+
fakeWaveCounter: fakeWaveFreq
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// 閳光偓閳光偓 Collider interpolation 閳光偓閳光偓
|
|
78
|
+
function computeCapsule(pos, rot, height, head, direction) {
|
|
79
|
+
rot.transform(Vector3.scale(AXIS_Y, height, _computeCapsuleAxis), direction);
|
|
80
|
+
Vector3.scale(direction, 0.5, _computeCapsuleHalfDir);
|
|
81
|
+
Vector3.sub(pos, _computeCapsuleHalfDir, head);
|
|
82
|
+
}
|
|
83
|
+
function colliderUpdate(collidersR, collidersRW, stepDelta, collisionScale) {
|
|
84
|
+
const curPos = new Vector3();
|
|
85
|
+
const curDir = new Quaternion();
|
|
86
|
+
const corner = new Vector3();
|
|
87
|
+
const center = new Vector3();
|
|
88
|
+
const tailCorner = new Vector3();
|
|
89
|
+
const tailCenter = new Vector3();
|
|
90
|
+
for(let i = 0; i < collidersR.length; i++){
|
|
91
|
+
const colR = collidersR[i];
|
|
92
|
+
const colRW = collidersRW[i];
|
|
93
|
+
colRW.radius = colR.radius * collisionScale;
|
|
94
|
+
Vector3.lerp(colRW.positionPreviousTransform, colRW.positionCurrentTransform, stepDelta, curPos);
|
|
95
|
+
Quaternion.slerp(colRW.directionPreviousTransform, colRW.directionCurrentTransform, stepDelta, curDir);
|
|
96
|
+
computeCapsule(curPos, curDir, colR.height, colRW.positionCurrent, colRW.directionCurrent);
|
|
97
|
+
if (colR.height > EPSILON) {
|
|
98
|
+
Vector3.scale(colRW.directionCurrent, 0.5, _computeCapsuleHalfDir);
|
|
99
|
+
Vector3.add(colRW.positionCurrent, _computeCapsuleHalfDir, colRW.boundsCenter);
|
|
100
|
+
colRW.boundsRadius = Math.sqrt(colRW.directionCurrent.magnitudeSq) * 0.5 + Math.max(colRW.radius, colRW.radius * colR.radiusTailScale);
|
|
101
|
+
} else {
|
|
102
|
+
colRW.boundsCenter.set(colRW.positionCurrent);
|
|
103
|
+
colRW.boundsRadius = colRW.radius;
|
|
104
|
+
}
|
|
105
|
+
// Update local bounds
|
|
106
|
+
Vector3.scale(VEC3_ONE, colR.radius, corner);
|
|
107
|
+
colRW.worldToLocal.transformPointAffine(colRW.positionCurrent, center);
|
|
108
|
+
Vector3.sub(center, corner, colRW.localBoundsMin);
|
|
109
|
+
Vector3.add(center, corner, colRW.localBoundsMax);
|
|
110
|
+
if (colR.height > EPSILON) {
|
|
111
|
+
Vector3.scale(VEC3_ONE, colR.radius * colR.radiusTailScale, tailCorner);
|
|
112
|
+
Vector3.add(colRW.positionCurrent, colRW.directionCurrent, tailCenter);
|
|
113
|
+
colRW.worldToLocal.transformPointAffine(tailCenter, tailCenter);
|
|
114
|
+
colRW.localBoundsMin.setXYZ(Math.min(colRW.localBoundsMin.x, tailCenter.x - tailCorner.x), Math.min(colRW.localBoundsMin.y, tailCenter.y - tailCorner.y), Math.min(colRW.localBoundsMin.z, tailCenter.z - tailCorner.z));
|
|
115
|
+
colRW.localBoundsMax.setXYZ(Math.max(colRW.localBoundsMax.x, tailCenter.x + tailCorner.x), Math.max(colRW.localBoundsMax.y, tailCenter.y + tailCorner.y), Math.max(colRW.localBoundsMax.z, tailCenter.z + tailCorner.z));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// 閳光偓閳光偓 Apply root motion transform 閳光偓閳光偓
|
|
120
|
+
function applySystemTransform(point, pivot, slideOffset, rotOffset, result) {
|
|
121
|
+
const rotated = rotOffset.transform(Vector3.sub(point, pivot, _applySystemTransformOffset), result || _applySystemTransformRotated);
|
|
122
|
+
Vector3.add(rotated, pivot, rotated);
|
|
123
|
+
return Vector3.add(rotated, slideOffset, rotated);
|
|
124
|
+
}
|
|
125
|
+
function pointMayCollideBroadPhase(point, colRW, padding = 0) {
|
|
126
|
+
const radius = colRW.boundsRadius + padding;
|
|
127
|
+
const delta = Vector3.sub(point, colRW.boundsCenter, _broadPhaseDelta);
|
|
128
|
+
return delta.magnitudeSq <= radius * radius;
|
|
129
|
+
}
|
|
130
|
+
function segmentMayCollideBroadPhase(point1, point2, colRW) {
|
|
131
|
+
const segmentDir = Vector3.sub(point2, point1, _broadPhaseSegmentDir);
|
|
132
|
+
const lenSq = segmentDir.magnitudeSq;
|
|
133
|
+
if (lenSq <= EPSILON) {
|
|
134
|
+
return pointMayCollideBroadPhase(point1, colRW);
|
|
135
|
+
}
|
|
136
|
+
const toCenter = Vector3.sub(colRW.boundsCenter, point1, _broadPhaseToCenter);
|
|
137
|
+
const t = clamp01(Vector3.dot(toCenter, segmentDir) / lenSq);
|
|
138
|
+
const closestPoint = Vector3.scale(segmentDir, t, _broadPhaseClosestPoint);
|
|
139
|
+
Vector3.add(point1, closestPoint, closestPoint);
|
|
140
|
+
const delta = Vector3.sub(colRW.boundsCenter, closestPoint, _broadPhaseDelta);
|
|
141
|
+
return delta.magnitudeSq <= colRW.boundsRadius * colRW.boundsRadius;
|
|
142
|
+
}
|
|
143
|
+
// 閳光偓閳光偓 Pass 1: Verlet integration + forces 閳光偓閳光偓
|
|
144
|
+
function pointUpdatePass1(pointsR, pointsRW, stepDelta, stepTime_x2_half, rootSlideOffset, rootRotationOffset, rootPosition, windForce, isPaused, grabbersR, grabbersRW, movableLimitTargets, flatPlanes) {
|
|
145
|
+
const currentTransformPos = new Vector3();
|
|
146
|
+
const moveDir = new Vector3();
|
|
147
|
+
const extForce = new Vector3();
|
|
148
|
+
const displacement = new Vector3();
|
|
149
|
+
const restore = new Vector3();
|
|
150
|
+
const tempVec0 = new Vector3();
|
|
151
|
+
const tempVec1 = new Vector3();
|
|
152
|
+
const planeOffset = new Vector3();
|
|
153
|
+
for(let index = 0; index < pointsR.length; index++){
|
|
154
|
+
const ptR = pointsR[index];
|
|
155
|
+
const ptRW = pointsRW[index];
|
|
156
|
+
Vector3.lerp(ptRW.positionPreviousTransform, ptRW.positionCurrentTransform, stepDelta, currentTransformPos);
|
|
157
|
+
if (ptR.weight <= EPSILON) {
|
|
158
|
+
ptRW.positionPrevious.set(ptRW.positionCurrent);
|
|
159
|
+
ptRW.positionCurrent.set(currentTransformPos);
|
|
160
|
+
} else {
|
|
161
|
+
applySystemTransform(ptRW.positionPrevious, rootPosition, rootSlideOffset, rootRotationOffset, ptRW.positionPrevious);
|
|
162
|
+
applySystemTransform(ptRW.positionCurrent, rootPosition, rootSlideOffset, rootRotationOffset, ptRW.positionCurrent);
|
|
163
|
+
displacement.setXYZ(0, 0, 0);
|
|
164
|
+
if (!isPaused) {
|
|
165
|
+
Vector3.sub(ptRW.positionCurrent, ptRW.positionPrevious, moveDir);
|
|
166
|
+
Vector3.scale(windForce, ptR.windForceScale / ptR.mass, extForce);
|
|
167
|
+
Vector3.add(ptR.gravity, extForce, extForce);
|
|
168
|
+
Vector3.scale(extForce, stepTime_x2_half, extForce);
|
|
169
|
+
Vector3.add(moveDir, extForce, displacement);
|
|
170
|
+
Vector3.scale(displacement, ptR.resistance, displacement);
|
|
171
|
+
Vector3.scale(displacement, 1.0 - clamp01(ptRW.friction * ptR.frictionScale), displacement);
|
|
172
|
+
}
|
|
173
|
+
ptRW.positionPrevious.set(ptRW.positionCurrent);
|
|
174
|
+
Vector3.add(ptRW.positionCurrent, displacement, ptRW.positionCurrent);
|
|
175
|
+
ptRW.friction = 0;
|
|
176
|
+
if (!isPaused) {
|
|
177
|
+
// Hardness restore
|
|
178
|
+
if (ptR.hardness > 0) {
|
|
179
|
+
Vector3.sub(currentTransformPos, ptRW.positionCurrent, restore);
|
|
180
|
+
Vector3.scale(restore, ptR.hardness, restore);
|
|
181
|
+
Vector3.add(ptRW.positionCurrent, restore, ptRW.positionCurrent);
|
|
182
|
+
}
|
|
183
|
+
// Force fade ratio
|
|
184
|
+
if (ptR.forceFadeRatio > 0) {
|
|
185
|
+
Vector3.lerp(ptRW.positionCurrent, currentTransformPos, ptR.forceFadeRatio, ptRW.positionCurrent);
|
|
186
|
+
}
|
|
187
|
+
// Grabber
|
|
188
|
+
if (ptRW.grabberIndex !== -1) {
|
|
189
|
+
const grR = grabbersR[ptRW.grabberIndex];
|
|
190
|
+
const grRW = grabbersRW[ptRW.grabberIndex];
|
|
191
|
+
if (grRW.enabled === 0) {
|
|
192
|
+
ptRW.grabberIndex = -1;
|
|
193
|
+
} else {
|
|
194
|
+
Vector3.sub(ptRW.positionCurrent, grRW.position, tempVec0);
|
|
195
|
+
Vector3.normalize(tempVec0, tempVec0);
|
|
196
|
+
Vector3.scale(tempVec0, ptRW.grabberDistance, tempVec1);
|
|
197
|
+
Vector3.add(grRW.position, tempVec1, tempVec1);
|
|
198
|
+
Vector3.sub(tempVec1, ptRW.positionCurrent, tempVec0);
|
|
199
|
+
Vector3.scale(tempVec0, grR.force, tempVec0);
|
|
200
|
+
Vector3.add(ptRW.positionCurrent, tempVec0, ptRW.positionCurrent);
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
let nearIndex = -1;
|
|
204
|
+
let sqrNearRange = 1000 * 1000;
|
|
205
|
+
for(let ig = 0; ig < grabbersR.length; ig++){
|
|
206
|
+
const grRW = grabbersRW[ig];
|
|
207
|
+
if (grRW.enabled !== 0) {
|
|
208
|
+
const sqrLen = Vector3.sub(grRW.position, ptRW.positionCurrent, tempVec0).magnitudeSq;
|
|
209
|
+
if (sqrLen < grabbersR[ig].radius * grabbersR[ig].radius && sqrLen < sqrNearRange) {
|
|
210
|
+
sqrNearRange = sqrLen;
|
|
211
|
+
nearIndex = ig;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (nearIndex !== -1) {
|
|
216
|
+
ptRW.grabberIndex = nearIndex;
|
|
217
|
+
ptRW.grabberDistance = Math.sqrt(sqrNearRange) / 2.0;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Movable limit
|
|
221
|
+
if (ptR.movableLimitIndex !== -1) {
|
|
222
|
+
const target = movableLimitTargets[ptR.movableLimitIndex];
|
|
223
|
+
const move = Vector3.sub(ptRW.positionCurrent, target, tempVec0);
|
|
224
|
+
const moveLen = move.magnitude;
|
|
225
|
+
if (moveLen > ptR.movableLimitRadius) {
|
|
226
|
+
Vector3.scale(move, ptR.movableLimitRadius / moveLen, move);
|
|
227
|
+
Vector3.add(target, move, ptRW.positionCurrent);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// Flat plane collision
|
|
232
|
+
for(let i = 0; i < flatPlanes.length; i++){
|
|
233
|
+
const fp = flatPlanes[i];
|
|
234
|
+
const dist = Vector3.dot(fp.normal, ptRW.positionCurrent) + fp.distance;
|
|
235
|
+
if (dist < 0) {
|
|
236
|
+
Vector3.scale(fp.normal, dist, planeOffset);
|
|
237
|
+
Vector3.sub(ptRW.positionCurrent, planeOffset, ptRW.positionCurrent);
|
|
238
|
+
ptRW.friction = 0.3;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// 閳光偓閳光偓 Surface collision (triangle-based cloth collision) 閳光偓閳光偓
|
|
245
|
+
function surfaceCollision(pointsRW, collidersR, collidersRW, surfaceConstraints) {
|
|
246
|
+
const surfaceResult = {
|
|
247
|
+
hit: false,
|
|
248
|
+
intersectionPoint: new Vector3(),
|
|
249
|
+
pushOut: new Vector3(),
|
|
250
|
+
pointOnCollider: new Vector3(),
|
|
251
|
+
radius: 0
|
|
252
|
+
};
|
|
253
|
+
const triCenter = new Vector3();
|
|
254
|
+
const pushDir = new Vector3();
|
|
255
|
+
const centerToPt = new Vector3();
|
|
256
|
+
const intersectToPt = new Vector3();
|
|
257
|
+
const pushVec = new Vector3();
|
|
258
|
+
for(let index = 0; index < surfaceConstraints.length; index += 6){
|
|
259
|
+
for(let i = 0; i < collidersR.length; i++){
|
|
260
|
+
const colR = collidersR[i];
|
|
261
|
+
const colRW = collidersRW[i];
|
|
262
|
+
if (colRW.enabled === 0) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const colliderPos = colRW.positionCurrent;
|
|
266
|
+
for(let j = 0; j < 6; j += 3){
|
|
267
|
+
const idxA = surfaceConstraints[index + j + 0];
|
|
268
|
+
const idxB = surfaceConstraints[index + j + 1];
|
|
269
|
+
const idxC = surfaceConstraints[index + j + 2];
|
|
270
|
+
const rwA = pointsRW[idxA];
|
|
271
|
+
const rwB = pointsRW[idxB];
|
|
272
|
+
const rwC = pointsRW[idxC];
|
|
273
|
+
const result = checkSurfaceCollision(rwA.positionCurrent, rwB.positionCurrent, rwC.positionCurrent, colliderPos, colR, colRW, surfaceResult);
|
|
274
|
+
if (result.hit) {
|
|
275
|
+
triCenter.setXYZ((rwA.positionCurrent.x + rwB.positionCurrent.x + rwC.positionCurrent.x) / 3, (rwA.positionCurrent.y + rwB.positionCurrent.y + rwC.positionCurrent.y) / 3, (rwA.positionCurrent.z + rwB.positionCurrent.z + rwC.positionCurrent.z) / 3);
|
|
276
|
+
const pushMag = result.pushOut.magnitude;
|
|
277
|
+
if (pushMag < EPSILON) {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
Vector3.scale(result.pushOut, 1 / pushMag, pushDir);
|
|
281
|
+
for(let k = 0; k < 3; k++){
|
|
282
|
+
const idxR = surfaceConstraints[index + j + k];
|
|
283
|
+
const rwPt = pointsRW[idxR];
|
|
284
|
+
Vector3.sub(rwPt.positionCurrent, triCenter, centerToPt);
|
|
285
|
+
Vector3.sub(rwPt.positionCurrent, result.intersectionPoint, intersectToPt);
|
|
286
|
+
const intersectLen = intersectToPt.magnitude;
|
|
287
|
+
let rate = intersectLen > EPSILON ? centerToPt.magnitude / intersectLen : 0;
|
|
288
|
+
rate = clamp01(Math.abs(rate));
|
|
289
|
+
Vector3.scale(pushDir, (result.radius - pushMag) * rate, pushVec);
|
|
290
|
+
Vector3.add(rwPt.positionCurrent, pushVec, rwPt.positionCurrent);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
// 閳光偓閳光偓 Constraint relaxation 閳光偓閳光偓
|
|
298
|
+
function constraintUpdate(pointsR, pointsRW, constraints, collidersR, collidersRW, constraintShrinkLimit, enableBroadPhase) {
|
|
299
|
+
const direction = new Vector3();
|
|
300
|
+
const dirN = new Vector3();
|
|
301
|
+
const disp = new Vector3();
|
|
302
|
+
const scaledDisp = new Vector3();
|
|
303
|
+
const pointResultA = {
|
|
304
|
+
hit: false,
|
|
305
|
+
point: new Vector3()
|
|
306
|
+
};
|
|
307
|
+
const pointResultB = {
|
|
308
|
+
hit: false,
|
|
309
|
+
point: new Vector3()
|
|
310
|
+
};
|
|
311
|
+
const lineResult = {
|
|
312
|
+
hit: false,
|
|
313
|
+
pointOnLine: new Vector3(),
|
|
314
|
+
pointOnCollider: new Vector3(),
|
|
315
|
+
radius: 0
|
|
316
|
+
};
|
|
317
|
+
const pushout = new Vector3();
|
|
318
|
+
const segment = new Vector3();
|
|
319
|
+
const pointToA = new Vector3();
|
|
320
|
+
const pointToB = new Vector3();
|
|
321
|
+
const pushN = new Vector3();
|
|
322
|
+
const pushVec = new Vector3();
|
|
323
|
+
for(let index = 0; index < constraints.length; index++){
|
|
324
|
+
const c = constraints[index];
|
|
325
|
+
const ptRA = pointsR[c.indexA];
|
|
326
|
+
const ptRB = pointsR[c.indexB];
|
|
327
|
+
const weightA = ptRA.weight;
|
|
328
|
+
const weightB = ptRB.weight;
|
|
329
|
+
if (weightA <= EPSILON && weightB <= EPSILON) {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const rwA = pointsRW[c.indexA];
|
|
333
|
+
const rwB = pointsRW[c.indexB];
|
|
334
|
+
Vector3.sub(rwB.positionCurrent, rwA.positionCurrent, direction);
|
|
335
|
+
const distance = direction.magnitude;
|
|
336
|
+
let shrinkLen = c.length;
|
|
337
|
+
let stretchLen = shrinkLen;
|
|
338
|
+
if (c.type === ConstraintType.Structural_Horizontal || c.type === ConstraintType.Bending_Horizontal || c.type === ConstraintType.Shear) {
|
|
339
|
+
stretchLen += ptRA.sliderJointLength + ptRB.sliderJointLength;
|
|
340
|
+
}
|
|
341
|
+
let force = 0;
|
|
342
|
+
if (distance <= shrinkLen) {
|
|
343
|
+
force = distance - shrinkLen;
|
|
344
|
+
} else if (distance >= stretchLen) {
|
|
345
|
+
force = distance - stretchLen;
|
|
346
|
+
}
|
|
347
|
+
const isShrink = force >= 0;
|
|
348
|
+
let power = 0;
|
|
349
|
+
switch(c.type){
|
|
350
|
+
case ConstraintType.Structural_Vertical:
|
|
351
|
+
power = isShrink ? ptRA.structuralShrinkVertical + ptRB.structuralShrinkVertical : ptRA.structuralStretchVertical + ptRB.structuralStretchVertical;
|
|
352
|
+
break;
|
|
353
|
+
case ConstraintType.Structural_Horizontal:
|
|
354
|
+
power = isShrink ? Math.min(constraintShrinkLimit, ptRA.structuralShrinkHorizontal + ptRB.structuralShrinkHorizontal) : ptRA.structuralStretchHorizontal + ptRB.structuralStretchHorizontal;
|
|
355
|
+
break;
|
|
356
|
+
case ConstraintType.Shear:
|
|
357
|
+
power = isShrink ? Math.min(constraintShrinkLimit, ptRA.shearShrink + ptRB.shearShrink) : ptRA.shearStretch + ptRB.shearStretch;
|
|
358
|
+
break;
|
|
359
|
+
case ConstraintType.Bending_Vertical:
|
|
360
|
+
power = isShrink ? ptRA.bendingShrinkVertical + ptRB.bendingShrinkVertical : ptRA.bendingStretchVertical + ptRB.bendingStretchVertical;
|
|
361
|
+
break;
|
|
362
|
+
case ConstraintType.Bending_Horizontal:
|
|
363
|
+
power = isShrink ? Math.min(constraintShrinkLimit, ptRA.bendingShrinkHorizontal + ptRB.bendingShrinkHorizontal) : ptRA.bendingStretchHorizontal + ptRB.bendingStretchHorizontal;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
if (power > 0) {
|
|
367
|
+
if (distance > EPSILON) {
|
|
368
|
+
Vector3.scale(direction, 1 / distance, dirN);
|
|
369
|
+
} else {
|
|
370
|
+
dirN.setXYZ(0, 0, 0);
|
|
371
|
+
}
|
|
372
|
+
Vector3.scale(dirN, force * power, disp);
|
|
373
|
+
const wAB = weightA + weightB;
|
|
374
|
+
Vector3.scale(disp, weightA / wAB, scaledDisp);
|
|
375
|
+
Vector3.add(rwA.positionCurrent, scaledDisp, rwA.positionCurrent);
|
|
376
|
+
Vector3.scale(disp, weightB / wAB, scaledDisp);
|
|
377
|
+
Vector3.sub(rwB.positionCurrent, scaledDisp, rwB.positionCurrent);
|
|
378
|
+
}
|
|
379
|
+
// Per-constraint collision
|
|
380
|
+
if (c.isCollision !== 0) {
|
|
381
|
+
let friction = 0;
|
|
382
|
+
for(let i = 0; i < collidersR.length; i++){
|
|
383
|
+
const colRW = collidersRW[i];
|
|
384
|
+
if (colRW.enabled === 0) {
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const colR = collidersR[i];
|
|
388
|
+
const canPointA = !enableBroadPhase || colR.isInverseCollider || pointMayCollideBroadPhase(rwA.positionCurrent, colRW, ptRA.pointRadius);
|
|
389
|
+
const canPointB = !enableBroadPhase || colR.isInverseCollider || pointMayCollideBroadPhase(rwB.positionCurrent, colRW, ptRB.pointRadius);
|
|
390
|
+
const canLine = !colR.isInverseCollider && (!enableBroadPhase || segmentMayCollideBroadPhase(rwA.positionCurrent, rwB.positionCurrent, colRW));
|
|
391
|
+
if (!canPointA && !canPointB && !canLine) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (colR.height > EPSILON) {
|
|
395
|
+
if (colR.isInverseCollider) {
|
|
396
|
+
if (ptRA.applyInvertCollision === 1 && canPointA) {
|
|
397
|
+
const res = pushInFromCapsule(colR, colRW, rwA.positionCurrent, pointResultA);
|
|
398
|
+
if (res.hit) {
|
|
399
|
+
rwA.positionCurrent.set(res.point);
|
|
400
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (ptRB.applyInvertCollision === 1 && canPointB) {
|
|
404
|
+
const res = pushInFromCapsule(colR, colRW, rwB.positionCurrent, pointResultB);
|
|
405
|
+
if (res.hit) {
|
|
406
|
+
rwB.positionCurrent.set(res.point);
|
|
407
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
if (canPointA) {
|
|
412
|
+
const res = pushoutFromCapsule(colR, colRW, rwA.positionCurrent, ptRA, pointResultA);
|
|
413
|
+
if (res.hit) {
|
|
414
|
+
rwA.positionCurrent.set(res.point);
|
|
415
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (canPointB) {
|
|
419
|
+
const res = pushoutFromCapsule(colR, colRW, rwB.positionCurrent, ptRB, pointResultB);
|
|
420
|
+
if (res.hit) {
|
|
421
|
+
rwB.positionCurrent.set(res.point);
|
|
422
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
if (colR.isInverseCollider) {
|
|
428
|
+
if (ptRA.applyInvertCollision === 1 && canPointA) {
|
|
429
|
+
const res = pushInFromSphere(colRW.positionCurrent, colRW.radius, rwA.positionCurrent, pointResultA);
|
|
430
|
+
if (res.hit) {
|
|
431
|
+
rwA.positionCurrent.set(res.point);
|
|
432
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
if (ptRB.applyInvertCollision === 1 && canPointB) {
|
|
436
|
+
const res = pushInFromSphere(colRW.positionCurrent, colRW.radius, rwB.positionCurrent, pointResultB);
|
|
437
|
+
if (res.hit) {
|
|
438
|
+
rwB.positionCurrent.set(res.point);
|
|
439
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
} else {
|
|
443
|
+
if (canPointA) {
|
|
444
|
+
const res = pushoutFromSphere(colRW.positionCurrent, colRW.radius, ptRA.pointRadius, rwA.positionCurrent, pointResultA);
|
|
445
|
+
if (res.hit) {
|
|
446
|
+
rwA.positionCurrent.set(res.point);
|
|
447
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (canPointB) {
|
|
451
|
+
const res = pushoutFromSphere(colRW.positionCurrent, colRW.radius, ptRB.pointRadius, rwB.positionCurrent, pointResultB);
|
|
452
|
+
if (res.hit) {
|
|
453
|
+
rwB.positionCurrent.set(res.point);
|
|
454
|
+
friction = Math.max(friction, colR.friction * 0.25);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
// Line segment collision
|
|
460
|
+
if (!colR.isInverseCollider && canLine) {
|
|
461
|
+
const lineRes = collisionDetection(colR, colRW, rwA.positionCurrent, rwB.positionCurrent, lineResult);
|
|
462
|
+
if (lineRes.hit) {
|
|
463
|
+
Vector3.sub(lineRes.pointOnLine, lineRes.pointOnCollider, pushout);
|
|
464
|
+
const pushDist = pushout.magnitude;
|
|
465
|
+
if (pushDist > EPSILON) {
|
|
466
|
+
const ptDist = Vector3.sub(rwB.positionCurrent, rwA.positionCurrent, segment).magnitude * 0.5;
|
|
467
|
+
const rateP1 = ptDist > EPSILON ? clamp01(Vector3.sub(lineRes.pointOnLine, rwA.positionCurrent, pointToA).magnitude / ptDist) : 0;
|
|
468
|
+
const rateP2 = ptDist > EPSILON ? clamp01(Vector3.sub(lineRes.pointOnLine, rwB.positionCurrent, pointToB).magnitude / ptDist) : 0;
|
|
469
|
+
Vector3.scale(pushout, 1 / pushDist, pushN);
|
|
470
|
+
Vector3.scale(pushN, Math.max(lineRes.radius - pushDist, 0), pushVec);
|
|
471
|
+
if (weightA > EPSILON) {
|
|
472
|
+
Vector3.scale(pushVec, rateP2, scaledDisp);
|
|
473
|
+
Vector3.add(rwA.positionCurrent, scaledDisp, rwA.positionCurrent);
|
|
474
|
+
}
|
|
475
|
+
if (weightB > EPSILON) {
|
|
476
|
+
Vector3.scale(pushVec, rateP1, scaledDisp);
|
|
477
|
+
Vector3.add(rwB.positionCurrent, scaledDisp, rwB.positionCurrent);
|
|
478
|
+
}
|
|
479
|
+
const dotUp = Vector3.dot(AXIS_Y, Vector3.normalize(pushout, pushN));
|
|
480
|
+
friction = Math.max(friction, colR.friction * clamp01(dotUp));
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
rwA.friction = Math.max(friction, rwA.friction);
|
|
486
|
+
rwB.friction = Math.max(friction, rwB.friction);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
// 閳光偓閳光偓 Pass 2: blend + fake wave 閳光偓閳光偓
|
|
491
|
+
function pointUpdatePass2(pointsR, pointsRW, positionsToTransform, stepDelta, fakeWaveFreq, blendRatio, isFakeWave, fakeWavePower) {
|
|
492
|
+
let freq = fakeWaveFreq;
|
|
493
|
+
const curTransformPos = new Vector3();
|
|
494
|
+
const fakeWindOffset = new Vector3();
|
|
495
|
+
for(let index = 0; index < pointsR.length; index++){
|
|
496
|
+
const ptR = pointsR[index];
|
|
497
|
+
const ptRW = pointsRW[index];
|
|
498
|
+
Vector3.lerp(ptRW.positionPreviousTransform, ptRW.positionCurrentTransform, stepDelta, curTransformPos);
|
|
499
|
+
const blend = smoothStep(0, 1, Math.max(ptR.forceFadeRatio, blendRatio));
|
|
500
|
+
Vector3.lerp(ptRW.positionCurrent, curTransformPos, blend, ptRW.positionToTransform);
|
|
501
|
+
if (isFakeWave && ptR.child === -1 && ptR.parent !== -1) {
|
|
502
|
+
const A = pointsRW[ptR.parent].positionToTransform;
|
|
503
|
+
const B = ptRW.positionToTransform;
|
|
504
|
+
const mAxis = Matrix4x4.lookAt(A, B, AXIS_Y);
|
|
505
|
+
// Column 1 = up axis of the look-at matrix
|
|
506
|
+
const col1 = mAxis.getCol(1).xyz();
|
|
507
|
+
Vector3.lerp(ptRW.fakeWindDirection, col1, 0.5, ptRW.fakeWindDirection);
|
|
508
|
+
freq += ptR.fakeWaveFreq;
|
|
509
|
+
const power = Math.sin(freq) * fakeWavePower * ptR.fakeWavePower;
|
|
510
|
+
Vector3.scale(ptRW.fakeWindDirection, power, fakeWindOffset);
|
|
511
|
+
Vector3.add(ptRW.positionToTransform, fakeWindOffset, ptRW.positionToTransform);
|
|
512
|
+
}
|
|
513
|
+
positionsToTransform[index] = ptRW.positionToTransform.clone();
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function applyResult(pointsR, pointsRW, positionsToTransform, blendRatio, // Current transform rotations (world and local) read from engine
|
|
517
|
+
transformRotations, transformLocalRotations) {
|
|
518
|
+
const results = new Array(pointsR.length);
|
|
519
|
+
for(let index = 0; index < pointsR.length; index++){
|
|
520
|
+
const ptR = pointsR[index];
|
|
521
|
+
const ptRW = pointsRW[index];
|
|
522
|
+
let worldRot = transformRotations[index];
|
|
523
|
+
let localRot = transformLocalRotations[index];
|
|
524
|
+
if (ptR.weight >= EPSILON) {
|
|
525
|
+
// Dynamic point
|
|
526
|
+
if (ptR.parent !== -1) {
|
|
527
|
+
const direction = Vector3.sub(ptRW.positionToTransform, positionsToTransform[ptR.parent]);
|
|
528
|
+
const realLen = direction.magnitude;
|
|
529
|
+
if (realLen > EPSILON) {
|
|
530
|
+
ptRW.directionPrevious = direction;
|
|
531
|
+
} else {
|
|
532
|
+
ptRW.positionToTransform = Vector3.add(positionsToTransform[ptR.parent], ptRW.directionPrevious);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
const pos = ptRW.positionToTransform;
|
|
536
|
+
// SetRotation with blendRatio=0 for dynamic points
|
|
537
|
+
// Step 1: Reset localRotation 閳?initialLocalRotation (blendRatio=0 閳?fully initial)
|
|
538
|
+
localRot = ptR.initialLocalRotation.clone();
|
|
539
|
+
// Step 2: Recompute worldRot from the new localRot
|
|
540
|
+
// In C#, setting localRotation immediately updates the world rotation.
|
|
541
|
+
// worldRot = parentWorldRot * localRot
|
|
542
|
+
// We approximate: the parent's current world rotation is in transformRotations[ptR.parent]
|
|
543
|
+
if (ptR.parent !== -1) {
|
|
544
|
+
// Use the parent's OUTPUT rotation (already computed if parent index < current index)
|
|
545
|
+
const parentRot = results[ptR.parent] ? results[ptR.parent].rotation : transformRotations[ptR.parent];
|
|
546
|
+
worldRot = Quaternion.multiply(parentRot, localRot);
|
|
547
|
+
} else {
|
|
548
|
+
worldRot = localRot.clone();
|
|
549
|
+
}
|
|
550
|
+
// Step 3: Aim toward child using updated worldRot
|
|
551
|
+
if (ptR.child !== -1) {
|
|
552
|
+
const childDir = Vector3.sub(positionsToTransform[ptR.child], ptRW.positionToTransform);
|
|
553
|
+
if (childDir.magnitudeSq > EPSILON) {
|
|
554
|
+
const aimVec = worldRot.transform(ptR.boneAxis);
|
|
555
|
+
const aimRot = Quaternion.unitVectorToUnitVector(aimVec, childDir);
|
|
556
|
+
worldRot = Quaternion.multiply(aimRot, worldRot);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
results[index] = {
|
|
560
|
+
position: pos,
|
|
561
|
+
rotation: worldRot,
|
|
562
|
+
localRotation: localRot
|
|
563
|
+
};
|
|
564
|
+
} else {
|
|
565
|
+
// Fixed point 閳?follow transform, blend rotation
|
|
566
|
+
const childBlend = ptR.child !== -1 ? Math.max(pointsR[ptR.child].forceFadeRatio, blendRatio) : blendRatio;
|
|
567
|
+
// Step 1: Blend localRotation
|
|
568
|
+
localRot = Quaternion.slerp(ptR.initialLocalRotation, localRot, childBlend);
|
|
569
|
+
// Step 2: Recompute worldRot from blended localRot
|
|
570
|
+
if (ptR.parent !== -1) {
|
|
571
|
+
const parentRot = results[ptR.parent] ? results[ptR.parent].rotation : transformRotations[ptR.parent];
|
|
572
|
+
worldRot = Quaternion.multiply(parentRot, localRot);
|
|
573
|
+
} else {
|
|
574
|
+
worldRot = localRot.clone();
|
|
575
|
+
}
|
|
576
|
+
// Step 3: Aim toward child with blend
|
|
577
|
+
if (ptR.child !== -1) {
|
|
578
|
+
const childDir = Vector3.sub(positionsToTransform[ptR.child], ptRW.positionToTransform);
|
|
579
|
+
if (childDir.magnitudeSq > EPSILON) {
|
|
580
|
+
const aimVec = worldRot.transform(ptR.boneAxis);
|
|
581
|
+
const aimRot = Quaternion.unitVectorToUnitVector(aimVec, childDir);
|
|
582
|
+
worldRot = Quaternion.slerp(Quaternion.multiply(aimRot, worldRot), worldRot, childBlend);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
results[index] = {
|
|
586
|
+
position: ptRW.positionCurrentTransform,
|
|
587
|
+
rotation: worldRot,
|
|
588
|
+
localRotation: localRot
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return results;
|
|
593
|
+
}
|
|
594
|
+
// 閳光偓閳光偓 Angle limiting (post-simulation) 閳光偓閳光偓
|
|
595
|
+
function applyAngleLimits(pointsR, pointsRW, config) {
|
|
596
|
+
if (config.angleLimit < 0) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
for(let index = 0; index < pointsR.length; index++){
|
|
600
|
+
const ptR = pointsR[index];
|
|
601
|
+
if (ptR.parent === -1) {
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const ptRW = pointsRW[index];
|
|
605
|
+
const ptRp = pointsR[ptR.parent];
|
|
606
|
+
const ptRWp = pointsRW[ptR.parent];
|
|
607
|
+
const superParentPos = ptRp.parent !== -1 ? pointsRW[ptRp.parent].positionCurrent : ptRWp.positionCurrent;
|
|
608
|
+
const boneDir = Vector3.sub(ptRW.positionCurrent, ptRWp.positionCurrent);
|
|
609
|
+
let parentBoneDir = Vector3.sub(ptRWp.positionCurrent, superParentPos);
|
|
610
|
+
if (parentBoneDir.magnitude === 0 || config.limitFromRoot) {
|
|
611
|
+
parentBoneDir = Vector3.sub(ptRW.positionCurrentTransform, ptRWp.positionCurrentTransform);
|
|
612
|
+
}
|
|
613
|
+
const angle = Vector3.angleBetween(parentBoneDir, boneDir);
|
|
614
|
+
const remaining = radian2degree(angle) - config.angleLimit;
|
|
615
|
+
if (remaining > 0) {
|
|
616
|
+
const axis = Vector3.cross(parentBoneDir, boneDir);
|
|
617
|
+
const rotated = Quaternion.fromAxisAngle(axis, degree2radian(-remaining)).transform(boneDir);
|
|
618
|
+
ptRW.positionCurrent = Vector3.add(ptRWp.positionCurrent, rotated);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export { applyAngleLimits, applyResult, simulate };
|
|
624
|
+
//# sourceMappingURL=solver.js.map
|