@babylonjs/core 6.21.0 → 6.21.2

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/Audio/soundTrack.js +1 -1
  2. package/Audio/soundTrack.js.map +1 -1
  3. package/Buffers/buffer.d.ts +2 -1
  4. package/Buffers/buffer.js +13 -5
  5. package/Buffers/buffer.js.map +1 -1
  6. package/Debug/rayHelper.d.ts +3 -3
  7. package/Debug/rayHelper.js +2 -2
  8. package/Debug/rayHelper.js.map +1 -1
  9. package/Engines/Native/nativeHelpers.d.ts +9 -0
  10. package/Engines/Native/nativeHelpers.js +296 -0
  11. package/Engines/Native/nativeHelpers.js.map +1 -0
  12. package/Engines/Native/nativeInterfaces.d.ts +92 -0
  13. package/Engines/Native/nativeInterfaces.js.map +1 -1
  14. package/Engines/engine.d.ts +6 -4
  15. package/Engines/engine.js +7 -1
  16. package/Engines/engine.js.map +1 -1
  17. package/Engines/nativeEngine.d.ts +3 -12
  18. package/Engines/nativeEngine.js +43 -226
  19. package/Engines/nativeEngine.js.map +1 -1
  20. package/Engines/thinEngine.js +4 -2
  21. package/Engines/thinEngine.js.map +1 -1
  22. package/Engines/webgpuEngine.js +2 -1
  23. package/Engines/webgpuEngine.js.map +1 -1
  24. package/FlowGraph/Blocks/Data/Logic/flowGraphLogicBlocks.d.ts +23 -0
  25. package/FlowGraph/Blocks/Data/Logic/flowGraphLogicBlocks.js +31 -0
  26. package/FlowGraph/Blocks/Data/Logic/flowGraphLogicBlocks.js.map +1 -0
  27. package/FlowGraph/Blocks/Data/Math/flowGraphNumberMathBlocks.d.ts +270 -0
  28. package/FlowGraph/Blocks/Data/Math/flowGraphNumberMathBlocks.js +347 -0
  29. package/FlowGraph/Blocks/Data/Math/flowGraphNumberMathBlocks.js.map +1 -0
  30. package/FlowGraph/Blocks/Data/Math/flowGraphVector2MathBlocks.d.ts +52 -0
  31. package/FlowGraph/Blocks/Data/Math/flowGraphVector2MathBlocks.js +71 -0
  32. package/FlowGraph/Blocks/Data/Math/flowGraphVector2MathBlocks.js.map +1 -0
  33. package/FlowGraph/Blocks/Data/Math/flowGraphVector3MathBlocks.d.ts +66 -0
  34. package/FlowGraph/Blocks/Data/Math/flowGraphVector3MathBlocks.js +86 -0
  35. package/FlowGraph/Blocks/Data/Math/flowGraphVector3MathBlocks.js.map +1 -0
  36. package/FlowGraph/Blocks/Data/Math/flowGraphVector4MathBlocks.d.ts +52 -0
  37. package/FlowGraph/Blocks/Data/Math/flowGraphVector4MathBlocks.js +71 -0
  38. package/FlowGraph/Blocks/Data/Math/flowGraphVector4MathBlocks.js.map +1 -0
  39. package/FlowGraph/Blocks/Data/flowGraphBinaryOperationBlock.d.ts +17 -0
  40. package/FlowGraph/Blocks/Data/flowGraphBinaryOperationBlock.js +19 -0
  41. package/FlowGraph/Blocks/Data/flowGraphBinaryOperationBlock.js.map +1 -0
  42. package/FlowGraph/Blocks/Data/flowGraphConstantOperationBlock.d.ts +13 -0
  43. package/FlowGraph/Blocks/Data/flowGraphConstantOperationBlock.js +16 -0
  44. package/FlowGraph/Blocks/Data/flowGraphConstantOperationBlock.js.map +1 -0
  45. package/FlowGraph/Blocks/Data/flowGraphUnaryOperationBlock.d.ts +15 -0
  46. package/FlowGraph/Blocks/Data/flowGraphUnaryOperationBlock.js +17 -0
  47. package/FlowGraph/Blocks/Data/flowGraphUnaryOperationBlock.js.map +1 -0
  48. package/FlowGraph/Blocks/Data/index.d.ts +4 -1
  49. package/FlowGraph/Blocks/Data/index.js +4 -1
  50. package/FlowGraph/Blocks/Data/index.js.map +1 -1
  51. package/FlowGraph/Blocks/Event/flowGraphSceneTickEventBlock.d.ts +16 -0
  52. package/FlowGraph/Blocks/Event/flowGraphSceneTickEventBlock.js +29 -0
  53. package/FlowGraph/Blocks/Event/flowGraphSceneTickEventBlock.js.map +1 -0
  54. package/FlowGraph/Blocks/Event/index.d.ts +1 -0
  55. package/FlowGraph/Blocks/Event/index.js +1 -0
  56. package/FlowGraph/Blocks/Event/index.js.map +1 -1
  57. package/FlowGraph/Blocks/Execution/Animation/flowGraphPauseAnimationBlock.d.ts +16 -0
  58. package/FlowGraph/Blocks/Execution/Animation/flowGraphPauseAnimationBlock.js +18 -0
  59. package/FlowGraph/Blocks/Execution/Animation/flowGraphPauseAnimationBlock.js.map +1 -0
  60. package/FlowGraph/Blocks/Execution/{flowGraphPlayAnimationBlock.d.ts → Animation/flowGraphPlayAnimationBlock.d.ts} +5 -5
  61. package/FlowGraph/Blocks/Execution/{flowGraphPlayAnimationBlock.js → Animation/flowGraphPlayAnimationBlock.js} +15 -8
  62. package/FlowGraph/Blocks/Execution/Animation/flowGraphPlayAnimationBlock.js.map +1 -0
  63. package/FlowGraph/Blocks/Execution/{flowGraphStopAnimationBlock.d.ts → Animation/flowGraphStopAnimationBlock.d.ts} +4 -4
  64. package/FlowGraph/Blocks/Execution/{flowGraphStopAnimationBlock.js → Animation/flowGraphStopAnimationBlock.js} +2 -2
  65. package/FlowGraph/Blocks/Execution/Animation/flowGraphStopAnimationBlock.js.map +1 -0
  66. package/FlowGraph/Blocks/Execution/Animation/index.d.ts +3 -0
  67. package/FlowGraph/Blocks/Execution/Animation/index.js +4 -0
  68. package/FlowGraph/Blocks/Execution/Animation/index.js.map +1 -0
  69. package/FlowGraph/Blocks/Execution/index.d.ts +1 -2
  70. package/FlowGraph/Blocks/Execution/index.js +2 -2
  71. package/FlowGraph/Blocks/Execution/index.js.map +1 -1
  72. package/FlowGraph/flowGraphDataConnection.d.ts +2 -2
  73. package/FlowGraph/flowGraphDataConnection.js +3 -3
  74. package/FlowGraph/flowGraphDataConnection.js.map +1 -1
  75. package/Gizmos/axisDragGizmo.d.ts +1 -1
  76. package/Gizmos/axisDragGizmo.js +0 -1
  77. package/Gizmos/axisDragGizmo.js.map +1 -1
  78. package/Gizmos/axisScaleGizmo.js +0 -1
  79. package/Gizmos/axisScaleGizmo.js.map +1 -1
  80. package/Gizmos/boundingBoxGizmo.d.ts +12 -0
  81. package/Gizmos/boundingBoxGizmo.js +37 -1
  82. package/Gizmos/boundingBoxGizmo.js.map +1 -1
  83. package/Gizmos/gizmo.d.ts +0 -4
  84. package/Gizmos/gizmo.js +19 -16
  85. package/Gizmos/gizmo.js.map +1 -1
  86. package/Gizmos/planeDragGizmo.js +0 -1
  87. package/Gizmos/planeDragGizmo.js.map +1 -1
  88. package/Gizmos/planeRotationGizmo.js +0 -1
  89. package/Gizmos/planeRotationGizmo.js.map +1 -1
  90. package/Gizmos/scaleGizmo.d.ts +2 -2
  91. package/Gizmos/scaleGizmo.js +5 -2
  92. package/Gizmos/scaleGizmo.js.map +1 -1
  93. package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js +1 -0
  94. package/Materials/Node/Blocks/Fragment/perturbNormalBlock.js.map +1 -1
  95. package/Materials/PBR/pbrBaseMaterial.d.ts +1 -0
  96. package/Materials/PBR/pbrBaseMaterial.js +12 -2
  97. package/Materials/PBR/pbrBaseMaterial.js.map +1 -1
  98. package/Materials/Textures/equiRectangularCubeTexture.js +24 -4
  99. package/Materials/Textures/equiRectangularCubeTexture.js.map +1 -1
  100. package/Materials/standardMaterial.d.ts +1 -0
  101. package/Materials/standardMaterial.js +6 -0
  102. package/Materials/standardMaterial.js.map +1 -1
  103. package/Maths/math.vector.d.ts +1 -1
  104. package/Maths/math.vector.js +1 -1
  105. package/Maths/math.vector.js.map +1 -1
  106. package/Meshes/Compression/dracoCompression.d.ts +25 -8
  107. package/Meshes/Compression/dracoCompression.js +187 -119
  108. package/Meshes/Compression/dracoCompression.js.map +1 -1
  109. package/Meshes/Node/Blocks/Textures/geometryTextureFetchBlock.d.ts +22 -2
  110. package/Meshes/Node/Blocks/Textures/geometryTextureFetchBlock.js +62 -4
  111. package/Meshes/Node/Blocks/Textures/geometryTextureFetchBlock.js.map +1 -1
  112. package/Meshes/Node/Blocks/debugBlock.d.ts +2 -1
  113. package/Meshes/Node/Blocks/debugBlock.js +8 -2
  114. package/Meshes/Node/Blocks/debugBlock.js.map +1 -1
  115. package/Meshes/Node/Blocks/randomBlock.d.ts +14 -3
  116. package/Meshes/Node/Blocks/randomBlock.js +40 -13
  117. package/Meshes/Node/Blocks/randomBlock.js.map +1 -1
  118. package/Meshes/Node/nodeGeometryBuildState.d.ts +2 -2
  119. package/Meshes/Node/nodeGeometryBuildState.js +5 -3
  120. package/Meshes/Node/nodeGeometryBuildState.js.map +1 -1
  121. package/Meshes/meshSimplification.d.ts +1 -1
  122. package/Meshes/meshSimplification.js.map +1 -1
  123. package/Meshes/subMesh.js +1 -1
  124. package/Meshes/subMesh.js.map +1 -1
  125. package/Physics/v2/IPhysicsEnginePlugin.d.ts +8 -0
  126. package/Physics/v2/IPhysicsEnginePlugin.js.map +1 -1
  127. package/Shaders/ShadersInclude/bumpFragmentFunctions.js +14 -2
  128. package/Shaders/ShadersInclude/bumpFragmentFunctions.js.map +1 -1
  129. package/package.json +1 -1
  130. package/FlowGraph/Blocks/Data/flowGraphAddBlock.d.ts +0 -23
  131. package/FlowGraph/Blocks/Data/flowGraphAddBlock.js +0 -18
  132. package/FlowGraph/Blocks/Data/flowGraphAddBlock.js.map +0 -1
  133. package/FlowGraph/Blocks/Execution/flowGraphPlayAnimationBlock.js.map +0 -1
  134. package/FlowGraph/Blocks/Execution/flowGraphStopAnimationBlock.js.map +0 -1
