Socket
Socket
Sign inDemoInstall

@microsoft/gltf-gen

Package Overview
Dependencies
4
Maintainers
15
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.20.0 to 0.21.0

built/tests/import-export.d.ts

22

built/gltffactory.d.ts

@@ -1,5 +0,1 @@

/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
/// <reference types="node" />

@@ -17,7 +13,25 @@ import { Material, Mesh, MeshPrimitive, Scene, Texture } from '.';

/**
* Convert the factory's scenes and other resources into a GLB buffer.
* @returns A buffer containing a glTF document in GLB format
*/
generateGLTF(): Buffer;
/**
* Populate this [[GltfFactory]] with the resources from a GLB file.
* @param path The file path to a GLB file.
*/
importFromGlb(path: string): Promise<void>;
/**
* Populate this [[GltfFactory]] with the resources from a glTF file.
* @param path
*/
importFromGltf(path: string): Promise<void>;
private importFromGltfData;
private importNode;
private static GetMeshInstanceHash;
/**
* Generate a [[GltfFactory]] from a single [[MeshPrimitive]].
* @param prim The sole prim in the glTF
*/
static FromSinglePrimitive(prim: MeshPrimitive): GltfFactory;
}
//# sourceMappingURL=gltffactory.d.ts.map
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/*!

@@ -6,5 +14,10 @@ * Copyright (c) Microsoft Corporation. All rights reserved.

*/
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_1 = require("fs");
const util_1 = require("util");
const readFile = util_1.promisify(fs_1.readFile);
const MRE = __importStar(require("@microsoft/mixed-reality-extension-common"));
const _1 = require(".");
const util_1 = require("./util");
const util_2 = require("./util");
const enums_1 = require("./enums");
/**

@@ -21,2 +34,3 @@ * Generates a glTF document from mesh data

/**
* Convert the factory's scenes and other resources into a GLB buffer.
* @returns A buffer containing a glTF document in GLB format

@@ -30,3 +44,3 @@ */

