@woosh/meep-engine 3.40.0 → 3.41.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 (203) hide show
  1. package/package.json +1 -1
  2. package/src/core/geom/3d/quaternion/swing_twist_clamp.d.ts +24 -0
  3. package/src/core/geom/3d/quaternion/swing_twist_clamp.d.ts.map +1 -0
  4. package/src/core/geom/3d/quaternion/swing_twist_clamp.js +62 -0
  5. package/src/core/geom/3d/quaternion/swing_twist_compose.d.ts +15 -0
  6. package/src/core/geom/3d/quaternion/swing_twist_compose.d.ts.map +1 -0
  7. package/src/core/geom/3d/quaternion/swing_twist_compose.js +36 -0
  8. package/src/core/geom/3d/quaternion/swing_twist_error.d.ts +39 -0
  9. package/src/core/geom/3d/quaternion/swing_twist_error.d.ts.map +1 -0
  10. package/src/core/geom/3d/quaternion/swing_twist_error.js +85 -0
  11. package/src/core/geom/vec3/v3_rigid_align_paired_unit_vectors.d.ts +11 -6
  12. package/src/core/geom/vec3/v3_rigid_align_paired_unit_vectors.d.ts.map +1 -1
  13. package/src/core/geom/vec3/v3_rigid_align_paired_unit_vectors.js +27 -13
  14. package/src/core/math/linalg/eigen/matrix_symmetric_4x4_eigen.d.ts +25 -0
  15. package/src/core/math/linalg/eigen/matrix_symmetric_4x4_eigen.d.ts.map +1 -0
  16. package/src/core/math/linalg/eigen/matrix_symmetric_4x4_eigen.js +166 -0
  17. package/src/core/math/linalg/eigen/matrix_top_eigenvector_power_iteration.d.ts +5 -3
  18. package/src/core/math/linalg/eigen/matrix_top_eigenvector_power_iteration.d.ts.map +1 -1
  19. package/src/core/math/linalg/eigen/matrix_top_eigenvector_power_iteration.js +5 -3
  20. package/src/engine/animation/curve/AnimationCurve.d.ts +3 -0
  21. package/src/engine/animation/curve/AnimationCurve.d.ts.map +1 -1
  22. package/src/engine/animation/curve/AnimationCurve.js +6 -1
  23. package/src/engine/ecs/EntityManager.d.ts +9 -1
  24. package/src/engine/ecs/EntityManager.d.ts.map +1 -1
  25. package/src/engine/ecs/EntityManager.js +26 -27
  26. package/src/engine/ecs/gpu/GPUStateAuthority.d.ts +20 -0
  27. package/src/engine/ecs/gpu/GPUStateAuthority.d.ts.map +1 -1
  28. package/src/engine/ecs/gpu/GPUStateAuthority.js +21 -0
  29. package/src/engine/ecs/gpu/GPU_AUTHORITY_EVENT_HOLD_CHANGE.d.ts +13 -0
  30. package/src/engine/ecs/gpu/GPU_AUTHORITY_EVENT_HOLD_CHANGE.d.ts.map +1 -0
  31. package/src/engine/ecs/gpu/GPU_AUTHORITY_EVENT_HOLD_CHANGE.js +12 -0
  32. package/src/engine/ecs/gpu/gpu_authority_grant.d.ts +6 -0
  33. package/src/engine/ecs/gpu/gpu_authority_grant.d.ts.map +1 -1
  34. package/src/engine/ecs/gpu/gpu_authority_grant.js +7 -1
  35. package/src/engine/ecs/gpu/gpu_authority_held.d.ts +14 -0
  36. package/src/engine/ecs/gpu/gpu_authority_held.d.ts.map +1 -0
  37. package/src/engine/ecs/gpu/gpu_authority_held.js +23 -0
  38. package/src/engine/ecs/gpu/gpu_authority_hold.d.ts +23 -0
  39. package/src/engine/ecs/gpu/gpu_authority_hold.d.ts.map +1 -0
  40. package/src/engine/ecs/gpu/gpu_authority_hold.js +41 -0
  41. package/src/engine/ecs/gpu/gpu_authority_unhold.d.ts +14 -0
  42. package/src/engine/ecs/gpu/gpu_authority_unhold.d.ts.map +1 -0
  43. package/src/engine/ecs/gpu/gpu_authority_unhold.js +32 -0
  44. package/src/engine/ecs/ik/IKGoal.d.ts +59 -0
  45. package/src/engine/ecs/ik/IKGoal.d.ts.map +1 -0
  46. package/src/engine/ecs/ik/IKGoal.js +83 -0
  47. package/src/engine/ecs/ik/IKGoalSystem.d.ts +75 -0
  48. package/src/engine/ecs/ik/IKGoalSystem.d.ts.map +1 -0
  49. package/src/engine/ecs/ik/IKGoalSystem.js +1456 -0
  50. package/src/engine/ecs/ik/PLAN.md +179 -0
  51. package/src/engine/ecs/speaker/VoiceSystem.d.ts +2 -2
  52. package/src/engine/ecs/speaker/VoiceSystem.d.ts.map +1 -1
  53. package/src/engine/ecs/storage/BinaryBufferSerializer.d.ts.map +1 -1
  54. package/src/engine/ecs/storage/BinaryBufferSerializer.js +4 -5
  55. package/src/engine/ecs/storage/binary/SERIALIZATION_COVERAGE.md +7 -3
  56. package/src/engine/ecs/storage/populateEngineSerializationRegistry.d.ts.map +1 -1
  57. package/src/engine/ecs/storage/populateEngineSerializationRegistry.js +1 -2
  58. package/src/engine/ecs/system/orderSystemsByDeclaredAccess.d.ts +33 -0
  59. package/src/engine/ecs/system/orderSystemsByDeclaredAccess.d.ts.map +1 -0
  60. package/src/engine/ecs/system/orderSystemsByDeclaredAccess.js +282 -0
  61. package/src/engine/ecs/transform/T64WorldCache.js +139 -139
  62. package/src/engine/ecs/transform/t64_evaluate_local.d.ts +25 -0
  63. package/src/engine/ecs/transform/t64_evaluate_local.d.ts.map +1 -0
  64. package/src/engine/ecs/transform/t64_evaluate_local.js +35 -0
  65. package/src/engine/ecs/transform/t64_evaluate_world.d.ts +5 -3
  66. package/src/engine/ecs/transform/t64_evaluate_world.d.ts.map +1 -1
  67. package/src/engine/ecs/transform/t64_evaluate_world.js +128 -126
  68. package/src/engine/graphics3/AnimationGraphSystem.d.ts +2 -0
  69. package/src/engine/graphics3/AnimationGraphSystem.d.ts.map +1 -1
  70. package/src/engine/graphics3/AnimationGraphSystem.js +337 -329
  71. package/src/engine/graphics3/AnimationSystem.d.ts +2 -0
  72. package/src/engine/graphics3/AnimationSystem.d.ts.map +1 -1
  73. package/src/engine/graphics3/AnimationSystem.js +360 -352
  74. package/src/engine/physics/cloth/MEASUREMENTS.md +130 -0
  75. package/src/engine/physics/cloth/PLAN.md +16 -1
  76. package/src/engine/physics/cloth/ecs/ClothColliderSystem.d.ts +5 -6
  77. package/src/engine/physics/cloth/ecs/ClothColliderSystem.d.ts.map +1 -1
  78. package/src/engine/physics/cloth/ecs/ClothColliderSystem.js +5 -6
  79. package/src/engine/physics/cloth/ecs/ClothInstance.d.ts +4 -7
  80. package/src/engine/physics/cloth/ecs/ClothInstance.d.ts.map +1 -1
  81. package/src/engine/physics/cloth/ecs/ClothInstance.js +4 -7
  82. package/src/engine/physics/cloth/ecs/ClothSystem.d.ts +4 -3
  83. package/src/engine/physics/cloth/ecs/ClothSystem.d.ts.map +1 -1
  84. package/src/engine/physics/cloth/ecs/ClothSystem.js +4 -3
  85. package/src/engine/physics/cloth/ecs/ClothWorld.d.ts.map +1 -1
  86. package/src/engine/physics/cloth/ecs/ClothWorld.js +10 -0
  87. package/src/engine/physics/cloth/ecs/cloth_seed_rig.d.ts +24 -24
  88. package/src/engine/physics/cloth/ecs/cloth_seed_rig.d.ts.map +1 -1
  89. package/src/engine/physics/cloth/ecs/cloth_seed_rig.js +46 -43
  90. package/src/engine/physics/cloth/ecs/cloth_write_back.d.ts +1 -1
  91. package/src/engine/physics/cloth/ecs/cloth_write_back.js +1 -1
  92. package/src/engine/physics/constraint/solve_constraints.d.ts +0 -35
  93. package/src/engine/physics/constraint/solve_constraints.d.ts.map +1 -1
  94. package/src/engine/physics/constraint/solve_constraints.js +1 -83
  95. package/src/engine/physics/ecs/ContactProbe.d.ts +73 -0
  96. package/src/engine/physics/ecs/ContactProbe.d.ts.map +1 -0
  97. package/src/engine/physics/ecs/ContactProbe.js +103 -0
  98. package/src/engine/physics/ecs/ContactProbeSystem.d.ts +44 -0
  99. package/src/engine/physics/ecs/ContactProbeSystem.d.ts.map +1 -0
  100. package/src/engine/physics/ecs/ContactProbeSystem.js +397 -0
  101. package/src/engine/physics/fluid/ecs/FluidComponent.d.ts +1 -1
  102. package/src/engine/physics/fluid/ecs/FluidComponent.js +1 -1
  103. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  104. package/src/engine/physics/inverse_kinematics/frame_fabrik/FrameFabrikProblem.d.ts +205 -0
  105. package/src/engine/physics/inverse_kinematics/frame_fabrik/FrameFabrikProblem.d.ts.map +1 -0
  106. package/src/engine/physics/inverse_kinematics/frame_fabrik/FrameFabrikProblem.js +245 -0
  107. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_limit_from_joint.d.ts +21 -0
  108. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_limit_from_joint.d.ts.map +1 -0
  109. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_limit_from_joint.js +63 -0
  110. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_solve.d.ts +33 -0
  111. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_solve.d.ts.map +1 -0
  112. package/src/engine/physics/inverse_kinematics/frame_fabrik/frame_fabrik_solve.js +874 -0
  113. package/src/engine/physics/inverse_kinematics/joint_hinge_mid_axis.d.ts +27 -0
  114. package/src/engine/physics/inverse_kinematics/joint_hinge_mid_axis.d.ts.map +1 -0
  115. package/src/engine/physics/inverse_kinematics/joint_hinge_mid_axis.js +77 -0
  116. package/src/engine/physics/inverse_kinematics/two_bone_path_solve.d.ts +36 -0
  117. package/src/engine/physics/inverse_kinematics/two_bone_path_solve.d.ts.map +1 -0
  118. package/src/engine/physics/inverse_kinematics/two_bone_path_solve.js +97 -0
  119. package/src/engine/physics/inverse_kinematics/two_joint_ik.d.ts +33 -11
  120. package/src/engine/physics/inverse_kinematics/two_joint_ik.d.ts.map +1 -1
  121. package/src/engine/physics/inverse_kinematics/two_joint_ik.js +158 -67
  122. package/src/shade/renderer/animation/ANIMATION_TRACK_BINDING_STRUCT.d.ts.map +1 -1
  123. package/src/shade/renderer/animation/ANIMATION_TRACK_BINDING_STRUCT.js +10 -1
  124. package/src/shade/renderer/animation/GPUAnimationManager.d.ts +10 -4
  125. package/src/shade/renderer/animation/GPUAnimationManager.d.ts.map +1 -1
  126. package/src/shade/renderer/animation/GPUAnimationManager.js +251 -23
  127. package/src/shade/renderer/animation/POSE_ACCUMULATOR_LAYOUT.d.ts +12 -4
  128. package/src/shade/renderer/animation/POSE_ACCUMULATOR_LAYOUT.d.ts.map +1 -1
  129. package/src/shade/renderer/animation/POSE_ACCUMULATOR_LAYOUT.js +12 -4
  130. package/src/shade/renderer/animation/chunk_pose_accumulator.d.ts.map +1 -1
  131. package/src/shade/renderer/animation/chunk_pose_accumulator.js +14 -1
  132. package/src/shade/renderer/animation/pose/PoseChannelIndex.d.ts +78 -0
  133. package/src/shade/renderer/animation/pose/PoseChannelIndex.d.ts.map +1 -0
  134. package/src/shade/renderer/animation/pose/PoseChannelIndex.js +274 -0
  135. package/src/shade/renderer/animation/pose/PosePlayback.d.ts +40 -3
  136. package/src/shade/renderer/animation/pose/PosePlayback.d.ts.map +1 -1
  137. package/src/shade/renderer/animation/pose/PosePlayback.js +77 -3
  138. package/src/shade/renderer/animation/pose/pose_evaluate_local.d.ts +21 -3
  139. package/src/shade/renderer/animation/pose/pose_evaluate_local.d.ts.map +1 -1
  140. package/src/shade/renderer/animation/pose/pose_evaluate_local.js +79 -11
  141. package/src/shade/renderer/animation/shader_animation_apply.js +1 -1
  142. package/src/shade/renderer/animation/shader_animation_pose_resolve.d.ts +3 -2
  143. package/src/shade/renderer/animation/shader_animation_pose_resolve.d.ts.map +1 -1
  144. package/src/shade/renderer/animation/shader_animation_pose_resolve.js +187 -176
  145. package/src/shade/renderer/loader/gltf/tiny-gltf.d.ts +1 -1
  146. package/src/shade/renderer/object_property/chunk_write_object_property.d.ts +3 -1
  147. package/src/shade/renderer/object_property/chunk_write_object_property.d.ts.map +1 -1
  148. package/src/shade/renderer/object_property/chunk_write_object_property.js +5 -2
  149. package/src/shade/renderer/object_property/chunk_write_object_property_node3d.d.ts +4 -0
  150. package/src/shade/renderer/object_property/chunk_write_object_property_node3d.d.ts.map +1 -1
  151. package/src/shade/renderer/object_property/chunk_write_object_property_node3d.js +6 -1
  152. package/src/shade/renderer/postprocess/nss/NSS.d.ts +1 -1
  153. package/src/shade/renderer/scene/optimization/geometry/geometry_deduplicate.d.ts +1 -1
  154. package/src/shade/renderer/scene/optimization/geometry/geometry_deduplicate.d.ts.map +1 -1
  155. package/src/shade/renderer/scene/optimization/geometry/geometry_deduplicate.js +135 -73
  156. package/src/shade/renderer/scene/optimization/geometry/internal/cover_greedy.d.ts +7 -0
  157. package/src/shade/renderer/scene/optimization/geometry/internal/cover_greedy.d.ts.map +1 -1
  158. package/src/shade/renderer/scene/optimization/geometry/internal/cover_greedy.js +29 -1
  159. package/src/shade/renderer/scene/optimization/geometry/internal/face_neighbourhood_hash.d.ts +19 -0
  160. package/src/shade/renderer/scene/optimization/geometry/internal/face_neighbourhood_hash.d.ts.map +1 -0
  161. package/src/shade/renderer/scene/optimization/geometry/internal/face_neighbourhood_hash.js +167 -0
  162. package/src/shade/renderer/scene/optimization/geometry/internal/neighbourhood_anchor_index.d.ts +25 -0
  163. package/src/shade/renderer/scene/optimization/geometry/internal/neighbourhood_anchor_index.d.ts.map +1 -0
  164. package/src/shade/renderer/scene/optimization/geometry/internal/neighbourhood_anchor_index.js +124 -0
  165. package/src/shade/renderer/scene/optimization/geometry/internal/seed_correspondence_from_face_pair.d.ts +19 -0
  166. package/src/shade/renderer/scene/optimization/geometry/internal/seed_correspondence_from_face_pair.d.ts.map +1 -0
  167. package/src/shade/renderer/scene/optimization/geometry/internal/seed_correspondence_from_face_pair.js +48 -0
  168. package/src/shade/renderer/scene/rows/GPUSceneRows.d.ts +17 -1
  169. package/src/shade/renderer/scene/rows/GPUSceneRows.d.ts.map +1 -1
  170. package/src/shade/renderer/scene/rows/GPUSceneRows.js +29 -3
  171. package/src/engine/ecs/ik/IKConstraint.d.ts +0 -52
  172. package/src/engine/ecs/ik/IKConstraint.d.ts.map +0 -1
  173. package/src/engine/ecs/ik/IKConstraint.js +0 -95
  174. package/src/engine/ecs/ik/IKProblem.d.ts +0 -45
  175. package/src/engine/ecs/ik/IKProblem.d.ts.map +0 -1
  176. package/src/engine/ecs/ik/IKProblem.js +0 -58
  177. package/src/engine/ecs/ik/IKSolver.d.ts +0 -8
  178. package/src/engine/ecs/ik/IKSolver.d.ts.map +0 -1
  179. package/src/engine/ecs/ik/IKSolver.js +0 -9
  180. package/src/engine/ecs/ik/InverseKinematics.d.ts +0 -48
  181. package/src/engine/ecs/ik/InverseKinematics.d.ts.map +0 -1
  182. package/src/engine/ecs/ik/InverseKinematics.js +0 -119
  183. package/src/engine/ecs/ik/InverseKinematicsSerializationAdapter.d.ts +0 -19
  184. package/src/engine/ecs/ik/InverseKinematicsSerializationAdapter.d.ts.map +0 -1
  185. package/src/engine/ecs/ik/InverseKinematicsSerializationAdapter.js +0 -72
  186. package/src/engine/ecs/ik/InverseKinematicsSystem.d.ts +0 -54
  187. package/src/engine/ecs/ik/InverseKinematicsSystem.d.ts.map +0 -1
  188. package/src/engine/ecs/ik/InverseKinematicsSystem.js +0 -212
  189. package/src/engine/ecs/ik/OneBoneSurfaceAlignmentSolver.d.ts +0 -8
  190. package/src/engine/ecs/ik/OneBoneSurfaceAlignmentSolver.d.ts.map +0 -1
  191. package/src/engine/ecs/ik/OneBoneSurfaceAlignmentSolver.js +0 -153
  192. package/src/engine/ecs/ik/TwoBoneInverseKinematicsSolver.d.ts +0 -5
  193. package/src/engine/ecs/ik/TwoBoneInverseKinematicsSolver.d.ts.map +0 -1
  194. package/src/engine/ecs/ik/TwoBoneInverseKinematicsSolver.js +0 -120
  195. package/src/engine/ecs/ik/ik_bone_chain.d.ts +0 -64
  196. package/src/engine/ecs/ik/ik_bone_chain.d.ts.map +0 -1
  197. package/src/engine/ecs/ik/ik_bone_chain.js +0 -142
  198. package/src/engine/ecs/ik/ik_probe_terrain_contact.d.ts +0 -42
  199. package/src/engine/ecs/ik/ik_probe_terrain_contact.d.ts.map +0 -1
  200. package/src/engine/ecs/ik/ik_probe_terrain_contact.js +0 -123
  201. package/src/engine/ecs/ik/ik_write_local_rotation.d.ts +0 -18
  202. package/src/engine/ecs/ik/ik_write_local_rotation.d.ts.map +0 -1
  203. package/src/engine/ecs/ik/ik_write_local_rotation.js +0 -32
