import{cq as U,t as K,G as I,fx as v,fw as q,fy as Q,a as g,a7 as D,E as M,s as ee,fM as Y,kO as te,fO as G,k_ as W,dU as se,ez as ie,k$ as re}from"./index.6c049565.js";import{K as ne,k as ae}from"./quat.4f976512.js";import{o as oe,r as he,e as ce}from"./quatf64.4ae3e6f1.js";import{c as fe}from"./meshFeatureSet.e448c389.js";import{i as ue}from"./georeference.11d1c93a.js";import{C as d,D as y}from"./enums.2d9e6f64.js";import{o as k,i as le,g as de,s as _e}from"./imageutils.2ce8d042.js";import"./mat3f64.6d32a1d7.js";import"./imageUtils.753f41b1.js";import"./earcut.afc1d357.js";import"./deduplicate.b87c02b8.js";import"./mat4f64.ff2a477c.js";import"./BufferView.f5101c27.js";import"./vec33.9bd40b84.js";var C;(function(i){i[i.JSON=1313821514]="JSON",i[i.BIN=5130562]="BIN"})(C||(C={}));class T{constructor(e,t){if(!e)throw new Error("GLB requires a JSON gltf chunk");this._length=T.HEADER_SIZE,this._length+=T.CHUNK_HEADER_SIZE;const s=this._textToArrayBuffer(e);if(this._length+=this._alignTo(s.byteLength,4),t&&(this._length+=T.CHUNK_HEADER_SIZE,this._length+=t.byteLength,t.byteLength%4))throw new Error("Expected BIN chunk length to be divisible by 4 at this point");this.buffer=new ArrayBuffer(this._length),this._outView=new DataView(this.buffer),this._writeHeader();const r=this._writeChunk(s,12,C.JSON,32);t&&this._writeChunk(t,r,C.BIN)}_writeHeader(){this._outView.setUint32(0,T.MAGIC,!0),this._outView.setUint32(4,T.VERSION,!0),this._outView.setUint32(8,this._length,!0)}_writeChunk(e,t,s,r=0){const n=this._alignTo(e.byteLength,4);for(this._outView.setUint32(t,n,!0),this._outView.setUint32(t+=4,s,!0),this._writeArrayBuffer(this._outView.buffer,e,t+=4,0,e.byteLength),t+=e.byteLength;t%4;)r&&this._outView.setUint8(t,r),t++;return t}_writeArrayBuffer(e,t,s,r,n){new Uint8Array(e,s,n).set(new Uint8Array(t,r,n),0)}_textToArrayBuffer(e){return new TextEncoder().encode(e).buffer}_alignTo(e,t){return t*Math.ceil(e/t)}}T.HEADER_SIZE=12,T.CHUNK_HEADER_SIZE=8,T.MAGIC=1179937895,T.VERSION=2;var R,b,E,p,H,L,Z;(function(i){i[i.External=0]="External",i[i.DataURI=1]="DataURI",i[i.GLB=2]="GLB"})(R||(R={})),function(i){i[i.External=0]="External",i[i.DataURI=1]="DataURI",i[i.GLB=2]="GLB"}(b||(b={})),function(i){i[i.ARRAY_BUFFER=34962]="ARRAY_BUFFER",i[i.ELEMENT_ARRAY_BUFFER=34963]="ELEMENT_ARRAY_BUFFER"}(E||(E={})),function(i){i.SCALAR="SCALAR",i.VEC2="VEC2",i.VEC3="VEC3",i.VEC4="VEC4",i.MAT2="MAT2",i.MAT3="MAT3",i.MAT4="MAT4"}(p||(p={})),function(i){i[i.POINTS=0]="POINTS",i[i.LINES=1]="LINES",i[i.LINE_LOOP=2]="LINE_LOOP",i[i.LINE_STRIP=3]="LINE_STRIP",i[i.TRIANGLES=4]="TRIANGLES",i[i.TRIANGLE_STRIP=5]="TRIANGLE_STRIP",i[i.TRIANGLE_FAN=6]="TRIANGLE_FAN"}(H||(H={})),function(i){i.OPAQUE="OPAQUE",i.MASK="MASK",i.BLEND="BLEND"}(L||(L={})),function(i){i[i.NoColor=0]="NoColor",i[i.FaceColor=1]="FaceColor",i[i.VertexColor=2]="VertexColor"}(Z||(Z={}));class pe{constructor(e,t,s,r,n){this._buffer=e,this._componentType=s,this._dataType=r,this._data=[],this._isFinalized=!1,this._accessorIndex=-1,this._accessorAttribute=null,this._accessorMin=null,this._accessorMax=null,t.bufferViews||(t.bufferViews=[]),this.index=t.bufferViews.length,this._bufferView={buffer:e.index,byteLength:-1,target:n};const a=this._getElementSize();a>=4&&n!==E.ELEMENT_ARRAY_BUFFER&&(this._bufferView.byteStride=a),t.bufferViews.push(this._bufferView),this._numComponentsForDataType=this._calculateNumComponentsForDataType()}push(e){const t=this._data.length;if(this._data.push(e),this._accessorIndex>=0){const s=t%this._numComponentsForDataType,r=this._accessorMin[s];this._accessorMin[s]=typeof r!="number"?e:Math.min(r,e);const n=this._accessorMax[s];this._accessorMax[s]=typeof n!="number"?e:Math.max(n,e)}}get dataSize(){return this._data.length*this._sizeComponentType()}get byteSize(){function e(t,s){return s*Math.ceil(t/s)}return e(this.dataSize,4)}getByteOffset(){if(!this._isFinalized)throw new Error("Cannot get BufferView offset until it is finalized");return this._buffer.getByteOffset(this)}get byteOffset(){if(!this._isFinalized)throw new Error("Cannot get BufferView offset until it is finalized");return this._buffer.getByteOffset(this)}_createTypedArray(e,t){switch(this._componentType){case d.BYTE:return new Int8Array(e,t);case d.FLOAT:return new Float32Array(e,t);case d.SHORT:return new Int16Array(e,t);case d.UNSIGNED_BYTE:return new Uint8Array(e,t);case d.UNSIGNED_INT:return new Uint32Array(e,t);case d.UNSIGNED_SHORT:return new Uint16Array(e,t)}}writeOutToBuffer(e,t){this._createTypedArray(e,t).set(this._data)}writeAsync(e){if(this._asyncWritePromise)throw new Error("Can't write multiple bufferView values asynchronously");return this._asyncWritePromise=e.then(t=>{const s=new Uint8Array(t);for(let r=0;r=0)throw new Error("Accessor was started without ending the previous one");this._accessorIndex=this._data.length,this._accessorAttribute=e;const t=this._numComponentsForDataType;this._accessorMin=new Array(t),this._accessorMax=new Array(t)}endAccessor(){if(this._accessorIndex<0)throw new Error("An accessor was not started, but was attempted to be ended");const e=this._getElementSize(),t=this._numComponentsForDataType,s=(this._data.length-this._accessorIndex)/t;if(s%1)throw new Error("An accessor was ended with missing component values");for(let n=0;nthis._finalizedPromiseResolve=e)}finalize(){const e=this._bufferView;return new Promise(t=>{const s=this._buffer.getViewFinalizePromises(this);this._asyncWritePromise&&s.push(this._asyncWritePromise),t(U(s))}).then(()=>{this._isFinalized=!0,e.byteOffset=this.getByteOffset(),e.byteLength=this.dataSize,this._finalizedPromiseResolve&&this._finalizedPromiseResolve()})}_getElementSize(){return this._sizeComponentType()*this._numComponentsForDataType}_sizeComponentType(){switch(this._componentType){case d.BYTE:case d.UNSIGNED_BYTE:return 1;case d.SHORT:case d.UNSIGNED_SHORT:return 2;case d.UNSIGNED_INT:case d.FLOAT:return 4}}_calculateNumComponentsForDataType(){switch(this._dataType){case p.SCALAR:return 1;case p.VEC2:return 2;case p.VEC3:return 3;case p.VEC4:case p.MAT2:return 4;case p.MAT3:return 9;case p.MAT4:return 16}}}class J{constructor(e){this._gltf=e,this._bufferViews=[],this._isFinalized=!1,e.buffers||(e.buffers=[]),this.index=e.buffers.length;const t={byteLength:-1};e.buffers.push(t),this._buffer=t}addBufferView(e,t,s){if(this._finalizePromise)throw new Error("Cannot add buffer view after fiinalizing buffer");const r=new pe(this,this._gltf,e,t,s);return this._bufferViews.push(r),r}getByteOffset(e){let t=0;for(const s of this._bufferViews){if(s===e)return t;t+=s.byteSize}throw new Error("Given bufferView was not present in this buffer")}getViewFinalizePromises(e){const t=[];for(const s of this._bufferViews){if(e&&s===e)return t;t.push(s.finalized)}return t}getArrayBuffer(){if(!this._isFinalized)throw new Error("Cannot get ArrayBuffer from Buffer before it is finalized");const e=this._getTotalSize(),t=new ArrayBuffer(e);let s=0;for(const r of this._bufferViews)r.writeOutToBuffer(t,s),s+=r.byteSize;return t}finalize(){var e;if(this._finalizePromise)throw new Error(`Buffer ${this.index} was already finalized`);return this._finalizePromise=new Promise(t=>{t(U(this.getViewFinalizePromises()))}).then(()=>{this._isFinalized=!0;const t=this.getArrayBuffer();this._buffer.byteLength=t.byteLength,this._buffer.uri=t}),(e=this._gltf.extras)==null||e.promises.push(this._finalizePromise),this._finalizePromise}_getTotalSize(){let e=0;for(const t of this._bufferViews)e+=t.byteSize;return e}}function me(i,e){if(i.components)for(const t of i.components)t.faces&&t.shading==="smooth"&&ge(t,e)}function ge(i,e){K(e.normal)&&(e.normal=new Float32Array(e.position.length));const t=i.faces,{position:s,normal:r}=e,n=t.length/3;for(let a=0;a{this._addScene(r)}),s&&t.binChunkBuffer.finalize()}_addScene(e){this.gltf.scenes||(this.gltf.scenes=[]);const t={};e.name&&(t.name=e.name),e.forEachNode(s=>{t.nodes||(t.nodes=[]);const r=this._addNode(s);t.nodes.push(r)}),this.gltf.scenes.push(t)}_addNode(e){this.gltf.nodes||(this.gltf.nodes=[]);const t={};e.name&&(t.name=e.name);const s=e.translation;Y(s,te)||(t.translation=G(s));const r=e.rotation;ne(r,oe)||(t.rotation=he(r));const n=e.scale;Y(n,W)||(t.scale=G(n)),e.mesh&&e.mesh.vertexAttributes.position?t.mesh=this._addMesh(e.mesh):e.forEachNode(h=>{t.children||(t.children=[]);const o=this._addNode(h);t.children.push(o)});const a=this.gltf.nodes.length;return this.gltf.nodes.push(t),a}_addMesh(e){this.gltf.meshes||(this.gltf.meshes=[]);const t={primitives:[]},s=this.gltf.extras,r=s.options.bufferOutputType===R.GLB;let n;n=r?s.binChunkBuffer:new J(this.gltf),this.params.origin||(this.params.origin=we(e));const a=ue(e.vertexAttributes,e.transform,this.params.origin,{geographic:this.params.geographic,unit:"meters"});me(e,a),this._flipYZAxis(a);const h=n.addBufferView(d.FLOAT,p.VEC3,E.ARRAY_BUFFER);let o,c,l,u;a.normal&&(o=n.addBufferView(d.FLOAT,p.VEC3,E.ARRAY_BUFFER)),e.vertexAttributes.uv&&(c=n.addBufferView(d.FLOAT,p.VEC2,E.ARRAY_BUFFER)),a.tangent&&(l=n.addBufferView(d.FLOAT,p.VEC4,E.ARRAY_BUFFER)),e.vertexAttributes.color&&(u=n.addBufferView(d.UNSIGNED_BYTE,p.VEC4,E.ARRAY_BUFFER)),h.startAccessor("POSITION"),o&&o.startAccessor("NORMAL"),c&&c.startAccessor("TEXCOORD_0"),l&&l.startAccessor("TANGENT"),u&&u.startAccessor("COLOR_0");const w=a.position.length/3,{position:m,normal:x,tangent:_}=a,{color:N,uv:A}=e.vertexAttributes;for(let f=0;f0&&e.components[0].faces?(B=n.addBufferView(d.UNSIGNED_INT,p.SCALAR,E.ELEMENT_ARRAY_BUFFER),this._addMeshVertexIndexed(B,e.components,t,$,z,V,F,P)):this._addMeshVertexNonIndexed(e.components,t,$,z,V,F,P),h.finalize(),o&&o.finalize(),c&&c.finalize(),l&&l.finalize(),B&&B.finalize(),u&&u.finalize(),r||n.finalize();const X=this.gltf.meshes.length;return this.gltf.meshes.push(t),X}_flipYZAxis({position:e,normal:t,tangent:s}){this._flipYZBuffer(e,3),this._flipYZBuffer(t,3),this._flipYZBuffer(s,4)}_flipYZBuffer(e,t){if(!K(e))for(let s=1,r=2;sh**2.1,n=h=>{const o=h.toRgba();return o[0]=r(o[0]/255),o[1]=r(o[1]/255),o[2]=r(o[2]/255),o};if(g(e.color)&&(s.pbrMetallicRoughness.baseColorFactor=n(e.color)),g(e.colorTexture)&&(s.pbrMetallicRoughness.baseColorTexture={index:this._addTexture(e.colorTexture)}),g(e.normalTexture)&&(s.normalTexture={index:this._addTexture(e.normalTexture)}),e instanceof fe){if(g(e.emissiveTexture)&&(s.emissiveTexture={index:this._addTexture(e.emissiveTexture)}),g(e.emissiveColor)){const h=n(e.emissiveColor);s.emissiveFactor=[h[0],h[1],h[2]]}g(e.occlusionTexture)&&(s.occlusionTexture={index:this._addTexture(e.occlusionTexture)}),g(e.metallicRoughnessTexture)&&(s.pbrMetallicRoughness.metallicRoughnessTexture={index:this._addTexture(e.metallicRoughnessTexture)}),s.pbrMetallicRoughness.metallicFactor=e.metallic,s.pbrMetallicRoughness.roughnessFactor=e.roughness}else s.pbrMetallicRoughness.metallicFactor=1,s.pbrMetallicRoughness.roughnessFactor=1,be.warnOnce("Meshes exported to GLTF without MeshMaterialMetallicRoughness material will appear different when imported back.");const a=this.gltf.materials.length;return this.gltf.materials.push(s),this._materialMap.push(e),a}_addTexture(e){var s;const t=(s=this.gltf.textures)!=null?s:[];return this.gltf.textures=t,se(this._textureMap,e,()=>{const r={sampler:this._addSampler(e),source:this._addImage(e)},n=t.length;return t.push(r),n})}_addImage(e){const t=this._imageMap.get(e);if(t!=null)return t;this.gltf.images||(this.gltf.images=[]);const s={};if(e.url)s.uri=e.url;else{const n=e.data;s.extras=n;for(let h=0;h(s.mimeType=l,c));h.writeAsync(o).then(()=>{h.finalize()}),s.bufferView=h.index;break}case b.DataURI:s.uri=le(n);break;default:a.promises.push(k(n).then(({data:h,type:o})=>{s.uri=h,s.mimeType=o}))}}const r=this.gltf.images.length;return this.gltf.images.push(s),this._imageMap.set(e,r),r}_addSampler(e){this.gltf.samplers||(this.gltf.samplers=[]);let t=y.REPEAT,s=y.REPEAT;if(typeof e.wrap=="string")switch(e.wrap){case"clamp":t=y.CLAMP_TO_EDGE,s=y.CLAMP_TO_EDGE;break;case"mirror":t=y.MIRRORED_REPEAT,s=y.MIRRORED_REPEAT}else{switch(e.wrap.vertical){case"clamp":s=y.CLAMP_TO_EDGE;break;case"mirror":s=y.MIRRORED_REPEAT}switch(e.wrap.horizontal){case"clamp":t=y.CLAMP_TO_EDGE;break;case"mirror":t=y.MIRRORED_REPEAT}}const r={wrapS:t,wrapT:s};for(let a=0;a{const m={origin:n};delete a.extras;const x=typeof e.jsonSpacing=="number"?e.jsonSpacing:4,_=JSON.stringify(a,(N,A)=>{if(N!=="extras"){if(A instanceof ArrayBuffer){if(de(A))switch(e.imageOutputType){case b.DataURI:case b.GLB:break;case b.External:default:{const O=`img${c}.png`;return c++,m[O]=A,O}}switch(e.bufferOutputType){case R.DataURI:return _e(A);case R.GLB:if(l)throw new Error("Already encountered an ArrayBuffer, there should only be one in the GLB format.");return void(l=A);case R.External:default:{const O=`data${o}.bin`;return o++,m[O]=A,O}}}return A}},x);return e.bufferOutputType===R.GLB||e.imageOutputType===b.GLB?m[j]=new T(_,l).buffer:m[Oe]=_,m})}function Me(i,e){return Se(i,{bufferOutputType:R.GLB,imageOutputType:b.GLB,jsonSpacing:0},e)}class Be{constructor(e,t){this._file={type:"model/gltf-binary",data:e},this.origin=t}buffer(){return Promise.resolve(this._file)}download(e){re(new Blob([this._file.data],{type:this._file.type}),e)}}function He(i,e){const t=new Re,s=new xe;return t.addScene(s),s.addNode(new Ne(i)),Me(t,e).then(r=>new Be(r[j],r.origin))}export{He as toBinaryGLTF};