ComponentShader.glsl.js 13 KB

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