@@ -1,5 +1,6 @@
1
- import type { IDisposable } from "../../scene";
2
- import { VertexData } from "../../Meshes/mesh.vertexData";
1
+ import type { IDisposable, Scene } from "../../scene";
2
+ import { Geometry } from "../geometry";
3
+ import { VertexData } from "../mesh.vertexData";
3
4
  /**
4
5
  * Configuration for Draco compression
5
6
  */
@@ -50,9 +51,9 @@ export interface IDracoCompressionConfiguration {
50
51
  * Decoding will automatically fallback to the JavaScript version if WebAssembly version is not configured or if WebAssembly is not supported by the browser.
51
52
  * Use `DracoCompression.DecoderAvailable` to determine if the decoder configuration is available for the current context.
52
53
  *
53
- * To decode Draco compressed data, get the default DracoCompression object and call decodeMeshAsync:
54
+ * To decode Draco compressed data, get the default DracoCompression object and call decodeMeshToGeometryAsync:
54
55
  * ```javascript
55
- * var vertexData = await DracoCompression.Default.decodeMeshAsync(data);
56
+ * var geometry = await DracoCompression.Default.decodeMeshToGeometryAsync(data);
56
57
  * ```
57
58
  *
58
59
  * @see https://playground.babylonjs.com/#DMZIBD#0
@@ -95,16 +96,32 @@ export declare class DracoCompression implements IDisposable {
95
96
  * @returns a promise that resolves when ready
96
97
  */
97
98
  whenReadyAsync(): Promise<void>;
99
+ private _decodeMeshAsync;
98
100
  /**
99
- * Decode Draco compressed mesh data to vertex data.
101
+ * Decode Draco compressed mesh data to Babylon geometry.
102
+ * @param name The name to use when creating the geometry
103
+ * @param scene The scene to use when creating the geometry
104
+ * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
105
+ * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
106
+ * @returns A promise that resolves with the decoded geometry
107
+ */
108
+ decodeMeshToGeometryAsync(name: string, scene: Scene, data: ArrayBuffer | ArrayBufferView, attributes?: {
109
+ [kind: string]: number;
110
+ }): Promise<Geometry>;
111
+ /** @internal */
112
+ _decodeMeshToGeometryForGltfAsync(name: string, scene: Scene, data: ArrayBuffer | ArrayBufferView, attributes: {
113
+ [kind: string]: number;
114
+ }, gltfNormalizedOverride: {
115
+ [kind: string]: boolean;
116
+ }): Promise<Geometry>;
117
+ /**
118
+ * Decode Draco compressed mesh data to Babylon vertex data.
100
119
  * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
101
120
  * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
102
- * @param dividers a list of optional dividers for normalization
103
121
  * @returns A promise that resolves with the decoded vertex data
122
+ * @deprecated Use {@link decodeMeshToGeometryAsync} for better performance in some cases
104
123
  */
105
124
  decodeMeshAsync(data: ArrayBuffer | ArrayBufferView, attributes?: {
106
125
  [kind: string]: number;
107
- }, dividers?: {
108
- [kind: string]: number;
109
126
  }): Promise<VertexData>;
110
127
  }
@@ -1,7 +1,10 @@
1
1
  /* eslint-disable @typescript-eslint/naming-convention */
2
2
  import { Tools } from "../../Misc/tools.js";
3
3
  import { AutoReleaseWorkerPool } from "../../Misc/workerPool.js";
4
- import { VertexData } from "../../Meshes/mesh.vertexData.js";
4
+ import { Geometry } from "../geometry.js";
5
+ import { VertexBuffer } from "../buffer.js";
6
+ import { VertexData } from "../mesh.vertexData.js";
7
+ import { Logger } from "../../Misc/logger.js";
5
8
  function createDecoderAsync(wasmBinary) {
6
9
  return new Promise((resolve) => {
7
10
  DracoDecoderModule({ wasmBinary: wasmBinary }).then((module) => {
@@ -9,73 +12,79 @@ function createDecoderAsync(wasmBinary) {
9
12
  });
10
13
  });
11
14
  }
12
- function decodeMesh(decoderModule, dataView, attributes, onIndicesData, onAttributeData, dividers) {
13
- const buffer = new decoderModule.DecoderBuffer();
14
- buffer.Init(dataView, dataView.byteLength);
15
- const decoder = new decoderModule.Decoder();
16
- let geometry;
17
- let status;
15
+ function decodeMesh(decoderModule, data, attributes, onIndicesData, onAttributeData) {
16
+ let decoder = null;
17
+ let buffer = null;
18
+ let geometry = null;
18
19
  try {
20
+ decoder = new decoderModule.Decoder();
21
+ buffer = new decoderModule.DecoderBuffer();
22
+ buffer.Init(data, data.byteLength);
23
+ let status;
19
24
  const type = decoder.GetEncodedGeometryType(buffer);
20
25
  switch (type) {
21
- case decoderModule.TRIANGULAR_MESH:
22
- geometry = new decoderModule.Mesh();
23
- status = decoder.DecodeBufferToMesh(buffer, geometry);
26
+ case decoderModule.TRIANGULAR_MESH: {
27
+ const mesh = new decoderModule.Mesh();
28
+ status = decoder.DecodeBufferToMesh(buffer, mesh);
29
+ if (!status.ok() || mesh.ptr === 0) {
30
+ throw new Error(status.error_msg());
31
+ }
32
+ const numFaces = mesh.num_faces();
33
+ const numIndices = numFaces * 3;
34
+ const byteLength = numIndices * 4;
35
+ const ptr = decoderModule._malloc(byteLength);
36
+ try {
37
+ decoder.GetTrianglesUInt32Array(mesh, byteLength, ptr);
38
+ const indices = new Uint32Array(numIndices);
39
+ indices.set(new Uint32Array(decoderModule.HEAPF32.buffer, ptr, numIndices));
40
+ onIndicesData(indices);
41
+ }
42
+ finally {
43
+ decoderModule._free(ptr);
44
+ }
45
+ geometry = mesh;
24
46
  break;
25
- case decoderModule.POINT_CLOUD:
26
- geometry = new decoderModule.PointCloud();
27
- status = decoder.DecodeBufferToPointCloud(buffer, geometry);
47
+ }
48
+ case decoderModule.POINT_CLOUD: {
49
+ const pointCloud = new decoderModule.PointCloud();
50
+ status = decoder.DecodeBufferToPointCloud(buffer, pointCloud);
51
+ if (!status.ok() || !pointCloud.ptr) {
52
+ throw new Error(status.error_msg());
53
+ }
54
+ geometry = pointCloud;
28
55
  break;
29
- default:
30
- throw new Error(`Invalid geometry type ${type}`);
31
- }
32
- if (!status.ok() || !geometry.ptr) {
33
- throw new Error(status.error_msg());
34
- }
35
- if (type === decoderModule.TRIANGULAR_MESH) {
36
- const numFaces = geometry.num_faces();
37
- const numIndices = numFaces * 3;
38
- const byteLength = numIndices * 4;
39
- const ptr = decoderModule._malloc(byteLength);
40
- try {
41
- decoder.GetTrianglesUInt32Array(geometry, byteLength, ptr);
42
- const indices = new Uint32Array(numIndices);
43
- indices.set(new Uint32Array(decoderModule.HEAPF32.buffer, ptr, numIndices));
44
- onIndicesData(indices);
45
56
  }
46
- finally {
47
- decoderModule._free(ptr);
57
+ default: {
58
+ throw new Error(`Invalid geometry type ${type}`);
48
59
  }
49
60
  }
50
- const processAttribute = (kind, attribute, divider = 1) => {
61
+ const numPoints = geometry.num_points();
62
+ const processAttribute = (decoder, geometry, kind, attribute) => {
63
+ const dataType = attribute.data_type();
51
64
  const numComponents = attribute.num_components();
52
- const numPoints = geometry.num_points();
65
+ const normalized = attribute.normalized();
66
+ const byteStride = attribute.byte_stride();
67
+ const byteOffset = attribute.byte_offset();
68
+ const dataTypeInfo = {
69
+ [decoderModule.DT_FLOAT32]: { typedArrayConstructor: Float32Array, heap: decoderModule.HEAPF32 },
70
+ [decoderModule.DT_INT8]: { typedArrayConstructor: Int8Array, heap: decoderModule.HEAP8 },
71
+ [decoderModule.DT_INT16]: { typedArrayConstructor: Int16Array, heap: decoderModule.HEAP16 },
72
+ [decoderModule.DT_INT32]: { typedArrayConstructor: Int32Array, heap: decoderModule.HEAP32 },
73
+ [decoderModule.DT_UINT8]: { typedArrayConstructor: Uint8Array, heap: decoderModule.HEAPU8 },
74
+ [decoderModule.DT_UINT16]: { typedArrayConstructor: Uint16Array, heap: decoderModule.HEAPU16 },
75
+ [decoderModule.DT_UINT32]: { typedArrayConstructor: Uint32Array, heap: decoderModule.HEAPU32 },
76
+ };
77
+ const info = dataTypeInfo[dataType];
78
+ if (!info) {
79
+ throw new Error(`Invalid data type ${dataType}`);
80
+ }
53
81
  const numValues = numPoints * numComponents;
54
- const byteLength = numValues * Float32Array.BYTES_PER_ELEMENT;
82
+ const byteLength = numValues * info.typedArrayConstructor.BYTES_PER_ELEMENT;
55
83
  const ptr = decoderModule._malloc(byteLength);
56
84
  try {
57
- decoder.GetAttributeDataArrayForAllPoints(geometry, attribute, decoderModule.DT_FLOAT32, byteLength, ptr);
58
- const values = new Float32Array(decoderModule.HEAPF32.buffer, ptr, numValues);
59
- if (kind === "color" && numComponents === 3) {
60
- const babylonData = new Float32Array(numPoints * 4);
61
- for (let i = 0, j = 0; i < babylonData.length; i += 4, j += numComponents) {
62
- babylonData[i + 0] = values[j + 0];
63
- babylonData[i + 1] = values[j + 1];
64
- babylonData[i + 2] = values[j + 2];
65
- babylonData[i + 3] = 1;
66
- }
67
- onAttributeData(kind, babylonData);
68
- }
69
- else {
70
- const babylonData = new Float32Array(numValues);
71
- babylonData.set(new Float32Array(decoderModule.HEAPF32.buffer, ptr, numValues));
72
- if (divider !== 1) {
73
- for (let i = 0; i < babylonData.length; i++) {
74
- babylonData[i] = babylonData[i] / divider;
75
- }
76
- }
77
- onAttributeData(kind, babylonData);
78
- }
85
+ decoder.GetAttributeDataArrayForAllPoints(geometry, attribute, dataType, byteLength, ptr);
86
+ const data = new info.typedArrayConstructor(info.heap.buffer, ptr, numValues);
87
+ onAttributeData(kind, data.slice(), byteOffset, byteStride, normalized);
79
88
  }
80
89
  finally {
81
90
  decoderModule._free(ptr);
@@ -85,32 +94,36 @@ function decodeMesh(decoderModule, dataView, attributes, onIndicesData, onAttrib
85
94
  for (const kind in attributes) {
86
95
  const id = attributes[kind];
87
96
  const attribute = decoder.GetAttributeByUniqueId(geometry, id);
88
- const divider = (dividers && dividers[kind]) || 1;
89
- processAttribute(kind, attribute, divider);
97
+ processAttribute(decoder, geometry, kind, attribute);
90
98
  }
91
99
  }
92
100
  else {
93
- const nativeAttributeTypes = {
94
- position: "POSITION",
95
- normal: "NORMAL",
96
- color: "COLOR",
97
- uv: "TEX_COORD",
101
+ const dracoAttributeTypes = {
102
+ position: decoderModule.POSITION,
103
+ normal: decoderModule.NORMAL,
104
+ color: decoderModule.COLOR,
105
+ uv: decoderModule.TEX_COORD,
98
106
  };
99
- for (const kind in nativeAttributeTypes) {
100
- const id = decoder.GetAttributeId(geometry, decoderModule[nativeAttributeTypes[kind]]);
107
+ for (const kind in dracoAttributeTypes) {
108
+ const id = decoder.GetAttributeId(geometry, dracoAttributeTypes[kind]);
101
109
  if (id !== -1) {
102
110
  const attribute = decoder.GetAttribute(geometry, id);
103
- processAttribute(kind, attribute);
111
+ processAttribute(decoder, geometry, kind, attribute);
104
112
  }
105
113
  }
106
114
  }
115
+ return numPoints;
107
116
  }
108
117
  finally {
109
118
  if (geometry) {
110
119
  decoderModule.destroy(geometry);
111
120
  }
112
- decoderModule.destroy(decoder);
113
- decoderModule.destroy(buffer);
121
+ if (buffer) {
122
+ decoderModule.destroy(buffer);
123
+ }
124
+ if (decoder) {
125
+ decoderModule.destroy(decoder);
126
+ }
114
127
  }
115
128
  }
116
129
  /**
@@ -119,15 +132,15 @@ function decodeMesh(decoderModule, dataView, attributes, onIndicesData, onAttrib
119
132
  function worker() {
120
133
  let decoderPromise;
121
134
  onmessage = (event) => {
122
- const data = event.data;
123
- switch (data.id) {
135
+ const message = event.data;
136
+ switch (message.id) {
124
137
  case "init": {
125
- const decoder = data.decoder;
138
+ const decoder = message.decoder;
126
139
  if (decoder.url) {
127
140
  importScripts(decoder.url);
128
141
  decoderPromise = DracoDecoderModule({ wasmBinary: decoder.wasmBinary });
129
142
  }
130
- postMessage("done");
143
+ postMessage({ id: "initDone" });
131
144
  break;
132
145
  }
133
146
  case "decodeMesh": {
@@ -135,12 +148,12 @@ function worker() {
135
148
  throw new Error("Draco decoder module is not available");
136
149
  }
137
150
  decoderPromise.then((decoder) => {
138
- decodeMesh(decoder, data.dataView, data.attributes, (indices) => {
139
- postMessage({ id: "indices", value: indices }, [indices.buffer]);
140
- }, (kind, data) => {
141
- postMessage({ id: kind, value: data }, [data.buffer]);
151
+ const numPoints = decodeMesh(decoder, message.dataView, message.attributes, (indices) => {
152
+ postMessage({ id: "indices", data: indices }, [indices.buffer]);
153
+ }, (kind, data, offset, stride, normalized) => {
154
+ postMessage({ id: "attribute", kind, data, offset, stride, normalized }, [data.buffer]);
142
155
  });
143
- postMessage("done");
156
+ postMessage({ id: "decodeMeshDone", totalVertices: numPoints });
144
157
  });
145
158
  break;
146
159
  }
@@ -175,9 +188,9 @@ function worker() {
175
188
  * Decoding will automatically fallback to the JavaScript version if WebAssembly version is not configured or if WebAssembly is not supported by the browser.
176
189
  * Use `DracoCompression.DecoderAvailable` to determine if the decoder configuration is available for the current context.
177
190
  *
178
- * To decode Draco compressed data, get the default DracoCompression object and call decodeMeshAsync:
191
+ * To decode Draco compressed data, get the default DracoCompression object and call decodeMeshToGeometryAsync:
179
192
  * ```javascript
180
- * var vertexData = await DracoCompression.Default.decodeMeshAsync(data);
193
+ * var geometry = await DracoCompression.Default.decodeMeshToGeometryAsync(data);
181
194
  * ```
182
195
  *
183
196
  * @see https://playground.babylonjs.com/#DMZIBD#0
@@ -233,8 +246,8 @@ export class DracoCompression {
233
246
  worker.removeEventListener("message", onMessage);
234
247
  reject(error);
235
248
  };
236
- const onMessage = (message) => {
237
- if (message.data === "done") {
249
+ const onMessage = (event) => {
250
+ if (event.data.id === "initDone") {
238
251
  worker.removeEventListener("error", onError);
239
252
  worker.removeEventListener("message", onMessage);
240
253
  resolve(worker);
@@ -289,52 +302,54 @@ export class DracoCompression {
289
302
  }
290
303
  return Promise.resolve();
291
304
  }
292
- /**
293
- * Decode Draco compressed mesh data to vertex data.
294
- * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
295
- * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
296
- * @param dividers a list of optional dividers for normalization
297
- * @returns A promise that resolves with the decoded vertex data
298
- */
299
- decodeMeshAsync(data, attributes, dividers) {
300
- const dataView = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
305
+ _decodeMeshAsync(engine, data, attributes, gltfNormalizedOverride) {
306
+ const dataView = data instanceof ArrayBuffer ? new Int8Array(data) : new Int8Array(data.buffer, data.byteOffset, data.byteLength);
307
+ const applyGltfNormalizedOverride = (kind, normalized) => {
308
+ if (gltfNormalizedOverride && gltfNormalizedOverride[kind] !== undefined) {
309
+ if (normalized !== gltfNormalizedOverride[kind]) {
310
+ Logger.Warn(`Normalized flag from Draco data (${normalized}) does not match normalized flag from glTF accessor (${gltfNormalizedOverride[kind]}). Using flag from glTF accessor.`);
311
+ }
312
+ return gltfNormalizedOverride[kind];
313
+ }
314
+ else {
315
+ return normalized;
316
+ }
317
+ };
301
318
  if (this._workerPoolPromise) {
302
319
  return this._workerPoolPromise.then((workerPool) => {
303
320
  return new Promise((resolve, reject) => {
304
321
  workerPool.push((worker, onComplete) => {
305
- const vertexData = new VertexData();
322
+ let resultIndices = null;
323
+ const resultAttributes = [];
306
324
  const onError = (error) => {
307
325
  worker.removeEventListener("error", onError);
308
326
  worker.removeEventListener("message", onMessage);
309
327
  reject(error);
310
328
  onComplete();
311
329
  };
312
- const onMessage = (message) => {
313
- if (message.data === "done") {
314
- worker.removeEventListener("error", onError);
315
- worker.removeEventListener("message", onMessage);
316
- resolve(vertexData);
317
- onComplete();
318
- }
319
- else if (message.data.id === "indices") {
320
- vertexData.indices = message.data.value;
321
- }
322
- else {
323
- // check normalization
324
- const divider = dividers && dividers[message.data.id] ? dividers[message.data.id] : 1;
325
- if (divider !== 1) {
326
- // normalize
327
- for (let i = 0; i < message.data.value.length; i++) {
328
- message.data.value[i] = message.data.value[i] / divider;
329
- }
330
+ const onMessage = (event) => {
331
+ const message = event.data;
332
+ switch (message.id) {
333
+ case "decodeMeshDone": {
334
+ worker.removeEventListener("error", onError);
335
+ worker.removeEventListener("message", onMessage);
336
+ resolve({ indices: resultIndices, attributes: resultAttributes, totalVertices: message.totalVertices });
337
+ onComplete();
338
+ break;
339
+ }
340
+ case "indices": {
341
+ resultIndices = message.data;
342
+ break;
343
+ }
344
+ case "attribute": {
345
+ resultAttributes.push(new VertexBuffer(engine, message.data, message.kind, false, undefined, message.stride, undefined, message.offset, undefined, undefined, applyGltfNormalizedOverride(message.kind, message.normalized), true));
346
+ break;
330
347
  }
331
- vertexData.set(message.data.value, message.data.id);
332
348
  }
333
349
  };
334
350
  worker.addEventListener("error", onError);
335
351
  worker.addEventListener("message", onMessage);
336
- const dataViewCopy = new Uint8Array(dataView.byteLength);
337
- dataViewCopy.set(new Uint8Array(dataView.buffer, dataView.byteOffset, dataView.byteLength));
352
+ const dataViewCopy = dataView.slice();
338
353
  worker.postMessage({ id: "decodeMesh", dataView: dataViewCopy, attributes: attributes }, [dataViewCopy.buffer]);
339
354
  });
340
355
  });
@@ -342,17 +357,70 @@ export class DracoCompression {
342
357
  }
343
358
  if (this._decoderModulePromise) {
344
359
  return this._decoderModulePromise.then((decoder) => {
345
- const vertexData = new VertexData();
346
- decodeMesh(decoder.module, dataView, attributes, (indices) => {
347
- vertexData.indices = indices;
348
- }, (kind, data) => {
349
- vertexData.set(data, kind);
350
- }, dividers);
351
- return vertexData;
360
+ let resultIndices = null;
361
+ const resultAttributes = [];
362
+ const numPoints = decodeMesh(decoder.module, dataView, attributes, (indices) => {
363
+ resultIndices = indices;
364
+ }, (kind, data, offset, stride, normalized) => {
365
+ resultAttributes.push(new VertexBuffer(null, data, kind, false, undefined, stride, undefined, offset, undefined, undefined, applyGltfNormalizedOverride(kind, normalized), true));
366
+ });
367
+ return { indices: resultIndices, attributes: resultAttributes, totalVertices: numPoints };
352
368
  });
353
369
  }
354
370
  throw new Error("Draco decoder module is not available");
355
371
  }
372
+ /**
373
+ * Decode Draco compressed mesh data to Babylon geometry.
374
+ * @param name The name to use when creating the geometry
375
+ * @param scene The scene to use when creating the geometry
376
+ * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
377
+ * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
378
+ * @returns A promise that resolves with the decoded geometry
379
+ */
380
+ decodeMeshToGeometryAsync(name, scene, data, attributes) {
381
+ return this._decodeMeshAsync(scene.getEngine(), data, attributes).then((meshData) => {
382
+ const geometry = new Geometry(name, scene);
383
+ if (meshData.indices) {
384
+ geometry.setIndices(meshData.indices);
385
+ }
386
+ for (const attribute of meshData.attributes) {
387
+ geometry.setVerticesBuffer(attribute);
388
+ }
389
+ return geometry;
390
+ });
391
+ }
392
+ /** @internal */
393
+ _decodeMeshToGeometryForGltfAsync(name, scene, data, attributes, gltfNormalizedOverride) {
394
+ return this._decodeMeshAsync(scene.getEngine(), data, attributes, gltfNormalizedOverride).then((meshData) => {
395
+ const geometry = new Geometry(name, scene);
396
+ if (meshData.indices) {
397
+ geometry.setIndices(meshData.indices);
398
+ }
399
+ for (const attribute of meshData.attributes) {
400
+ geometry.setVerticesBuffer(attribute);
401
+ }
402
+ return geometry;
403
+ });
404
+ }
405
+ /**
406
+ * Decode Draco compressed mesh data to Babylon vertex data.
407
+ * @param data The ArrayBuffer or ArrayBufferView for the Draco compression data
408
+ * @param attributes A map of attributes from vertex buffer kinds to Draco unique ids
409
+ * @returns A promise that resolves with the decoded vertex data
410
+ * @deprecated Use {@link decodeMeshToGeometryAsync} for better performance in some cases
411
+ */
412
+ decodeMeshAsync(data, attributes) {
413
+ return this._decodeMeshAsync(null, data, attributes).then((meshData) => {
414
+ const vertexData = new VertexData();
415
+ if (meshData.indices) {
416
+ vertexData.indices = meshData.indices;
417
+ }
418
+ for (const attribute of meshData.attributes) {
419
+ vertexData.set(attribute.getFloatData(meshData.totalVertices), attribute.getKind());
420
+ }
421
+ return vertexData;
422
+ });
423
+ }
356
424
  }
357
425
  /**
358
426
  * The configuration. Defaults to the following urls: