/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../../../core/Logger.js";import{clamp as t}from"../../../../core/mathUtils.js";import{createRenderScreenPointArray3 as r}from"../../../../core/screenUtils.js";import{copy as o}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{set as i,subtract as s,dot as n,scale as a,add as c,copy as l,length as m,distance as u}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as f}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{ONES as p}from"../../../../core/libs/gl-matrix-2/factories/vec4f64.js";import{distance2 as h,fromPoints as g,closestLineSegmentPoint as d,create as j}from"../../../../geometry/support/lineSegment.js";import{fromPoints as b,signedDistance as x,getNormal as y,create as L}from"../../../../geometry/support/plane.js";import{isColorHighlightOrOLID as v}from"../core/shaderLibrary/ShaderOutput.js";import S from"../lib/GLMaterial.js";import{Material as M,MaterialParameters as _}from"../lib/Material.js";import{isTranslationMatrix as w}from"../lib/Util.js";import{DefaultBufferWriter as C}from"./DefaultBufferWriter.js";import{PositionColorLayout as T,PositionLayout as A}from"./DefaultLayouts.js";import{NativeLineTechnique as V}from"../shaders/NativeLineTechnique.js";import{NativeLineTechniqueConfiguration as N}from"../shaders/NativeLineTechniqueConfiguration.js";import{alphaCutoff as R}from"../../../../webscene/support/AlphaCutoff.js";class U extends M{constructor(e){super(e,P),this._configuration=new N,this.produces=new Map([[2,e=>v(e)]])}getConfiguration(e,t){return super.getConfiguration(e,t,this._configuration),this._configuration.hasSlicePlane=this.parameters.hasSlicePlane,this._configuration.hasVertexColors=this.parameters.hasVertexColors,this._configuration.transparent=this.parameters.color[3]<1||this.parameters.width<1,this._configuration.hasOccludees=t.hasOccludees,this._configuration}get visible(){return this.parameters.color[3]>=R}intersect(t,r,f,p,j,L){const{options:v,camera:S,rayBegin:M,rayEnd:_}=f;if(!v.selectionMode||!t.visible||!S)return;if(!w(r))return void e.getLogger("esri.views.3d.webgl-engine.materials.NativeLineMaterial").error("intersection assumes a translation-only matrix");const C=t.attributes.get("position").data,T=H;o(T,f.point);const A=2;i(I[0],T[0]-A,T[1]+A,0),i(I[1],T[0]+A,T[1]+A,0),i(I[2],T[0]+A,T[1]-A,0),i(I[3],T[0]-A,T[1]-A,0);for(let e=0;e<4;e++)if(!S.unprojectFromRenderScreen(I[e],J[e]))return;b(S.eye,J[0],J[1],K),b(S.eye,J[1],J[2],Q),b(S.eye,J[2],J[3],Y),b(S.eye,J[3],J[0],Z);let V=Number.MAX_VALUE,N=0;for(let e=0;e<C.length-5;e+=3){if(q[0]=C[e]+r[12],q[1]=C[e+1]+r[13],q[2]=C[e+2]+r[14],B[0]=C[e+3]+r[12],B[1]=C[e+4]+r[13],B[2]=C[e+5]+r[14],x(K,q)<0&&x(K,B)<0||x(Q,q)<0&&x(Q,B)<0||x(Y,q)<0&&x(Y,B)<0||x(Z,q)<0&&x(Z,B)<0)continue;const t=S.projectToRenderScreen(q,W),o=S.projectToRenderScreen(B,X);if(null==t||null==o)continue;if(t[2]<0&&o[2]>0){s(D,q,B);const e=S.frustum,r=-x(e[4],q)/n(D,y(e[4]));if(a(D,D,r),c(q,q,D),!S.projectToRenderScreen(q,t))continue}else if(t[2]>0&&o[2]<0){s(D,B,q);const e=S.frustum,t=-x(e[4],B)/n(D,y(e[4]));if(a(D,D,t),c(B,B,D),!S.projectToRenderScreen(B,o))continue}else if(t[2]<0&&o[2]<0)continue;t[2]=0,o[2]=0;const i=h(g(t,o,F),T);i<V&&(V=i,l(G,q),l(z,B),N=e/3)}if(V<A*A){let e=Number.MAX_VALUE;if(d(g(G,z,F),g(M,_,k),O)){s(O,O,M);const t=m(O);a(O,O,1/t),e=t/u(M,_)}L(e,O,N)}}intersectDraped(e,r,o,i,s){if(!r.options.selectionMode)return;const n=e.attributes.get("position").data,a=e.attributes.get("size"),c=a?a.data[0]:0,l=o[0],m=o[1],u=((c+1)/2+4)*e.screenToWorldRatio;let f=Number.MAX_VALUE,p=0;for(let h=0;h<n.length-5;h+=3){const e=n[h],r=n[h+1],o=l-e,i=m-r,s=n[h+3]-e,a=n[h+4]-r,c=t((s*o+a*i)/(s*s+a*a),0,1),u=s*c-o,g=a*c-i,d=u*u+g*g;d<f&&(f=d,p=h/3)}f<u*u&&i(s.distance,s.normal,p)}createGLMaterial(e){return new E(e)}createBufferWriter(){const e=this.parameters.hasVertexColors?T:A;return new C(e)}}class E extends S{beginSlot(e){return this.getTechnique(V,e)}}class P extends _{constructor(){super(...arguments),this.color=p,this.hasVertexColors=!1,this.hasSlicePlane=!1,this.width=1}}const q=f(),B=f(),D=f(),O=f(),W=r(),X=r(),G=f(),z=f(),F=j(),k=j(),H=f(),I=[r(),r(),r(),r()],J=[f(),f(),f(),f()],K=L(),Q=L(),Y=L(),Z=L();export{U as NativeLineMaterial,P as Parameters};
