/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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{Float2PassUniform as r}from"../../shaderModules/Float2PassUniform.js";import{glsl as n}from"../../shaderModules/glsl.js";import{Matrix3PassUniform as o}from"../../shaderModules/Matrix3PassUniform.js";import{Texture2DDrawUniform as s}from"../../shaderModules/Texture2DDrawUniform.js";import{Texture2DPassUniform as m}from"../../shaderModules/Texture2DPassUniform.js";function d(d,l){const c=d.fragment,{hasVertexTangents:i,doubleSidedMode:x,hasNormalTexture:T,textureCoordinateType:u,bindType:g,hasNormalTextureTransform:v}=l;i?(d.attributes.add("tangent","vec4"),d.varyings.add("vTangent","vec4"),2===x?c.code.add(n`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);
}`):c.code.add(n`mat3 computeTangentSpace(vec3 normal) {
float tangentHeadedness = vTangent.w;
vec3 tangent = normalize(vTangent.xyz);
vec3 bitangent = cross(normal, tangent) * tangentHeadedness;
return mat3(tangent, bitangent, normal);
}`)):c.code.add(n`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!==u&&(d.include(a,l),c.uniforms.add(1===g?new m("normalTexture",e=>e.textureNormal):new s("normalTexture",e=>e.textureNormal)),v&&(c.uniforms.add(new r("scale",e=>e.scale??t)),c.uniforms.add(new o("normalTextureTransformMatrix",t=>t.normalTextureTransformMatrix??e))),c.code.add(n`vec3 computeTextureNormal(mat3 tangentSpace, vec2 uv) {
vec3 rawNormal = textureLookup(normalTexture, uv).rgb * 2.0 - 1.0;`),v&&c.code.add(n`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;`),c.code.add(n`return tangentSpace * rawNormal;
}`))}export{d as ComputeNormalTexture};
