"use strict";exports.__esModule=!0,exports.addVector3=addVector3,exports.convertUnityToThreeJSRotation=convertUnityToThreeJSRotation,exports.createColorHandler=createColorHandler,exports.getBoundingSphere=getBoundingSphere,exports.getCenterTopPosition=getCenterTopPosition,exports.getIntersects=void 0,exports.gltfToObject3D=gltfToObject3D,exports.mergeGeometries=mergeGeometries,exports.threeCoordinateToScreenPoint=threeCoordinateToScreenPoint;var _three=require("three"),BufferGeometryUtils=_interopRequireWildcard(require("three/examples/jsm/utils/BufferGeometryUtils")),_util=require("../common/util");function _getRequireWildcardCache(a){if("function"!=typeof WeakMap)return null;var b=new WeakMap,c=new WeakMap;return(_getRequireWildcardCache=function(a){return a?c:b})(a)}function _interopRequireWildcard(b,c){if(!c&&b&&b.__esModule)return b;if(null===b||"object"!=typeof b&&"function"!=typeof b)return{default:b};var d=_getRequireWildcardCache(c);if(d&&d.has(b))return d.get(b);var e={__proto__:null},f=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var a in b)if("default"!=a&&{}.hasOwnProperty.call(b,a)){var g=f?Object.getOwnPropertyDescriptor(b,a):null;g&&(g.get||g.set)?Object.defineProperty(e,a,g):e[a]=b[a]}return e.default=b,d&&d.set(b,e),e}const getIntersects=(a,b,c,d)=>{const e=new _three.Raycaster,f=new _three.Vector2;let g,h;Array.isArray(a)?(g=a[0],h=a[1]):(a.preventDefault(),g=a.clientX,h=a.clientY);let{left:i,top:j,width:k,height:l}=b.getBoundingClientRect();return f.x=2*((g-i)/k)-1,f.y=2*-((h-j)/l)+1,e.setFromCamera(f,c),e.intersectObjects(d)};exports.getIntersects=getIntersects;function standardizeColorAttribute(a){const b=a.attributes.color;if(b){const c=b.isBufferAttribute&&b.array instanceof Float32Array;if(4!==b.itemSize||!c){const c=new Float32Array(4*a.attributes.position.count),d=b.itemSize;for(let e=0;e<a.attributes.position.count;e++)e<b.count?(c[4*e]=b.getX(e),c[4*e+1]=b.getY(e),c[4*e+2]=b.getZ(e),c[4*e+3]=3<d?b.getW(e):1):(c[4*e]=1,c[4*e+1]=1,c[4*e+2]=1,c[4*e+3]=1);a.setAttribute("color",new _three.BufferAttribute(c,4))}}else{const b=new Float32Array(4*a.attributes.position.count).fill(1);a.setAttribute("color",new _three.BufferAttribute(b,4))}return a}function standardizeGeometryAttributes(a,b){const c=new Set(Object.keys(a.attributes));return c.has("position")?(b.forEach((b,d)=>{if(!c.has(d)){const c=a.attributes.position.count,e="color"===d?1:0,f=new Float32Array(c*b);f.fill(e),a.setAttribute(d,new _three.BufferAttribute(f,b))}}),c.forEach(c=>{b.has(c)||a.deleteAttribute(c)}),a):null}function mergeGeometries(a){try{const b=[],c=[],d=new Map;if(a.traverse(a=>{const b=a;b.isMesh&&b.geometry&&Object.keys(b.geometry.attributes).forEach(a=>{let c=b.geometry.attributes[a].itemSize;"color"===a&&(c=4),d.has(a)?d.set(a,Math.max(c,d.get(a))):d.set(a,c)})}),d.has("position")||d.set("position",3),d.has("normal")||d.set("normal",3),d.has("uv")||d.set("uv",2),d.set("color",4),a.traverse(a=>{const e=a;if(e.isMesh){let a=e.geometry.clone();if(a=standardizeGeometryAttributes(a,d),!a)return;e.isInstancedMesh?(a=standardizeColorAttribute(a),b.push(a),c.push(e.material)):(a.applyMatrix4(e.matrixWorld),a=standardizeColorAttribute(a),b.push(a),c.push(e.material))}}),0===b.length)return;const e=BufferGeometryUtils.mergeGeometries(b,!0),f=new _three.Mesh(e,c);return f}catch(a){}}const encodeName=a=>{a.forEach(a=>{a.material&&(0,_util.toArray)(a.material).forEach(a=>{let b=a.name||"";/[^\w]/g.test(b)&&(a.name=encodeURIComponent(b))}),a.children&&encodeName(a.children)})};function gltfToObject3D(a){let b;return b=a instanceof _three.Object3D?a:a.scene,b.children&&1==b.children.length&&b.children[0].position.set(0,0,0),encodeName([b]),a.animations.forEach(a=>{b.animations.push(a.clone())}),{model:b,clone:b}}function convertUnityToThreeJSRotation(a){const b=new _three.Euler;return b.set(-_three.MathUtils.degToRad(a.z),-_three.MathUtils.degToRad(a.y),_three.MathUtils.degToRad(a.x),"ZXY"),b}function createColorHandler(a={}){let b={color:a.color||"#FF0000",opacity:a.opacity||.5};const c=new _three.ShaderMaterial({transparent:!0,uniforms:{color:{value:new _three.Color(b.color||"#FF0000")},opacity:{value:b.opacity||.5}},vertexShader:`
      // varying vec3 vNormal;

      void main(){
        // vNormal = normalize(normalMatrix * normal);
        gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
      }
    `,fragmentShader:`
      uniform float opacity;
      uniform vec3 color;
      // varying vec3 vNormal;

      void main(){
        gl_FragColor = vec4(color.rgb, opacity);
        // gl_FragColor = vec4(color * vNormal, opacity);
      }
    `});c.userData.isColorMaterial=!0,c.needsUpdate=!0,c.polygonOffset=!0,c.polygonOffsetFactor=-2,c.polygonOffsetUnits=-2,c.side=_three.DoubleSide,c.dithering=!0,c.precision="highp";const d=a=>a.isMesh&&!a.isLine2,e=a=>{a.forEach(a=>{a.traverseVisible(a=>{if(d(a)){if(!Array.isArray(a.material))return;let b=[];a.geometry=a.geometry.clone(),a.material.forEach((c,d)=>{if(!c.userData.isColorMaterial)return void b.push(c);c.dispose();a.geometry.groups.forEach((b,c)=>{b.materialIndex===d?a.geometry.groups.splice(c,1):b.materialIndex>d&&(b.materialIndex-=1)})}),a.geometry.groups=a.geometry.groups.filter(a=>!a.color),a.material=b}})})};return{clear:e,drawColor:(a,b={})=>{let f=c;b.color&&(f.uniforms.color.value=new _three.Color(b.color)),b.opacity&&(f.uniforms.opacity.value=b.opacity),f.visible=!1!==b.visible,a.forEach(a=>{e([a]),a.traverseVisible(a=>{if(d(a)){for(Array.isArray(a.material)||(a.material=(0,_util.toArray)(a.material));a.material.length>a.geometry.groups.length;)a.geometry.groups.push({start:0,count:+Infinity,materialIndex:0});a.material=(0,_util.toArray)(a.material).concat([f]),a.geometry.groups&&(a.geometry.groups=a.geometry.groups.filter(a=>!a.color)),a.geometry.groups=[...a.geometry.groups,{start:0,count:+Infinity,materialIndex:a.material.length-1,color:!0}]}})})},dispose(){c.dispose()}}}function getBoundingSphere(a){const b=new _three.Box3().setFromObject(a),c=new _three.Sphere;return b.getBoundingSphere(c),c}function getCenterTopPosition(a){let b=new _three.Vector3,c=a.object||a.target||a;"function"==typeof c.updateWorldMatrix&&c.updateWorldMatrix(!0,!0);let d=new _three.Box3,e=a.isInstancedMesh||c.isInstancedMesh,f=void 0===a.instancedId?a.instanceId:a.instancedId;if(e&&void 0!==f){let a=new _three.Matrix4;c.getMatrixAt(f,a),a.premultiply(c.matrixWorld),null===c.geometry.boundingBox&&c.geometry.computeBoundingBox(),d.copy(c.geometry.boundingBox).applyMatrix4(a)}else d.setFromObject(c);return d.getCenter(b),b.y=d.max.y,b}function addVector3(a,b){return{x:(a.x||0)+(b.x||0),y:(a.y||0)+(b.y||0),z:(a.z||0)+(b.z||0)}}function threeCoordinateToScreenPoint(a,b,c){let d=new _three.Vector3(a.x,a.y,a.z),e=d.project(c),f=b,g=f.getBoundingClientRect(),h=(e.x+1)*f.offsetWidth/2+g.left,i=(-e.y+1)*f.offsetHeight/2+g.top;return{x:h,y:i}}