/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{normalFromMat4 as e}from"../../../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as r}from"../../../../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{IDENTITY as o}from"../../../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{set as a}from"../../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as n}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{DoublePrecision as i}from"../util/DoublePrecision.glsl.js";import{addViewNormal as t}from"../util/View.glsl.js";import{Float3BindUniform as s}from"../../shaderModules/Float3BindUniform.js";import{glsl as l}from"../../shaderModules/glsl.js";import{Matrix3PassUniform as m}from"../../shaderModules/Matrix3PassUniform.js";import{Matrix4PassUniform as c}from"../../shaderModules/Matrix4PassUniform.js";import{encodeDoubleHi as d,encodeDoubleLo as v}from"../../../../../webgl/doublePrecisionUtils.js";import{NoParameters as g}from"../../../../../webgl/NoParameters.js";class x extends g{constructor(){super(...arguments),this.modelTransformation=null}}const u=r();function p(r,n){const{hasModelTransformation:g,instancedDoublePrecision:x,instanced:p,output:f,hasVertexTangents:w}=n;g&&(r.vertex.uniforms.add(new c("model",e=>e.modelTransformation??o)),r.vertex.uniforms.add(new m("normalLocalOriginFromModel",r=>(e(u,r.modelTransformation??o),u)))),p&&x&&(r.attributes.add("instanceModelOriginHi","vec3"),r.attributes.add("instanceModelOriginLo","vec3"),r.attributes.add("instanceModel","mat3"),r.attributes.add("instanceModelNormal","mat3"));const _=r.vertex;x&&(_.include(i,n),_.uniforms.add(new s("viewOriginHi",e=>d(a(M,e.camera.viewInverseTransposeMatrix[3],e.camera.viewInverseTransposeMatrix[7],e.camera.viewInverseTransposeMatrix[11]),M)),new s("viewOriginLo",e=>v(a(M,e.camera.viewInverseTransposeMatrix[3],e.camera.viewInverseTransposeMatrix[7],e.camera.viewInverseTransposeMatrix[11]),M)))),_.code.add(l`
    vec3 getVertexInLocalOriginSpace() {
      return ${g?x?"(model * vec4(instanceModel * localPosition().xyz, 1.0)).xyz":"(model * localPosition()).xyz":x?"instanceModel * localPosition().xyz":"localPosition().xyz"};
    }

    vec3 subtractOrigin(vec3 _pos) {
      ${x?l`
          // Issue: (should be resolved now with invariant position) https://devtopia.esri.com/WebGIS/arcgis-js-api/issues/56280
          vec3 originDelta = dpAdd(viewOriginHi, viewOriginLo, -instanceModelOriginHi, -instanceModelOriginLo);
          return _pos - originDelta;`:"return vpos;"}
    }
    `),_.code.add(l`
    vec3 dpNormal(vec4 _normal) {
      return normalize(${g?x?"normalLocalOriginFromModel * (instanceModelNormal * _normal.xyz)":"normalLocalOriginFromModel * _normal.xyz":x?"instanceModelNormal * _normal.xyz":"_normal.xyz"});
    }
    `),2===f&&(t(_),_.code.add(l`
    vec3 dpNormalView(vec4 _normal) {
      return normalize((viewNormal * ${g?x?"vec4(normalLocalOriginFromModel * (instanceModelNormal * _normal.xyz), 1.0)":"vec4(normalLocalOriginFromModel * _normal.xyz, 1.0)":x?"vec4(instanceModelNormal * _normal.xyz, 1.0)":"_normal"}).xyz);
    }
    `)),w&&_.code.add(l`
    vec4 dpTransformVertexTangent(vec4 _tangent) {
      ${g?x?"return vec4(normalLocalOriginFromModel * (instanceModelNormal * _tangent.xyz), _tangent.w);":"return vec4(normalLocalOriginFromModel * _tangent.xyz, _tangent.w);":x?"return vec4(instanceModelNormal * _tangent.xyz, _tangent.w);":"return _tangent;"}
    }`)}const M=n();export{x as InstancedDoublePassParameters,p as InstancedDoublePrecision};
