@configura/babylon-view 2.0.0-alpha.2 → 2.0.0-alpha.21

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 (134) hide show
  1. package/.eslintrc.json +5 -18
  2. package/LICENSE +201 -201
  3. package/README.md +1 -1
  4. package/dist/animation/AnimatableObject.d.ts +8 -8
  5. package/dist/animation/AnimatableObject.js +3 -3
  6. package/dist/animation/animator/Animator.d.ts +33 -33
  7. package/dist/animation/animator/Animator.js +58 -58
  8. package/dist/animation/animator/AnimatorEasing.d.ts +16 -16
  9. package/dist/animation/animator/AnimatorEasing.js +31 -31
  10. package/dist/animation/animator/AnimatorEasingMatrix.d.ts +14 -14
  11. package/dist/animation/animator/AnimatorEasingMatrix.js +16 -16
  12. package/dist/animation/animator/AnimatorHighlight.d.ts +16 -16
  13. package/dist/animation/animator/AnimatorHighlight.js +32 -32
  14. package/dist/animation/animator/AnimatorPointToPoint.d.ts +8 -8
  15. package/dist/animation/animator/AnimatorPointToPoint.js +14 -14
  16. package/dist/animation/animator/AnimatorQueue.d.ts +13 -13
  17. package/dist/animation/animator/AnimatorQueue.js +57 -57
  18. package/dist/animation/animator/AnimatorScale.d.ts +8 -8
  19. package/dist/animation/animator/AnimatorScale.js +13 -13
  20. package/dist/animation/animator/AnimatorSpin.d.ts +10 -10
  21. package/dist/animation/animator/AnimatorSpin.js +13 -13
  22. package/dist/animation/animator/EasingFunctions.d.ts +35 -35
  23. package/dist/animation/animator/EasingFunctions.js +137 -137
  24. package/dist/animation/coordinator/Coordinator.d.ts +28 -28
  25. package/dist/animation/coordinator/Coordinator.js +53 -53
  26. package/dist/animation/coordinator/CoordinatorDropAndSpin.d.ts +22 -22
  27. package/dist/animation/coordinator/CoordinatorDropAndSpin.js +138 -138
  28. package/dist/animation/coordinator/CoordinatorIdentity.d.ts +11 -11
  29. package/dist/animation/coordinator/CoordinatorIdentity.js +14 -14
  30. package/dist/animation/coordinator/CoordinatorNodeQueues.d.ts +18 -18
  31. package/dist/animation/coordinator/CoordinatorNodeQueues.js +50 -50
  32. package/dist/animation/coordinator/CoordinatorPulse.d.ts +21 -21
  33. package/dist/animation/coordinator/CoordinatorPulse.js +47 -47
  34. package/dist/animation/coordinator/CoordinatorPulseBounce.d.ts +14 -14
  35. package/dist/animation/coordinator/CoordinatorPulseBounce.js +35 -35
  36. package/dist/animation/coordinator/CoordinatorPulseHighlight.d.ts +13 -13
  37. package/dist/animation/coordinator/CoordinatorPulseHighlight.js +29 -29
  38. package/dist/animation/coordinator/CoordinatorPulseInflate.d.ts +14 -14
  39. package/dist/animation/coordinator/CoordinatorPulseInflate.js +23 -23
  40. package/dist/camera/CameraCreator.d.ts +5 -5
  41. package/dist/camera/CameraCreator.js +4 -4
  42. package/dist/camera/CfgArcRotateCameraPointersInput.d.ts +26 -26
  43. package/dist/camera/CfgArcRotateCameraPointersInput.js +266 -266
  44. package/dist/camera/CfgOrbitalCamera.d.ts +76 -73
  45. package/dist/camera/CfgOrbitalCamera.js +281 -260
  46. package/dist/camera/CfgOrbitalCameraControlProps.d.ts +14 -11
  47. package/dist/camera/CfgOrbitalCameraControlProps.js +7 -6
  48. package/dist/camera/GradingApplier.d.ts +3 -3
  49. package/dist/camera/GradingApplier.js +48 -48
  50. package/dist/engine/EngineCreator.d.ts +3 -3
  51. package/dist/engine/EngineCreator.js +10 -10
  52. package/dist/geometry/CfgGeometry.d.ts +29 -29
  53. package/dist/geometry/CfgGeometry.js +146 -146
  54. package/dist/geometry/CfgMesh.d.ts +10 -10
  55. package/dist/geometry/CfgMesh.js +38 -38
  56. package/dist/geometry/geoSplitter.d.ts +8 -8
  57. package/dist/geometry/geoSplitter.js +192 -192
  58. package/dist/geometry/stretch/CfgMorphTarget.d.ts +15 -15
  59. package/dist/geometry/stretch/CfgMorphTarget.js +65 -65
  60. package/dist/geometry/stretch/CfgStretchData.d.ts +116 -116
  61. package/dist/geometry/stretch/CfgStretchData.js +350 -350
  62. package/dist/geometry/stretch/CfgStretchMorphGeometry.d.ts +16 -16
  63. package/dist/geometry/stretch/CfgStretchMorphGeometry.js +95 -95
  64. package/dist/index.d.ts +16 -16
  65. package/dist/index.js +16 -16
  66. package/dist/io/CfgHistoryToCameraConfConnector.d.ts +31 -29
  67. package/dist/io/CfgHistoryToCameraConfConnector.js +90 -80
  68. package/dist/io/CfgIOCameraConfConnector.d.ts +35 -35
  69. package/dist/io/CfgIOCameraConfConnector.js +81 -82
  70. package/dist/io/CfgObservableStateToCameraConfConnector.d.ts +10 -10
  71. package/dist/io/CfgObservableStateToCameraConfConnector.js +11 -11
  72. package/dist/io/CfgWindowMessageToCameraConfConnector.d.ts +10 -10
  73. package/dist/io/CfgWindowMessageToCameraConfConnector.js +11 -11
  74. package/dist/light/CfgDirectionalLight.d.ts +8 -8
  75. package/dist/light/CfgDirectionalLight.js +18 -18
  76. package/dist/light/CfgHemisphericLight.d.ts +7 -7
  77. package/dist/light/CfgHemisphericLight.js +17 -17
  78. package/dist/light/CfgPointLight.d.ts +8 -8
  79. package/dist/light/CfgPointLight.js +18 -18
  80. package/dist/light/DefaultLightRig.d.ts +19 -19
  81. package/dist/light/DefaultLightRig.js +77 -77
  82. package/dist/light/LightRigCreator.d.ts +9 -9
  83. package/dist/light/LightRigCreator.js +3 -3
  84. package/dist/material/CfgMaterial.d.ts +68 -68
  85. package/dist/material/CfgMaterial.js +482 -482
  86. package/dist/material/DummyMaterialCreator.d.ts +4 -4
  87. package/dist/material/DummyMaterialCreator.js +15 -15
  88. package/dist/material/material.d.ts +18 -18
  89. package/dist/material/material.js +128 -128
  90. package/dist/material/texture.d.ts +14 -14
  91. package/dist/material/texture.js +306 -306
  92. package/dist/nodes/CfgContentRootNode.d.ts +19 -19
  93. package/dist/nodes/CfgContentRootNode.js +75 -75
  94. package/dist/nodes/CfgDeferredMeshNode.d.ts +55 -55
  95. package/dist/nodes/CfgDeferredMeshNode.js +378 -378
  96. package/dist/nodes/CfgProductNode.d.ts +127 -127
  97. package/dist/nodes/CfgProductNode.js +598 -598
  98. package/dist/nodes/CfgSymNode.d.ts +50 -50
  99. package/dist/nodes/CfgSymNode.js +249 -249
  100. package/dist/nodes/CfgSymRootNode.d.ts +45 -45
  101. package/dist/nodes/CfgSymRootNode.js +229 -229
  102. package/dist/nodes/CfgTransformNode.d.ts +33 -33
  103. package/dist/nodes/CfgTransformNode.js +83 -83
  104. package/dist/scene/SceneCreator.d.ts +6 -6
  105. package/dist/scene/SceneCreator.js +22 -22
  106. package/dist/utilities/CfgBoundingBox.d.ts +21 -21
  107. package/dist/utilities/CfgBoundingBox.js +81 -81
  108. package/dist/utilities/anchor/anchor.d.ts +50 -50
  109. package/dist/utilities/anchor/anchor.js +133 -133
  110. package/dist/utilities/anchor/anchorMap.d.ts +20 -20
  111. package/dist/utilities/anchor/anchorMap.js +111 -111
  112. package/dist/utilities/utilities3D.d.ts +70 -70
  113. package/dist/utilities/utilities3D.js +265 -265
  114. package/dist/utilities/utilitiesColor.d.ts +18 -18
  115. package/dist/utilities/utilitiesColor.js +50 -50
  116. package/dist/utilities/utilitiesImage.d.ts +6 -6
  117. package/dist/utilities/utilitiesImage.js +107 -107
  118. package/dist/utilities/utilitiesSymRootIdentifier.d.ts +7 -7
  119. package/dist/utilities/utilitiesSymRootIdentifier.js +26 -26
  120. package/dist/view/BaseView.d.ts +78 -78
  121. package/dist/view/BaseView.js +303 -303
  122. package/dist/view/BaseViewConfiguration.d.ts +32 -32
  123. package/dist/view/BaseViewConfiguration.js +10 -10
  124. package/dist/view/RenderEnv.d.ts +43 -43
  125. package/dist/view/RenderEnv.js +7 -7
  126. package/dist/view/SingleProductDefaultCameraView.d.ts +38 -37
  127. package/dist/view/SingleProductDefaultCameraView.js +149 -145
  128. package/dist/view/SingleProductDefaultCameraViewConfiguration.d.ts +44 -42
  129. package/dist/view/SingleProductDefaultCameraViewConfiguration.js +11 -11
  130. package/dist/view/SingleProductView.d.ts +44 -44
  131. package/dist/view/SingleProductView.js +212 -212
  132. package/dist/view/SingleProductViewConfiguration.d.ts +32 -32
  133. package/dist/view/SingleProductViewConfiguration.js +19 -19
  134. package/package.json +5 -5
