@woosh/meep-engine 2.84.6 → 2.84.9

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 (34) hide show
  1. package/build/meep.cjs +137 -107
  2. package/build/meep.min.js +1 -1
  3. package/build/meep.module.js +137 -107
  4. package/package.json +10 -5
  5. package/src/core/cache/LoadingCache.js +2 -1
  6. package/src/core/cache/LoadingCache.spec.js +13 -1
  7. package/src/core/collection/array/arrayIndexByEquality.js +1 -0
  8. package/src/core/collection/list/List.js +25 -17
  9. package/src/core/collection/list/List.spec.js +73 -0
  10. package/src/core/geom/3d/SurfacePoint3.js +3 -40
  11. package/src/core/geom/Vector3.js +25 -14
  12. package/src/core/model/stat/LinearModifier.spec.js +5 -6
  13. package/src/core/process/PromiseWatcher.spec.js +27 -23
  14. package/src/core/process/worker/WorkerBuilder.js +5 -1
  15. package/src/core/process/worker/WorkerProxy.js +3 -2
  16. package/src/engine/animation/behavior/animateProperty.js +4 -4
  17. package/src/engine/ecs/Entity.spec.js +33 -0
  18. package/src/engine/ecs/EntityComponentDataset.js +17 -11
  19. package/src/engine/ecs/EntityReference.js +12 -0
  20. package/src/engine/ecs/dynamic_actions/actions/definition/SpeakLineActionDescription.js +1 -1
  21. package/src/engine/ecs/transform/Transform.js +1 -1
  22. package/src/engine/ecs/transform/Transform.spec.js +44 -0
  23. package/src/engine/graphics/camera/CameraShake.js +1 -127
  24. package/src/engine/graphics/camera/CameraShakeBehavior.js +91 -0
  25. package/src/engine/graphics/camera/CameraShakeTraumaBehavior.js +38 -0
  26. package/src/engine/graphics/ecs/animation/animator/AnimationGraphSystem.js +9 -23
  27. package/src/engine/graphics/ecs/animation/animator/blending/BlendStateMatrix.js +11 -6
  28. package/src/engine/graphics/ecs/animation/animator/graph/AnimationGraph.js +20 -12
  29. package/src/engine/graphics/ecs/animation/animator/graph/AnimationState.js +3 -3
  30. package/src/engine/graphics/ecs/path/tube/build/estimatePathViaIterativeIntegral.js +1 -1
  31. package/src/engine/graphics/particles/particular/engine/parameter/ParameterLookupTable.js +6 -6
  32. package/src/engine/intelligence/behavior/ecs/SendEventBehavior.js +43 -0
  33. package/src/view/View.js +52 -30
  34. package/src/view/writeCssTransformMatrix.js +26 -0
@@ -0,0 +1,38 @@
1
+ import { clamp01 } from "../../../core/math/clamp01.js";
2
+ import { makeCubicCurve } from "../../../core/math/spline/makeCubicCurve.js";
3
+ import { Behavior } from "../../intelligence/behavior/Behavior.js";
4
+ import { BehaviorStatus } from "../../intelligence/behavior/BehaviorStatus.js";
5
+
6
+ export class CameraShakeTraumaBehavior extends Behavior {
7
+
8
+ /**
9
+ *
10
+ * @param {CameraShakeBehavior} shakeBehavior
11
+ * @param {number} decay amount by which trauma decays per second
12
+ */
13
+ constructor({
14
+ shakeBehavior,
15
+ decay = 1,
16
+ }) {
17
+ super();
18
+
19
+
20
+ this.decay = decay;
21
+ this.trauma = 0;
22
+
23
+ this.shakeBehavior = shakeBehavior;
24
+
25
+ this.formula = makeCubicCurve(0, 0.1, 0.1, 1);
26
+ }
27
+
28
+ tick(timeDelta) {
29
+ const shake = this.formula(clamp01(this.trauma));
30
+
31
+ this.trauma = clamp01(this.trauma - timeDelta * this.decay);
32
+
33
+
34
+ this.shakeBehavior.strength = shake;
35
+
36
+ return BehaviorStatus.Running;
37
+ }
38
+ }
@@ -1,31 +1,17 @@
1
+ import { Matrix4 } from "three";
2
+ import { assert } from "../../../../../core/assert.js";
3
+ import Vector4 from "../../../../../core/geom/Vector4.js";
1
4
  import { max3 } from "../../../../../core/math/max3.js";