@@ -1,352 +1,360 @@
1
- import { assert } from "../../core/assert.js";
2
- import { PosePlayback } from "../../shade/renderer/animation/pose/PosePlayback.js";
3
- import { Animation } from "../ecs/animation/Animation.js";
4
- import { System } from "../ecs/System.js";
5
- import { SGMesh } from "../graphics/ecs/mesh-v2/aggregate/SGMesh.js";
6
- import { ClipListPlayer } from "./animation/ClipListPlayer.js";
7
- import { write_clip_playback } from "./animation/write_clip_playback.js";
8
- import {
9
- register_instance_clips,
10
- unregister_instance_clips
11
- } from "./register_instance_animation.js";
12
-
13
- /**
14
- * @type {ClipPlayback[]}
15
- */
16
- const NOTHING = Object.freeze([]);
17
-
18
- /**
19
- * Drives every entity's {@link Animation} component: runs the clocks of the clips its list names,
20
- * and tells the renderer what each of them weighs and where it is playing.
21
- *
22
- * The list path is what the game uses wherever there is no state machine — buildings, critters and
23
- * the strategy map's groups — and {@link ClipListPlayer} is what plays it. This is the ECS half:
24
- * which entity has which model, and when the ids the renderer issued for it stopped meaning
25
- * anything.
26
- *
27
- * **It is the CPU that plays the animation and the GPU that draws it.** Shade can advance a clip
28
- * itself, and this never asks it to — `AnimationClipFlags.Playing` is left clear and every audible
29
- * clip's time is written down each frame from the player's own clock. That is I-4: the pose
30
- * gameplay reads and the pose on the screen have to be the same pose, and they cannot be if two
31
- * clocks are running.
32
- *
33
- * **Nothing here is culled.** The legacy system skipped entities under 32 px², which let camera
34
- * framing decide whether a clip ever reached its end. What may be culled is drawing, and drawing is
35
- * Shade's to cull.
36
- */
37
- export class AnimationSystem extends System {
38
- /**
39
- * @type {GraphicsEngine}
40
- */
41
- #graphics;
42
-
43
- /**
44
- * The system that owns the scene, the model instances, and the ids the animation manager issued
45
- * for them.
46
- *
47
- * @type {MeshSystem}
48
- */
49
- #meshes;
50
-
51
- /**
52
- * @type {Map<number, {player: ClipListPlayer, instance: PrefabInstance|null, clip_registration: InstanceClipRegistration|null, played_slots: Int32Array, written: Float32Array, poses: PosePlayback[]}>}
53
- */
54
- #entries = new Map();
55
-
56
- /**
57
- * @param {GraphicsEngine} graphics
58
- * @param {MeshSystem} meshes
59
- */
60
- constructor(graphics, meshes) {
61
- super();
62
-
63
- assert.defined(graphics, 'graphics');
64
- assert.defined(meshes, 'meshes');
65
-
66
- this.#graphics = graphics;
67
- this.#meshes = meshes;
68
-
69
- this.dependencies = [Animation, SGMesh];
70
- }
71
-
72
- /**
73
- * What an entity is playing, and where each of them is.
74
- *
75
- * One entry per clip in the component's list, in the list's own order. Empty for an entity this
76
- * system is not driving — one that has left, or one whose model has not arrived, which is the
77
- * same wait the clip durations are on.
78
- *
79
- * @param {number} entity
80
- * @returns {ClipPlayback[]}
81
- */
82
- playbacks_of(entity) {
83
- const entry = this.#entries.get(entity);
84
-
85
- if (entry === undefined) {
86
- return NOTHING;
87
- }
88
-
89
- return entry.player.playbacks;
90
- }
91
-
92
- /**
93
- * What the entity's model is playing, in the form the CPU pose evaluator takes: one entry per
94
- * clip the model carries, at the weight and the time the renderer has been given.
95
- *
96
- * **Only the clips the list names are here, because they are the only ones registered** (D37).
97
- * The GPU's accumulator marks a joint dirty for every bound clip that writes to it, weight and
98
- * all — so a bound clip at weight zero resolves the joints only it drives to zero rather than
99
- * leaving them at rest. The answer is not to hand the surplus to the evaluator so it agrees with
100
- * a mistake; it is not to bind them, and then a clip of the model nothing names moves nothing.
101
- *
102
- * The entries are this system's own and are rewritten by the next call, which is what keeps a
103
- * query on a per-frame path from allocating.
104
- *
105
- * @param {PosePlayback[]} target appended to
106
- * @param {number} entity
107
- * @returns {boolean} whether this system is driving the entity — `false` for one with no
108
- * {@link Animation}, and for one whose model has not arrived
109
- */
110
- write_pose_playbacks(target, entity) {
111
- const entry = this.#entries.get(entity);
112
-
113
- if (entry === undefined || entry.instance === null) {
114
- return false;
115
- }
116
-
117
- const clips = entry.instance.clips;
118
- const weights = entry.player.weights;
119
- const poses = entry.poses;
120
- const played = entry.played_slots;
121
-
122
- for (let i = 0; i < played.length; i++) {
123
- const slot = played[i];
124
- const pose = poses[slot];
125
-
126
- pose.clip = clips[slot];
127
- pose.weight = weights[slot];
128
-
129
- // a clip the list names but has stopped playing is where it stopped, and at weight zero
130
- // that is nowhere in particular
131
- pose.time = 0;
132
- }
133
-
134
- const playbacks = entry.player.playbacks;
135
-
136
- for (let i = 0; i < playbacks.length; i++) {
137
- const playback = playbacks[i];
138
-
139
- poses[playback.slot].time = playback.time;
140
- }
141
-
142
- for (let i = 0; i < played.length; i++) {
143
- target.push(poses[played[i]]);
144
- }
145
-
146
- return true;
147
- }
148
-
149
- /**
150
- * @param {Animation} component
151
- * @param {SGMesh} model
152
- * @param {number} entity
153
- */
154
- link(component, model, entity) {
155
- this.#entries.set(entity, {
156
- player: ClipListPlayer.from(component),
157
- instance: null,
158
- clip_registration: null,
159
- played_slots: new Int32Array(0),
160
- written: new Float32Array(0),
161
- poses: []
162
- });
163
- }
164
-
165
- /**
166
- * @param {Animation} component
167
- * @param {SGMesh} model
168
- * @param {number} entity
169
- */
170
- unlink(component, model, entity) {
171
- const entry = this.#entries.get(entity);
172
-
173
- if (entry !== undefined) {
174
- this.#release_clips(entry);
175
- }
176
-
177
- this.#entries.delete(entity);
178
- }
179
-
180
- async shutdown(entityManager) {
181
- for (const entry of this.#entries.values()) {
182
- this.#release_clips(entry);
183
- }
184
-
185
- this.#entries.clear();
186
- }
187
-
188
- /**
189
- * Give this entity's clips back, if the manager that issued them is still the one running.
190
- *
191
- * **An id may only be handed back to the manager that issued it.** A renderer restart replaces
192
- * the GPU scene context and voids every id it gave out, so a registration stamped with a context
193
- * that is no longer current is forgotten rather than returned.
194
- *
195
- * @param {object} entry
196
- */
197
- #release_clips(entry) {
198
- this.#release_clips_only(entry);
199
-
200
- entry.played_slots = new Int32Array(0);
201
- }
202
-
203
- /**
204
- * The registration alone, leaving {@link #refresh_played_slots}'s answer where it is — which is
205
- * what a re-registration wants, since the set it is about to register with is the current one.
206
- *
207
- * @param {object} entry
208
- */
209
- #release_clips_only(entry) {
210
- const registration = entry.clip_registration;
211
-
212
- if (registration === null) {
213
- return;
214
- }
215
-
216
- if (registration.context === this.#graphics.scene_context(this.#meshes.scene)) {
217
- unregister_instance_clips(registration);
218
- }
219
-
220
- entry.clip_registration = null;
221
- }
222
-
223
- /**
224
- * Which of the model's clips the list currently names, kept up to date.
225
- *
226
- * **The list is edited while the game runs** — `ClipListPlayer` reconciles its playbacks against
227
- * it every tick, and `StopAnimationBehavior` empties it outright — so which clips are playable
228
- * is not settled once at bind.
229
- *
230
- * Maintained here rather than alongside the registration because **the pose evaluator needs it
231
- * whether or not there is a device**: `query_entity_node_world_pose` answers on the CPU, from
232
- * the clips this driver plays, and a game with rendering disabled still asks (I-4).
233
- *
234
- * @param {object} entry
235
- * @returns {boolean} whether the set changed
236
- */
237
- #refresh_played_slots(entry) {
238
- const playbacks = entry.player.playbacks;
239
- const played = entry.played_slots;
240
-
241
- /*
242
- The distinct slots, and distinct is the point: two entries of the list can name one clip of
243
- the model, so the number of playbacks is not the number of clips. Comparing the two would
244
- report a change on every frame for a list that had not moved, and a change here is a
245
- re-registration.
246
- */
247
- const wanted = [];
248
-
249
- for (let i = 0; i < playbacks.length; i++) {
250
- const slot = playbacks[i].slot;
251
-
252
- if (wanted.indexOf(slot) === -1) {
253
- wanted.push(slot);
254
- }
255
- }
256
-
257
- if (wanted.length === played.length) {
258
- let same = true;
259
-
260
- for (let i = 0; i < wanted.length; i++) {
261
- if (played[i] !== wanted[i]) {
262
- same = false;
263
- break;
264
- }
265
- }
266
-
267
- if (same) {
268
- return false;
269
- }
270
- }
271
-
272
- entry.played_slots = Int32Array.from(wanted);
273
-
274
- return true;
275
- }
276
-
277
- /**
278
- * Keep the registered clips equal to the ones the list names.
279
- *
280
- * A clip that leaves the list has to be unregistered, or it stays bound at weight zero and takes
281
- * the joints only it drives down to zero with it.
282
- *
283
- * @param {object} entry
284
- * @param {PrefabInstance} instance
285
- * @param {GPUSceneContext} context
286
- * @param {boolean} set_changed whether {@link #refresh_played_slots} just moved
287
- * @returns {boolean} whether the registration was replaced
288
- */
289
- #refresh_clips(entry, instance, context, set_changed) {
290
- const registration = entry.clip_registration;
291
-
292
- if (!set_changed && registration !== null && registration.context === context) {
293
- return false;
294
- }
295
-
296
- this.#release_clips_only(entry);
297
-
298
- entry.clip_registration = register_instance_clips(context, instance, entry.played_slots);
299
-
300
- return true;
301
- }
302
-
303
- /**
304
- * @param {number} time_delta_seconds
305
- */
306
- update(time_delta_seconds) {
307
- const ecd = this.entityManager.dataset;
308
-
309
- const context = this.#graphics.scene_context(this.#meshes.scene);
310
-
311
- // No device — before the first start, and again after a stop. The clocks still run: a clip
312
- // that ends while the renderer is being replaced still says so.
313
- const manager = context === null ? null : context.animation_manager;
314
-
315
- for (const [entity, entry] of this.#entries) {
316
- const instance = this.#meshes.instance_of(entity);
317
-
318
- if (instance === null) {
319
- // the model has not arrived, and its clips are what say how long they run
320
- continue;
321
- }
322
-
323
- if (instance !== entry.instance) {
324
- entry.instance = instance;
325
-
326
- entry.player.bind(entity, ecd, instance);
327
-
328
- entry.written = new Float32Array(instance.clips.length).fill(-1);
329
- entry.poses = instance.clips.map(() => new PosePlayback());
330
-
331
- // ids the manager issued for the model that was here before name clips this one
332
- // does not have
333
- this.#release_clips(entry);
334
- }
335
-
336
- entry.player.tick(time_delta_seconds);
337
-
338
- const set_changed = this.#refresh_played_slots(entry);
339
-
340
- if (context === null) {
341
- continue;
342
- }
343
-
344
- if (this.#refresh_clips(entry, instance, context, set_changed)) {
345
- // whatever the last registration was told is not what this one knows
346
- entry.written.fill(-1);
347
- }
348
-
349
- write_clip_playback(manager, entry.player, entry.clip_registration.ids, entry.written);
350
- }
351
- }
352
- }
1
+ import { assert } from "../../core/assert.js";
2
+ import { ResourceAccessKind } from "../../core/model/ResourceAccessKind.js";
3
+ import { ResourceAccessSpecification } from "../../core/model/ResourceAccessSpecification.js";
4
+ import { PosePlayback } from "../../shade/renderer/animation/pose/PosePlayback.js";
5
+ import { Animation } from "../ecs/animation/Animation.js";
6
+ import { System } from "../ecs/System.js";
7
+ import { SGMesh } from "../graphics/ecs/mesh-v2/aggregate/SGMesh.js";
8
+ import { ClipListPlayer } from "./animation/ClipListPlayer.js";
9
+ import { write_clip_playback } from "./animation/write_clip_playback.js";
10
+ import {
11
+ register_instance_clips,
12
+ unregister_instance_clips
13
+ } from "./register_instance_animation.js";
14
+
15
+ /**
16
+ * @type {ClipPlayback[]}
17
+ */
18
+ const NOTHING = Object.freeze([]);
19
+
20
+ /**
21
+ * Drives every entity's {@link Animation} component: runs the clocks of the clips its list names,
22
+ * and tells the renderer what each of them weighs and where it is playing.
23
+ *
24
+ * The list path is what the game uses wherever there is no state machine — buildings, critters and
25
+ * the strategy map's groups — and {@link ClipListPlayer} is what plays it. This is the ECS half:
26
+ * which entity has which model, and when the ids the renderer issued for it stopped meaning
27
+ * anything.
28
+ *
29
+ * **It is the CPU that plays the animation and the GPU that draws it.** Shade can advance a clip
30
+ * itself, and this never asks it to — `AnimationClipFlags.Playing` is left clear and every audible
31
+ * clip's time is written down each frame from the player's own clock. That is I-4: the pose
32
+ * gameplay reads and the pose on the screen have to be the same pose, and they cannot be if two
33
+ * clocks are running.
34
+ *
35
+ * **Nothing here is culled.** The legacy system skipped entities under 32 px², which let camera
36
+ * framing decide whether a clip ever reached its end. What may be culled is drawing, and drawing is
37
+ * Shade's to cull.
38
+ */
39
+ export class AnimationSystem extends System {
40
+ /**
41
+ * @type {GraphicsEngine}
42
+ */
43
+ #graphics;
44
+
45
+ /**
46
+ * The system that owns the scene, the model instances, and the ids the animation manager issued
47
+ * for them.
48
+ *
49
+ * @type {MeshSystem}
50
+ */
51
+ #meshes;
52
+
53
+ /**
54
+ * @type {Map<number, {player: ClipListPlayer, instance: PrefabInstance|null, clip_registration: InstanceClipRegistration|null, played_slots: Int32Array, written: Float32Array, poses: PosePlayback[]}>}
55
+ */
56
+ #entries = new Map();
57
+
58
+ /**
59
+ * @param {GraphicsEngine} graphics
60
+ * @param {MeshSystem} meshes
61
+ */
62
+ constructor(graphics, meshes) {
63
+ super();
64
+
65
+ assert.defined(graphics, 'graphics');
66
+ assert.defined(meshes, 'meshes');
67
+
68
+ this.#graphics = graphics;
69
+ this.#meshes = meshes;
70
+
71
+ this.dependencies = [Animation, SGMesh];
72
+
73
+ // this tick's clip times are this system's to set: whatever evaluates a pose from them
74
+ // (exact queries, IK's base pose) reads Animation and so runs after it
75
+ this.components_used = [
76
+ ResourceAccessSpecification.from(Animation, ResourceAccessKind.Read | ResourceAccessKind.Write),
77
+ ];
78
+ }
79
+
80
+ /**
81
+ * What an entity is playing, and where each of them is.
82
+ *
83
+ * One entry per clip in the component's list, in the list's own order. Empty for an entity this
84
+ * system is not driving — one that has left, or one whose model has not arrived, which is the
85
+ * same wait the clip durations are on.
86
+ *
87
+ * @param {number} entity
88
+ * @returns {ClipPlayback[]}
89
+ */
90
+ playbacks_of(entity) {
91
+ const entry = this.#entries.get(entity);
92
+
93
+ if (entry === undefined) {
94
+ return NOTHING;
95
+ }
96
+
97
+ return entry.player.playbacks;
98
+ }
99
+
100
+ /**
101
+ * What the entity's model is playing, in the form the CPU pose evaluator takes: one entry per
102
+ * clip the model carries, at the weight and the time the renderer has been given.
103
+ *
104
+ * **Only the clips the list names are here, because they are the only ones registered** (D37).
105
+ * The GPU's accumulator marks a joint dirty for every bound clip that writes to it, weight and
106
+ * all — so a bound clip at weight zero resolves the joints only it drives to zero rather than
107
+ * leaving them at rest. The answer is not to hand the surplus to the evaluator so it agrees with
108
+ * a mistake; it is not to bind them, and then a clip of the model nothing names moves nothing.
109
+ *
110
+ * The entries are this system's own and are rewritten by the next call, which is what keeps a
111
+ * query on a per-frame path from allocating.
112
+ *
113
+ * @param {PosePlayback[]} target appended to
114
+ * @param {number} entity
115
+ * @returns {boolean} whether this system is driving the entity — `false` for one with no
116
+ * {@link Animation}, and for one whose model has not arrived
117
+ */
118
+ write_pose_playbacks(target, entity) {
119
+ const entry = this.#entries.get(entity);
120
+
121
+ if (entry === undefined || entry.instance === null) {
122
+ return false;
123
+ }
124
+
125
+ const clips = entry.instance.clips;
126
+ const weights = entry.player.weights;
127
+ const poses = entry.poses;
128
+ const played = entry.played_slots;
129
+
130
+ for (let i = 0; i < played.length; i++) {
131
+ const slot = played[i];
132
+ const pose = poses[slot];
133
+
134
+ pose.clip = clips[slot];
135
+ pose.weight = weights[slot];
136
+
137
+ // a clip the list names but has stopped playing is where it stopped, and at weight zero
138
+ // that is nowhere in particular
139
+ pose.time = 0;
140
+ }
141
+
142
+ const playbacks = entry.player.playbacks;
143
+
144
+ for (let i = 0; i < playbacks.length; i++) {
145
+ const playback = playbacks[i];
146
+
147
+ poses[playback.slot].time = playback.time;
148
+ }
149
+
150
+ for (let i = 0; i < played.length; i++) {
151
+ target.push(poses[played[i]]);
152
+ }
153
+
154
+ return true;
155
+ }
156
+
157
+ /**
158
+ * @param {Animation} component
159
+ * @param {SGMesh} model
160
+ * @param {number} entity
161
+ */
162
+ link(component, model, entity) {
163
+ this.#entries.set(entity, {
164
+ player: ClipListPlayer.from(component),
165
+ instance: null,
166
+ clip_registration: null,
167
+ played_slots: new Int32Array(0),
168
+ written: new Float32Array(0),
169
+ poses: []
170
+ });
171
+ }
172
+
173
+ /**
174
+ * @param {Animation} component
175
+ * @param {SGMesh} model
176
+ * @param {number} entity
177
+ */
178
+ unlink(component, model, entity) {
179
+ const entry = this.#entries.get(entity);
180
+
181
+ if (entry !== undefined) {
182
+ this.#release_clips(entry);
183
+ }
184
+
185
+ this.#entries.delete(entity);
186
+ }
187
+
188
+ async shutdown(entityManager) {
189
+ for (const entry of this.#entries.values()) {
190
+ this.#release_clips(entry);
191
+ }
192
+
193
+ this.#entries.clear();
194
+ }
195
+
196
+ /**
197
+ * Give this entity's clips back, if the manager that issued them is still the one running.
198
+ *
199
+ * **An id may only be handed back to the manager that issued it.** A renderer restart replaces
200
+ * the GPU scene context and voids every id it gave out, so a registration stamped with a context
201
+ * that is no longer current is forgotten rather than returned.
202
+ *
203
+ * @param {object} entry
204
+ */
205
+ #release_clips(entry) {
206
+ this.#release_clips_only(entry);
207
+
208
+ entry.played_slots = new Int32Array(0);
209
+ }
210
+
211
+ /**
212
+ * The registration alone, leaving {@link #refresh_played_slots}'s answer where it is — which is
213
+ * what a re-registration wants, since the set it is about to register with is the current one.
214
+ *
215
+ * @param {object} entry
216
+ */
217
+ #release_clips_only(entry) {
218
+ const registration = entry.clip_registration;
219
+
220
+ if (registration === null) {
221
+ return;
222
+ }
223
+
224
+ if (registration.context === this.#graphics.scene_context(this.#meshes.scene)) {
225
+ unregister_instance_clips(registration);
226
+ }
227
+
228
+ entry.clip_registration = null;
229
+ }
230
+
231
+ /**
232
+ * Which of the model's clips the list currently names, kept up to date.
233
+ *
234
+ * **The list is edited while the game runs** — `ClipListPlayer` reconciles its playbacks against
235
+ * it every tick, and `StopAnimationBehavior` empties it outright — so which clips are playable
236
+ * is not settled once at bind.
237
+ *
238
+ * Maintained here rather than alongside the registration because **the pose evaluator needs it
239
+ * whether or not there is a device**: `query_entity_node_world_pose` answers on the CPU, from
240
+ * the clips this driver plays, and a game with rendering disabled still asks (I-4).
241
+ *
242
+ * @param {object} entry
243
+ * @returns {boolean} whether the set changed
244
+ */
245
+ #refresh_played_slots(entry) {
246
+ const playbacks = entry.player.playbacks;
247
+ const played = entry.played_slots;
248
+
249
+ /*
250
+ The distinct slots, and distinct is the point: two entries of the list can name one clip of
251
+ the model, so the number of playbacks is not the number of clips. Comparing the two would
252
+ report a change on every frame for a list that had not moved, and a change here is a
253
+ re-registration.
254
+ */
255
+ const wanted = [];
256
+
257
+ for (let i = 0; i < playbacks.length; i++) {
258
+ const slot = playbacks[i].slot;
259
+
260
+ if (wanted.indexOf(slot) === -1) {
261
+ wanted.push(slot);
262
+ }
263
+ }
264
+
265
+ if (wanted.length === played.length) {
266
+ let same = true;
267
+
268
+ for (let i = 0; i < wanted.length; i++) {
269
+ if (played[i] !== wanted[i]) {
270
+ same = false;
271
+ break;
272
+ }
273
+ }
274
+
275
+ if (same) {
276
+ return false;
277
+ }
278
+ }
279
+
280
+ entry.played_slots = Int32Array.from(wanted);
281
+
282
+ return true;
283
+ }
284
+
285
+ /**
286
+ * Keep the registered clips equal to the ones the list names.
287
+ *
288
+ * A clip that leaves the list has to be unregistered, or it stays bound at weight zero and takes
289
+ * the joints only it drives down to zero with it.
290
+ *
291
+ * @param {object} entry
292
+ * @param {PrefabInstance} instance
293
+ * @param {GPUSceneContext} context
294
+ * @param {boolean} set_changed whether {@link #refresh_played_slots} just moved
295
+ * @returns {boolean} whether the registration was replaced
296
+ */
297
+ #refresh_clips(entry, instance, context, set_changed) {
298
+ const registration = entry.clip_registration;
299
+
300
+ if (!set_changed && registration !== null && registration.context === context) {
301
+ return false;
302
+ }
303
+
304
+ this.#release_clips_only(entry);
305
+
306
+ entry.clip_registration = register_instance_clips(context, instance, entry.played_slots);
307
+
308
+ return true;
309
+ }
310
+
311
+ /**
312
+ * @param {number} time_delta_seconds
313
+ */
314
+ update(time_delta_seconds) {
315
+ const ecd = this.entityManager.dataset;
316
+
317
+ const context = this.#graphics.scene_context(this.#meshes.scene);
318
+
319
+ // No device — before the first start, and again after a stop. The clocks still run: a clip
320
+ // that ends while the renderer is being replaced still says so.
321
+ const manager = context === null ? null : context.animation_manager;
322
+
323
+ for (const [entity, entry] of this.#entries) {
324
+ const instance = this.#meshes.instance_of(entity);
325
+
326
+ if (instance === null) {
327
+ // the model has not arrived, and its clips are what say how long they run
328
+ continue;
329
+ }
330
+
331
+ if (instance !== entry.instance) {
332
+ entry.instance = instance;
333
+
334
+ entry.player.bind(entity, ecd, instance);
335
+
336
+ entry.written = new Float32Array(instance.clips.length).fill(-1);
337
+ entry.poses = instance.clips.map(() => new PosePlayback());
338
+
339
+ // ids the manager issued for the model that was here before name clips this one
340
+ // does not have
341
+ this.#release_clips(entry);
342
+ }
343
+
344
+ entry.player.tick(time_delta_seconds);
345
+
346
+ const set_changed = this.#refresh_played_slots(entry);
347
+
348
+ if (context === null) {
349
+ continue;
350
+ }
351
+
352
+ if (this.#refresh_clips(entry, instance, context, set_changed)) {
353
+ // whatever the last registration was told is not what this one knows
354
+ entry.written.fill(-1);
355
+ }
356
+
357
+ write_clip_playback(manager, entry.player, entry.clip_registration.ids, entry.written);
358
+ }
359
+ }
360
+ }