cloudLayerP.glsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "hlslCompat.glsl"
  23. #include "torque.glsl"
  24. //-----------------------------------------------------------------------------
  25. // Structures
  26. //-----------------------------------------------------------------------------
  27. //ConnectData
  28. in vec4 texCoord12;
  29. #define IN_texCoord12 texCoord12
  30. in vec4 texCoord34;
  31. #define IN_texCoord34 texCoord34
  32. in vec3 vLightTS; // light vector in tangent space, denormalized
  33. #define IN_vLightTS vLightTS
  34. in vec3 vViewTS; // view vector in tangent space, denormalized
  35. #define IN_vViewTS vViewTS
  36. in float worldDist;
  37. #define IN_worldDist worldDist
  38. //-----------------------------------------------------------------------------
  39. // Uniforms
  40. //-----------------------------------------------------------------------------
  41. uniform sampler2D normalHeightMap;
  42. uniform vec3 ambientColor;
  43. uniform vec3 sunColor;
  44. uniform float cloudCoverage;
  45. uniform vec3 cloudBaseColor;
  46. uniform float cloudExposure;
  47. out vec4 OUT_col;
  48. //-----------------------------------------------------------------------------
  49. // Globals
  50. //-----------------------------------------------------------------------------
  51. // The per-color weighting to be used for luminance calculations in RGB order.
  52. const vec3 LUMINANCE_VECTOR = vec3(0.2125f, 0.7154f, 0.0721f);
  53. //-----------------------------------------------------------------------------
  54. // Functions
  55. //-----------------------------------------------------------------------------
  56. // Calculates the Rayleigh phase function
  57. float getRayleighPhase( float angle )
  58. {
  59. return 0.75 * ( 1.0 + pow( angle, 2.0 ) );
  60. }
  61. // Returns the output rgb color given a texCoord and parameters it uses
  62. // for lighting calculation.
  63. vec3 ComputeIllumination( vec2 texCoord,
  64. vec3 vLightTS,
  65. vec3 vViewTS,
  66. vec3 vNormalTS )
  67. {
  68. //return noiseNormal;
  69. //return vNormalTS;
  70. vec3 vLightTSAdj = vec3( -vLightTS.x, -vLightTS.y, vLightTS.z );
  71. float dp = dot( vNormalTS, vLightTSAdj );
  72. // Calculate the amount of illumination (lightTerm)...
  73. // We do both a rim lighting effect and a halfLambertian lighting effect
  74. // and combine the result.
  75. float halfLambertTerm = clamp( pow( dp * 0.5 + 0.5, 1.0 ), 0.0, 1.0 );
  76. float rimLightTerm = pow( ( 1.0 - dp ), 1.0 );
  77. float lightTerm = clamp( halfLambertTerm * 1.0 + rimLightTerm * dp, 0.0, 1.0 );
  78. lightTerm *= 0.5;
  79. // Use a simple RayleighPhase function to simulate single scattering towards
  80. // the camera.
  81. float angle = dot( vLightTS, vViewTS );
  82. lightTerm *= getRayleighPhase( angle );
  83. // Combine terms and colors into the output color.
  84. //vec3 lightColor = ( lightTerm * sunColor * fOcclusionShadow ) + ambientColor;
  85. vec3 lightColor = mix( ambientColor, sunColor, lightTerm );
  86. //lightColor = mix( lightColor, ambientColor, cloudCoverage );
  87. vec3 finalColor = cloudBaseColor * lightColor;
  88. return finalColor;
  89. }
  90. void main()
  91. {
  92. // Normalize the interpolated vectors:
  93. vec3 vViewTS = normalize( vViewTS );
  94. vec3 vLightTS = normalize( vLightTS );
  95. vec4 cResultColor = vec4( 0, 0, 0, 1 );
  96. vec2 texSample = IN_texCoord12.xy;
  97. vec4 noise1 = texture( normalHeightMap, IN_texCoord12.zw );
  98. noise1 = normalize( ( noise1 - 0.5 ) * 2.0 );
  99. //return noise1;
  100. vec4 noise2 = texture( normalHeightMap, IN_texCoord34.xy );
  101. noise2 = normalize( ( noise2 - 0.5 ) * 2.0 );
  102. //return noise2;
  103. vec3 noiseNormal = normalize( noise1 + noise2 ).xyz;
  104. //return vec4( noiseNormal, 1.0 );
  105. float noiseHeight = noise1.a * noise2.a * ( cloudCoverage / 2.0 + 0.5 );
  106. vec3 vNormalTS = normalize( texture( normalHeightMap, texSample ).xyz * 2.0 - 1.0 );
  107. vNormalTS += noiseNormal;
  108. vNormalTS = normalize( vNormalTS );
  109. // Compute resulting color for the pixel:
  110. cResultColor.rgb = ComputeIllumination( texSample, vLightTS, vViewTS, vNormalTS );
  111. float coverage = ( cloudCoverage - 0.5 ) * 2.0;
  112. cResultColor.a = texture( normalHeightMap, texSample ).a + coverage + noiseHeight;
  113. if ( cloudCoverage > -1.0 )
  114. cResultColor.a /= 1.0 + coverage;
  115. cResultColor.a = clamp( cResultColor.a * pow( saturate(cloudCoverage), 0.25 ), 0.0, 1.0 );
  116. cResultColor.a = mix( cResultColor.a, 0.0, 1.0 - pow(IN_worldDist,2.0) );
  117. OUT_col = hdrEncode(cResultColor);
  118. }