@babylonjs/serializers 8.31.3 → 8.32.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.
@@ -15,6 +15,10 @@ export declare class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2
15
15
  enabled: boolean;
16
16
  /** Defines whether this extension is required */
17
17
  required: boolean;
18
+ /**
19
+ * Internal state to emit warning about instance color alpha once
20
+ */
21
+ private _instanceColorWarned;
18
22
  private _exporter;
19
23
  private _wasUsed;
20
24
  constructor(exporter: GLTFExporter);
@@ -3,7 +3,17 @@ import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
3
3
  import "@babylonjs/core/Meshes/thinInstanceMesh.js";
4
4
  import { TmpVectors, Quaternion, Vector3 } from "@babylonjs/core/Maths/math.vector.js";
5
5
  import { ConvertToRightHandedPosition, ConvertToRightHandedRotation } from "../glTFUtilities.js";
6
+ import { Logger } from "@babylonjs/core/Misc/logger.js";
6
7
  const NAME = "EXT_mesh_gpu_instancing";
8
+ function ColorBufferToRGBAToRGB(colorBuffer, instanceCount) {
9
+ const colorBufferRgb = new Float32Array(instanceCount * 3);
10
+ for (let i = 0; i < instanceCount; i++) {
11
+ colorBufferRgb[i * 3 + 0] = colorBuffer[i * 4 + 0];
12
+ colorBufferRgb[i * 3 + 1] = colorBuffer[i * 4 + 1];
13
+ colorBufferRgb[i * 3 + 2] = colorBuffer[i * 4 + 2];
14
+ }
15
+ return colorBufferRgb;
16
+ }
7
17
  /**
8
18
  * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing/README.md)
9
19
  */
@@ -16,6 +26,10 @@ export class EXT_mesh_gpu_instancing {
16
26
  this.enabled = true;
17
27
  /** Defines whether this extension is required */
18
28
  this.required = false;
29
+ /**
30
+ * Internal state to emit warning about instance color alpha once
31
+ */
32
+ this._instanceColorWarned = false;
19
33
  this._wasUsed = false;
20
34
  this._exporter = exporter;
21
35
  }
