Skip to content

Instantly share code, notes, and snippets.

@JDeuce
Created April 8, 2021 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDeuce/6bc0e97510ce4ce7973cb9db1cda47cb to your computer and use it in GitHub Desktop.
Save JDeuce/6bc0e97510ce4ce7973cb9db1cda47cb to your computer and use it in GitHub Desktop.
// This is a minimal example showing how to create the Draco encoder module.
// The encoder module is created asynchronously, so you need to set a
// callback to make sure it is initialized before you try and call the module.
'use_strict';
import draco3d from 'draco3dgltf';
import fs from 'fs';
let encoderModule = null;
let decoderModule = null;
// The code to create the encoder module is asynchronous.
// draco3d.createEncoderModule will return a promise to a funciton with a
// module as a parameter when the module has been fully initialized.
draco3d.createEncoderModule({}).then(function(module) {
// This is reached when everything is ready, and you can call methods on
// Module.
encoderModule = module;
console.log('Encoder Module Initialized!');
moduleInitialized();
});
draco3d.createDecoderModule({}).then(function(module) {
// This is reached when everything is ready, and you can call methods on
// Module.
decoderModule = module;
console.log('Decoder Module Initialized!');
moduleInitialized();
});
function moduleInitialized() {
if (!encoderModule || !decoderModule)
{
return;
}
const encoder = new encoderModule.Encoder();
const pointCloudBuilder = new encoderModule.PointCloudBuilder();
const pointCloud = new encoderModule.PointCloud();
const count = 1;
const vertComponentCount = 3;
const positionArray = new Float32Array([
1.0, 2.0, 3.0,
]);
const pos_att_id = pointCloudBuilder.AddFloatAttribute(pointCloud, pointCloudBuilder.POSITION, count, vertComponentCount, positionArray);
const numberOfComponents = 3;
const encodedDracoData = new encoderModule.DracoInt8Array();
const deduplicate = false;
const length = encoder.EncodePointCloudToDracoBuffer(pointCloud, deduplicate, encodedDracoData);
if (length <= 0)
{
console.log("Failed!");
return;
}
else
{
console.log(`Encoded ${pointCloud.num_points()} points to ${length} bytes`);
}
const encodedData = Buffer.alloc(length);
for (let i = 0; i < length; i++)
{
encodedData[i] = encodedDracoData.GetValue(i);
}
console.log("Point cloud done! - " + length);
const base64 = encodedData.toString('base64');
console.log(`Base64 - ${base64.length} - ${base64}`);
cleanup(encoder);
cleanup(pointCloudBuilder);
const decoder = new decoderModule.Decoder();
console.log("XX " + decoder.GetEncodedGeometryType(encodedData));
const decodedGeometry = decodeDracoData(encodedData, decoder);
}
function decodeDracoData(rawBuffer, decoder) {
const buffer = new decoderModule.DecoderBuffer();
buffer.Init(new Int8Array(rawBuffer), rawBuffer.byteLength);
const geometryType = decoder.GetEncodedGeometryType(buffer);
let dracoGeometry;
let status;
if (geometryType === decoderModule.TRIANGULAR_MESH) {
dracoGeometry = new decoderModule.Mesh();
status = decoder.DecodeBufferToMesh(buffer, dracoGeometry);
} else if (geometryType === decoderModule.POINT_CLOUD) {
dracoGeometry = new decoderModule.PointCloud();
status = decoder.DecodeBufferToPointCloud(buffer, dracoGeometry);
} else {
const errorMsg = 'Error: Unknown geometry type.';
console.error(errorMsg);
}
if (!status.ok() || dracoGeometry.ptr() === 0)
{
console.log("Decoding failed " + status.error_msg());
}
decoderModule.destroy(buffer);
return dracoGeometry;
}
function cleanup(encoder) {
encoderModule.destroy(encoder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment