/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{IDENTITY as e}from"../../../../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{ONES as t}from"../../../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{VertexTextureCoordinates as a}from"../attributes/VertexTextureCoordinates.glsl.js";import{Float2DrawUniform as r}from"../../shaderModules/Float2DrawUniform.js";import{Float2PassUniform as n}from"../../shaderModules/Float2PassUniform.js";import{glsl as o}from"../../shaderModules/glsl.js";import{Matrix3PassUniform as s}from"../../shaderModules/Matrix3PassUniform.js";import{Texture2DDrawUniform as m}from"../../shaderModules/Texture2DDrawUniform.js";import{Texture2DPassUniform as d}from"../../shaderModules/Texture2DPassUniform.js";function l(e,t){return i(e,t)}function c(e,t){return i(e,t)}function i(l,c){const i=l.fragment,{hasVertexTangents:u,doubleSidedMode:x,hasNormalTexture:T,textureCoordinateType:g,bindType:f,hasNormalTextureTransform:v}=c;u?(l.attributes.add("tangent","vec4"),l.varyings.add("vTangent","vec4"),2===x?i.code.add(o`mat3 computeTangentSpace(vec3 normal) {
float tangentHeadedness = gl_FrontFacing ? vTangent.w : -vTangent.w;
vec3 tangent = normalize(gl_FrontFacing ? vTangent.xyz : -vTangent.xyz);
vec3 bitangent = cross(normal, tangent) * tangentHeadedness;
return mat3(tangent, bitangent, normal);
}`):i.code.add(o`mat3 computeTangentSpace(vec3 normal) {
float tangentHeadedness = vTangent.w;
vec3 tangent = normalize(vTangent.xyz);
vec3 bitangent = cross(normal, tangent) * tangentHeadedness;
return mat3(tangent, bitangent, normal);
}`)):i.code.add(o`mat3 computeTangentSpace(vec3 normal, vec3 pos, vec2 st) {
vec3 Q1 = dFdx(pos);
vec3 Q2 = dFdy(pos);
vec2 stx = dFdx(st);
vec2 sty = dFdy(st);
float det = stx.t * sty.s - sty.t * stx.s;
vec3 T = stx.t * Q2 - sty.t * Q1;
T = T - normal * dot(normal, T);
T *= inversesqrt(max(dot(T,T), 1.e-10));
vec3 B = sign(det) * cross(normal, T);
return mat3(T, B, normal);
}`),T&&0!==g&&(l.include(a,c),i.uniforms.add(1===f?new d("normalTexture",e=>e.textureNormal):new m("normalTexture",e=>e.textureNormal)),v&&(i.uniforms.add(1===f?new n("scale",e=>e.scale??t):new r("scale",e=>e.scale??t)),i.uniforms.add(new s("normalTextureTransformMatrix",t=>t.normalTextureTransformMatrix??e))),i.code.add(o`vec3 computeTextureNormal(mat3 tangentSpace, vec2 uv) {
vec3 rawNormal = textureLookup(normalTexture, uv).rgb * 2.0 - 1.0;`),v&&i.code.add(o`mat3 normalRotation = mat3(normalTextureTransformMatrix[0][0]/scale[0], normalTextureTransformMatrix[0][1]/scale[1], 0.0,
normalTextureTransformMatrix[1][0]/scale[0], normalTextureTransformMatrix[1][1]/scale[1], 0.0,
0.0, 0.0, 0.0 );
rawNormal.xy = (normalRotation * vec3(rawNormal.x, rawNormal.y, 1.0)).xy;`),i.code.add(o`return tangentSpace * rawNormal;
}`))}export{c as ComputeNormalTextureDraw,l as ComputeNormalTexturePass};
