UNPKG

@shopware-ag/dive

Version:

Shopware Spatial Framework

4 lines (3 loc) 10.3 kB
"use strict";var G=Object.create;var C=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var q=(c,t,n)=>t in c?C(c,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):c[t]=n;var H=(c,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of v(t))!j.call(c,s)&&s!==n&&C(c,s,{get:()=>t[s],enumerable:!(r=B(t,s))||r.enumerable});return c};var I=(c,t,n)=>(n=c!=null?G(W(c)):{},H(t||!c||!c.__esModule?C(n,"default",{value:c,enumerable:!0}):n,c));var p=(c,t,n)=>q(c,typeof t!="symbol"?t+"":t,n);const z=require("three/examples/jsm/loaders/GLTFLoader.js"),Y=require("three/examples/jsm/loaders/USDLoader.js"),J=require("./FileTypes-Ba65iQSJ.cjs"),w=require("three/webgpu");require("three/examples/jsm/loaders/HDRLoader.js");require("three/tsl");const U=require("./isFileTypeSupported-Dx01kdxM.cjs"),R=require("./parse-error-Dy_EE4rL.cjs"),$=require("three/examples/jsm/loaders/DRACOLoader.js"),T=require("./AssetCache-5FgjqaKg.cjs");function N(){let c,t;onmessage=function(e){const o=e.data;switch(o.type){case"init":c=o.decoderConfig,t=new Promise(function(i){c.onModuleLoaded=function(_){i({draco:_})},DracoDecoderModule(c)});break;case"decode":const d=o.buffer,l=o.taskConfig;t.then(i=>{const _=i.draco,a=new _.Decoder;try{const u=n(_,a,new Int8Array(d),l),h=u.attributes.map(g=>g.array.buffer);u.index&&h.push(u.index.array.buffer),self.postMessage({type:"decode",id:o.id,geometry:u},h)}catch(u){console.error(u),self.postMessage({type:"error",id:o.id,error:u.message})}finally{_.destroy(a)}});break}};function n(e,o,d,l){const i=l.attributeIDs,_=l.attributeTypes;let a,u;const h=o.GetEncodedGeometryType(d);if(h===e.TRIANGULAR_MESH)a=new e.Mesh,u=o.DecodeArrayToMesh(d,d.byteLength,a);else if(h===e.POINT_CLOUD)a=new e.PointCloud,u=o.DecodeArrayToPointCloud(d,d.byteLength,a);else throw new Error("THREE.DRACOLoader: Unexpected geometry type.");if(!u.ok()||a.ptr===0)throw new Error("THREE.DRACOLoader: Decoding failed: "+u.error_msg());const g={index:null,attributes:[]};for(const A in i){const D=self[_[A]];let y,m;if(l.useUniqueIDs)m=i[A],y=o.GetAttributeByUniqueId(a,m);else{if(m=o.GetAttributeId(a,e[i[A]]),m===-1)continue;y=o.GetAttribute(a,m)}const E=s(e,o,a,A,D,y);A==="color"&&(E.vertexColorSpace=l.vertexColorSpace),g.attributes.push(E)}return h===e.TRIANGULAR_MESH&&(g.index=r(e,o,a)),e.destroy(a),g}function r(e,o,d){const i=d.num_faces()*3,_=i*4,a=e._malloc(_);o.GetTrianglesUInt32Array(d,_,a);const u=new Uint32Array(e.HEAPF32.buffer,a,i).slice();return e._free(a),{array:u,itemSize:1}}function s(e,o,d,l,i,_){const a=d.num_points(),u=_.num_components(),h=f(e,i),g=u*i.BYTES_PER_ELEMENT,A=Math.ceil(g/4)*4,D=A/i.BYTES_PER_ELEMENT,y=a*g,m=a*A,E=e._malloc(y);o.GetAttributeDataArrayForAllPoints(d,_,h,y,E);const O=new i(e.HEAPF32.buffer,E,y/i.BYTES_PER_ELEMENT);let L;if(g===A)L=O.slice();else{L=new i(m/i.BYTES_PER_ELEMENT);let M=0;for(let b=0,x=O.length;b<x;b+=u){for(let S=0;S<u;S++)L[M+S]=O[b+S];M+=D}}return e._free(E),{name:l,count:a,itemSize:u,array:L,stride:D}}function f(e,o){switch(o){case Float32Array:return e.DT_FLOAT32;case Int8Array:return e.DT_INT8;case Int16Array:return e.DT_INT16;case Int32Array:return e.DT_INT32;case Uint8Array:return e.DT_UINT8;case Uint16Array:return e.DT_UINT16;case Uint32Array:return e.DT_UINT32}}}const P={LOAD_DRACO_JS_DECODER:async()=>(await import("three/examples/jsm/libs/draco/draco_decoder.js?raw")).default,LOAD_DRACO_WASM_WRAPPER:async()=>(await import("three/examples/jsm/libs/draco/draco_wasm_wrapper.js?raw")).default,LOAD_DRACO_WASM_DECODER:async()=>{const c=(await import("three/examples/jsm/libs/draco/draco_decoder.wasm?url")).default;return await(await fetch(c)).arrayBuffer()}};class k extends $.DRACOLoader{constructor(){super(...arguments);p(this,"decoderPending",null);p(this,"decoderConfig",{type:"js",wasmBinary:null});p(this,"workerSourceURL","")}async _initDecoder(){if(this.decoderPending)return this.decoderPending;const n=typeof WebAssembly!="object"||this.decoderConfig.type==="js",r=[];return n?r.push(P.LOAD_DRACO_JS_DECODER()):(r.push(P.LOAD_DRACO_WASM_WRAPPER()),r.push(P.LOAD_DRACO_WASM_DECODER())),this.decoderPending=Promise.all(r).then(s=>{const f=s[0];n||(this.decoderConfig.wasmBinary=s[1]);const e=N.toString(),o=["/* draco decoder */",f,"","/* worker */",e.substring(e.indexOf("{")+1,e.lastIndexOf("}"))].join(` `);this.workerSourceURL=URL.createObjectURL(new Blob([o]))}),this.decoderPending}}function V(){let c=null,t=null;const n=[[/'CONFIG_CONTROL_DESIGN'\s*\)/g,"'AP203_CONFIGURATION_CONTROLLED_3D_DESIGN_OF_MECHANICAL_PARTS_AND_ASSEMBLIES_MIM_LF')"],[/'AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF\.\s*\{[\s\S]*?\}\s*'/g,"'AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF'"]];function r(){return c||(c=occtimportjs({locateFile:function(){return t}})),c}function s(e){for(var o=new TextDecoder("utf-8",{fatal:!1}),d=new TextEncoder,l=o.decode(e),i=0;i<n.length;i++){var _=n[i][0],a=n[i][1];l=l.replace(_,a)}return new Uint8Array(d.encode(l))}function f(e,o){var d=s(o);try{var l=e.ReadStepFile(d,null);if(l.success&&l.root)return l}catch{}try{return e.ReadStepFile(o,null)}catch{return{success:!1,root:{meshes:[],children:[]},meshes:[]}}}onmessage=async function(e){var o=e.data;switch(o.type){case"init":t=o.wasmUrl;break;case"parse":try{var d=await r(),l=new Uint8Array(o.buffer),i=o.fileType,_;if(i==="step"||i==="stp"?_=f(d,l):_=d.ReadIgesFile(l,null),!_.success||!_.root){self.postMessage({type:"error",id:o.id,error:"Failed to parse CAD file"});return}self.postMessage({type:"result",id:o.id,result:_})}catch(a){self.postMessage({type:"error",id:o.id,error:a instanceof Error?a.message:String(a)})}break}}}const F={LOAD_OCCT_JS:async()=>(await Promise.resolve().then(()=>require("./occt-import-js-DdbbsFBH.cjs"))).default,LOAD_OCCT_WASM_URL:async()=>(await Promise.resolve().then(()=>require("./occt-import-js-BQ1yZHCj.cjs"))).default};class Z{constructor(){p(this,"_workerPending",null);p(this,"_worker",null);p(this,"_nextId",0);p(this,"_pending",new Map)}_getWorker(){return this._workerPending?this._workerPending:(this._workerPending=(async()=>{const[t,n]=await Promise.all([F.LOAD_OCCT_JS(),F.LOAD_OCCT_WASM_URL()]),r=V.toString(),s=["/* occt-import-js */",t,"","/* step worker */",r.substring(r.indexOf("{")+1,r.lastIndexOf("}"))].join(` `),f=URL.createObjectURL(new Blob([s])),e=new Worker(f),o=new URL(n,window.location.href).href;return e.postMessage({type:"init",wasmUrl:o}),e.onmessage=d=>{const{type:l,id:i,result:_,error:a}=d.data,u=this._pending.get(i);if(u)if(this._pending.delete(i),l==="result")try{u.resolve(this._buildScene(_))}catch(h){u.reject(h instanceof Error?h:new Error(String(h)))}else u.reject(new Error(a??"Worker error"))},e.onerror=d=>{for(const[,{reject:l}]of this._pending)l(new Error(d.message??"Worker error"));this._pending.clear()},this._worker=e,e})(),this._workerPending)}async parseAsync(t,n){const r=this._nextId++,s=await this._getWorker();return new Promise((f,e)=>{this._pending.set(r,{resolve:f,reject:e});const o=t.slice(0);s.postMessage({type:"parse",id:r,buffer:o,fileType:n},[o])})}dispose(){if(this._worker){for(const[,{reject:t}]of this._pending)t(new Error("STEPLoader disposed"));this._pending.clear(),this._worker.terminate(),this._worker=null,this._workerPending=null}}_buildScene(t){const n=new w.Group;return n.name=t.root.name??"CAD Model",this._buildNode(t.root,t.meshes,n),n.rotation.x=-Math.PI/2,n.rotation.z=Math.PI/2,n}_buildNode(t,n,r){const s=new w.Group;s.name=t.name??"Part";for(const f of t.meshes){const e=n[f];if(e!=null&&e.attributes){const o=this._createMesh(e);s.add(o)}}for(const f of t.children)this._buildNode(f,n,s);r.add(s)}_createMesh(t){var l,i;const{position:n,normal:r}=t.attributes,s=t.index??t.attributes.index,f=new w.BufferGeometry;f.setAttribute("position",new w.BufferAttribute(new Float32Array(n.array),3)),(l=r==null?void 0:r.array)!=null&&l.length?f.setAttribute("normal",new w.BufferAttribute(new Float32Array(r.array),3)):f.computeVertexNormals(),(i=s==null?void 0:s.array)!=null&&i.length&&f.setIndex(new w.BufferAttribute(new Uint32Array(s.array),1));const e=t.color,o=new w.MeshStandardMaterial({metalness:.3,roughness:.6});e&&e.length>=3?o.color.setRGB(e[0],e[1],e[2]):o.color.setHex(13421772);const d=new w.Mesh(f,o);return d.name=t.name??"Mesh",d.castShadow=!0,d.receiveShadow=!0,d}}class K{constructor(){p(this,"_gltfLoader");p(this,"_usdLoader");p(this,"_stepLoader");const t=new k;t.setDecoderConfig({type:"wasm"}),this._gltfLoader=new z.GLTFLoader,this._gltfLoader.setDRACOLoader(t),this._usdLoader=new Y.USDLoader,this._stepLoader=new Z}async load(t,n){let r;if(n)r=n;else if(r=U.getFileTypeFromUri(t),r.length===0&&(r=await this._detectFileTypeFromContent(t),r.length===0))throw new R.FileTypeError("No file extension found in URI","");if(!U.isFileTypeSupported(r))throw new R.FileTypeError(`Unsupported file type: ${r}. Supported types: ${J.SUPPORTED_FILE_TYPES.join(", ")}`,r);const s=T.AssetCache.read(t);if(s)return s.arrayBuffer?this._parse(s.arrayBuffer,r):s.promise.then(o=>this._parse(o,r));const e=await T.AssetCache.create(t).load();return this._parse(e,r)}async _detectFileTypeFromContent(t){try{const n=T.AssetCache.read(t);let r;n?n.arrayBuffer?r=n.arrayBuffer:r=await n.promise:r=await T.AssetCache.create(t).load();const s=new Uint8Array(r);return s.length>=12&&String.fromCharCode(s[0],s[1],s[2],s[3])==="glTF"?s[12]===0?"glb":"gltf":s.length>=4&&s[0]===80&&s[1]===75&&(s[2]===3||s[2]===5)&&(s[3]===4||s[3]===6)?"usdz":s.length>=20&&new TextDecoder("ascii").decode(s.subarray(0,20)).startsWith("ISO-10303-21")?"step":s.length>=80&&s[72]===83?"iges":""}catch{return""}}async _parse(t,n){try{switch(n){case"glb":case"gltf":{const r=await this._gltfLoader.parseAsync(t,"");return r.scene.animations=r.animations,r.scene}case"usdz":{const r=this._usdLoader.parse(t);return r.animations=[],r}case"step":case"stp":{const r=await this._stepLoader.parseAsync(t,n);return r.animations=[],r}case"iges":case"igs":{const r=await this._stepLoader.parseAsync(t,n);return r.animations=[],r}}}catch(r){throw r instanceof Error?new R.ParseError(`Failed to parse ${n} file: ${r.message}`,r):new R.ParseError(`Failed to parse ${n} file`)}}}exports.AssetLoader=K;exports.DRACOWorker=N;exports.DracoLoader=k;