/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{create as r}from"../../../../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{create as o}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{DoublePrecision as e}from"../util/DoublePrecision.glsl.js";import{Float3DrawUniform as t}from"../../shaderModules/Float3DrawUniform.js";import{Float3PassUniform as a}from"../../shaderModules/Float3PassUniform.js";import{If as i,glsl as s}from"../../shaderModules/glsl.js";import{IntegerDrawUniform as m}from"../../shaderModules/IntegerDrawUniform.js";import{Matrix3DrawUniform as n}from"../../shaderModules/Matrix3DrawUniform.js";import{Matrix3PassUniform as d}from"../../shaderModules/Matrix3PassUniform.js";import{Matrix4BindUniform as l}from"../../shaderModules/Matrix4BindUniform.js";import{Texture2DPassUniform as f}from"../../shaderModules/Texture2DPassUniform.js";import{NoParameters as v}from"../../../../../webgl/NoParameters.js";function c(r,o){const{attributes:v,vertex:c,varyings:w,fragment:F}=r;c.include(e),v.add("position","vec3"),w.add("vPositionWorldCameraRelative","vec3"),w.add("vPosition_view","vec3",{invariant:!0}),c.uniforms.add(new a("transformWorldFromViewTH",r=>r.transformWorldFromViewTH),new a("transformWorldFromViewTL",r=>r.transformWorldFromViewTL),new d("transformViewFromCameraRelativeRS",r=>r.transformViewFromCameraRelativeRS),new l("transformProjFromView",r=>r.camera.projectionMatrix));const{vertexPositionRotationType:u,useTransformationTexture:W}=o,T=1===u,x=2===u,p=T||x;W?c.uniforms.add(new m("transformationDrawId",r=>r.transformationDrawId),new f("transformationTexture",r=>r.transformationTexture)):(c.uniforms.add(new t("transformWorldFromModelTH",r=>r.transformWorldFromModelTH),new t("transformWorldFromModelTL",r=>r.transformWorldFromModelTL)),p&&c.uniforms.add(new n("transformWorldFromModelRS",r=>r.transformWorldFromModelRS))),c.code.add(s`
      ${i(p,s`
          mat3 modelTransformation() {
          ${i(W,s`
                return mat3(
                  texelFetch(transformationTexture, ivec2(2, transformationDrawId), 0).xyz,
                  texelFetch(transformationTexture, ivec2(3, transformationDrawId), 0).xyz,
                  texelFetch(transformationTexture, ivec2(4, transformationDrawId), 0).xyz
                );`,s`return transformWorldFromModelRS;`)}
          }
        `)}

      vec3 originL() {
        return ${i(W,s`texelFetch(transformationTexture, ivec2(0, transformationDrawId), 0).xyz;`,s`transformWorldFromModelTL;`)}
      }

      vec3 originH() {
        return ${i(W,s`texelFetch(transformationTexture, ivec2(1, transformationDrawId), 0).xyz;`,s`transformWorldFromModelTH;`)};
      }

      vec3 positionWorldCameraRelative() {

      vec3 rotatedModelPosition = ${i(T,s`modelTransformation() *`)} position;

      vec3 transform_CameraRelativeFromModel = dpAdd(
        originL(),
        originH(),
        -transformWorldFromViewTL,
        -transformWorldFromViewTH
      );

      return transform_CameraRelativeFromModel + rotatedModelPosition;
    }

    vec3 positionForDraping() {
      return ${i(x,s`modelTransformation() *`)} position;
    }
  `),c.code.add(s`
    void forwardPosition(float fOffset) {
      vPositionWorldCameraRelative = positionWorldCameraRelative();
      if (fOffset != 0.0) {
        vPositionWorldCameraRelative += fOffset * ${o.spherical?s`normalize(transformWorldFromViewTL + vPositionWorldCameraRelative)`:s`vec3(0.0, 0.0, 1.0)`};
      }

      vPosition_view = transformViewFromCameraRelativeRS * vPositionWorldCameraRelative;
      gl_Position = transformProjFromView * vec4(vPosition_view, 1.0);
    }
  `),F.uniforms.add(new a("transformWorldFromViewTL",r=>r.transformWorldFromViewTL)),c.code.add(s`vec3 positionWorld() {
return transformWorldFromViewTL + vPositionWorldCameraRelative;
}`),F.code.add(s`vec3 positionWorld() {
return transformWorldFromViewTL + vPositionWorldCameraRelative;
}`)}class w extends v{constructor(){super(...arguments),this.transformWorldFromViewTH=o(),this.transformWorldFromViewTL=o(),this.transformViewFromCameraRelativeRS=r()}}class F extends v{constructor(){super(...arguments),this.transformWorldFromModelRS=r(),this.transformWorldFromModelTH=o(),this.transformWorldFromModelTL=o(),this.transformationDrawId=0}}export{c as VertexPosition,F as VertexPositionDrawParameters,w as VertexPositionPassParameters};