(this.scenes.length ? this.scenes.reduce((acc, s) => acc + s.getByteSize(scanId), 0) : 0);
const binaryData = Buffer.allocUnsafe(util_1.roundUpToNextMultipleOf4(dataBufferSize));
const binaryData = Buffer.allocUnsafe(util_2.roundUpToNextMultipleOf4(dataBufferSize));
const document = {

@@ -64,2 +78,314 @@ asset: {

}
/**
* Populate this [[GltfFactory]] with the resources from a GLB file.
* @param path The file path to a GLB file.
*/
async importFromGlb(path) {
var _a, _b;
const glbData = await readFile(path);
// read header
const magic = glbData.readUInt32LE(0);
const version = glbData.readUInt32LE(4);
const totalLength = glbData.readUInt32LE(8);
const jsonLength = glbData.readUInt32LE(12);
const jsonMagic = glbData.readUInt32LE(16);
const binLength = glbData.length > (20 + jsonLength) ? glbData.readUInt32LE(20 + jsonLength) : -1;
const binMagic = glbData.length > (24 + jsonLength) ? glbData.readUInt32LE(24 + jsonLength) : -1;
// validate header
if (magic !== 0x46546c67 || version !== 2 || totalLength !== glbData.length
|| jsonMagic !== 0x4e4f534a || binMagic !== -1 && binMagic !== 0x004e4942) {
throw new Error(`${path}: Bad GLB header`);
}
// read JSON
const jsonBin = glbData.toString('utf8', 20, 20 + jsonLength);
const json = JSON.parse(jsonBin);
// load buffers
const buffers = await Promise.all((_b = (_a = json.buffers) === null || _a === void 0 ? void 0 : _a.map(async (bufferDef, i) => {
if (i === 0 && !bufferDef.uri) {
if (binMagic !== -1) {
return glbData.slice(28 + jsonLength, 28 + jsonLength + binLength);
}
else {
throw new Error(`${path}: Expected embedded binary data`);
}
}
else {
return await readFile(path_1.resolve(path, bufferDef.uri));
}
}), (_b !== null && _b !== void 0 ? _b : [])));
// process data
this.importFromGltfData(json, buffers);
}
/**
* Populate this [[GltfFactory]] with the resources from a glTF file.
* @param path
*/
async importFromGltf(path) {
const gltfData = await readFile(path, { encoding: 'utf8' });
const json = JSON.parse(gltfData);
const buffers = [];
for (const bufferDef of json.buffers) {
const b = await readFile(path_1.resolve(path, bufferDef.uri));
buffers.push(b);
}
this.importFromGltfData(json, buffers);
}
importFromGltfData(json, buffers) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
// textures
for (const texDef of json.textures || []) {
const tex = new _1.Texture({
name: texDef.name,
wrapS: _1.TextureWrapMode.Repeat,
wrapT: _1.TextureWrapMode.Repeat
});
if (texDef.sampler !== undefined) {
const sampler = json.samplers[texDef.sampler];
tex.wrapS = (_a = sampler.wrapS, (_a !== null && _a !== void 0 ? _a : _1.TextureWrapMode.Repeat));
tex.wrapT = (_b = sampler.wrapT, (_b !== null && _b !== void 0 ? _b : _1.TextureWrapMode.Repeat));
tex.minFilter = sampler.minFilter;
tex.magFilter = sampler.magFilter;
}
const imgDef = json.images[texDef.source];
const img = tex.source = new _1.Image({
name: imgDef.name,
mimeType: imgDef.mimeType
});
if (imgDef.bufferView !== undefined) {
const bvDef = json.bufferViews[imgDef.bufferView];
const buf = buffers[bvDef.buffer];
img.embeddedData = buf.slice(bvDef.byteOffset, bvDef.byteOffset + bvDef.byteLength);
}
else {
img.uri = imgDef.uri;
}
this.textures.push(tex);
}
// materials
for (const matDef of json.materials || []) {
const mat = new _1.Material({
name: matDef.name,
baseColorTexCoord: (_d = (_c = matDef.pbrMetallicRoughness) === null || _c === void 0 ? void 0 : _c.baseColorTexture) === null || _d === void 0 ? void 0 : _d.texCoord,
metallicRoughnessTexCoord: (_f = (_e = matDef.pbrMetallicRoughness) === null || _e === void 0 ? void 0 : _e.metallicRoughnessTexture) === null || _f === void 0 ? void 0 : _f.texCoord,
metallicFactor: (_g = matDef.pbrMetallicRoughness) === null || _g === void 0 ? void 0 : _g.metallicFactor,
roughnessFactor: (_h = matDef.pbrMetallicRoughness) === null || _h === void 0 ? void 0 : _h.roughnessFactor,
normalTexCoord: (_j = matDef.normalTexture) === null || _j === void 0 ? void 0 : _j.texCoord,
normalTexScale: (_k = matDef.normalTexture) === null || _k === void 0 ? void 0 : _k.scale,
occlusionTexCoord: (_l = matDef.occlusionTexture) === null || _l === void 0 ? void 0 : _l.texCoord,
occlusionTexStrength: (_m = matDef.occlusionTexture) === null || _m === void 0 ? void 0 : _m.strength,
emissiveTexCoord: (_o = matDef.emissiveTexture) === null || _o === void 0 ? void 0 : _o.texCoord,
alphaMode: matDef.alphaMode === "BLEND" ? _1.AlphaMode.Blend :
matDef.alphaMode === "MASK" ? _1.AlphaMode.Mask :
_1.AlphaMode.Opaque,
alphaCutoff: matDef.alphaCutoff,
doubleSided: matDef.doubleSided
});
if (matDef.pbrMetallicRoughness) {
const pbr = matDef.pbrMetallicRoughness;
if (pbr.baseColorTexture) {
mat.baseColorTexture = this.textures[pbr.baseColorTexture.index];
}
if (pbr.baseColorFactor) {
mat.baseColorFactor = MRE.Color4.FromArray(pbr.baseColorFactor);
}
if (pbr.metallicRoughnessTexture) {
mat.metallicRoughnessTexture = this.textures[pbr.metallicRoughnessTexture.index];
}
}
if (matDef.normalTexture) {
mat.normalTexture = this.textures[matDef.normalTexture.index];
}
if (matDef.occlusionTexture) {
mat.occlusionTexture = this.textures[matDef.occlusionTexture.index];
}
if (matDef.emissiveTexture) {
mat.emissiveTexture = this.textures[matDef.emissiveTexture.index];
}
if (matDef.emissiveFactor) {
mat.emissiveFactor = MRE.Color3.FromArray(matDef.emissiveFactor);
}
this.materials.push(mat);
}
// meshes
const instancingHash = {};
for (const meshDef of json.meshes || []) {
const mesh = new _1.Mesh({
name: meshDef.name
});
// mesh primitives
for (const primDef of meshDef.primitives) {
if (primDef.mode !== undefined && primDef.mode !== 4) {
throw new Error(`Import failed: can only import meshes in triangle format`);
}
// if all the attribute accessors are the same, don't re-parse, just instance
const sig = GltfFactory.GetMeshInstanceHash(primDef);
const prim = new _1.MeshPrimitive({ material: this.materials[primDef.material] }, instancingHash[sig]);
if (instancingHash[sig]) {
mesh.primitives.push(prim);
continue;
}
instancingHash[sig] = prim;
const posAcc = json.accessors[primDef.attributes[_1.Vertex.PositionAttribute.attributeName]];
const nrmAcc = json.accessors[primDef.attributes[_1.Vertex.NormalAttribute.attributeName]];
const tngAcc = json.accessors[primDef.attributes[_1.Vertex.TangentAttribute.attributeName]];
const uv0Acc = json.accessors[primDef.attributes[_1.Vertex.TexCoordAttribute[0].attributeName]];
const uv1Acc = json.accessors[primDef.attributes[_1.Vertex.TexCoordAttribute[1].attributeName]];
const clrAcc = json.accessors[primDef.attributes[_1.Vertex.ColorAttribute.attributeName]];
const indAcc = json.accessors[primDef.indices];
const posBV = json.bufferViews[(_p = posAcc) === null || _p === void 0 ? void 0 : _p.bufferView];
const nrmBV = json.bufferViews[(_q = nrmAcc) === null || _q === void 0 ? void 0 : _q.bufferView];
const tngBV = json.bufferViews[(_r = tngAcc) === null || _r === void 0 ? void 0 : _r.bufferView];
const uv0BV = json.bufferViews[(_s = uv0Acc) === null || _s === void 0 ? void 0 : _s.bufferView];
const uv1BV = json.bufferViews[(_t = uv1Acc) === null || _t === void 0 ? void 0 : _t.bufferView];
const clrBV = json.bufferViews[(_u = clrAcc) === null || _u === void 0 ? void 0 : _u.bufferView];
const indBV = json.bufferViews[(_v = indAcc) === null || _v === void 0 ? void 0 : _v.bufferView];
// vertices (assume all attributes have the same count)
for (let i = 0; i < posAcc.count; i++) {
const vert = new _1.Vertex();
if (posAcc) {
const attr = _1.Vertex.PositionAttribute;
const offset = (_w = posBV.byteOffset, (_w !== null && _w !== void 0 ? _w : 0)) + (_x = posAcc.byteOffset, (_x !== null && _x !== void 0 ? _x : 0)) +
i * ((_y = posBV.byteStride, (_y !== null && _y !== void 0 ? _y : 0)) + attr.byteSize);
vert.position = new MRE.Vector3(buffers[posBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[posBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize), buffers[posBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize));
}
if (nrmAcc) {
const attr = _1.Vertex.NormalAttribute;
const offset = (_z = nrmBV.byteOffset, (_z !== null && _z !== void 0 ? _z : 0)) + (_0 = nrmAcc.byteOffset, (_0 !== null && _0 !== void 0 ? _0 : 0)) +
i * ((_2 = nrmBV.byteStride, (_2 !== null && _2 !== void 0 ? _2 : 0)) + attr.byteSize);
vert.normal = new MRE.Vector3(buffers[nrmBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[nrmBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize), buffers[nrmBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize));
}
if (tngAcc) {
const attr = _1.Vertex.TangentAttribute;
const offset = (_3 = tngBV.byteOffset, (_3 !== null && _3 !== void 0 ? _3 : 0)) + (_4 = tngAcc.byteOffset, (_4 !== null && _4 !== void 0 ? _4 : 0)) +
i * ((_5 = tngBV.byteStride, (_5 !== null && _5 !== void 0 ? _5 : 0)) + attr.byteSize);
vert.tangent = new MRE.Vector4(buffers[tngBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[tngBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize), buffers[tngBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize), buffers[tngBV.buffer].readFloatLE(offset + 3 * attr.elementByteSize));
}
if (uv0Acc) {
const attr = _1.Vertex.TexCoordAttribute[0];
const offset = (_6 = uv0BV.byteOffset, (_6 !== null && _6 !== void 0 ? _6 : 0)) + (_7 = uv0Acc.byteOffset, (_7 !== null && _7 !== void 0 ? _7 : 0)) +
i * ((_8 = uv0BV.byteStride, (_8 !== null && _8 !== void 0 ? _8 : 0)) + attr.byteSize);
if (uv0Acc.componentType === _1.AccessorComponentType.Float) {
vert.texCoord0 = new MRE.Vector2(buffers[uv0BV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[uv0BV.buffer].readFloatLE(offset + 1 * attr.elementByteSize));
}
else if (uv0Acc.componentType === _1.AccessorComponentType.UByte) {
vert.texCoord0 = new MRE.Vector2(buffers[uv0BV.buffer].readUInt8(offset + 0 * attr.elementByteSize), buffers[uv0BV.buffer].readUInt8(offset + 1 * attr.elementByteSize))
.multiplyByFloats(1 / 0xff, 1 / 0xff);
}
else if (uv0Acc.componentType === _1.AccessorComponentType.UShort) {
vert.texCoord0 = new MRE.Vector2(buffers[uv0BV.buffer].readUInt16LE(offset + 0 * attr.elementByteSize), buffers[uv0BV.buffer].readUInt16LE(offset + 1 * attr.elementByteSize))
.multiplyByFloats(1 / 0xffff, 1 / 0xffff);
}
}
if (uv1Acc) {
const attr = _1.Vertex.TexCoordAttribute[1];
const offset = (_9 = uv1BV.byteOffset, (_9 !== null && _9 !== void 0 ? _9 : 0)) + (_10 = uv1Acc.byteOffset, (_10 !== null && _10 !== void 0 ? _10 : 0)) +
i * ((_11 = uv1BV.byteStride, (_11 !== null && _11 !== void 0 ? _11 : 0)) + attr.byteSize);
if (uv1Acc.componentType === _1.AccessorComponentType.Float) {
vert.texCoord1 = new MRE.Vector2(buffers[uv1BV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[uv1BV.buffer].readFloatLE(offset + 1 * attr.elementByteSize));
}
else if (uv1Acc.componentType === _1.AccessorComponentType.UByte) {
vert.texCoord1 = new MRE.Vector2(buffers[uv1BV.buffer].readUInt8(offset + 0 * attr.elementByteSize), buffers[uv1BV.buffer].readUInt8(offset + 1 * attr.elementByteSize))
.multiplyByFloats(1 / 0xff, 1 / 0xff);
}
else if (uv1Acc.componentType === _1.AccessorComponentType.UShort) {
vert.texCoord1 = new MRE.Vector2(buffers[uv1BV.buffer].readUInt16LE(offset + 0 * attr.elementByteSize), buffers[uv1BV.buffer].readUInt16LE(offset + 1 * attr.elementByteSize))
.multiplyByFloats(1 / 0xffff, 1 / 0xffff);
}
}
if (clrAcc) {
// TODO: fix accessor size
const attr = _1.Vertex.ColorAttribute;
const offset = (_12 = clrBV.byteOffset, (_12 !== null && _12 !== void 0 ? _12 : 0)) + (_13 = clrAcc.byteOffset, (_13 !== null && _13 !== void 0 ? _13 : 0)) +
i * ((_14 = clrBV.byteStride, (_14 !== null && _14 !== void 0 ? _14 : 0)) + attr.byteSize);
if (clrAcc.componentType === _1.AccessorComponentType.Float) {
vert.color0 = new MRE.Color4(buffers[clrBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize), buffers[clrBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize), buffers[clrBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize));
if (clrAcc.type === enums_1.AccessorType.Vec4) {
vert.color0.a = buffers[clrBV.buffer]
.readFloatLE(offset + 3 * attr.elementByteSize);
}
}
else if (clrAcc.componentType === _1.AccessorComponentType.UByte) {
vert.color0 = new MRE.Color4(buffers[clrBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize) * 1 / 0xff, buffers[clrBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize) * 1 / 0xff, buffers[clrBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize) * 1 / 0xff);
if (clrAcc.type === enums_1.AccessorType.Vec4) {
vert.color0.a = buffers[clrBV.buffer]
.readFloatLE(offset + 3 * attr.elementByteSize) * 1 / 0xff;
}
}
else if (clrAcc.componentType === _1.AccessorComponentType.UShort) {
vert.color0 = new MRE.Color4(buffers[clrBV.buffer].readFloatLE(offset + 0 * attr.elementByteSize) * 1 / 0xffff, buffers[clrBV.buffer].readFloatLE(offset + 1 * attr.elementByteSize) * 1 / 0xffff, buffers[clrBV.buffer].readFloatLE(offset + 2 * attr.elementByteSize) * 1 / 0xffff);
if (clrAcc.type === enums_1.AccessorType.Vec4) {
vert.color0.a = buffers[clrBV.buffer]
.readFloatLE(offset + 3 * attr.elementByteSize) * 1 / 0xffff;
}
}
}
prim.vertices.push(vert);
}
// indices
const indexSize = ((_15 = indAcc) === null || _15 === void 0 ? void 0 : _15.componentType) === _1.AccessorComponentType.UShort ? 2 : 4;
if (indAcc) {
for (let i = 0; i < indAcc.count; i++) {
const offset = (_16 = indBV.byteOffset, (_16 !== null && _16 !== void 0 ? _16 : 0)) + (_17 = indAcc.byteOffset, (_17 !== null && _17 !== void 0 ? _17 : 0)) +
i * ((_18 = indBV.byteStride, (_18 !== null && _18 !== void 0 ? _18 : 0)) + indexSize);
let index;
switch (indAcc.componentType) {
case _1.AccessorComponentType.UShort:
index = buffers[indBV.buffer].readUInt16LE(offset);
break;
case _1.AccessorComponentType.UInt:
index = buffers[indBV.buffer].readUInt32LE(offset);
break;
}
prim.triangles.push(index);
}
}
mesh.primitives.push(prim);
}
this.meshes.push(mesh);
}
// scenes
for (const sceneDef of json.scenes || []) {
const scene = new _1.Scene({
name: sceneDef.name,
nodes: (_19 = sceneDef.nodes, (_19 !== null && _19 !== void 0 ? _19 : [])).map(nodeId => this.importNode(json, nodeId))
});
this.scenes.push(scene);
}
}
importNode(json, nodeId) {
var _a;
const nodeDef = json.nodes[nodeId];
const node = new _1.Node({
name: nodeDef.name,
mesh: this.meshes[nodeDef.mesh]
});
if (nodeDef.matrix) {
node.matrix = MRE.Matrix.FromArray(nodeDef.matrix);
}
else {
node.translation = nodeDef.translation ? MRE.Vector3.FromArray(nodeDef.translation) : MRE.Vector3.Zero();
node.rotation = nodeDef.rotation ? MRE.Quaternion.FromArray(nodeDef.rotation) : MRE.Quaternion.Identity();
node.scale = nodeDef.scale ? MRE.Vector3.FromArray(nodeDef.scale) : MRE.Vector3.One();
}
node.children = (_a = nodeDef.children, (_a !== null && _a !== void 0 ? _a : [])).map(childId => this.importNode(json, childId));
return node;
}
static GetMeshInstanceHash(primDef) {
const attributeNames = [
_1.Vertex.PositionAttribute.attributeName,
_1.Vertex.NormalAttribute.attributeName,
_1.Vertex.TangentAttribute.attributeName,
_1.Vertex.TexCoordAttribute[0].attributeName,
_1.Vertex.TexCoordAttribute[1].attributeName,
_1.Vertex.ColorAttribute.attributeName
];
return attributeNames
.map(attr => primDef.attributes[attr])
.join('-')
+ '-' + primDef.indices;
}
/**
* Generate a [[GltfFactory]] from a single [[MeshPrimitive]].
* @param prim The sole prim in the glTF
*/
static FromSinglePrimitive(prim) {

@@ -66,0 +392,0 @@ return new GltfFactory([new _1.Scene({

10

built/image.d.ts

@@ -12,2 +12,4 @@ /*!

embeddedFilePath?: string;
embeddedData?: Buffer;
mimeType?: string;
}

@@ -17,3 +19,3 @@ export declare class Image extends Serializable implements ImageLike {

/**
* A URI to a texture, resolved by the model consumer. Don't set alongside [[embeddedFilePath]].
* A URI to a texture, resolved by the model consumer. Don't set alongside [[embeddedFilePath]] or [[embeddedData]].
*/

@@ -23,6 +25,10 @@ uri: string;

* A path to a local texture file, resolved during serialization and packed into the model.
* Don't set alongside [[uri]].
* Don't set alongside [[uri]] or [[embeddedData]].
*/
embeddedFilePath: string;
private embeddedFileSize;
/**
* The image's binary data. Don't set alongside [[uri]] or [[embeddedFilePath]].
*/
embeddedData: Buffer;
private manualMime;

@@ -29,0 +35,0 @@ /**

@@ -17,2 +17,4 @@ "use strict";

this.embeddedFilePath = init.embeddedFilePath;
this.embeddedData = init.embeddedData;
this.mimeType = init.mimeType;
}

@@ -45,3 +47,3 @@ /**

};
if (this.embeddedFilePath) {
if (this.embeddedFilePath || this.embeddedData) {
image.bufferView = this._embedImage(document, data);

@@ -56,2 +58,5 @@ }

_embedImage(document, data) {
if (!document.bufferViews) {
document.bufferViews = [];
}
let lastBV;

@@ -64,12 +69,12 @@ if (document.bufferViews.length > 0) {

byteOffset: lastBV ? Math.ceil((lastBV.byteOffset + lastBV.byteLength) / 4) * 4 : 0,
byteLength: this.embeddedFileSize
byteLength: this.embeddedData ? this.embeddedData.length : this.embeddedFileSize
};
const bufferViewData = data.slice(bufferView.byteOffset, bufferView.byteOffset + bufferView.byteLength);
// fill padding with zeros
for (let i = util_1.roundUpToNextMultipleOf4(bufferView.byteOffset + bufferView.byteLength) - 1; i >= bufferView.byteOffset + bufferView.byteLength; i--) {
data.writeUInt8(0, i);
const bufferViewData = data.slice(bufferView.byteOffset, util_1.roundUpToNextMultipleOf4(bufferView.byteOffset + bufferView.byteLength));
if (this.embeddedData) {
// write 4-byte padding
bufferViewData.writeInt32LE(0, util_1.roundUpToNextMultipleOf4(bufferView.byteLength) - 4);
this.embeddedData.copy(bufferViewData);
}
fs_1.readFileSync(this.embeddedFilePath).copy(bufferViewData);
if (!document.bufferViews) {
document.bufferViews = [];
else {
fs_1.readFileSync(this.embeddedFilePath).copy(bufferViewData);
}

@@ -88,4 +93,8 @@ document.bufferViews.push(bufferView);

const stat = fs_1.statSync(this.embeddedFilePath);
return this.embeddedFileSize = stat.size;
this.embeddedFileSize = stat.size;
return util_1.roundUpToNextMultipleOf4(stat.size);
}
else if (this.embeddedData) {
return util_1.roundUpToNextMultipleOf4(this.embeddedData.length);
}
else {

@@ -92,0 +101,0 @@ return 0;

@@ -16,3 +16,3 @@ "use strict";

serialize(document, data) {
if (this.cachedSerialId) {
if (this.cachedSerialId !== undefined) {
return this.cachedSerialId;

@@ -28,3 +28,3 @@ }

document.meshes.push(mesh);
return this.cachedSerialId = document.meshes.length - 1;
return this.cachedSerialId = (document.meshes.length - 1);
}

@@ -31,0 +31,0 @@ getByteSize(scanId) {

@@ -24,2 +24,3 @@ /*!

private usesColor0;
private attributes;
constructor(init?: MeshPrimitiveLike, instanceParent?: MeshPrimitive);

@@ -26,0 +27,0 @@ private _updateAttributeUsage;

@@ -6,6 +6,13 @@ "use strict";

*/
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const enums_1 = require("./enums");
const util_1 = require("./util");
const vertex_1 = require("./vertex");
const VA = __importStar(require("./vertexattributes"));
class MeshPrimitive {

@@ -16,2 +23,10 @@ // eslint-disable-next-line default-param-last

this.triangles = [];
this.attributes = {
position: new VA.PositionAttribute(),
normal: new VA.NormalAttribute(),
tangent: new VA.TangentAttribute(),
texCoord0: new VA.TexCoordAttribute(0),
texCoord1: new VA.TexCoordAttribute(1),
color: new VA.ColorAttribute(0)
};
if (instanceParent) {

@@ -43,19 +58,19 @@ this.instanceParent = instanceParent;

const indexSize = this.vertices.length <= 65535 ? 2 : 4;
const posBufSize = util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.positionAttribute.byteSize);
const posBufSize = util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.position.byteSize);
const indexBufSize = util_1.roundUpToNextMultipleOf4(this.triangles.length * indexSize);
let count = posBufSize + indexBufSize;
if (this.usesNormals) {
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.normalAttribute.byteSize);
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.normal.byteSize);
}
if (this.usesTangents) {
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.tangentAttribute.byteSize);
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.tangent.byteSize);
}
if (this.usesTexCoord0) {
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.texCoordAttribute[0].byteSize);
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.texCoord0.byteSize);
}
if (this.usesTexCoord1) {
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.texCoordAttribute[1].byteSize);
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.texCoord1.byteSize);
}
if (this.usesColor0) {
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * vertex_1.Vertex.colorAttribute.byteSize);
count += util_1.roundUpToNextMultipleOf4(this.vertices.length * this.attributes.color.byteSize);
}

@@ -79,3 +94,3 @@ if (this.material !== undefined) {

attributes: {
POSITION: this._serializeAttribute(vertex_1.Vertex.positionAttribute, document, data)
POSITION: this._serializeAttribute(this.attributes.position, document, data)
},

@@ -85,15 +100,15 @@ indices: this._serializeIndices(document, data)

if (this.usesNormals) {
prim.attributes.NORMAL = this._serializeAttribute(vertex_1.Vertex.normalAttribute, document, data);
prim.attributes.NORMAL = this._serializeAttribute(this.attributes.normal, document, data);
}
if (this.usesTangents) {
prim.attributes.TANGENT = this._serializeAttribute(vertex_1.Vertex.tangentAttribute, document, data);
prim.attributes.TANGENT = this._serializeAttribute(this.attributes.tangent, document, data);
}
if (this.usesTexCoord0) {
prim.attributes.TEXCOORD_0 = this._serializeAttribute(vertex_1.Vertex.texCoordAttribute[0], document, data);
prim.attributes.TEXCOORD_0 = this._serializeAttribute(this.attributes.texCoord0, document, data);
}
if (this.usesTexCoord1) {
prim.attributes.TEXCOORD_1 = this._serializeAttribute(vertex_1.Vertex.texCoordAttribute[1], document, data);
prim.attributes.TEXCOORD_1 = this._serializeAttribute(this.attributes.texCoord1, document, data);
}
if (this.usesColor0) {
prim.attributes.COLOR_0 = this._serializeAttribute(vertex_1.Vertex.colorAttribute, document, data);
prim.attributes.COLOR_0 = this._serializeAttribute(this.attributes.color, document, data);
}

@@ -100,0 +115,0 @@ if (this.material) {

@@ -25,3 +25,3 @@ "use strict";

serialize(document, data) {
if (this.cachedSerialId) {
if (this.cachedSerialId !== undefined) {
return this.cachedSerialId;

@@ -28,0 +28,0 @@ }

@@ -16,3 +16,3 @@ "use strict";

serialize(document, data) {
if (this.cachedSerialId) {
if (this.cachedSerialId !== undefined) {
return this.cachedSerialId;

@@ -19,0 +19,0 @@ }

@@ -7,4 +7,4 @@ /// <reference types="node" />

shouldPrintBuffer: boolean;
run(): Buffer;
run(): Buffer | Promise<Buffer>;
}
//# sourceMappingURL=index.d.ts.map

@@ -16,6 +16,9 @@ "use strict";

const triangle_1 = __importDefault(require("./triangle"));
const import_export_1 = __importDefault(require("./import-export"));
const meshprimitive_1 = __importDefault(require("./meshprimitive"));
const util_1 = require("./util");
(async () => {
const tests = [new empty_1.default(), new triangle_1.default(), new prim_dupe_1.default(), new material_1.default(), new meshprimitive_1.default()];
const tests = [
new empty_1.default(), new triangle_1.default(), new prim_dupe_1.default(), new material_1.default(), new meshprimitive_1.default(), new import_export_1.default()
];
for (const test of tests) {

@@ -28,3 +31,9 @@ console.log(`+==========================================+

try {
result = test.run();
const output = test.run();
if (output instanceof Buffer) {
result = output;
}
else {
result = await output;
}
}

@@ -36,3 +45,3 @@ catch (ex) {

time = process.hrtime(time);
console.log(`Test completed in ${time[0] * 1000 + time[1] / 1000} ms\n`);
console.log(`Test completed in ${(time[0] * 1e3 + time[1] * 1e-6).toFixed(3)} ms\n`);
const jsonStart = 20;

@@ -46,3 +55,3 @@ const jsonLength = result.readInt32LE(12);

console.log('Output Data:');
util_1.prettyPrintBuffer(result, jsonStart + jsonLength + 8);
util_1.prettyPrintBuffer(result, jsonStart + jsonLength + 8, true);
}

@@ -49,0 +58,0 @@ const validationResult = await validator.validateBytes(new Uint8Array(result));

@@ -20,7 +20,7 @@ "use strict";

this.shouldPrintJson = true;
this.shouldPrintBuffer = true;
this.shouldPrintBuffer = false;
}
run() {
const vertices = [];
const MAX_SHORT = 65535;
const MAX_SHORT = 0xffff;
// Add enough vertices so the triangles field needs integer sized

@@ -27,0 +27,0 @@ // numbers to reference all of them

@@ -7,3 +7,3 @@ /*!

/** @hidden */
export declare function prettyPrintBuffer(buffer: Buffer, offset: number): void;
export declare function prettyPrintBuffer(buffer: Buffer, offset?: number, labelOffsetAsZero?: boolean): void;
//# sourceMappingURL=util.d.ts.map

@@ -9,21 +9,27 @@ "use strict";

/** @hidden */
function prettyPrintBuffer(buffer, offset) {
const rawString = buffer.toString('hex', offset);
let prettyString = '0000 | ';
for (let i = 0; i < rawString.length; i++) {
prettyString += rawString[i];
if (i % 32 === 31) {
const offsetString = ((i + 1) / 2).toString(16).padStart(4, '0');
prettyString += `\n${offsetString} | `;
}
else if (i % 8 === 7) {
prettyString += ' ';
}
else if (i % 2 === 1) {
prettyString += ' ';
}
function prettyPrintBuffer(buffer, offset = 0, labelOffsetAsZero = false) {
const offsetDigitCount = Math.ceil(Math.log2(buffer.length) / 4);
// loop over lines
for (let i = offset; i < buffer.length; i += 16) {
// line label
const offsetStringValue = labelOffsetAsZero ? i - offset : i;
const offsetString = offsetStringValue.toString(16).padStart(offsetDigitCount, '0');
// byte data
const byteString = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
.map(group => {
return group.map(bi => {
const byte = i + bi;
return byte >= buffer.length ? ' ' : buffer.toString('hex', byte, byte + 1);
}).join(' ');
}).join(' ');
// byte data in ascii encoding (non-control characters only)
const asciiString = buffer.toString('ascii', i, i + 16)
.split('').map(c => {
const code = c.charCodeAt(0);
return code >= 0x20 ? c : ' ';
}).join('');
console.log(`${offsetString} | ${byteString} | ${asciiString}`);
}
console.log(prettyString + '\n');
}
exports.prettyPrintBuffer = prettyPrintBuffer;
//# sourceMappingURL=util.js.map

@@ -27,9 +27,9 @@ /*!

constructor(init?: VertexLike);
static positionAttribute: VertexAttribute;
static normalAttribute: VertexAttribute;
static tangentAttribute: VertexAttribute;
static texCoordAttribute: VertexAttribute[];
static colorAttribute: VertexAttribute;
static PositionAttribute: VertexAttribute;
static NormalAttribute: VertexAttribute;
static TangentAttribute: VertexAttribute;
static TexCoordAttribute: VertexAttribute[];
static ColorAttribute: VertexAttribute;
}
export {};
//# sourceMappingURL=vertex.d.ts.map

@@ -44,10 +44,10 @@ "use strict";

exports.Vertex = Vertex;
Vertex.positionAttribute = new vertexattributes_1.PositionAttribute();
Vertex.normalAttribute = new vertexattributes_1.NormalAttribute();
Vertex.tangentAttribute = new vertexattributes_1.TangentAttribute();
Vertex.texCoordAttribute = [
Vertex.PositionAttribute = new vertexattributes_1.PositionAttribute();
Vertex.NormalAttribute = new vertexattributes_1.NormalAttribute();
Vertex.TangentAttribute = new vertexattributes_1.TangentAttribute();
Vertex.TexCoordAttribute = [
new vertexattributes_1.TexCoordAttribute(0),
new vertexattributes_1.TexCoordAttribute(1)
];
Vertex.colorAttribute = new vertexattributes_1.ColorAttribute(0);
Vertex.ColorAttribute = new vertexattributes_1.ColorAttribute(0);
//# sourceMappingURL=vertex.js.map
{
"name": "@microsoft/gltf-gen",
"version": "0.20.0",
"version": "0.21.0",
"description": "Generate glTF buffers on the fly from vertex data",

@@ -48,6 +48,6 @@ "main": "built/index.js",

"dependencies": {
"@microsoft/mixed-reality-extension-common": "^0.20.0",
"@microsoft/mixed-reality-extension-common": "^0.21.0",
"mime-types": "~2.1.19"
},
"gitHead": "8ab221d4ed08c6201e559b30a2b220ca4aa8b89d"
"gitHead": "e771bea26616acfd4d9957a364836870ccad75bf"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc