ComponentShader.glsl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{earth as e,mars as o,moon as r}from"../geometry/support/Ellipsoid.js";import{OverlayIndex as a,RenderTargetType as i}from"../views/3d/terrain/interfaces.js";import{IntegratedMeshMode as l}from"../views/3d/webgl-engine/collections/Component/Material/ComponentTechniqueConfiguration.js";import{ComponentData as t,ComponentDataType as d}from"../views/3d/webgl-engine/collections/Component/Material/shader/ComponentData.glsl.js";import{VertexDiscardByOpacity as n}from"../views/3d/webgl-engine/collections/Component/Material/shader/VertexDiscardByOpacity.glsl.js";import{ForwardLinearDepth as s}from"../views/3d/webgl-engine/core/shaderLibrary/ForwardLinearDepth.glsl.js";import{ShaderOutput as c}from"../views/3d/webgl-engine/core/shaderLibrary/ShaderOutputOptions.js";import{SlicePass as m}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{NormalAttributeType as g}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/NormalAttribute.glsl.js";import{TextureCoordinateAttribute as v}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/TextureCoordinateAttribute.glsl.js";import{VertexColor as u}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexColor.glsl.js";import{VertexNormal as h}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexNormal.glsl.js";import{VertexPosition as p}from"../views/3d/webgl-engine/core/shaderLibrary/attributes/VertexPosition.glsl.js";import{OutputDepth as C}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputDepth.glsl.js";import{OutputHighlight as w}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl.js";import{ReadLinearDepth as b}from"../views/3d/webgl-engine/core/shaderLibrary/output/ReadLinearDepth.glsl.js";import{ComputeMaterialColor as f}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeMaterialColor.glsl.js";import{ComputeNormalTexture as x}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeNormalTexture.glsl.js";import{ComputeShadingNormal as y}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ComputeShadingNormal.glsl.js";import{EvaluateSceneLighting as M}from"../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateSceneLighting.glsl.js";import{multipassTerrainTest as L}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl.js";import{PBRMode as N,PhysicallyBasedRenderingParameters as O}from"../views/3d/webgl-engine/core/shaderLibrary/shading/PhysicallyBasedRenderingParameters.glsl.js";import{ReadBaseColorTexture as j}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadBaseColorTexture.glsl.js";import{ReadShadowMapPass as S}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl.js";import{OverlayIM as T,getColorTexture as P}from"../views/3d/webgl-engine/core/shaderLibrary/terrain/Overlay.glsl.js";import{symbolAlphaCutoff as W}from"../views/3d/webgl-engine/core/shaderLibrary/util/AlphaCutoff.js";import{DiscardOrAdjustAlphaDraw as A}from"../views/3d/webgl-engine/core/shaderLibrary/util/AlphaDiscard.glsl.js";import{EllipsoidMode as R}from"../views/3d/webgl-engine/core/shaderLibrary/util/EllipsoidMode.js";import{Float3PassUniform as D}from"../views/3d/webgl-engine/core/shaderModules/Float3PassUniform.js";import{FloatPassUniform as B}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as _}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{Matrix4Uniform as E}from"../views/3d/webgl-engine/core/shaderModules/Matrix4Uniform.js";import{ShaderBuilder as F}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{Texture2DPassUniform as $}from"../views/3d/webgl-engine/core/shaderModules/Texture2DPassUniform.js";import{TransparencyPassType as z}from"../views/3d/webgl-engine/lib/basicInterfaces.js";import{VertexAttribute as V}from"../views/3d/webgl-engine/lib/VertexAttribute.js";const I=new Map([[V.POSITION,0],[V.NORMAL,1],[V.NORMALCOMPRESSED,1],[V.COLOR,2],[V.UV0,3],[V.UVREGION,4],[V.COMPONENTINDEX,5]]);function G(a){const i=new F;i.include(p,a),i.include(h,a),i.include(u,a),i.include(v,a),i.include(s,a),i.include(t,a),i.include(A,a),i.include(m,a),i.include(j,a),i.include(n,a);const{vertex:V,fragment:I}=i;I.uniforms.add(new E("view")),a.pbrMode!==N.Normal&&a.pbrMode!==N.Schematic||(i.include(O,a),a.hasNormalTexture&&i.include(x,a)),a.output===c.Shadow&&a.componentData===d.Varying?V.code.add(_`#define discardShadows(castShadows) { if(!castShadows) { gl_Position = vec4(1e38, 1e38, 1e38, 1.0); return; } }`):V.code.add(_`#define discardShadows(castShadows) {}`);const G=a.integratedMeshMode===l.ColorOverlay||a.integratedMeshMode===l.ColorOverlayWithWater,k=G&&a.output===c.Color&&a.pbrMode===N.WaterOnIntegratedMesh;return G&&(i.include(M,a),i.include(T,a),a.spherical?V.code.add(_`
  6. const float invEllipsoidRadius = ${_.float(1/(a.ellipsoidMode===R.Earth?e.radius:a.ellipsoidMode===R.Mars?o.radius:r.radius))};
  7. vec2 projectOverlay(vec3 pos) {
  8. return pos.xy / (1.0 + invEllipsoidRadius * pos.z);
  9. }
  10. `):V.code.add(_`vec2 projectOverlay(vec3 pos) { return pos.xy; }`)),k&&(i.varyings.add("tbnTangent","vec3"),i.varyings.add("tbnBiTangent","vec3"),i.varyings.add("groundNormal","vec3")),V.code.add(_`
  11. void main() {
  12. bool castShadows;
  13. vec4 externalColor = forwardExternalColor(castShadows);
  14. discardShadows(castShadows);
  15. vertexDiscardByOpacity(externalColor.a);
  16. if (externalColor.a < ${_.float(W)}) {
  17. // Discard this vertex
  18. gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
  19. return;
  20. }
  21. forwardPosition(readElevationOffset());
  22. forwardNormal();
  23. forwardTextureCoordinates();
  24. forwardVertexColor();
  25. forwardLinearDepth();
  26. ${k?a.spherical?_`
  27. groundNormal = normalize(positionWorld());
  28. tbnTangent = normalize(cross(vec3(0.0, 0.0, 1.0), groundNormal));
  29. tbnBiTangent = normalize(cross(groundNormal, tbnTangent));`:_`
  30. groundNormal = vec3(0.0, 0.0, 1.0);
  31. tbnTangent = vec3(1.0, 0.0, 0.0);
  32. tbnBiTangent = vec3(0.0, 1.0, 0.0);`:""}
  33. ${G?_`setOverlayVTC(projectOverlay(position));`:""}
  34. }
  35. `),a.output===c.Alpha&&(I.include(b),i.include(L,a),i.include(f,a),G&&I.uniforms.add(new $("ovColorTex",((e,o)=>P(e,o)))),I.code.add(_`
  36. void main() {
  37. discardBySlice(vPositionWorldCameraRelative);
  38. ${a.hasMultipassTerrain?_`terrainDepthTest(gl_FragCoord, vPosition_view.z);`:""}
  39. vec4 textureColor = readBaseColorTexture();
  40. discardOrAdjustAlpha(textureColor);
  41. vec4 externalColor;
  42. int externalColorMixMode;
  43. readExternalColor(externalColor, externalColorMixMode);
  44. vec4 materialColor = computeMaterialColor(
  45. textureColor,
  46. externalColor,
  47. externalColorMixMode
  48. );
  49. ${G?_`
  50. vec4 overlayColor = getOverlayColor(ovColorTex, vtcOverlay);
  51. materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}
  52. gl_FragColor = vec4(materialColor.a);
  53. }
  54. `)),a.output===c.Color&&(I.include(b),i.include(L,a),i.include(f,a),i.include(y,a),i.include(M,a),a.receiveShadows?(i.include(S,a),I.code.add(_`float evaluateShadow() {
  55. return readShadowMap(vPositionWorldCameraRelative, linearDepth);
  56. }`)):I.code.add(_`float evaluateShadow() { return 0.0; }`),G&&I.uniforms.add(new $("ovColorTex",((e,o)=>P(e,o)))),I.code.add(_`
  57. void main() {
  58. discardBySlice(vPositionWorldCameraRelative);
  59. ${a.hasMultipassTerrain?_`terrainDepthTest(gl_FragCoord, vPosition_view.z);`:""}
  60. vec4 textureColor = readBaseColorTexture();
  61. discardOrAdjustAlpha(textureColor);
  62. vec4 externalColor;
  63. int externalColorMixMode;
  64. readExternalColor(externalColor, externalColorMixMode);
  65. vec4 materialColor = computeMaterialColor(
  66. textureColor,
  67. externalColor,
  68. externalColorMixMode
  69. );
  70. ${G?_`vec4 overlayColor = getOverlayColor(ovColorTex, vtcOverlay);`:""}
  71. `),a.pbrMode===N.Normal||a.pbrMode===N.Schematic?(I.uniforms.add(new D("lightingMainIntensity",((e,o)=>o.lighting.mainLight.intensity))),I.code.add(_`
  72. ${a.pbrMode===N.Normal?_`
  73. applyPBRFactors();
  74. if (int(externalColorMixMode) == 3) {
  75. mrr = vec3(0.0, 0.6, 0.2);
  76. }`:""}
  77. vec3 normalVertex = shadingNormalWorld();
  78. float additionalIrradiance = 0.02 * lightingMainIntensity[2];
  79. `),a.hasNormalTexture?I.code.add(_`mat3 tangentSpace = computeTangentSpace(normalVertex, vPositionWorldCameraRelative, vuv0);
  80. vec3 shadingNormal = computeTextureNormal(tangentSpace, vuv0);`):I.code.add(_`vec3 shadingNormal = normalVertex;`),I.code.add(_`${a.spherical?_`vec3 normalGround = normalize(positionWorld());`:_`vec3 normalGround = vec3(0.0, 0.0, 1.0);`}
  81. `),I.code.add(_`
  82. vec3 viewDir = normalize(vPositionWorldCameraRelative);
  83. float ssao = 1.0 - occlusion * (1.0 - evaluateAmbientOcclusion());
  84. ${a.snowCover?_`
  85. vec3 surfaceNormal = normalize(shadingNormalWorld());
  86. float snow = smoothstep(0.5, 0.55, dot(surfaceNormal, normalize(positionWorld())));
  87. materialColor.rgb = mix(materialColor.rgb, vec3(1), snow);
  88. shadingNormal = mix(shadingNormal, surfaceNormal, snow);
  89. ssao = mix(ssao, 0.0, snow);
  90. mrr = mix(mrr, vec3(0.0, 1.0, 0.04), snow);
  91. emission = mix(emission, vec3(0.0), snow);`:""}
  92. ${G?_` materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}
  93. vec3 additionalLight = evaluateAdditionalLighting(ssao, positionWorld());
  94. vec4 shadedColor = vec4(evaluateSceneLightingPBR(shadingNormal, materialColor.rgb, evaluateShadow(), ssao, additionalLight, viewDir, normalGround, mrr, emission, additionalIrradiance), materialColor.a);
  95. `)):(a.receiveShadows?I.code.add(_`float shadow = evaluateShadow();`):a.spherical?(I.uniforms.add(new B("lightingGlobalFactor",((e,o)=>o.lighting.globalFactor))),I.code.add(_`float additionalAmbientScale = additionalDirectedAmbientLight(positionWorld());
  96. float shadow = lightingGlobalFactor * (1.0 - additionalAmbientScale);`)):I.code.add(_`float shadow = 0.0;`),k&&I.uniforms.add(new $("ovNormalTex",((e,o)=>U(o)))),a.snowCover&&(i.extensions.add("GL_OES_standard_derivatives"),I.code.add(_`vec3 surfaceNormal = normalize(cross(dFdx(vPositionWorldCameraRelative), dFdy(vPositionWorldCameraRelative)));
  97. float snow = smoothstep(0.5, 0.55, dot(surfaceNormal, normalize(positionWorld())));
  98. materialColor.rgb = mix(materialColor.rgb, vec3(1), snow);`)),I.code.add(_`
  99. float ambientOcclusion = evaluateAmbientOcclusion();
  100. vec3 additionalLight = evaluateAdditionalLighting(ambientOcclusion, positionWorld());
  101. ${G?_` materialColor = materialColor * (1.0 - overlayColor.a) + overlayColor;`:""}
  102. vec4 shadedColor = vec4(evaluateSceneLighting(shadingNormalWorld(), materialColor.rgb, shadow, ambientOcclusion, additionalLight), materialColor.a);
  103. ${k?_`
  104. vec4 overlayWaterMask = getOverlayColor(ovNormalTex, vtcOverlay);
  105. float waterNormalLength = length(overlayWaterMask);
  106. if (waterNormalLength > 0.95) {
  107. mat3 tbnMatrix = mat3(tbnTangent, tbnBiTangent, groundNormal);
  108. vec4 waterColorLinear = getOverlayWaterColor(overlayWaterMask, overlayColor, -normalize(vPositionWorldCameraRelative), shadow, groundNormal, tbnMatrix, vPosition_view, positionWorld());
  109. vec4 waterColorNonLinear = delinearizeGamma(vec4(waterColorLinear.xyz, 1.0));
  110. // un-gamma the ground color to mix in linear space
  111. shadedColor = mix(shadedColor, waterColorNonLinear, waterColorLinear.w);
  112. }`:""}
  113. `)),I.code.add(_`
  114. gl_FragColor = highlightSlice(shadedColor, vPositionWorldCameraRelative);
  115. ${a.transparencyPassType===z.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}
  116. }
  117. `)),a.output!==c.Depth&&a.output!==c.Shadow||(i.include(C,a),I.code.add(_`void main() {
  118. discardBySlice(vPositionWorldCameraRelative);
  119. vec4 textureColor = readBaseColorTexture();
  120. discardOrAdjustAlpha(textureColor);
  121. outputDepth(linearDepth);
  122. }`)),a.output===c.Normal&&(i.include(y,a),I.code.add(_`
  123. void main() {
  124. discardBySlice(vPositionWorldCameraRelative);
  125. vec4 textureColor = readBaseColorTexture();
  126. discardOrAdjustAlpha(textureColor);
  127. // note: the alpha component needs to be 1.0 in order for this material
  128. // to influence ambient occlusion, see the ssao fragment shader
  129. float alpha = ${a.normalType===g.Ground?"0.0":"1.0"};
  130. gl_FragColor = vec4(vec3(.5) + .5 * shadingNormal_view(), alpha);
  131. }
  132. `)),a.output===c.Highlight&&(i.include(w),I.code.add(_`
  133. void main() {
  134. discardBySlice(vPositionWorldCameraRelative);
  135. vec4 textureColor = readBaseColorTexture();
  136. discardOrAdjustAlpha(textureColor);
  137. ${G?_`
  138. vec4 overlayColor = getCombinedOverlayColor();
  139. if (overlayColor.a == 0.0) {
  140. gl_FragColor = vec4(0.0);
  141. return;
  142. }`:""}
  143. outputHighlight();
  144. }
  145. `)),i}function U(e){return 0===e.overlays.length?null:e.overlays[a.INNER].getValidTexture(i.Water)}const k=Object.freeze(Object.defineProperty({__proto__:null,attributeLocations:I,build:G,getOverlayNormalTexture:U},Symbol.toStringTag,{value:"Module"}));export{k as C,I as a,G as b,U as g};