5
+ import { ResourceAccessKind } from "../../../../../core/model/ResourceAccessKind.js";
6
+ import { ResourceAccessSpecification } from "../../../../../core/model/ResourceAccessSpecification.js";
2
7
  import { System } from "../../../../ecs/System.js";
3
- import { AnimationGraph } from "./graph/AnimationGraph.js";
8
+ import { projectSphere } from "../../../util/projectSphere.js";
9
+ import { CameraSystem } from "../../camera/CameraSystem.js";
4
10
  import Mesh, { MeshFlags } from "../../mesh/Mesh.js";
11
+ import { MeshEvents } from "../../mesh/MeshEvents.js";
5
12
  import { MeshSystem } from "../../mesh/MeshSystem.js";
6
- import { assert } from "../../../../../core/assert.js";
7
- import Vector4 from "../../../../../core/geom/Vector4.js";
8
- import { Matrix4 } from "three";
9
- import { CameraSystem } from "../../camera/CameraSystem.js";
13
+ import { AnimationGraph } from "./graph/AnimationGraph.js";
10
14
  import { AnimationGraphFlag } from "./graph/AnimationGraphFlag.js";
11
- import { projectSphere } from "../../../util/projectSphere.js";
12
- import { MeshEvents } from "../../mesh/MeshEvents.js";
13
- import { ResourceAccessSpecification } from "../../../../../core/model/ResourceAccessSpecification.js";
14
- import { ResourceAccessKind } from "../../../../../core/model/ResourceAccessKind.js";
15
-
16
- /**
17
- *
18
- * @param {AnimationGraph} graph
19
- * @param {Mesh} mesh
20
- * @param {number} entity
21
- * @param {EntityComponentDataset} ecd
22
- */
23
- function attach(graph, mesh, entity, ecd) {
24
- graph.attach(mesh);
25
-
26
- graph.link(entity, ecd);
27
-
28
- }
29
15
 
30
16
  /**
31
17
  * @type {Vector4}
@@ -1,7 +1,14 @@
1
+ import { assert } from "../../../../../../core/assert.js";
1
2
  import { lerp } from "../../../../../../core/math/lerp.js";
2
3
 
3
4
  export class BlendStateMatrix {
5
+ /**
6
+ *
7
+ * @param {number} size
8
+ */
4
9
  constructor(size) {
10
+ assert.isNonNegativeInteger(size, 'size');
11
+
5
12
  this.weights = new Float32Array(size);
6
13
  this.timeScales = new Float32Array(size);
7
14
  }
