rl-loadout-lib
Version:
Load Rocket League assets into three.js
108 lines • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const three_1 = require("three");
const skeleton_1 = require("../utils/three/skeleton");
/**
* Abstract 3D model object, that loads gltf models.
*/
class AbstractObject {
constructor(assets) {
if (assets != undefined) {
this.validate(assets.gltf);
this.handleScene(assets.gltf.scene);
}
}
handleScene(scene) {
this.scene = scene;
traverseMaterials(this.scene, material => {
if (material.map) {
material.map.encoding = three_1.LinearEncoding;
}
if (material.emissiveMap) {
material.emissiveMap.encoding = three_1.LinearEncoding;
}
if (material.map || material.emissiveMap) {
material.needsUpdate = true;
}
});
scene.updateMatrixWorld(true);
this.handleModel(scene);
}
validate(gltf) {
if (!('KHR_draco_mesh_compression' in gltf.parser.extensions)) {
console.warn(`Model not DRACO compressed.`);
}
}
/**
* Add the model to a scene.
* @param scene THREE scene
*/
addToScene(scene) {
scene.add(this.scene);
}
/**
* Remove the model from a scene.
* @param scene THREE scene
*/
removeFromScene(scene) {
scene.remove(this.scene);
}
/**
* Set the environment map to all objects in this scene
* @param envMap environment map texture
*/
setEnvMap(envMap) {
this.scene.traverse(object => {
if (object['isMesh']) {
const mat = object.material;
mat.envMap = envMap;
mat.needsUpdate = true;
}
});
}
/**
* Set the position and rotation of the anchor to this object.
* @param anchor the anchor object
*/
applyAnchor(anchor) {
if (anchor == undefined) {
return;
}
this.scene.position.copy(anchor.position);
this.scene.rotation.copy(anchor.rotation);
}
/**
* Dispose of the object.
*/
dispose() {
this.scene.dispose();
}
/**
* Set the visibility of the object.
* @param visible visibility of the object
*/
visible(visible) {
this.scene.visible = visible;
}
copy(other) {
const scene = skeleton_1.SkeletonUtils.clone(other.scene);
this.handleScene(scene);
}
}
exports.AbstractObject = AbstractObject;
/**
* Executes the callback on this object's material and all descendants' materials.
* @param object Object3D object to traverse through
* @param callback this will be called on all the materials
*/
function traverseMaterials(object, callback) {
object.traverse((node) => {
if (!node.isMesh) {
return;
}
const materials = Array.isArray(node.material) ? node.material : [node.material];
materials.forEach(callback);
});
}
exports.traverseMaterials = traverseMaterials;
//# sourceMappingURL=object.js.map