WaterSurface.glsl.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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{ForwardLinearDepth as e}from"../views/3d/webgl-engine/core/shaderLibrary/ForwardLinearDepth.glsl.js";import{ShaderOutput as r}from"../views/3d/webgl-engine/core/shaderLibrary/ShaderOutput.js";import{SliceDraw as i}from"../views/3d/webgl-engine/core/shaderLibrary/Slice.glsl.js";import{Transform as o}from"../views/3d/webgl-engine/core/shaderLibrary/Transform.glsl.js";import{OutputHighlight as a}from"../views/3d/webgl-engine/core/shaderLibrary/output/OutputHighlight.glsl.js";import{EvaluateAmbientLighting as s}from"../views/3d/webgl-engine/core/shaderLibrary/shading/EvaluateAmbientLighting.glsl.js";import{MainLighting as n,addMainLightDirection as t,addMainLightIntensity as l}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MainLighting.glsl.js";import{multipassTerrainTest as d}from"../views/3d/webgl-engine/core/shaderLibrary/shading/MultipassTerrainTest.glsl.js";import{NormalUtils as g}from"../views/3d/webgl-engine/core/shaderLibrary/shading/NormalUtils.glsl.js";import{PBRMode as v}from"../views/3d/webgl-engine/core/shaderLibrary/shading/PhysicallyBasedRenderingParameters.glsl.js";import{ReadShadowMapDraw as m}from"../views/3d/webgl-engine/core/shaderLibrary/shading/ReadShadowMap.glsl.js";import{Water as c}from"../views/3d/webgl-engine/core/shaderLibrary/shading/Water.glsl.js";import{WaterDistortion as p}from"../views/3d/webgl-engine/core/shaderLibrary/shading/WaterDistortion.glsl.js";import{symbolAlphaCutoff as u}from"../views/3d/webgl-engine/core/shaderLibrary/util/AlphaCutoff.js";import{ColorConversion as w}from"../views/3d/webgl-engine/core/shaderLibrary/util/ColorConversion.glsl.js";import{addProjViewLocalOrigin as h,addCameraPosition as f}from"../views/3d/webgl-engine/core/shaderLibrary/util/View.glsl.js";import{Float4PassUniform as b}from"../views/3d/webgl-engine/core/shaderModules/Float4PassUniform.js";import{FloatPassUniform as y}from"../views/3d/webgl-engine/core/shaderModules/FloatPassUniform.js";import{glsl as j}from"../views/3d/webgl-engine/core/shaderModules/interfaces.js";import{ShaderBuilder as C}from"../views/3d/webgl-engine/core/shaderModules/ShaderBuilder.js";import{TransparencyPassType as F}from"../views/3d/webgl-engine/lib/TransparencyPassType.js";import{VertexAttribute as L}from"../views/3d/webgl-engine/lib/VertexAttribute.js";function P(P){const _=new C,{vertex:M,fragment:S}=_;h(M,P),_.include(o,P),_.attributes.add(L.POSITION,"vec3"),_.attributes.add(L.UV0,"vec2");const x=new b("waterColor",(e=>e.color));if(P.output===r.Color&&P.isDraped)return _.varyings.add("vpos","vec3"),M.uniforms.add(x),M.code.add(j`
  6. void main(void) {
  7. if (waterColor.a < ${j.float(u)}) {
  8. // Discard this vertex
  9. gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
  10. return;
  11. }
  12. vpos = position;
  13. gl_Position = transformPosition(proj, view, vpos);
  14. }
  15. `),S.uniforms.add(x),S.code.add(j`void main() {
  16. gl_FragColor = waterColor;
  17. }`),_;switch(P.output!==r.Color&&P.output!==r.Alpha||(_.include(g,P),_.include(e,P),_.varyings.add("vuv","vec2"),_.varyings.add("vpos","vec3"),_.varyings.add("vnormal","vec3"),_.varyings.add("vtbnMatrix","mat3"),P.hasMultipassTerrain&&_.varyings.add("depth","float"),M.uniforms.add(x),M.code.add(j`
  18. void main(void) {
  19. if (waterColor.a < ${j.float(u)}) {
  20. // Discard this vertex
  21. gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
  22. return;
  23. }
  24. vuv = uv0;
  25. vpos = position;
  26. vnormal = getLocalUp(vpos, localOrigin);
  27. vtbnMatrix = getTBNMatrix(vnormal);
  28. ${P.hasMultipassTerrain?"depth = (view * vec4(vpos, 1.0)).z;":""}
  29. gl_Position = transformPosition(proj, view, vpos);
  30. ${P.output===r.Color?"forwardLinearDepth();":""}
  31. }
  32. `)),_.include(d,P),P.output){case r.Alpha:_.include(i,P),S.uniforms.add(x),S.code.add(j`
  33. void main() {
  34. discardBySlice(vpos);
  35. ${P.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}
  36. gl_FragColor = vec4(waterColor.a);
  37. }
  38. `);break;case r.Color:_.include(n,P),_.include(s,{pbrMode:v.Disabled,lightingSphericalHarmonicsOrder:2}),_.include(p),_.include(i,P),_.include(m,P),_.include(c,P),S.uniforms.add([x,new y("timeElapsed",(e=>e.timeElapsed)),M.uniforms.get("view"),M.uniforms.get("localOrigin")]),f(S,P),S.include(w),t(S),l(S),S.code.add(j`
  39. void main() {
  40. discardBySlice(vpos);
  41. ${P.hasMultipassTerrain?"terrainDepthTest(gl_FragCoord, depth);":""}
  42. vec3 localUp = vnormal;
  43. // the created normal is in tangent space
  44. vec4 tangentNormalFoam = getSurfaceNormalAndFoam(vuv, timeElapsed);
  45. // we rotate the normal according to the tangent-bitangent-normal-Matrix
  46. vec3 n = normalize(vtbnMatrix * tangentNormalFoam.xyz);
  47. vec3 v = -normalize(vpos - cameraPosition);
  48. float shadow = ${P.receiveShadows?j`1.0 - readShadowMap(vpos, linearDepth)`:"1.0"};
  49. vec4 vPosView = view * vec4(vpos, 1.0);
  50. vec4 final = vec4(getSeaColor(n, v, mainLightDirection, waterColor.rgb, mainLightIntensity, localUp, shadow, tangentNormalFoam.w, vPosView.xyz, vpos + localOrigin), waterColor.w);
  51. // gamma correction
  52. gl_FragColor = delinearizeGamma(final);
  53. gl_FragColor = highlightSlice(gl_FragColor, vpos);
  54. ${P.transparencyPassType===F.Color?"gl_FragColor = premultiplyAlpha(gl_FragColor);":""}
  55. }
  56. `);break;case r.Normal:_.include(g,P),_.include(p,P),_.include(i,P),_.varyings.add("vpos","vec3"),_.varyings.add("vuv","vec2"),M.uniforms.add(x),M.code.add(j`
  57. void main(void) {
  58. if (waterColor.a < ${j.float(u)}) {
  59. // Discard this vertex
  60. gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
  61. return;
  62. }
  63. vuv = uv0;
  64. vpos = position;
  65. gl_Position = transformPosition(proj, view, vpos);
  66. }
  67. `),S.uniforms.add(new y("timeElapsed",(e=>e.timeElapsed))),S.code.add(j`void main() {
  68. discardBySlice(vpos);
  69. vec4 tangentNormalFoam = getSurfaceNormalAndFoam(vuv, timeElapsed);
  70. tangentNormalFoam.xyz = normalize(tangentNormalFoam.xyz);
  71. gl_FragColor = vec4((tangentNormalFoam.xyz + vec3(1.0)) * 0.5, tangentNormalFoam.w);
  72. }`);break;case r.Highlight:_.include(a,P),_.varyings.add("vpos","vec3"),M.uniforms.add(x),M.code.add(j`
  73. void main(void) {
  74. if (waterColor.a < ${j.float(u)}) {
  75. // Discard this vertex
  76. gl_Position = vec4(1e38, 1e38, 1e38, 1.0);
  77. return;
  78. }
  79. vpos = position;
  80. gl_Position = transformPosition(proj, view, vpos);
  81. }
  82. `),_.include(i,P),S.code.add(j`void main() {
  83. discardBySlice(vpos);
  84. outputHighlight();
  85. }`)}return _}const _=Object.freeze(Object.defineProperty({__proto__:null,build:P},Symbol.toStringTag,{value:"Module"}));export{_ as W,P as b};