@@ -64,15 +71,13 @@ export class BlendStateMatrix {
64
71
  *
65
72
  * @param {number} v
66
73
  */
67
- divideScalar(v) {
68
- const tW = this.weights;
69
- const tTS = this.timeScales;
74
+ weightsMultiplyScalar(v) {
75
+ const weights = this.weights;
70
76
 
71
- const n = tW.length;
77
+ const n = weights.length;
72
78
 
73
79
  for (let i = 0; i < n; i++) {
74
- tW[i] /= v;
75
- tTS[i] /= v;
80
+ weights[i] *= v;
76
81
  }
77
82
  }
78
83
 
@@ -1,16 +1,16 @@
1
- import { AnimationState } from "./AnimationState.js";
2
- import { AnimationTransition } from "./AnimationTransition.js";
3
- import { AnimationStateType } from "./AnimationStateType.js";
4
1
  import { AnimationMixer } from "three";
2
+ import { assert } from "../../../../../../core/assert.js";
3
+ import { computeHashIntegerArray } from "../../../../../../core/collection/array/computeHashIntegerArray.js";
4
+ import { computeHashFloat } from "../../../../../../core/primitives/numbers/computeHashFloat.js";
5
+ import { threeUpdateTransform } from "../../../../util/threeUpdateTransform.js";
5
6
  import { AnimationGraphFlag } from "./AnimationGraphFlag.js";
6
- import { writeAnimationGraphDefinitionToJSON } from "./definition/serialization/writeAnimationGraphDefinitionToJSON.js";
7
+ import { AnimationState } from "./AnimationState.js";
8
+ import { AnimationStateType } from "./AnimationStateType.js";
9
+ import { AnimationTransition } from "./AnimationTransition.js";
7
10
  import {
8
11
  readAnimationGraphDefinitionFromJSON
9
12
  } from "./definition/serialization/readAnimationGraphDefinitionFromJSON.js";
10
- import { assert } from "../../../../../../core/assert.js";
11
- import { threeUpdateTransform } from "../../../../util/threeUpdateTransform.js";
12
- import { computeHashIntegerArray } from "../../../../../../core/collection/array/computeHashIntegerArray.js";
13
- import { computeHashFloat } from "../../../../../../core/primitives/numbers/computeHashFloat.js";
13
+ import { writeAnimationGraphDefinitionToJSON } from "./definition/serialization/writeAnimationGraphDefinitionToJSON.js";
14
14
 
15
15
  export class AnimationGraph {
16
16
  constructor() {
@@ -142,15 +142,22 @@ export class AnimationGraph {
142
142
  };
143
143
  }
144
144
 
145
+ /**
146
+ *
147
+ * @param {*} def Graph definition JSON
148
+ * @param {number} state
149
+ * @param {number} debtTime
150
+ * @param {number} flags
151
+ */
145
152
  fromJSON({ def, state, debtTime = 0, flags = 0 }) {
146
- const graphDefinition = readAnimationGraphDefinitionFromJSON(def);
153
+ const graph_definition = readAnimationGraphDefinitionFromJSON(def);
147
154
 
148
155
  this.debtTime = debtTime;
149
156
  this.flags = flags;
150
- this.initialize(graphDefinition);
157
+ this.initialize(graph_definition);
151
158
 
152
159
  if (state === undefined) {
153
- state = graphDefinition.states.indexOf(graphDefinition.startingSate);
160
+ state = graph_definition.states.indexOf(graph_definition.startingSate);
154
161
  }
155
162
 
156
163
  this.state = this.states[state];
@@ -522,7 +529,7 @@ export class AnimationGraph {
522
529
  }
523
530
 
524
531
  // normalize
525
- currentBlendState.divideScalar(nAT);
532
+ currentBlendState.weightsMultiplyScalar(1 / nAT);
526
533
  }
527
534
  } else {
528
535
  //no active transitions, copy current state's blend matrix
@@ -553,6 +560,7 @@ export class AnimationGraph {
553
560
 
554
561
  if (action === undefined) {
555
562
  console.warn(`Action[${i}] is undefined`);
563
+ continue;
556
564
  }
557
565
 
558
566
  action.weight = weight;
@@ -1,5 +1,5 @@
1
- import { AnimationStateType } from "./AnimationStateType.js";
2
1
  import { BlendStateMatrix } from "../blending/BlendStateMatrix.js";
2
+ import { AnimationStateType } from "./AnimationStateType.js";
3
3
 
4
4
  export class AnimationState {
5
5
  constructor() {
@@ -11,13 +11,13 @@ export class AnimationState {
11
11
  this.def = null;
12
12
 
13
13
  /**
14
- *
14
+ * Incoming transitions
15
15
  * @type {AnimationTransition[]}
16
16
  */
17
17
  this.inEdges = [];
18
18
 
19
19
  /**
20
- *
20
+ * Outgoing transitions
21
21
  * @type {AnimationTransition[]}
22
22
  */
23
23
  this.outEdges = [];
@@ -18,7 +18,7 @@ export function estimatePathViaIterativeIntegral(path, epsilon) {
18
18
  return 0;
19
19
  }
20
20
 
21
- for (let p = epsilon; ; i += epsilon) {
21
+ for (let p = epsilon; ; p += epsilon) {
22
22
  const ended = path.sample(p1, p);
23
23
 
24
24
  if (ended) {
@@ -1,12 +1,12 @@
1
- import { inverseLerp } from "../../../../../../core/math/inverseLerp.js";
2
1
  import { assert } from "../../../../../../core/assert.js";
2
+ import { computeHashIntegerArray } from "../../../../../../core/collection/array/computeHashIntegerArray.js";
3
+ import { isArrayEqualStrict } from "../../../../../../core/collection/array/isArrayEqualStrict.js";
4
+ import { computeHashFloatArray } from "../../../../../../core/math/hash/computeHashFloatArray.js";
5
+ import { inverseLerp } from "../../../../../../core/math/inverseLerp.js";
3
6
  import { max2 } from "../../../../../../core/math/max2.js";
4
7
  import { min2 } from "../../../../../../core/math/min2.js";
5
- import { ParameterLookupTableFlags } from "./ParameterLookupTableFlags.js";
6
8
  import { ParameterLookupTableSerializationAdapter } from "../emitter/serde/ParameterLookupTableSerializationAdapter.js";
7
- import { computeHashIntegerArray } from "../../../../../../core/collection/array/computeHashIntegerArray.js";
8
- import { computeHashFloatArray } from "../../../../../../core/math/hash/computeHashFloatArray.js";
9
- import { isArrayEqualStrict } from "../../../../../../core/collection/array/isArrayEqualStrict.js";
9
+ import { ParameterLookupTableFlags } from "./ParameterLookupTableFlags.js";
10
10
 
11
11
 
12
12
  export class ParameterLookupTable {
@@ -174,7 +174,7 @@ export class ParameterLookupTable {
174
174
  const lowValue = data[low_offset + i];
175
175
  const highValue = data[high_offset + i];
176
176
 
177
- // inlined for speed: mix(low, high, fraction)
177
+ // inlined for speed: lerp(low, high, fraction)
178
178
  const value = lowValue * (1 - fraction) + highValue * fraction;
179
179
 
180
180
  result[i] = value;
@@ -0,0 +1,43 @@
1
+ import { assert } from "../../../../core/assert.js";
2
+ import { BehaviorStatus } from "../BehaviorStatus.js";
3
+ import { EntityBehavior } from "./EntityBehavior.js";
4
+
5
+ export class SendEventBehavior extends EntityBehavior {
6
+ constructor() {
7
+ super();
8
+
9
+ this.event = "";
10
+ this.data = {};
11
+ this.target = -1;
12
+ }
13
+
14
+ fromJSON({ event, data = {}, target = -1 }) {
15
+ assert.typeOf(event, 'string', 'event');
16
+
17
+ this.event = event;
18
+ this.data = data;
19
+ this.target = target;
20
+ }
21
+
22
+ static fromJSON(j) {
23
+ const r = new SendEventBehavior();
24
+
25
+ r.fromJSON(j);
26
+
27
+ return r;
28
+ }
29
+
30
+ toString() {
31
+ return `SendEventBehavior{ event: ${this.event}, data:${this.data}, target:${this.target} }`;
32
+ }
33
+
34
+ tick(timeDelta) {
35
+ // console.log(`-> ${this}`);
36
+
37
+ const target = this.target !== -1 ? this.target : this.entity;
38
+
39
+ this.ecd.sendEvent(target, this.event, this.data);
40
+
41
+ return BehaviorStatus.Succeeded;
42
+ }
43
+ }
package/src/view/View.js CHANGED
@@ -3,15 +3,18 @@
3
3
  * @copyright Alex Goldring 2018
4
4
  */
5
5
 
6
- import Vector2 from "../core/geom/Vector2.js";
7
-
8
- import AABB2 from "../core/geom/2d/aabb/AABB2.js";
6
+ import { assert } from "../core/assert.js";
9
7
  import Signal from "../core/events/signal/Signal.js";
10
8
  import { SignalBinding } from "../core/events/signal/SignalBinding.js";
11
- import { assert } from "../core/assert.js";
12
- import Vector1 from "../core/geom/Vector1.js";
9
+
10
+ import AABB2 from "../core/geom/2d/aabb/AABB2.js";
13
11
 
14
12
  import { m3_cm_compose_transform } from "../core/geom/mat3/m3_cm_compose_transform.js";
13
+ import Vector1 from "../core/geom/Vector1.js";
14
+ import Vector2 from "../core/geom/Vector2.js";
15
+ import { epsilonEquals } from "../core/math/epsilonEquals.js";
16
+ import { FLT_EPSILON_32 } from "../core/math/FLT_EPSILON_32.js";
17
+ import { writeCssTransformMatrix } from "./writeCssTransformMatrix.js";
15
18
 
16
19
 
17
20
  const scratch_m3_0 = new Float32Array(9);
@@ -29,26 +32,7 @@ function setElementTransform(domElement, position, scale, rotation) {
29
32
 
30
33
  m3_cm_compose_transform(m3, position.x, position.y, scale.x, scale.y, 0, 0, rotation);
31
34
 
32
-
33
- /*
34
- * CSS matrix is:
35
- * a c e
36
- * b d f
37
- * 0 0 1
38
- */
39
-
40
- const a = m3[0];
41
- const b = m3[3];
42
- const c = m3[1];
43
- const d = m3[4];
44
- const e = m3[2];
45
- const f = m3[5];
46
-
47
- const transform = "matrix(" + a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f + ")";
48
-
49
- const style = domElement.style;
50
-
51
- style.transform = transform;
35
+ writeCssTransformMatrix(m3, domElement);
52
36
  }
53
37
 
54
38
  /**
@@ -88,6 +72,9 @@ const INITIAL_FLAGS = ViewFlags.Visible;
88
72
  * @class
89
73
  */
90
74
  class View {
75
+ #transform_written = new Float32Array(9);
76
+ #transform_current = new Float32Array(9);
77
+
91
78
  /**
92
79
  * @constructor
93
80
  */
@@ -112,31 +99,32 @@ class View {
112
99
  this.flags = INITIAL_FLAGS;
113
100
 
114
101
  /**
115
- *
102
+ * @readonly
116
103
  * @type {Vector2}
117
104
  */
118
105
  const position = this.position = new Vector2(0, 0);
119
106
 
120
107
  /**
121
- *
108
+ * @readonly
122
109
  * @type {Vector1}
123
110
  */
124
111
  const rotation = this.rotation = new Vector1(0);
125
112
 
126
113
  /**
127
- *
114
+ * @readonly
128
115
  * @type {Vector2}
129
116
  */
130
117
  const scale = this.scale = new Vector2(1, 1);
131
118
 
132
119
  /**
133
- *
120
+ * @readonly
134
121
  * @type {Vector2}
135
122
  */
136
123
  const size = this.size = new Vector2(0, 0);
137
124
 
138
125
  /**
139
126
  * Origin from which rotation and scaling is applied
127
+ * @readonly
140
128
  * @type {Vector2}
141
129
  */
142
130
  this.transformOrigin = new Vector2(0.5, 0.5);
@@ -274,7 +262,41 @@ class View {
274
262
  * @private
275
263
  */
276
264
  __updateTransform() {
277
- setElementTransform(this.el, this.position, this.scale, this.rotation.getValue());
265
+ const position = this.position;
266
+ const scale = this.scale;
267
+ const rotation = this.rotation.getValue();
268
+
269
+ m3_cm_compose_transform(this.#transform_current, position.x, position.y, scale.x, scale.y, 0, 0, rotation);
270
+
271
+ this.#tryWriteTransform();
272
+ }
273
+
274
+ #tryWriteTransform() {
275
+
276
+ const current = this.#transform_current;
277
+ const written = this.#transform_written;
278
+
279
+ for (let i = 0; i < 9; i++) {
280
+ const a = current[i];
281
+ const b = written[i];
282
+
283
+ if (epsilonEquals(a, b, FLT_EPSILON_32)) {
284
+ // common path
285
+ continue;
286
+ }
287
+
288
+ this.#writeTransform();
289
+ return true;
290
+
291
+ }
292
+
293
+ return false;
294
+ }
295
+
296
+ #writeTransform() {
297
+ writeCssTransformMatrix(this.#transform_current, this.el);
298
+
299
+ this.#transform_written.set(this.#transform_current);
278
300
  }
279
301
 
280
302
  /**
@@ -0,0 +1,26 @@
1
+ /**
2
+ *
3
+ * @param {Float32Array} m3
4
+ * @param {HTMLElement} domElement
5
+ */
6
+ export function writeCssTransformMatrix(m3, domElement) {
7
+ /*
8
+ * CSS matrix is:
9
+ * a c e
10
+ * b d f
11
+ * 0 0 1
12
+ */
13
+
14
+ const a = m3[0];
15
+ const b = m3[3];
16
+ const c = m3[1];
17
+ const d = m3[4];
18
+ const e = m3[2];
19
+ const f = m3[5];
20
+
21
+ const transform = "matrix(" + a + ',' + b + ',' + c + ',' + d + ',' + e + ',' + f + ")";
22
+
23
+ const style = domElement.style;
24
+
25
+ style.transform = transform;
26
+ }