@@ -1,350 +1,350 @@
1
- import { Vector3 } from "@babylonjs/core/Maths/math.vector.js";
2
- import { SymGfxMode } from "@configura/web-core/dist/cm/format/cmsym/components/SymComponent.js";
3
- import { convertLength, hashCode } from "@configura/web-utilities";
4
- import { MESH_UNIT } from "../../nodes/CfgSymRootNode.js";
5
- import { cmPointToVector3 } from "../../utilities/utilities3D.js";
6
- function calculateNormals(sp, ep) {
7
- const normal = ep.subtract(sp);
8
- const spToEpLength = normal.length();
9
- const normalizedNormal = normal.normalizeToNew();
10
- return {
11
- spToEpLength,
12
- normal,
13
- normalizedNormal,
14
- };
15
- }
16
- /**
17
- * How far along from sp to ep is a point on a plane crossing the sp to ep line?
18
- * 1 means at ep, 0 means at sp.
19
- */
20
- export function calculateAlongness(p, sp, spToEpLength, normalizedNormal) {
21
- const towardTp = p.subtract(sp);
22
- return ((normalizedNormal.x * towardTp.x +
23
- normalizedNormal.y * towardTp.y +
24
- normalizedNormal.z * towardTp.z) /
25
- spToEpLength);
26
- }
27
- /**
28
- * Where does the point p end up given the CfgStretchDatas and the StretchMap.
29
- * This is the same equations that Babylon uses for stretching, only Babylon does it in the
30
- * shaders during rendering and is thus GPU accelerated.
31
- * This version is much slower, but that is okay as long as this function is sparingly used.
32
- */
33
- export const toStretchedPoint = (p, stretchDatas, stretchReferenceLengthsByMeasureParamCode) => stretchDatas.reduce((acc, stretchData) => {
34
- const referenceLength = stretchReferenceLengthsByMeasureParamCode.get(stretchData.measureParam);
35
- if (referenceLength === undefined) {
36
- return acc;
37
- }
38
- return acc.add(stretchData
39
- .stretchPointToTwice(p)
40
- .scale(stretchData.calculateInfluenceGivenTwiceMorphTarget(referenceLength.current)));
41
- }, p);
42
- function spOrEpToPoint(node) {
43
- const trans = node.symTransform(true);
44
- if (trans.scale.x !== 1 || trans.scale.y !== 1 || trans.scale.z !== 1) {
45
- throw new Error("We do not expect scale on sp or ep");
46
- }
47
- if (trans.rot !== undefined) {
48
- throw new Error("We do not expect rotation on sp or ep");
49
- }
50
- if (trans.pivot !== undefined) {
51
- throw new Error("We do not expect pivot on sp or ep");
52
- }
53
- const pos = trans.pos;
54
- if (pos === undefined) {
55
- return new Vector3();
56
- }
57
- return cmPointToVector3(pos);
58
- }
59
- /**
60
- * Please note that stretch is not bound to be contained between sp and ep.
61
- *
62
- * The thinking in the equations in CfgStretchData is that we want to support MorphTargets in an as
63
- * efficient manner as possible. As MorphTargets work by morphing between pre-compiled meshes we
64
- * calculate what happens if you stretch to twice the referenceLength, to make MorphTargets for
65
- * twice the referenceLength, and then calculate how these MorphTargets should be applied to
66
- * achieve to requested referenceLength.
67
- *
68
- * This adaption to MorphTargets might make the code for stretching points for other purposes look
69
- * (be) a bit complicated. However, as a mesh often contains millions of vertices that needs to be
70
- * moved and calculating for bounding boxes, and anchor points and such moves very few vertices,
71
- * this is okay.
72
- *
73
- * Using "Twice" as the precomputed target is arbitrarily, we just chose it because it makes
74
- * equations simple.
75
- */
76
- export class CfgStretchData {
77
- /**
78
- * It is assumed that the symNode passed really is stretch (symNodeIsStretch has been run)
79
- * so any data missing for stretch will be an Exception.
80
- *
81
- * @param detailLevel
82
- * @param measureParam Name of the parameter controlling the stretch
83
- * @param stretchSymNodeId Id of the SymNode. This is used for anchoring.
84
- * @param originalReferenceLength Server-side calculated original referenceLength. We do not
85
- * actually use this for anything, but it can be handy for debug.
86
- * @param sp Start Point, the start anchor of the stretch line.
87
- * @param ep End Point, the end anchor of the stretch line.
88
- * @param spToEpLength Distance from sp to ep.
89
- * @param originalSpToEpLength spToEpLength before transforms.
90
- * @param normal The normal is from sp to ep and is the normal of every plane that divides this
91
- * stretch.
92
- * @param normalNormalized Normalized version of normal
93
- * @param sections The slices the model is sliced into
94
- */
95
- constructor(detailLevel, measureParam, stretchSymNodeId, originalReferenceLength, sp, ep, spToEpLength, originalSpToEpLength, normal, normalNormalized, sections) {
96
- this.detailLevel = detailLevel;
97
- this.measureParam = measureParam;
98
- this.stretchSymNodeId = stretchSymNodeId;
99
- this.originalReferenceLength = originalReferenceLength;
100
- this.sp = sp;
101
- this.ep = ep;
102
- this.spToEpLength = spToEpLength;
103
- this.originalSpToEpLength = originalSpToEpLength;
104
- this.normal = normal;
105
- this.normalNormalized = normalNormalized;
106
- this.sections = sections;
107
- /** How far along between sp and ep is p? */
108
- this.getAlongness = (p) => calculateAlongness(p, this.sp, this.spToEpLength, this.normalNormalized);
109
- }
110
- static make(logger, symNode, detailLevel) {
111
- const symMeasure = symNode.getMeasure();
112
- if (symMeasure === undefined) {
113
- throw new Error("No symMeasure found");
114
- }
115
- const { measureParam, sections, originalLength } = symMeasure;
116
- if (measureParam === undefined) {
117
- throw new Error("No measureParam");
118
- }
119
- const stretchSymNodeId = symNode.id;
120
- if (stretchSymNodeId !== measureParam) {
121
- logger.warn(`measureParam (${measureParam}) has different ID from parent symNode (${stretchSymNodeId})`);
122
- }
123
- const children = symNode.children(logger, true, true);
124
- let sp;
125
- let ep;
126
- const detailLevelToDividerNameToPos = new Map();
127
- let detailLevelNodeName3D;
128
- let detailLevelNodeName2D;
129
- for (const [name, node] of children) {
130
- const symReps = node.symReps(true);
131
- const details = symReps._details;
132
- // We relax the requirements on detail-level a bit when it comes to sp and ep.
133
- // If we can not find sp and/or ep that is the right detail-level we take the
134
- // first one we found.
135
- if (sp === undefined && name === "sp") {
136
- sp = spOrEpToPoint(node);
137
- }
138
- else if (ep === undefined && name === "ep") {
139
- ep = spOrEpToPoint(node);
140
- }
141
- // We only handle children which either has no detail-level or has an acceptable
142
- // detail-level
143
- if (details !== undefined) {
144
- const detailMask = details.values().next().value;
145
- if (!detailMask.includes(detailLevel)) {
146
- continue;
147
- }
148
- }
149
- if (name.startsWith("divider")) {
150
- const nodeChildren = node.children(logger, true, true);
151
- for (const [subLevelNodeName, subNode] of nodeChildren) {
152
- const trans = subNode.symTransform(true);
153
- if (trans === undefined) {
154
- continue;
155
- }
156
- const pos = trans.pos;
157
- if (pos === undefined) {
158
- continue;
159
- }
160
- let dividerNameToPos = detailLevelToDividerNameToPos.get(subLevelNodeName);
161
- if (dividerNameToPos === undefined) {
162
- dividerNameToPos = new Map();
163
- detailLevelToDividerNameToPos.set(subLevelNodeName, dividerNameToPos);
164
- }
165
- dividerNameToPos.set(name, cmPointToVector3(pos));
166
- }
167
- }
168
- else if (name.startsWith("3D")) {
169
- detailLevelNodeName3D = name;
170
- }
171
- else if (name.startsWith("2D")) {
172
- detailLevelNodeName2D = name;
173
- }
174
- else if (name === "sp") {
175
- sp = spOrEpToPoint(node);
176
- }
177
- else if (name === "ep") {
178
- ep = spOrEpToPoint(node);
179
- }
180
- }
181
- if (sp === undefined) {
182
- throw new Error("Start point missing");
183
- }
184
- if (ep === undefined) {
185
- throw new Error("End point missing");
186
- }
187
- if (sp.equals(ep)) {
188
- throw new Error("Start point and End point are equal");
189
- }
190
- const { spToEpLength, normal, normalizedNormal } = calculateNormals(sp, ep);
191
- if (detailLevelToDividerNameToPos.size === 0) {
192
- throw new Error("Dividers missing");
193
- }
194
- if (detailLevelNodeName3D === undefined && detailLevelNodeName2D === undefined) {
195
- throw new Error("Details level node name missing");
196
- }
197
- let detailLevelGfxMode = SymGfxMode.undefined;
198
- let dividerNameToPos;
199
- if (detailLevelNodeName3D !== undefined) {
200
- dividerNameToPos = detailLevelToDividerNameToPos.get(detailLevelNodeName3D);
201
- if (dividerNameToPos !== undefined) {
202
- detailLevelGfxMode = SymGfxMode.x3D;
203
- }
204
- }
205
- if (dividerNameToPos === undefined && detailLevelNodeName2D !== undefined) {
206
- dividerNameToPos = detailLevelToDividerNameToPos.get(detailLevelNodeName2D);
207
- if (dividerNameToPos !== undefined) {
208
- detailLevelGfxMode = SymGfxMode.x2D;
209
- }
210
- }
211
- if (dividerNameToPos === undefined) {
212
- throw new Error("Dividers for detail level missing");
213
- }
214
- const processedSections = CfgStretchData.processSections(sections, detailLevel, detailLevelGfxMode, dividerNameToPos, sp, spToEpLength, normalizedNormal);
215
- return new this(detailLevel, measureParam, stretchSymNodeId, originalLength, sp, ep, spToEpLength, spToEpLength, normal, normalizedNormal, processedSections);
216
- }
217
- static processSections(sections, detailLevel, detailLevelGfxMode, dividerNameToPos, sp, spToEpLength, normalizedNormal) {
218
- // The morph target shall morph to making the morphed thing
219
- // twice as long as before morph, that is, 100% increase.
220
- // The sizeChange parameters are assumed to aggregate to 1.
221
- // The alongness goes from 0 at sp to 1 at ep. So summing these
222
- // should make the postStretchEndAlongness of the last one be 2.
223
- let accumulatedSizeChange = 0;
224
- let startAlongness = 0;
225
- let atTwiceStartAlongness = 0;
226
- return sections.map((section, sectionIndex) => {
227
- const { move, sizeChange, gfxModeToDetailLevelToMultiSelector } = section;
228
- const sectionsLength = sections.length;
229
- if (sectionsLength < 1) {
230
- throw new Error("At lease one section needed for stretch");
231
- }
232
- const first = sectionIndex === 0;
233
- const last = sectionIndex === sectionsLength - 1;
234
- const detailLevelToMultiSelector = gfxModeToDetailLevelToMultiSelector.get(detailLevelGfxMode);
235
- if (detailLevelToMultiSelector === undefined) {
236
- throw new Error("No x3D or x2D for section");
237
- }
238
- const multiSelector = detailLevelToMultiSelector.get(detailLevel);
239
- if (multiSelector === undefined) {
240
- throw new Error("No multiSelector for detail level");
241
- }
242
- const selectors = multiSelector.selectors;
243
- const selectorsLength = selectors.length;
244
- if ((first || last ? 1 : 2) !== selectorsLength) {
245
- throw new Error("Divider plane count expected to be 1 for first or last and 2 for others");
246
- }
247
- let endAlongness;
248
- if (last) {
249
- endAlongness = 1;
250
- }
251
- else {
252
- const dividerId = selectors[selectorsLength - 1].dividerId;
253
- const pos = dividerNameToPos.get(dividerId);
254
- if (pos === undefined) {
255
- throw new Error("No position found for symDividerSelector");
256
- }
257
- endAlongness = calculateAlongness(pos, sp, spToEpLength, normalizedNormal);
258
- }
259
- accumulatedSizeChange += sizeChange;
260
- const atTwiceEndAlongness = endAlongness + accumulatedSizeChange;
261
- const result = {
262
- last,
263
- move,
264
- sizeChange,
265
- startAlongness,
266
- endAlongness,
267
- atTwiceTranslation: move && 0 < sectionIndex
268
- ? atTwiceEndAlongness - endAlongness
269
- : atTwiceStartAlongness - startAlongness,
270
- atTwiceScale: move
271
- ? 0
272
- : (atTwiceEndAlongness - atTwiceStartAlongness) /
273
- (endAlongness - startAlongness) -
274
- 1,
275
- };
276
- startAlongness = endAlongness;
277
- atTwiceStartAlongness = atTwiceEndAlongness;
278
- return result;
279
- });
280
- }
281
- get hash() {
282
- if (this._hash === undefined) {
283
- this._hash = hashCode(`${this.detailLevel}-${this.measureParam}-${this.sp.asArray().join(",")}-${this.ep
284
- .asArray()
285
- .join(",")}-${this.originalSpToEpLength}-${this.sections
286
- .map((section) => `${section.startAlongness}-${section.endAlongness}-${section.move ? "Y" : "N"}-${section.sizeChange}`)
287
- .join(",")}`);
288
- }
289
- return this._hash;
290
- }
291
- /**
292
- * If the Matrix is identity the original CfgStretchData is returned.
293
- * If not a new CfgStretchData will be created.
294
- * sp, ep, spToEpLength, normal, normalNormalized, scale will be cloned and transformed
295
- * The rest will still reference the original objects
296
- */
297
- applyMatrix(matrix) {
298
- if (matrix.isIdentity()) {
299
- return this;
300
- }
301
- const sp = Vector3.TransformCoordinates(this.sp, matrix);
302
- const ep = Vector3.TransformCoordinates(this.ep, matrix);
303
- const { spToEpLength, normal, normalizedNormal } = calculateNormals(sp, ep);
304
- return new CfgStretchData(this.detailLevel, this.measureParam, this.stretchSymNodeId, this.originalReferenceLength, sp, ep, spToEpLength, this.spToEpLength, normal, normalizedNormal, this.sections);
305
- }
306
- /**
307
- * Calculates the translation of a point which moves it from its original position to where it
308
- * would be if we stretched to twice the reference length. Please note: this gives the
309
- * translation, not the new location. The new location will be p.add(stretchPointToTwice(p))
310
- */
311
- stretchPointToTwice(p) {
312
- const alongness = this.getAlongness(p);
313
- const { sections, normal } = this;
314
- for (const section of sections) {
315
- const endAlongness = section.endAlongness;
316
- if (section.last || alongness <= endAlongness) {
317
- let translation = section.atTwiceTranslation;
318
- if (!section.move) {
319
- translation += (alongness - section.startAlongness) * section.atTwiceScale;
320
- }
321
- return normal.scale(translation);
322
- }
323
- }
324
- throw new Error("Should not be possible");
325
- }
326
- /**
327
- * Given a reference length calculates the influence that should be applied to a MorphTarget to
328
- * achieve the requested length, given that these MorphTargets are constructed so that they
329
- * will double the length when the influence 1 is applied.
330
- *
331
- * Please note that the referenceLength is the wanted length between sp and ep, that is, the
332
- * stretch line. This line does not have to be span the entire model. Also, there can be
333
- * multiple stretch lines that are not orthogonal to this, and the will also affect the actual
334
- * size. Finally there can be transforms applied in the tree. So, the referenceLength will only
335
- * be the actual length of the model in specific cases. However, we believe that models with
336
- * stretch are often constructed in such a way that in real use cases the referenceLength is
337
- * the actual real length.
338
- */
339
- calculateInfluenceGivenTwiceMorphTarget(referenceLength) {
340
- const { originalSpToEpLength } = this;
341
- if (referenceLength.length === 0) {
342
- // CET considers 0 as a no-value and will return the stretch to the default value.
343
- // If this changes, remove this workaround.
344
- return 0;
345
- }
346
- return ((convertLength(referenceLength.length, referenceLength.unit, MESH_UNIT) -
347
- originalSpToEpLength) /
348
- originalSpToEpLength);
349
- }
350
- }
1
+ import { Vector3 } from "@babylonjs/core/Maths/math.vector.js";
2
+ import { SymGfxMode } from "@configura/web-core/dist/cm/format/cmsym/components/SymComponent.js";
3
+ import { convertLength, hashCode } from "@configura/web-utilities";
4
+ import { MESH_UNIT } from "../../nodes/CfgSymRootNode.js";
5
+ import { cmPointToVector3 } from "../../utilities/utilities3D.js";
6
+ function calculateNormals(sp, ep) {
7
+ const normal = ep.subtract(sp);
8
+ const spToEpLength = normal.length();
9
+ const normalizedNormal = normal.normalizeToNew();
10
+ return {
11
+ spToEpLength,
12
+ normal,
13
+ normalizedNormal,
14
+ };
15
+ }
16
+ /**
17
+ * How far along from sp to ep is a point on a plane crossing the sp to ep line?
18
+ * 1 means at ep, 0 means at sp.
19
+ */
20
+ export function calculateAlongness(p, sp, spToEpLength, normalizedNormal) {
21
+ const towardTp = p.subtract(sp);
22
+ return ((normalizedNormal.x * towardTp.x +
23
+ normalizedNormal.y * towardTp.y +
24
+ normalizedNormal.z * towardTp.z) /
25
+ spToEpLength);
26
+ }
27
+ /**
28
+ * Where does the point p end up given the CfgStretchDatas and the StretchMap.
29
+ * This is the same equations that Babylon uses for stretching, only Babylon does it in the
30
+ * shaders during rendering and is thus GPU accelerated.
31
+ * This version is much slower, but that is okay as long as this function is sparingly used.
32
+ */
33
+ export const toStretchedPoint = (p, stretchDatas, stretchReferenceLengthsByMeasureParamCode) => stretchDatas.reduce((acc, stretchData) => {
34
+ const referenceLength = stretchReferenceLengthsByMeasureParamCode.get(stretchData.measureParam);
35
+ if (referenceLength === undefined) {
36
+ return acc;
37
+ }
38
+ return acc.add(stretchData
39
+ .stretchPointToTwice(p)
40
+ .scale(stretchData.calculateInfluenceGivenTwiceMorphTarget(referenceLength.current)));
41
+ }, p);
42
+ function spOrEpToPoint(node) {
43
+ const trans = node.symTransform(true);
44
+ if (trans.scale.x !== 1 || trans.scale.y !== 1 || trans.scale.z !== 1) {
45
+ throw new Error("We do not expect scale on sp or ep");
46
+ }
47
+ if (trans.rot !== undefined) {
48
+ throw new Error("We do not expect rotation on sp or ep");
49
+ }
50
+ if (trans.pivot !== undefined) {
51
+ throw new Error("We do not expect pivot on sp or ep");
52
+ }
53
+ const pos = trans.pos;
54
+ if (pos === undefined) {
55
+ return new Vector3();
56
+ }
57
+ return cmPointToVector3(pos);
58
+ }
59
+ /**
60
+ * Please note that stretch is not bound to be contained between sp and ep.
61
+ *
62
+ * The thinking in the equations in CfgStretchData is that we want to support MorphTargets in an as
63
+ * efficient manner as possible. As MorphTargets work by morphing between pre-compiled meshes we
64
+ * calculate what happens if you stretch to twice the referenceLength, to make MorphTargets for
65
+ * twice the referenceLength, and then calculate how these MorphTargets should be applied to
66
+ * achieve to requested referenceLength.
67
+ *
68
+ * This adaption to MorphTargets might make the code for stretching points for other purposes look
69
+ * (be) a bit complicated. However, as a mesh often contains millions of vertices that needs to be
70
+ * moved and calculating for bounding boxes, and anchor points and such moves very few vertices,
71
+ * this is okay.
72
+ *
73
+ * Using "Twice" as the precomputed target is arbitrarily, we just chose it because it makes
74
+ * equations simple.
75
+ */
76
+ export class CfgStretchData {
77
+ /**
78
+ * It is assumed that the symNode passed really is stretch (symNodeIsStretch has been run)
79
+ * so any data missing for stretch will be an Exception.
80
+ *
81
+ * @param detailLevel
82
+ * @param measureParam Name of the parameter controlling the stretch
83
+ * @param stretchSymNodeId Id of the SymNode. This is used for anchoring.
84
+ * @param originalReferenceLength Server-side calculated original referenceLength. We do not
85
+ * actually use this for anything, but it can be handy for debug.
86
+ * @param sp Start Point, the start anchor of the stretch line.
87
+ * @param ep End Point, the end anchor of the stretch line.
88
+ * @param spToEpLength Distance from sp to ep.
89
+ * @param originalSpToEpLength spToEpLength before transforms.
90
+ * @param normal The normal is from sp to ep and is the normal of every plane that divides this
91
+ * stretch.
92
+ * @param normalNormalized Normalized version of normal
93
+ * @param sections The slices the model is sliced into
94
+ */
95
+ constructor(detailLevel, measureParam, stretchSymNodeId, originalReferenceLength, sp, ep, spToEpLength, originalSpToEpLength, normal, normalNormalized, sections) {
96
+ this.detailLevel = detailLevel;
97
+ this.measureParam = measureParam;
98
+ this.stretchSymNodeId = stretchSymNodeId;
99
+ this.originalReferenceLength = originalReferenceLength;
100
+ this.sp = sp;
101
+ this.ep = ep;
102
+ this.spToEpLength = spToEpLength;
103
+ this.originalSpToEpLength = originalSpToEpLength;
104
+ this.normal = normal;
105
+ this.normalNormalized = normalNormalized;
106
+ this.sections = sections;
107
+ /** How far along between sp and ep is p? */
108
+ this.getAlongness = (p) => calculateAlongness(p, this.sp, this.spToEpLength, this.normalNormalized);
109
+ }
110
+ static make(logger, symNode, detailLevel) {
111
+ const symMeasure = symNode.getMeasure();
112
+ if (symMeasure === undefined) {
113
+ throw new Error("No symMeasure found");
114
+ }
115
+ const { measureParam, sections, originalLength } = symMeasure;
116
+ if (measureParam === undefined) {
117
+ throw new Error("No measureParam");
118
+ }
119
+ const stretchSymNodeId = symNode.id;
120
+ if (stretchSymNodeId !== measureParam) {
121
+ logger.warn(`measureParam (${measureParam}) has different ID from parent symNode (${stretchSymNodeId})`);
122
+ }
123
+ const children = symNode.children(logger, true, true);
124
+ let sp;
125
+ let ep;
126
+ const detailLevelToDividerNameToPos = new Map();
127
+ let detailLevelNodeName3D;
128
+ let detailLevelNodeName2D;
129
+ for (const [name, node] of children) {
130
+ const symReps = node.symReps(true);
131
+ const details = symReps._details;
132
+ // We relax the requirements on detail-level a bit when it comes to sp and ep.
133
+ // If we can not find sp and/or ep that is the right detail-level we take the
134
+ // first one we found.
135
+ if (sp === undefined && name === "sp") {
136
+ sp = spOrEpToPoint(node);
137
+ }
138
+ else if (ep === undefined && name === "ep") {
139
+ ep = spOrEpToPoint(node);
140
+ }
141
+ // We only handle children which either has no detail-level or has an acceptable
142
+ // detail-level
143
+ if (details !== undefined) {
144
+ const detailMask = details.values().next().value;
145
+ if (!detailMask.includes(detailLevel)) {
146
+ continue;
147
+ }
148
+ }
149
+ if (name.startsWith("divider")) {
150
+ const nodeChildren = node.children(logger, true, true);
151
+ for (const [subLevelNodeName, subNode] of nodeChildren) {
152
+ const trans = subNode.symTransform(true);
153
+ if (trans === undefined) {
154
+ continue;
155
+ }
156
+ const pos = trans.pos;
157
+ if (pos === undefined) {
158
+ continue;
159
+ }
160
+ let dividerNameToPos = detailLevelToDividerNameToPos.get(subLevelNodeName);
161
+ if (dividerNameToPos === undefined) {
162
+ dividerNameToPos = new Map();
163
+ detailLevelToDividerNameToPos.set(subLevelNodeName, dividerNameToPos);
164
+ }
165
+ dividerNameToPos.set(name, cmPointToVector3(pos));
166
+ }
167
+ }
168
+ else if (name.startsWith("3D")) {
169
+ detailLevelNodeName3D = name;
170
+ }
171
+ else if (name.startsWith("2D")) {
172
+ detailLevelNodeName2D = name;
173
+ }
174
+ else if (name === "sp") {
175
+ sp = spOrEpToPoint(node);
176
+ }
177
+ else if (name === "ep") {
178
+ ep = spOrEpToPoint(node);
179
+ }
180
+ }
181
+ if (sp === undefined) {
182
+ throw new Error("Start point missing");
183
+ }
184
+ if (ep === undefined) {
185
+ throw new Error("End point missing");
186
+ }
187
+ if (sp.equals(ep)) {
188
+ throw new Error("Start point and End point are equal");
189
+ }
190
+ const { spToEpLength, normal, normalizedNormal } = calculateNormals(sp, ep);
191
+ if (detailLevelToDividerNameToPos.size === 0) {
192
+ throw new Error("Dividers missing");
193
+ }
194
+ if (detailLevelNodeName3D === undefined && detailLevelNodeName2D === undefined) {
195
+ throw new Error("Details level node name missing");
196
+ }
197
+ let detailLevelGfxMode = SymGfxMode.undefined;
198
+ let dividerNameToPos;
199
+ if (detailLevelNodeName3D !== undefined) {
200
+ dividerNameToPos = detailLevelToDividerNameToPos.get(detailLevelNodeName3D);
201
+ if (dividerNameToPos !== undefined) {
202
+ detailLevelGfxMode = SymGfxMode.x3D;
203
+ }
204
+ }
205
+ if (dividerNameToPos === undefined && detailLevelNodeName2D !== undefined) {
206
+ dividerNameToPos = detailLevelToDividerNameToPos.get(detailLevelNodeName2D);
207
+ if (dividerNameToPos !== undefined) {
208
+ detailLevelGfxMode = SymGfxMode.x2D;
209
+ }
210
+ }
211
+ if (dividerNameToPos === undefined) {
212
+ throw new Error("Dividers for detail level missing");
213
+ }
214
+ const processedSections = CfgStretchData.processSections(sections, detailLevel, detailLevelGfxMode, dividerNameToPos, sp, spToEpLength, normalizedNormal);
215
+ return new this(detailLevel, measureParam, stretchSymNodeId, originalLength, sp, ep, spToEpLength, spToEpLength, normal, normalizedNormal, processedSections);
216
+ }
217
+ static processSections(sections, detailLevel, detailLevelGfxMode, dividerNameToPos, sp, spToEpLength, normalizedNormal) {
218
+ // The morph target shall morph to making the morphed thing
219
+ // twice as long as before morph, that is, 100% increase.
220
+ // The sizeChange parameters are assumed to aggregate to 1.
221
+ // The alongness goes from 0 at sp to 1 at ep. So summing these
222
+ // should make the postStretchEndAlongness of the last one be 2.
223
+ let accumulatedSizeChange = 0;
224
+ let startAlongness = 0;
225
+ let atTwiceStartAlongness = 0;
226
+ return sections.map((section, sectionIndex) => {
227
+ const { move, sizeChange, gfxModeToDetailLevelToMultiSelector } = section;
228
+ const sectionsLength = sections.length;
229
+ if (sectionsLength < 1) {
230
+ throw new Error("At lease one section needed for stretch");
231
+ }
232
+ const first = sectionIndex === 0;
233
+ const last = sectionIndex === sectionsLength - 1;
234
+ const detailLevelToMultiSelector = gfxModeToDetailLevelToMultiSelector.get(detailLevelGfxMode);
235
+ if (detailLevelToMultiSelector === undefined) {
236
+ throw new Error("No x3D or x2D for section");
237
+ }
238
+ const multiSelector = detailLevelToMultiSelector.get(detailLevel);
239
+ if (multiSelector === undefined) {
240
+ throw new Error("No multiSelector for detail level");
241
+ }
242
+ const selectors = multiSelector.selectors;
243
+ const selectorsLength = selectors.length;
244
+ if ((first || last ? 1 : 2) !== selectorsLength) {
245
+ throw new Error("Divider plane count expected to be 1 for first or last and 2 for others");
246
+ }
247
+ let endAlongness;
248
+ if (last) {
249
+ endAlongness = 1;
250
+ }
251
+ else {
252
+ const dividerId = selectors[selectorsLength - 1].dividerId;
253
+ const pos = dividerNameToPos.get(dividerId);
254
+ if (pos === undefined) {
255
+ throw new Error("No position found for symDividerSelector");
256
+ }
257
+ endAlongness = calculateAlongness(pos, sp, spToEpLength, normalizedNormal);
258
+ }
259
+ accumulatedSizeChange += sizeChange;
260
+ const atTwiceEndAlongness = endAlongness + accumulatedSizeChange;
261
+ const result = {
262
+ last,
263
+ move,
264
+ sizeChange,
265
+ startAlongness,
266
+ endAlongness,
267
+ atTwiceTranslation: move && 0 < sectionIndex
268
+ ? atTwiceEndAlongness - endAlongness
269
+ : atTwiceStartAlongness - startAlongness,
270
+ atTwiceScale: move
271
+ ? 0
272
+ : (atTwiceEndAlongness - atTwiceStartAlongness) /
273
+ (endAlongness - startAlongness) -
274
+ 1,
275
+ };
276
+ startAlongness = endAlongness;
277
+ atTwiceStartAlongness = atTwiceEndAlongness;
278
+ return result;
279
+ });
280
+ }
281
+ get hash() {
282
+ if (this._hash === undefined) {
283
+ this._hash = hashCode(`${this.detailLevel}-${this.measureParam}-${this.sp.asArray().join(",")}-${this.ep
284
+ .asArray()
285
+ .join(",")}-${this.originalSpToEpLength}-${this.sections
286
+ .map((section) => `${section.startAlongness}-${section.endAlongness}-${section.move ? "Y" : "N"}-${section.sizeChange}`)
287
+ .join(",")}`);
288
+ }
289
+ return this._hash;
290
+ }
291
+ /**
292
+ * If the Matrix is identity the original CfgStretchData is returned.
293
+ * If not a new CfgStretchData will be created.
294
+ * sp, ep, spToEpLength, normal, normalNormalized, scale will be cloned and transformed
295
+ * The rest will still reference the original objects
296
+ */
297
+ applyMatrix(matrix) {
298
+ if (matrix.isIdentity()) {
299
+ return this;
300
+ }
301
+ const sp = Vector3.TransformCoordinates(this.sp, matrix);
302
+ const ep = Vector3.TransformCoordinates(this.ep, matrix);
303
+ const { spToEpLength, normal, normalizedNormal } = calculateNormals(sp, ep);
304
+ return new CfgStretchData(this.detailLevel, this.measureParam, this.stretchSymNodeId, this.originalReferenceLength, sp, ep, spToEpLength, this.spToEpLength, normal, normalizedNormal, this.sections);
305
+ }
306
+ /**
307
+ * Calculates the translation of a point which moves it from its original position to where it
308
+ * would be if we stretched to twice the reference length. Please note: this gives the
309
+ * translation, not the new location. The new location will be p.add(stretchPointToTwice(p))
310
+ */
311
+ stretchPointToTwice(p) {
312
+ const alongness = this.getAlongness(p);
313
+ const { sections, normal } = this;
314
+ for (const section of sections) {
315
+ const endAlongness = section.endAlongness;
316
+ if (section.last || alongness <= endAlongness) {
317
+ let translation = section.atTwiceTranslation;
318
+ if (!section.move) {
319
+ translation += (alongness - section.startAlongness) * section.atTwiceScale;
320
+ }
321
+ return normal.scale(translation);
322
+ }
323
+ }
324
+ throw new Error("Should not be possible");
325
+ }
326
+ /**
327
+ * Given a reference length calculates the influence that should be applied to a MorphTarget to
328
+ * achieve the requested length, given that these MorphTargets are constructed so that they
329
+ * will double the length when the influence 1 is applied.
330
+ *
331
+ * Please note that the referenceLength is the wanted length between sp and ep, that is, the
332
+ * stretch line. This line does not have to be span the entire model. Also, there can be
333
+ * multiple stretch lines that are not orthogonal to this, and the will also affect the actual
334
+ * size. Finally there can be transforms applied in the tree. So, the referenceLength will only
335
+ * be the actual length of the model in specific cases. However, we believe that models with
336
+ * stretch are often constructed in such a way that in real use cases the referenceLength is
337
+ * the actual real length.
338
+ */
339
+ calculateInfluenceGivenTwiceMorphTarget(referenceLength) {
340
+ const { originalSpToEpLength } = this;
341
+ if (referenceLength.length === 0) {
342
+ // CET considers 0 as a no-value and will return the stretch to the default value.
343
+ // If this changes, remove this workaround.
344
+ return 0;
345
+ }
346
+ return ((convertLength(referenceLength.length, referenceLength.unit, MESH_UNIT) -
347
+ originalSpToEpLength) /
348
+ originalSpToEpLength);
349
+ }
350
+ }