@@ -87,6 +101,24 @@ export class EXT_mesh_gpu_instancing {
87
101
  if (hasAnyInstanceWorldScale) {
88
102
  extension.attributes["SCALE"] = this._buildAccessor(scaleBuffer, "VEC3" /* AccessorType.VEC3 */, babylonNode.thinInstanceCount, bufferManager);
89
103
  }
104
+ let colorBuffer = babylonNode._userThinInstanceBuffersStorage?.data?.instanceColor;
105
+ if (colorBuffer) {
106
+ const instanceCount = babylonNode.thinInstanceCount;
107
+ const accessorType = "VEC3" /* AccessorType.VEC3 */;
108
+ if (babylonNode.hasVertexAlpha && colorBuffer.length === instanceCount * 4) {
109
+ if (!this._instanceColorWarned) {
110
+ Logger.Warn("EXT_mesh_gpu_instancing: Exporting instance colors as RGB, alpha channel of instance color is not exported");
111
+ this._instanceColorWarned = true;
112
+ }
113
+ colorBuffer = ColorBufferToRGBAToRGB(colorBuffer, instanceCount);
114
+ }
115
+ else if (colorBuffer.length === instanceCount * 4) {
116
+ colorBuffer = ColorBufferToRGBAToRGB(colorBuffer, instanceCount);
117
+ }
118
+ if (colorBuffer.length === instanceCount * 3) {
119
+ extension.attributes["_COLOR_0"] = this._buildAccessor(colorBuffer, accessorType, instanceCount, bufferManager);
120
+ }
121
+ }
90
122
  /* eslint-enable @typescript-eslint/naming-convention*/
91
123
  node.extensions = node.extensions || {};
92
124
  node.extensions[NAME] = extension;
@@ -1 +1 @@
1
- {"version":3,"file":"EXT_mesh_gpu_instancing.js","sourceRoot":"","sources":["../../../../../../dev/serializers/src/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG/C,OAAO,EAAE,IAAI,EAAE,uCAAyB;AACxC,oDAAsC;AACtC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,6CAA+B;AACzE,OAAO,EAAE,4BAA4B,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAE9F,MAAM,IAAI,GAAG,yBAAy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AAK,CAAC,CAAC;QAC5F,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzC,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;IAChD,CAAC;CACJ;AAED,6DAA6D;AAC7D,YAAY,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,uBAAuB,CAAC,QAAQ,CAAC,CAAC,CAAC","sourcesContent":["import type { INode, IEXTMeshGpuInstancing } from \"babylonjs-gltf2interface\";\r\nimport { AccessorType, AccessorComponentType } from \"babylonjs-gltf2interface\";\r\nimport type { IGLTFExporterExtensionV2 } from \"../glTFExporterExtension\";\r\nimport type { BufferManager } from \"../bufferManager\";\r\nimport { GLTFExporter } from \"../glTFExporter\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { Node } from \"core/node\";\r\nimport { Mesh } from \"core/Meshes/mesh\";\r\nimport \"core/Meshes/thinInstanceMesh\";\r\nimport { TmpVectors, Quaternion, Vector3 } from \"core/Maths/math.vector\";\r\nimport { ConvertToRightHandedPosition, ConvertToRightHandedRotation } from \"../glTFUtilities\";\r\n\r\nconst NAME = \"EXT_mesh_gpu_instancing\";\r\n\r\n/**\r\n * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing/README.md)\r\n */\r\n// eslint-disable-next-line @typescript-eslint/naming-convention\r\nexport class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {\r\n /** Name of this extension */\r\n public readonly name = NAME;\r\n\r\n /** Defines whether this extension is enabled */\r\n public enabled = true;\r\n\r\n /** Defines whether this extension is required */\r\n public required = false;\r\n\r\n private _exporter: GLTFExporter;\r\n\r\n private _wasUsed = false;\r\n\r\n constructor(exporter: GLTFExporter) {\r\n this._exporter = exporter;\r\n }\r\n\r\n public dispose() {}\r\n\r\n /** @internal */\r\n public get wasUsed() {\r\n return this._wasUsed;\r\n }\r\n\r\n /**\r\n * After node is exported\r\n * @param context the GLTF context when loading the asset\r\n * @param node the node exported\r\n * @param babylonNode the corresponding babylon node\r\n * @param nodeMap map from babylon node id to node index\r\n * @param convertToRightHanded true if we need to convert data from left hand to right hand system.\r\n * @param bufferManager buffer manager\r\n * @returns nullable promise, resolves with the node\r\n */\r\n public async postExportNodeAsync(\r\n context: string,\r\n node: Nullable<INode>,\r\n babylonNode: Node,\r\n nodeMap: Map<Node, number>,\r\n convertToRightHanded: boolean,\r\n bufferManager: BufferManager\r\n ): Promise<Nullable<INode>> {\r\n return await new Promise((resolve) => {\r\n if (node && babylonNode instanceof Mesh) {\r\n if (babylonNode.hasThinInstances && this._exporter) {\r\n this._wasUsed = true;\r\n\r\n const noTranslation = Vector3.Zero();\r\n const noRotation = Quaternion.Identity();\r\n const noScale = Vector3.One();\r\n\r\n // retrieve all the instance world matrix\r\n const matrix = babylonNode.thinInstanceGetWorldMatrices();\r\n\r\n const iwt = TmpVectors.Vector3[2];\r\n const iwr = TmpVectors.Quaternion[1];\r\n const iws = TmpVectors.Vector3[3];\r\n\r\n let hasAnyInstanceWorldTranslation = false;\r\n let hasAnyInstanceWorldRotation = false;\r\n let hasAnyInstanceWorldScale = false;\r\n\r\n // prepare temp buffers\r\n const translationBuffer = new Float32Array(babylonNode.thinInstanceCount * 3);\r\n const rotationBuffer = new Float32Array(babylonNode.thinInstanceCount * 4);\r\n const scaleBuffer = new Float32Array(babylonNode.thinInstanceCount * 3);\r\n\r\n let i = 0;\r\n for (const m of matrix) {\r\n m.decompose(iws, iwr, iwt);\r\n\r\n if (convertToRightHanded) {\r\n ConvertToRightHandedPosition(iwt);\r\n ConvertToRightHandedRotation(iwr);\r\n }\r\n\r\n // fill the temp buffer\r\n translationBuffer.set(iwt.asArray(), i * 3);\r\n rotationBuffer.set(iwr.normalize().asArray(), i * 4); // ensure the quaternion is normalized\r\n scaleBuffer.set(iws.asArray(), i * 3);\r\n\r\n // this is where we decide if there is any transformation\r\n hasAnyInstanceWorldTranslation = hasAnyInstanceWorldTranslation || !iwt.equalsWithEpsilon(noTranslation);\r\n hasAnyInstanceWorldRotation = hasAnyInstanceWorldRotation || !iwr.equalsWithEpsilon(noRotation);\r\n hasAnyInstanceWorldScale = hasAnyInstanceWorldScale || !iws.equalsWithEpsilon(noScale);\r\n\r\n i++;\r\n }\r\n\r\n const extension: IEXTMeshGpuInstancing = {\r\n attributes: {},\r\n };\r\n\r\n // do we need to write TRANSLATION ?\r\n if (hasAnyInstanceWorldTranslation) {\r\n extension.attributes[\"TRANSLATION\"] = this._buildAccessor(translationBuffer, AccessorType.VEC3, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n // do we need to write ROTATION ?\r\n if (hasAnyInstanceWorldRotation) {\r\n // we decided to stay on FLOAT for now see https://github.com/BabylonJS/Babylon.js/pull/12495\r\n extension.attributes[\"ROTATION\"] = this._buildAccessor(rotationBuffer, AccessorType.VEC4, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n // do we need to write SCALE ?\r\n if (hasAnyInstanceWorldScale) {\r\n extension.attributes[\"SCALE\"] = this._buildAccessor(scaleBuffer, AccessorType.VEC3, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n\r\n /* eslint-enable @typescript-eslint/naming-convention*/\r\n node.extensions = node.extensions || {};\r\n node.extensions[NAME] = extension;\r\n }\r\n }\r\n resolve(node);\r\n });\r\n }\r\n\r\n private _buildAccessor(buffer: Float32Array, type: AccessorType, count: number, bufferManager: BufferManager): number {\r\n // build the buffer view\r\n const bv = bufferManager.createBufferView(buffer);\r\n\r\n // finally build the accessor\r\n const accessor = bufferManager.createAccessor(bv, type, AccessorComponentType.FLOAT, count);\r\n this._exporter._accessors.push(accessor);\r\n return this._exporter._accessors.length - 1;\r\n }\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\r\nGLTFExporter.RegisterExtension(NAME, (exporter) => new EXT_mesh_gpu_instancing(exporter));\r\n"]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AAW,EAAE,aAAa,CAAC,CAAC;wBACrE,CAAC;wBACD,IAAI,WAAW,CAAC,MAAM,KAAK,aAAa,GAAG,CAAC,EAAE,CAAC;4BAC3C,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,cAAc,CAAC,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,CAAC,CAAC;wBACpH,CAAC;oBACL,CAAC;oBAED,uDAAuD;oBACvD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC;oBACxC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;gBACtC,CAAC;YACL,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;IAEO,cAAc,CAAC,MAAoB,EAAE,IAAkB,EAAE,KAAa,EAAE,aAA4B;QACxG,wBAAwB;QACxB,MAAM,EAAE,GAAG,aAAa,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;QAElD,6BAA6B;QAC7B,MAAM,QAAQ,GAAG,aAAa,CAAC,cAAc,CAAC,EAAE,EAAE,IAAI,0CAA+B,KAAK,CAAC,CAAC;QAC5F,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzC,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;IAChD,CAAC;CACJ;AAED,6DAA6D;AAC7D,YAAY,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,uBAAuB,CAAC,QAAQ,CAAC,CAAC,CAAC","sourcesContent":["import type { INode, IEXTMeshGpuInstancing } from \"babylonjs-gltf2interface\";\r\nimport { AccessorType, AccessorComponentType } from \"babylonjs-gltf2interface\";\r\nimport type { IGLTFExporterExtensionV2 } from \"../glTFExporterExtension\";\r\nimport type { BufferManager } from \"../bufferManager\";\r\nimport { GLTFExporter } from \"../glTFExporter\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { Node } from \"core/node\";\r\nimport { Mesh } from \"core/Meshes/mesh\";\r\nimport \"core/Meshes/thinInstanceMesh\";\r\nimport { TmpVectors, Quaternion, Vector3 } from \"core/Maths/math.vector\";\r\nimport { ConvertToRightHandedPosition, ConvertToRightHandedRotation } from \"../glTFUtilities\";\r\n\r\nimport { Logger } from \"core/Misc/logger\";\r\n\r\nconst NAME = \"EXT_mesh_gpu_instancing\";\r\n\r\nfunction ColorBufferToRGBAToRGB(colorBuffer: Float32Array, instanceCount: number) {\r\n const colorBufferRgb = new Float32Array(instanceCount * 3);\r\n\r\n for (let i = 0; i < instanceCount; i++) {\r\n colorBufferRgb[i * 3 + 0] = colorBuffer[i * 4 + 0];\r\n colorBufferRgb[i * 3 + 1] = colorBuffer[i * 4 + 1];\r\n colorBufferRgb[i * 3 + 2] = colorBuffer[i * 4 + 2];\r\n }\r\n return colorBufferRgb;\r\n}\r\n\r\n/**\r\n * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing/README.md)\r\n */\r\n// eslint-disable-next-line @typescript-eslint/naming-convention\r\nexport class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {\r\n /** Name of this extension */\r\n public readonly name = NAME;\r\n\r\n /** Defines whether this extension is enabled */\r\n public enabled = true;\r\n\r\n /** Defines whether this extension is required */\r\n public required = false;\r\n\r\n /**\r\n * Internal state to emit warning about instance color alpha once\r\n */\r\n private _instanceColorWarned = false;\r\n\r\n private _exporter: GLTFExporter;\r\n\r\n private _wasUsed = false;\r\n\r\n constructor(exporter: GLTFExporter) {\r\n this._exporter = exporter;\r\n }\r\n\r\n public dispose() {}\r\n\r\n /** @internal */\r\n public get wasUsed() {\r\n return this._wasUsed;\r\n }\r\n\r\n /**\r\n * After node is exported\r\n * @param context the GLTF context when loading the asset\r\n * @param node the node exported\r\n * @param babylonNode the corresponding babylon node\r\n * @param nodeMap map from babylon node id to node index\r\n * @param convertToRightHanded true if we need to convert data from left hand to right hand system.\r\n * @param bufferManager buffer manager\r\n * @returns nullable promise, resolves with the node\r\n */\r\n public async postExportNodeAsync(\r\n context: string,\r\n node: Nullable<INode>,\r\n babylonNode: Node,\r\n nodeMap: Map<Node, number>,\r\n convertToRightHanded: boolean,\r\n bufferManager: BufferManager\r\n ): Promise<Nullable<INode>> {\r\n return await new Promise((resolve) => {\r\n if (node && babylonNode instanceof Mesh) {\r\n if (babylonNode.hasThinInstances && this._exporter) {\r\n this._wasUsed = true;\r\n\r\n const noTranslation = Vector3.Zero();\r\n const noRotation = Quaternion.Identity();\r\n const noScale = Vector3.One();\r\n\r\n // retrieve all the instance world matrix\r\n const matrix = babylonNode.thinInstanceGetWorldMatrices();\r\n\r\n const iwt = TmpVectors.Vector3[2];\r\n const iwr = TmpVectors.Quaternion[1];\r\n const iws = TmpVectors.Vector3[3];\r\n\r\n let hasAnyInstanceWorldTranslation = false;\r\n let hasAnyInstanceWorldRotation = false;\r\n let hasAnyInstanceWorldScale = false;\r\n\r\n // prepare temp buffers\r\n const translationBuffer = new Float32Array(babylonNode.thinInstanceCount * 3);\r\n const rotationBuffer = new Float32Array(babylonNode.thinInstanceCount * 4);\r\n const scaleBuffer = new Float32Array(babylonNode.thinInstanceCount * 3);\r\n\r\n let i = 0;\r\n for (const m of matrix) {\r\n m.decompose(iws, iwr, iwt);\r\n\r\n if (convertToRightHanded) {\r\n ConvertToRightHandedPosition(iwt);\r\n ConvertToRightHandedRotation(iwr);\r\n }\r\n\r\n // fill the temp buffer\r\n translationBuffer.set(iwt.asArray(), i * 3);\r\n rotationBuffer.set(iwr.normalize().asArray(), i * 4); // ensure the quaternion is normalized\r\n scaleBuffer.set(iws.asArray(), i * 3);\r\n\r\n // this is where we decide if there is any transformation\r\n hasAnyInstanceWorldTranslation = hasAnyInstanceWorldTranslation || !iwt.equalsWithEpsilon(noTranslation);\r\n hasAnyInstanceWorldRotation = hasAnyInstanceWorldRotation || !iwr.equalsWithEpsilon(noRotation);\r\n hasAnyInstanceWorldScale = hasAnyInstanceWorldScale || !iws.equalsWithEpsilon(noScale);\r\n\r\n i++;\r\n }\r\n\r\n const extension: IEXTMeshGpuInstancing = {\r\n attributes: {},\r\n };\r\n\r\n // do we need to write TRANSLATION ?\r\n if (hasAnyInstanceWorldTranslation) {\r\n extension.attributes[\"TRANSLATION\"] = this._buildAccessor(translationBuffer, AccessorType.VEC3, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n // do we need to write ROTATION ?\r\n if (hasAnyInstanceWorldRotation) {\r\n // we decided to stay on FLOAT for now see https://github.com/BabylonJS/Babylon.js/pull/12495\r\n extension.attributes[\"ROTATION\"] = this._buildAccessor(rotationBuffer, AccessorType.VEC4, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n // do we need to write SCALE ?\r\n if (hasAnyInstanceWorldScale) {\r\n extension.attributes[\"SCALE\"] = this._buildAccessor(scaleBuffer, AccessorType.VEC3, babylonNode.thinInstanceCount, bufferManager);\r\n }\r\n let colorBuffer = babylonNode._userThinInstanceBuffersStorage?.data?.instanceColor;\r\n if (colorBuffer) {\r\n const instanceCount = babylonNode.thinInstanceCount;\r\n const accessorType = AccessorType.VEC3;\r\n if (babylonNode.hasVertexAlpha && colorBuffer.length === instanceCount * 4) {\r\n if (!this._instanceColorWarned) {\r\n Logger.Warn(\"EXT_mesh_gpu_instancing: Exporting instance colors as RGB, alpha channel of instance color is not exported\");\r\n this._instanceColorWarned = true;\r\n }\r\n colorBuffer = ColorBufferToRGBAToRGB(colorBuffer, instanceCount);\r\n } else if (colorBuffer.length === instanceCount * 4) {\r\n colorBuffer = ColorBufferToRGBAToRGB(colorBuffer, instanceCount);\r\n }\r\n if (colorBuffer.length === instanceCount * 3) {\r\n extension.attributes[\"_COLOR_0\"] = this._buildAccessor(colorBuffer, accessorType, instanceCount, bufferManager);\r\n }\r\n }\r\n\r\n /* eslint-enable @typescript-eslint/naming-convention*/\r\n node.extensions = node.extensions || {};\r\n node.extensions[NAME] = extension;\r\n }\r\n }\r\n resolve(node);\r\n });\r\n }\r\n\r\n private _buildAccessor(buffer: Float32Array, type: AccessorType, count: number, bufferManager: BufferManager): number {\r\n // build the buffer view\r\n const bv = bufferManager.createBufferView(buffer);\r\n\r\n // finally build the accessor\r\n const accessor = bufferManager.createAccessor(bv, type, AccessorComponentType.FLOAT, count);\r\n this._exporter._accessors.push(accessor);\r\n return this._exporter._accessors.length - 1;\r\n }\r\n}\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\r\nGLTFExporter.RegisterExtension(NAME, (exporter) => new EXT_mesh_gpu_instancing(exporter));\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babylonjs/serializers",
3
- "version": "8.31.3",
3
+ "version": "8.32.0",
4
4
  "main": "index.js",
5
5
  "module": "index.js",
6
6
  "types": "index.d.ts",
@@ -18,10 +18,10 @@
18
18
  "postcompile": "build-tools -c add-js-to-es6"
19
19
  },
20
20
  "devDependencies": {
21
- "@babylonjs/core": "^8.31.3",
21
+ "@babylonjs/core": "^8.32.0",
22
22
  "@dev/build-tools": "^1.0.0",
23
23
  "@lts/serializers": "^1.0.0",
24
- "babylonjs-gltf2interface": "^8.31.3"
24
+ "babylonjs-gltf2interface": "^8.32.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@babylonjs/core": "^8.0.0",