cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
2 lines (1 loc) • 6.25 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var e=require("../../../build/three.module.js");const t=new WeakMap;class r extends e.Loader{constructor(e){super(e),this.decoderPath="",this.decoderConfig={},this.decoderBinary=null,this.decoderPending=null,this.workerLimit=4,this.workerPool=[],this.workerNextTaskID=1,this.workerSourceURL="",this.defaultAttributeIDs={position:"POSITION",normal:"NORMAL",color:"COLOR",uv:"TEX_COORD"},this.defaultAttributeTypes={position:"Float32Array",normal:"Float32Array",color:"Float32Array",uv:"Float32Array"}}setDecoderPath(e){return this.decoderPath=e,this}setDecoderConfig(e){return this.decoderConfig=e,this}setWorkerLimit(e){return this.workerLimit=e,this}load(t,r,o,s){const i=new e.FileLoader(this.manager);i.setPath(this.path),i.setResponseType("arraybuffer"),i.setRequestHeader(this.requestHeader),i.setWithCredentials(this.withCredentials),i.load(t,(e=>{this.parse(e,r,s)}),o,s)}parse(t,r,o){this.decodeDracoFile(t,r,null,null,e.SRGBColorSpace).catch(o)}decodeDracoFile(t,r,o,s,i=e.LinearSRGBColorSpace){const n={attributeIDs:o||this.defaultAttributeIDs,attributeTypes:s||this.defaultAttributeTypes,useUniqueIDs:!!o,vertexColorSpace:i};return this.decodeGeometry(t,n).then(r)}decodeGeometry(e,r){const o=JSON.stringify(r);if(t.has(e)){const r=t.get(e);if(r.key===o)return r.promise;if(0===e.byteLength)throw new Error("THREE.DRACOLoader: Unable to re-decode a buffer with different settings. Buffer has already been transferred.")}let s;const i=this.workerNextTaskID++,n=e.byteLength,a=this._getWorker(i,n).then((t=>(s=t,new Promise(((t,o)=>{s._callbacks[i]={resolve:t,reject:o},s.postMessage({type:"decode",id:i,taskConfig:r,buffer:e},[e])}))))).then((e=>this._createGeometry(e.geometry)));return a.catch((()=>!0)).then((()=>{s&&i&&this._releaseTask(s,i)})),t.set(e,{key:o,promise:a}),a}_createGeometry(t){const r=new e.BufferGeometry;t.index&&r.setIndex(new e.BufferAttribute(t.index.array,1));for(let o=0;o<t.attributes.length;o++){const s=t.attributes[o],i=s.name,n=s.array,a=s.itemSize,c=new e.BufferAttribute(n,a);"color"===i&&this._assignVertexColorSpace(c,s.vertexColorSpace),r.setAttribute(i,c)}return r}_assignVertexColorSpace(t,r){if(r!==e.SRGBColorSpace)return;const o=new e.Color;for(let e=0,s=t.count;e<s;e++)o.fromBufferAttribute(t,e).convertSRGBToLinear(),t.setXYZ(e,o.r,o.g,o.b)}_loadLibrary(t,r){const o=new e.FileLoader(this.manager);return o.setPath(this.decoderPath),o.setResponseType(r),o.setWithCredentials(this.withCredentials),new Promise(((e,r)=>{o.load(t,e,void 0,r)}))}preload(){return this._initDecoder(),this}_initDecoder(){if(this.decoderPending)return this.decoderPending;const e="object"!=typeof WebAssembly||"js"===this.decoderConfig.type,t=[];return e?t.push(this._loadLibrary("draco_decoder.js","text")):(t.push(this._loadLibrary("draco_wasm_wrapper.js","text")),t.push(this._loadLibrary("draco_decoder.wasm","arraybuffer"))),this.decoderPending=Promise.all(t).then((t=>{const r=t[0];e||(this.decoderConfig.wasmBinary=t[1]);const s=o.toString(),i=["/* draco decoder */",r,"","/* worker */",s.substring(s.indexOf("{")+1,s.lastIndexOf("}"))].join("\n");this.workerSourceURL=URL.createObjectURL(new Blob([i]))})),this.decoderPending}_getWorker(e,t){return this._initDecoder().then((()=>{if(this.workerPool.length<this.workerLimit){const e=new Worker(this.workerSourceURL);e._callbacks={},e._taskCosts={},e._taskLoad=0,e.postMessage({type:"init",decoderConfig:this.decoderConfig}),e.onmessage=function(t){const r=t.data;switch(r.type){case"decode":e._callbacks[r.id].resolve(r);break;case"error":e._callbacks[r.id].reject(r)}},this.workerPool.push(e)}else this.workerPool.sort((function(e,t){return e._taskLoad>t._taskLoad?-1:1}));const r=this.workerPool[this.workerPool.length-1];return r._taskCosts[e]=t,r._taskLoad+=t,r}))}_releaseTask(e,t){e._taskLoad-=e._taskCosts[t],delete e._callbacks[t],delete e._taskCosts[t]}debug(){}dispose(){for(let e=0;e<this.workerPool.length;++e)this.workerPool[e].terminate();return this.workerPool.length=0,""!==this.workerSourceURL&&URL.revokeObjectURL(this.workerSourceURL),this}}function o(){let e,t;function r(e,t,r,o,s,i){const n=i.num_components(),a=r.num_points()*n,c=a*s.BYTES_PER_ELEMENT,d=function(e,t){switch(t){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}}(e,s),l=e._malloc(c);t.GetAttributeDataArrayForAllPoints(r,i,d,c,l);const u=new s(e.HEAPF32.buffer,l,a).slice();return e._free(l),{name:o,array:u,itemSize:n}}onmessage=function(o){const s=o.data;switch(s.type){case"init":e=s.decoderConfig,t=new Promise((function(t){e.onModuleLoaded=function(e){t({draco:e})},DracoDecoderModule(e)}));break;case"decode":const o=s.buffer,i=s.taskConfig;t.then((e=>{const t=e.draco,n=new t.Decoder;try{const e=function(e,t,o,s){const i=s.attributeIDs,n=s.attributeTypes;let a,c;const d=t.GetEncodedGeometryType(o);if(d===e.TRIANGULAR_MESH)a=new e.Mesh,c=t.DecodeArrayToMesh(o,o.byteLength,a);else{if(d!==e.POINT_CLOUD)throw new Error("THREE.DRACOLoader: Unexpected geometry type.");a=new e.PointCloud,c=t.DecodeArrayToPointCloud(o,o.byteLength,a)}if(!c.ok()||0===a.ptr)throw new Error("THREE.DRACOLoader: Decoding failed: "+c.error_msg());const l={index:null,attributes:[]};for(const u in i){const o=self[n[u]];let c,d;if(s.useUniqueIDs)d=i[u],c=t.GetAttributeByUniqueId(a,d);else{if(d=t.GetAttributeId(a,e[i[u]]),-1===d)continue;c=t.GetAttribute(a,d)}const h=r(e,t,a,u,o,c);"color"===u&&(h.vertexColorSpace=s.vertexColorSpace),l.attributes.push(h)}d===e.TRIANGULAR_MESH&&(l.index=function(e,t,r){const o=3*r.num_faces(),s=4*o,i=e._malloc(s);t.GetTrianglesUInt32Array(r,s,i);const n=new Uint32Array(e.HEAPF32.buffer,i,o).slice();return e._free(i),{array:n,itemSize:1}}(e,t,a));return e.destroy(a),l}(t,n,new Int8Array(o),i),a=e.attributes.map((e=>e.array.buffer));e.index&&a.push(e.index.array.buffer),self.postMessage({type:"decode",id:s.id,geometry:e},a)}catch(a){self.postMessage({type:"error",id:s.id,error:a.message})}finally{t.destroy(n)}}))}}}exports.DRACOLoader=r;