/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{identity as t,multiply as e,translate as r,scale as i,rotate as s}from"../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as o}from"../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as a}from"../../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{fromValues as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f32.js";import{normalizeMapX as l}from"../../../../geometry/support/normalizeUtils.js";import{DisplayObject as f}from"../../engine/DisplayObject.js";import{createProgramDescriptor as u}from"../../engine/webgl/Utils.js";import{BufferObject as c}from"../../../webgl/BufferObject.js";import{PrimitiveType as m,DataType as h}from"../../../webgl/enums.js";import{VertexArrayObject as _}from"../../../webgl/VertexArrayObject.js";import{VertexBuffer as d}from"../../../webgl/VertexBuffer.js";const p=Math.PI/180,g=4;class v extends f{constructor(t){super(),this._program=null,this._vao=null,this._vertexBuffer=null,this._indexBuffer=null,this._dvsMat3=o(),this._localOrigin={x:0,y:0},this._getBounds=t}destroy(){super.destroy(),this._vao&&(this._vao.dispose(),this._vao=null,this._vertexBuffer=null,this._indexBuffer=null)}doRender(t){const{context:e}=t,r=this._getBounds();if(r.length<1)return;this._createShaderProgram(e),this._updateMatricesAndLocalOrigin(t),this._updateBufferData(e,r),e.setBlendingEnabled(!0),e.setDepthTestEnabled(!1),e.setStencilWriteMask(0),e.setStencilTestEnabled(!1),e.setBlendFunction(1,771),e.setColorMask(!0,!0,!0,!0);const i=this._program;e.bindVAO(this._vao),e.useProgram(i),i.setUniformMatrix3fv("u_dvsMat3",this._dvsMat3),e.gl.lineWidth(1),e.drawElements(m.LINES,8*r.length,h.UNSIGNED_INT,0),e.bindVAO(null)}_createTransforms(){return{displayViewScreenMat3:o()}}_createShaderProgram(t){if(this._program)return;const e="precision highp float;\n        uniform mat3 u_dvsMat3;\n\n        attribute vec2 a_position;\n\n        void main() {\n          mediump vec3 pos = u_dvsMat3 * vec3(a_position, 1.0);\n          gl_Position = vec4(pos.xy, 0.0, 1.0);\n        }",r="precision mediump float;\n      void main() {\n        gl_FragColor = vec4(0.75, 0.0, 0.0, 0.75);\n      }";this._program=t.programCache.get(e,r,x().attributes)}_updateMatricesAndLocalOrigin(o){const{state:f}=o,{displayMat3:u,size:c,resolution:m,pixelRatio:h,rotation:_,viewpoint:d}=f,g=p*_,{x:v,y:x}=d.targetGeometry,b=l(v,f.spatialReference);this._localOrigin.x=b,this._localOrigin.y=x;const B=h*c[0],y=h*c[1],j=m*B,M=m*y,O=t(this._dvsMat3);e(O,O,u),r(O,O,a(B/2,y/2)),i(O,O,n(c[0]/j,-y/M,1)),s(O,O,-g)}_updateBufferData(t,e){const{x:r,y:i}=this._localOrigin,s=2*g*e.length,o=new Float32Array(s),a=new Uint32Array(8*e.length);let n=0,l=0;for(const u of e)u&&(o[2*n]=u[0]-r,o[2*n+1]=u[1]-i,o[2*n+2]=u[0]-r,o[2*n+3]=u[3]-i,o[2*n+4]=u[2]-r,o[2*n+5]=u[3]-i,o[2*n+6]=u[2]-r,o[2*n+7]=u[1]-i,a[l]=n+0,a[l+1]=n+3,a[l+2]=n+3,a[l+3]=n+2,a[l+4]=n+2,a[l+5]=n+1,a[l+6]=n+1,a[l+7]=n+0,n+=4,l+=8);const f=x();this._vertexBuffer?this._vertexBuffer.setData(o.buffer):this._vertexBuffer=new d(t,f.bufferLayout,o.buffer,35048),this._indexBuffer?this._indexBuffer.setData(a):this._indexBuffer=c.createIndex(t,35048,a),this._vao||(this._vao=new _(t,this._vertexBuffer,this._indexBuffer))}}const x=()=>u("bounds",[{location:0,name:"a_position",count:2,type:h.FLOAT}]);export{v as default};
