import { AnimationMixer } from "three";

class GltfObject {
    constructor(engine, obj) {
        return new Promise(async(resolve, reject)=>{
            let gltf = await engine.load(obj.value, obj.path);
            let gltfObj = gltf.scene;
            gltfObj.traverse(function (object) {
                object.frustumCulled = false;
                if (obj.name && obj.name == object.name) {
                    gltfObj = object;
                }
            });
                
            engine.meshUtils.assignMaterial(gltfObj, obj);
            if (gltf.animations && gltf.animations.length) {
                let mixer = new AnimationMixer(gltfObj);
                engine.mixers.push(mixer);
                let action = mixer.clipAction(gltf.animations[0]);
                action.setLoop(LoopPingPong);
                action.play();
            }
            this.object = gltfObj;
            this.source = gltf;
            resolve(this)
        })
    }
}

export { GltfObject }