"use strict";exports.__esModule=!0,exports.createDTModel=createDTModel;var _three=require("three"),_util=require("../common/util"),_tool=require("./tool");function createDTModel(a,b,c,d=[]){function e(){let a=new _three.Vector3,d=new _three.Quaternion,e=new _three.Vector3;if(b instanceof _three.InstancedMesh){let f=new _three.Matrix4;b.getMatrixAt(c,f),f.decompose(a,d,e)}else b.updateMatrix(),b.matrix.decompose(a,d,e);return{position:a,scale:e,quaternion:d}}function f(a,d,e){if(b instanceof _three.InstancedMesh){let f=new _three.Matrix4;f.compose(a,d,e),b.setMatrixAt(c,f),b.instanceMatrix.needsUpdate=!0,b.computeBoundingSphere(),b.computeBoundingBox()}else b.position.copy(a),b.quaternion.copy(d),b.scale.copy(e),b.updateMatrixWorld(!0)}function g(){let a=new _three.Matrix4;return b instanceof _three.InstancedMesh?(b.getMatrixAt(c,a),b.parent&&(b.parent.updateMatrixWorld(!0),a.premultiply(b.parent.matrixWorld))):(b.updateMatrixWorld(!0),a.copy(b.matrixWorld)),a}function h(a,c){let e=!1;c||(c=new Set,e=!0);for(let e of d){let d=e.target,f=!1,g=d.parent;for(;g;){if(g===b){f=!0;break}g=g.parent}if(!f)if(d instanceof _three.InstancedMesh&&void 0!==e.instancedId){let b=new _three.Matrix4;d.getMatrixAt(e.instancedId,b),b.premultiply(a),d.setMatrixAt(e.instancedId,b),d.instanceMatrix.needsUpdate=!0,c.add(d)}else d.applyMatrix4(a),d.updateMatrixWorld(!0);"function"==typeof e._applyDelta&&e._applyDelta(a,c)}e&&c.forEach(a=>{a.computeBoundingSphere&&a.computeBoundingSphere(),a.computeBoundingBox&&a.computeBoundingBox()})}let i=(0,_tool.createColorHandler)(),j="",k=(0,_util.extend)({get target(){return b},get modelId(){return a.modelId||b.uuid},get isInstancedMesh(){return b instanceof _three.InstancedMesh},get instancedId(){return c},get url(){return a.url||""},_applyDelta(a,b){h(a,b)},get position(){let{position:a}=e();return{x:a.x,y:a.y,z:a.z}},set position(a){let b=g(),{position:c,quaternion:i,scale:j}=e();if(c.set(a.x,a.y,a.z),f(c,i,j),0<d.length){let a=g(),c=a.clone().multiply(b.invert());h(c)}},get rotation(){let{quaternion:a}=e(),b=new _three.Euler;return b.setFromQuaternion(a.normalize()),{x:b.x,y:b.y,z:b.z,order:b.order}},set rotation(a){let b=g(),c=new _three.Euler(a.x,a.y,a.z,a.order),{position:i,quaternion:j,scale:k}=e();if(j.setFromEuler(c),f(i,j,k),0<d.length){let a=g(),c=a.clone().multiply(b.invert());h(c)}},get color(){return j},set color(a){let[e,f=1]=a.split(" ");b instanceof _three.InstancedMesh?(b.setColorAt(c,new _three.Color(e)),b.instanceColor&&(b.instanceColor.needsUpdate=!0)):e?i.drawColor([b],{color:e,opacity:+f}):i.clear([b]);for(let b of d)b.color=a;j=a},set opacity(a){if(b instanceof _three.InstancedMesh);else{const c=new Map;b.traverse(b=>{if(b.isMesh&&b.material){const d=Array.isArray(b.material),e=d?b.material:[b.material];let f=!1;const g=e.map(b=>{if(b.userData&&b.userData.isColorMaterial)return b;let d=b;if(!d.userData.__isOpacityCloned)if(c.has(d))d=c.get(d),f=!0;else{let a=d;d=d.clone(),d.userData.__isOpacityCloned=!0,c.set(a,d),f=!0}return d.transparent=!0,d.opacity=a,d.needsUpdate=!0,d});f&&(b.material=d?g:g[0])}})}for(let b of d)b.opacity=a},dispose:async function(){for(let a of d)await a.dispose();b.removeFromParent(),b.traverse(a=>{a.geometry&&a.geometry.dispose(),a.material&&(0,_util.toArray)(a.material||[]).forEach(a=>{a.dispose()})})},removeFromParent:async function(){for(let a of d)await a.removeFromParent();b.removeFromParent()},add:async function(a){b.add(a.target||a)},attach:async function(a){b.attach(a.target||a)},remove:async function(a){a.removeFromParent()},replace:a=>{let c=b.parent;c&&(a.position.copy(b.position),a.quaternion.copy(b.quaternion),a.scale.copy(b.scale),c.add(a.target||a),b.removeFromParent(),b=a)}},b);return k}