Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFSerializer : Ext mesh gpu instancing #12495

Merged
merged 5 commits into from
May 27, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {
const rotationBuffer = new Float32Array(babylonNode.thinInstanceCount * 4);
const scaleBuffer = new Float32Array(babylonNode.thinInstanceCount * 3);

matrix.forEach((m, i) => {
let i = 0;
for (const m of matrix) {
m.decompose(iws, iwr, iwt);

// fill the temp buffer
Expand All @@ -83,16 +84,17 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {
hasAnyInstanceWorldTranslation = hasAnyInstanceWorldTranslation || !iwt.equalsWithEpsilon(noTranslation);
hasAnyInstanceWorldRotation = hasAnyInstanceWorldRotation || !iwr.equalsWithEpsilon(noRotation);
hasAnyInstanceWorldScale = hasAnyInstanceWorldScale || !iws.equalsWithEpsilon(noScale);
});

/* eslint-disable @typescript-eslint/naming-convention*/
const gpu_instancing: IEXTMeshGpuInstancing = {
i++;
}

const extension: IEXTMeshGpuInstancing = {
attributes: {},
};

// do we need to write TRANSLATION ?
if (hasAnyInstanceWorldTranslation) {
gpu_instancing.attributes["TRANSLATION"] = this._buildAccessor(
extension.attributes["TRANSLATION"] = this._buildAccessor(
translationBuffer,
AccessorType.VEC3,
babylonNode.thinInstanceCount,
Expand All @@ -112,11 +114,11 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {
// force to float if wrong type.
componentType = AccessorComponentType.FLOAT;
}
gpu_instancing.attributes["ROTATION"] = this._buildAccessor(rotationBuffer, AccessorType.VEC4, babylonNode.thinInstanceCount, binaryWriter, componentType);
extension.attributes["ROTATION"] = this._buildAccessor(rotationBuffer, AccessorType.VEC4, babylonNode.thinInstanceCount, binaryWriter, componentType);
}
// do we need to write SCALE ?
if (hasAnyInstanceWorldScale) {
gpu_instancing.attributes["SCALE"] = this._buildAccessor(
extension.attributes["SCALE"] = this._buildAccessor(
scaleBuffer,
AccessorType.VEC3,
babylonNode.thinInstanceCount,
Expand All @@ -127,20 +129,14 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {

/* eslint-enable @typescript-eslint/naming-convention*/
node.extensions = node.extensions || {};
node.extensions[NAME] = gpu_instancing;
node.extensions[NAME] = extension;
}
}
resolve(node);
});
}

private _buildAccessor(
buffer: Float32Array | Int8Array | Int16Array,
type: AccessorType,
count: number,
binaryWriter: _BinaryWriter,
componentType: AccessorComponentType
): number {
private _buildAccessor(buffer: Float32Array, type: AccessorType, count: number, binaryWriter: _BinaryWriter, componentType: AccessorComponentType): number {
// write the buffer
const bufferOffset = binaryWriter.getByteOffset();
switch (componentType) {
Expand All @@ -160,6 +156,7 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {
for (let i = 0; i != buffer.length; i++) {
binaryWriter.setInt16(buffer[i] * 32767);
}

deltakosh marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
Expand All @@ -170,7 +167,13 @@ export class EXT_mesh_gpu_instancing implements IGLTFExporterExtensionV2 {

// finally build the accessor
const accessorIndex = this._exporter._accessors.length;
const accessor: IAccessor = { bufferView: bufferViewIndex, componentType: componentType, count: count, type: type };
const accessor: IAccessor = {
bufferView: bufferViewIndex,
componentType: componentType,
count: count,
type: type,
normalized: componentType == AccessorComponentType.BYTE || componentType == AccessorComponentType.SHORT,
};
this._exporter._accessors.push(accessor);
return accessorIndex;
}
Expand Down