/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{i1616to32 as t,i16 as e}from"../../../number.js";import{AlignedVertexSpec as s}from"./AlignedVertexSpec.js";import{packDataView as i}from"./dataViewUtils.js";class c{static fromVertexSpec(t,e){const i=s.fromVertexSpec(t,e);return new c(i)}constructor(t){this._spec=t,this._packed=new Uint8Array(this._spec.stride*this._spec.packVertexCount),this._packedU32View=new Uint32Array(this._packed.buffer),this._dataView=new DataView(this._packed.buffer)}get attributeLayout(){return this._spec.attributeLayout}get stride(){return this._spec.stride}writeVertex(t,e,s,i,c,a){for(let p=0;p<this._spec.packVertexCount;p++){const t=p*this._spec.stride;this._packPosition(s,i,t),this._packId(e,t);const r=this._spec.bitset;if(a){if(r.packTessellation){const e=r.packTessellation(a,c,s,i);this._pack(e,r,t)}for(const e of this._spec.standardAttributes)if(null!=e.packTessellation){const p=e.packTessellation(a,c,s,i);this._pack(p,e,t)}else if(e.packAlternating?.packTessellation){const t=e.packAlternating.packTessellation(a,c,s,i);for(let s=0;s<this._spec.packVertexCount;s++){const i=t[s];this._pack(i,e,s*this._spec.stride)}}}}t.vertexWriteRegion(this._packedU32View)}pack(t,e){for(const s of this._spec.standardAttributes)if(s.pack&&"string"!=typeof s.pack){const i=s.pack(t,e);for(let t=0;t<this._spec.packVertexCount;t++)this._pack(i,s,t*this._spec.stride)}else if(s.packAlternating?.pack){const i=s.packAlternating.pack(t,e);for(let t=0;t<this._spec.packVertexCount;t++){const e=i[t];this._pack(e,s,t*this._spec.stride)}}}_packPosition(s,i,c){const{offset:a}=this._spec.position,p=this._spec.position.packPrecisionFactor??1,r=e(s*p),o=e(i*p),n=t(r,o);this._dataView.setUint32(c+a,n,!0)}_packId(t,e){const s=t*(this._spec.id.packPrecisionFactor??1),i=4278190080&this._dataView.getUint32(e+this._spec.id.offset,!0);this._dataView.setUint32(e+this._spec.id.offset,s|i,!0)}_pack(t,e,s){i(this._dataView,t,e,s)}}export{c as MeshWriterVertexPack};
