/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{fromQuat as e}from"../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as t,IDENTITY as o}from"../../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{transformMat3 as r}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{fromValues as l,create as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{BufferViewVec3f as i,BufferViewVec2f as a,BufferViewVec4u8 as u,BufferViewVec2i16 as f}from"../../../../geometry/support/buffer/BufferView.js";import{computeNormals as s,writeAddedVerticesAndNormals as c}from"../IntegratedMeshNormals.js";import{ElevationRange as d}from"../../support/ElevationRange.js";import{VertexBufferLayoutCreationParameters as p,createVertexBufferLayout as m}from"../../webgl-engine/collections/Component/SourceGeometry.js";function b(e,t,o,r,n,i,a){const u=new Array,f=l(0,0,0),b=new d,g={origin:t,ellipsoidRadius:o,isGlobal:r,ecefRotation:n};for(const l of e){const{componentIndex:e,positionView:t,indicesView:o,texCoord0Data:r,colorView:n,normalsData:d}=l,x=t.count,B=r?1:0,w=new p(null!=n,null!=d||a,B),S=a&&null==d?s(o.typedBuffer,t.typedBuffer,t.typedBufferStride):null,V=S?.outputVertexCount??x,h=m(w),v=h.createBuffer(V),C=S?h.fields.get("normalCompressed"):null;if(null!=S&&null==C)throw new Error("computeNormals requires a reserved NORMALCOMPRESSED attribute in layout");let R;if(y(g,{texCoord0Data:r,colorView:n,packedNormalsSource:S?S.packedNormals:d??null},v,{positionView:t,tileObb:i,obbHalfSizeAccumulator:f,elevationRange:b}),null!=S){const e=h.stride/4,t=C.offset/4,o=x,r=new Uint32Array(v.buffer);c(r,e,t,r,e,o,V,S.appendedVertexToOriginal,S.packedNormals),R=S.indices}else R=o.typedBuffer;u.push({componentIndex:e,positionView:t,indicesView:o,layout:h,textureCoordinateType:B,interleavedVertexData:v.buffer,indices:R})}return{vertexProcessingOutputs:u,obbHalfSizeAccumulator:f,elevationRange:b}}function y(t,l,n,s){const{origin:c,ellipsoidRadius:d,isGlobal:p,ecefRotation:m}=t,{texCoord0Data:b,colorView:y,packedNormalsSource:B}=l,{positionView:w,tileObb:S,obbHalfSizeAccumulator:V,elevationRange:h}=s,v=n.getField("position",i),C=v.typedBuffer,R=v.typedBufferStride,M=null!=b?n.getField("uv0",a):null,j=M?.typedBuffer,A=M?.typedBufferStride??0,N=y?.typedBuffer,O=y?.elementCount??0,D=null!=y?n.getField("color",u):null,E=D?.typedBuffer,k=D?.typedBufferStride??0,F=null!=B?n.getField("normalCompressed",f):null,q=null!=F?new Uint32Array(F.typedBuffer.buffer,F.typedBuffer.byteOffset,F.typedBuffer.length/2):null,z=null!=F?F.typedBufferStride/2:0,G=e(x,S.quaternionConjugate),H=r(g,S.center,G),I=G[0],P=G[1],T=G[2],U=G[3],L=G[4],J=G[5],K=G[6],Q=G[7],W=G[8],X=H[0],Y=H[1],Z=H[2],$=m[0],_=m[1],ee=m[2],te=m[3],oe=m[4],re=m[5],le=m[6],ne=m[7],ie=m[8],ae=m!==o,ue=c[0],fe=c[1],se=c[2],ce=w.count,de=w.typedBufferStride,pe=w.typedBuffer,me=null!=j&&null!=b,be=null!=E&&null!=N&&(3===O||4===O),ye=4===O,ge=null!=q&&null!=B;let xe=V[0],Be=V[1],we=V[2];for(let e=0;e<ce;++e){const t=e*de,o=pe[t],r=pe[t+1],l=pe[t+2],n=o+ue,i=r+fe,a=l+se,u=p?Math.sqrt(n*n+i*i+a*a)-d:a;h.expandElevationRangeValues(u,u);const f=Math.abs(n*I+i*U+a*K-X),s=Math.abs(n*P+i*L+a*Q-Y),c=Math.abs(n*T+i*J+a*W-Z);xe=Math.max(xe,f),Be=Math.max(Be,s),we=Math.max(we,c),ae&&(pe[t]=o*$+r*te+l*le,pe[t+1]=o*_+r*oe+l*ne,pe[t+2]=o*ee+r*re+l*ie);const m=e*R;if(C[m]=o,C[m+1]=r,C[m+2]=l,me){const t=2*e,o=e*A;j[o]=b[t],j[o+1]=b[t+1]}if(be){const t=e*k;if(ye){const o=4*e;E[t]=N[o],E[t+1]=N[o+1],E[t+2]=N[o+2],E[t+3]=N[o+3]}else{const o=3*e;E[t]=N[o],E[t+1]=N[o+1],E[t+2]=N[o+2],E[t+3]=255}}ge&&(q[e*z]=B[e])}V[0]=xe,V[1]=Be,V[2]=we}const g=n(),x=t();export{b as processTiles3DVertices};
