loaders.gl
Version:
Framework-independent loaders for 3D graphics formats
406 lines (361 loc) • 12 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var GLTFParser =
/*#__PURE__*/
function () {
function GLTFParser(gltf) {
_classCallCheck(this, GLTFParser);
this.gltf = gltf;
this.log = console; // eslint-disable-line
this.out = {};
}
_createClass(GLTFParser, [{
key: "parse",
value: function parse() {
var _this = this;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
// Load all images
this.out.images = (this.gltf.images || []).map(function (image) {
return _this.parseImage(image);
}).filter(Boolean); // Parse all scenes
this.out.scenes = (this.gltf.scenes || []).map(function (scene) {
return _this.parseImage(scene);
}).filter(Boolean);
if (this.gltf.scene) {
this.out.scene = this.gltf.scenes[this.gltf.scene];
}
}
}, {
key: "parseScene",
value: function parseScene() {}
}, {
key: "parseImage",
value: function parseImage(image) {
return this.config.createImage(image);
}
}, {
key: "parseMesh",
value: function parseMesh(mesh) {
var _this2 = this;
// Each primitive is intended to correspond to a draw call
var primitives = (mesh.primitives || []).map(function (primitive) {
return _this2.parseMeshPrimitive(primitive);
});
return primitives.length === 1 ? primitives[0] : this.config.createGroup(primitives);
}
}, {
key: "parseMeshPrimitive",
value: function parseMeshPrimitive(primitive) {
// if (!primitive.attributes)
// this.log.warn(primitive without attributes`)
var attributes = primitive.attributes || {};
attributes = this.config.mapAttributes(attributes);
return attributes;
}
}, {
key: "parseAccessor",
value: function parseAccessor(accessor) {
return this.config.createBuffer(accessor);
} // ACCESSORS
}, {
key: "getScene",
value: function getScene(index) {
return this._get('scenes', index);
}
}, {
key: "getNode",
value: function getNode(index) {
return this._get('nodes', index);
}
}, {
key: "getSkin",
value: function getSkin(index) {
return this._get('skins', index);
}
}, {
key: "getMesh",
value: function getMesh(index) {
return this._get('meshes', index);
}
}, {
key: "getMaterial",
value: function getMaterial(index) {
return this._get('materials', index);
}
}, {
key: "getAccessor",
value: function getAccessor(index) {
return this._get('accessors', index);
}
}, {
key: "getTexture",
value: function getTexture(index) {
return this._get('textures', index);
}
}, {
key: "getSampler",
value: function getSampler(index) {
return this._get('samplers', index);
}
}, {
key: "getImage",
value: function getImage(index) {
return this._get('images', index);
}
}, {
key: "getBufferView",
value: function getBufferView(index) {
return this._get('bufferViews', index);
}
}, {
key: "getBuffer",
value: function getBuffer(index) {
return this._get('buffers', index);
}
}, {
key: "_get",
value: function _get(array, index) {
var object = this.gltf[array] && this.gltf[array][index];
if (!object) {
console.warn("glTF file error: Could not resolve ".concat(array, "[").concat(index, "]")); // eslint-disable-line
}
return object;
} // PREPARATION STEP: CROSS-LINK INDEX RESOLUTION, ENUM LOOKUP, CONVENIENCE CALCULATIONS
/* eslint-disable complexity */
}, {
key: "resolve",
value: function resolve() {
var _this3 = this;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var gltf = this.gltf;
(gltf.bufferViews || []).forEach(function (bufView, i) {
return _this3.resolveBufferView(bufView, i);
});
(gltf.images || []).forEach(function (image, i) {
return _this3.resolveImage(image, i);
});
(gltf.samplers || []).forEach(function (sampler, i) {
return _this3.resolveSampler(sampler, i);
});
(gltf.textures || []).forEach(function (texture, i) {
return _this3.resolveTexture(texture, i);
});
(gltf.accessors || []).forEach(function (accessor, i) {
return _this3.resolveAccessor(accessor, i);
});
(gltf.materials || []).forEach(function (material, i) {
return _this3.resolveMaterial(material, i);
});
(gltf.meshes || []).forEach(function (mesh, i) {
return _this3.resolveMesh(mesh, i);
});
(gltf.nodes || []).forEach(function (node, i) {
return _this3.resolveNode(node, i);
});
(gltf.skins || []).forEach(function (skin, i) {
return _this3.resolveSkin(skin, i);
});
(gltf.scenes || []).forEach(function (scene, i) {
return _this3.resolveScene(scene, i);
});
if (gltf.scene) {
gltf.scene = gltf.scenes[this.gltf.scene];
}
return gltf;
}
/* eslint-enable complexity */
}, {
key: "resolveScene",
value: function resolveScene(scene, index) {
var _this4 = this;
scene.id = "scene-".concat(index);
scene.nodes = (scene.nodes || []).map(function (node) {
return _this4.getNode(node);
});
}
}, {
key: "resolveNode",
value: function resolveNode(node, index) {
var _this5 = this;
node.id = "node-".concat(index);
node.children = (node.children || []).map(function (child) {
return _this5.getNode(child);
});
if (node.mesh) {
node.mesh = this.getMesh(node.mesh);
}
if (node.camera) {
node.camera = this.getCamera(node.camera);
}
if (node.skin) {
node.skin = this.getSkin(node.skin);
}
}
}, {
key: "resolveSkin",
value: function resolveSkin(skin, index) {
skin.id = "skin-".concat(index);
skin.inverseBindMatrices = this.getAccessor(skin.inverseBindMatrices);
}
}, {
key: "resolveMesh",
value: function resolveMesh(mesh, index) {
mesh.id = "mesh-".concat(index);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = mesh.primitives[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var primitive = _step.value;
for (var attribute in primitive.attributes) {
primitive.attributes[attribute] = this.getAccessor(primitive.attributes[attribute]);
}
if (primitive.indices) {
primitive.indices = this.getAccessor(primitive.indices);
}
if (primitive.material) {
primitive.material = this.getMaterial(primitive.material);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
key: "resolveMaterial",
value: function resolveMaterial(material, index) {
material.id = "material-".concat(index);
if (material.normalTexture) {
this.normalTexture = this.getTexture(material.normalTexture);
}
if (material.occlusionTexture) {
this.occlusionTexture = this.getTexture(material.occlusionTexture);
}
if (material.emissiveTexture) {
this.emissiveTexture = this.getTexture(material.emissiveTexture);
}
if (material.pbrMetallicRoughness) {
var mr = material.pbrMetallicRoughness;
if (mr.baseColorTexture) {
mr.baseColorTexture = this.getTexture(mr.baseColorTexture);
}
if (mr.metallicRoughnessTexture) {
mr.metallicRoughnessTexture = this.getTexture(mr.metallicRoughnessTexture);
}
}
}
}, {
key: "resolveAccessor",
value: function resolveAccessor(accessor, index) {
accessor.id = "accessor-".concat(index);
accessor.bufferView = this.getBufferView(accessor.bufferView); // Look up enums
accessor.bytesPerComponent = this.enumAccessorBytes(accessor);
accessor.components = this.enumAccessorType(accessor);
accessor.bytesPerElement = accessor.bytesPerComponent * accessor.components;
}
}, {
key: "resolveTexture",
value: function resolveTexture(texture, index) {
texture.id = "texture-".concat(index);
texture.sampler = this.getSampler(texture.sampler);
texture.source = this.getImage(texture.source);
}
}, {
key: "resolveSampler",
value: function resolveSampler(sampler, index) {
sampler.id = "sampler-".concat(index); // Map textual parameters to GL parameter values
this.parameters = {};
for (var key in sampler) {
var glEnum = this.enumSamplerParameter(sampler[key]);
this.parameters[glEnum] = sampler[key];
}
}
}, {
key: "resolveImage",
value: function resolveImage(image, index) {
image.id = "image-".concat(index);
if (image.bufferView) {
image.bufferView = this.getBufferView(image.bufferView);
} // TODO - Handle URIs etc
}
}, {
key: "resolveBufferView",
value: function resolveBufferView(bufferView, index) {
bufferView.id = "bufferView-".concat(index);
bufferView.buffer = this.getBuffer(bufferView.buffer);
} // PREPROC
}, {
key: "resolveCamera",
value: function resolveCamera(camera) {
// TODO - resolve step should not create
if (camera.perspective) {
camera.matrix = this.config.createPerspectiveMatrix(camera.perspective);
}
if (camera.orthographic) {
camera.matrix = this.config.createOrthographicMatrix(camera.orthographic);
}
} // ENUM LOOKUP
}, {
key: "enumAccessorBytes",
value: function enumAccessorBytes(componentType) {
var BYTES = {
5120: 1,
// BYTE
5121: 1,
// UNSIGNED_BYTE
5122: 2,
// SHORT
5123: 2,
// UNSIGNED_SHORT
5125: 4,
// UNSIGNED_INT
5126: 4 // FLOAT
};
return BYTES[componentType];
}
}, {
key: "enumAccessorType",
value: function enumAccessorType(type) {
var COMPONENTS = {
SCALAR: 1,
VEC2: 2,
VEC3: 3,
VEC4: 4,
MAT2: 4,
MAT3: 9,
MAT4: 16
};
return COMPONENTS[type];
}
}, {
key: "enumSamplerParameter",
value: function enumSamplerParameter(parameter) {
var GL_TEXTURE_MAG_FILTER = 0x2800;
var GL_TEXTURE_MIN_FILTER = 0x2801;
var GL_TEXTURE_WRAP_S = 0x2802;
var GL_TEXTURE_WRAP_T = 0x2803;
var PARAMETER_MAP = {
magFilter: GL_TEXTURE_MAG_FILTER,
minFilter: GL_TEXTURE_MIN_FILTER,
wrapS: GL_TEXTURE_WRAP_S,
wrapT: GL_TEXTURE_WRAP_T
};
return PARAMETER_MAP[parameter];
}
}]);
return GLTFParser;
}();
export { GLTFParser as default };
//# sourceMappingURL=gltf-parser.js.map