torque.glsl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifndef _TORQUE_GLSL_
  23. #define _TORQUE_GLSL_
  24. float M_HALFPI_F = 1.57079632679489661923;
  25. float M_PI_F = 3.14159265358979323846;
  26. float M_2PI_F = 6.28318530717958647692;
  27. /// Calculate fog based on a start and end positions in worldSpace.
  28. float computeSceneFog( vec3 startPos,
  29. vec3 endPos,
  30. float fogDensity,
  31. float fogDensityOffset,
  32. float fogHeightFalloff )
  33. {
  34. float f = length( startPos - endPos ) - fogDensityOffset;
  35. float h = 1.0 - ( endPos.z * fogHeightFalloff );
  36. return exp( -fogDensity * f * h );
  37. }
  38. /// Calculate fog based on a start and end position and a height.
  39. /// Positions do not need to be in worldSpace but height does.
  40. float computeSceneFog( vec3 startPos,
  41. vec3 endPos,
  42. float height,
  43. float fogDensity,
  44. float fogDensityOffset,
  45. float fogHeightFalloff )
  46. {
  47. float f = length( startPos - endPos ) - fogDensityOffset;
  48. float h = 1.0 - ( height * fogHeightFalloff );
  49. return exp( -fogDensity * f * h );
  50. }
  51. /// Calculate fog based on a distance, height is not used.
  52. float computeSceneFog( float dist, float fogDensity, float fogDensityOffset )
  53. {
  54. float f = dist - fogDensityOffset;
  55. return exp( -fogDensity * f );
  56. }
  57. /// Convert a vec4 uv in viewport space to render target space.
  58. vec2 viewportCoordToRenderTarget( vec4 inCoord, vec4 rtParams )
  59. {
  60. vec2 outCoord = inCoord.xy / inCoord.w;
  61. outCoord = ( outCoord * rtParams.zw ) + rtParams.xy;
  62. return outCoord;
  63. }
  64. /// Convert a vec2 uv in viewport space to render target space.
  65. vec2 viewportCoordToRenderTarget( vec2 inCoord, vec4 rtParams )
  66. {
  67. vec2 outCoord = ( inCoord * rtParams.zw ) + rtParams.xy;
  68. return outCoord;
  69. }
  70. /// Convert a vec4 quaternion into a 3x3 matrix.
  71. mat3x3 quatToMat( vec4 quat )
  72. {
  73. float xs = quat.x * 2.0;
  74. float ys = quat.y * 2.0;
  75. float zs = quat.z * 2.0;
  76. float wx = quat.w * xs;
  77. float wy = quat.w * ys;
  78. float wz = quat.w * zs;
  79. float xx = quat.x * xs;
  80. float xy = quat.x * ys;
  81. float xz = quat.x * zs;
  82. float yy = quat.y * ys;
  83. float yz = quat.y * zs;
  84. float zz = quat.z * zs;
  85. mat3x3 mat;
  86. mat[0][0] = 1.0 - (yy + zz);
  87. mat[1][0] = xy - wz;
  88. mat[2][0] = xz + wy;
  89. mat[0][1] = xy + wz;
  90. mat[1][1] = 1.0 - (xx + zz);
  91. mat[2][1] = yz - wx;
  92. mat[0][2] = xz - wy;
  93. mat[1][2] = yz + wx;
  94. mat[2][2] = 1.0 - (xx + yy);
  95. return mat;
  96. }
  97. /// The number of additional substeps we take when refining
  98. /// the results of the offset parallax mapping function below.
  99. ///
  100. /// You should turn down the number of steps if your needing
  101. /// more performance out of your parallax surfaces. Increasing
  102. /// the number doesn't yeild much better results and is rarely
  103. /// worth the additional cost.
  104. ///
  105. #define PARALLAX_REFINE_STEPS 3
  106. /// Performs fast parallax offset mapping using
  107. /// multiple refinement steps.
  108. ///
  109. /// @param texMap The texture map whos alpha channel we sample the parallax depth.
  110. /// @param texCoord The incoming texture coordinate for sampling the parallax depth.
  111. /// @param negViewTS The negative view vector in tangent space.
  112. /// @param depthScale The parallax factor used to scale the depth result.
  113. ///
  114. vec2 parallaxOffset( sampler2D texMap, vec2 texCoord, vec3 negViewTS, float depthScale )
  115. {
  116. float depth = texture( texMap, texCoord ).a/(PARALLAX_REFINE_STEPS*2);
  117. vec2 offset = negViewTS.xy * vec2( depth * depthScale )/vec2(PARALLAX_REFINE_STEPS*2);
  118. for ( int i=0; i < PARALLAX_REFINE_STEPS; i++ )
  119. {
  120. depth = ( depth + texture( texMap, texCoord + offset ).a )/(PARALLAX_REFINE_STEPS*2);
  121. offset = negViewTS.xy * vec2( depth * depthScale )/vec2(PARALLAX_REFINE_STEPS*2);
  122. }
  123. return offset;
  124. }
  125. /// Same as parallaxOffset but for dxtnm where depth is stored in the red channel instead of the alpha
  126. vec2 parallaxOffsetDxtnm(sampler2D texMap, vec2 texCoord, vec3 negViewTS, float depthScale)
  127. {
  128. float depth = texture(texMap, texCoord).r/(PARALLAX_REFINE_STEPS*2);
  129. vec2 offset = negViewTS.xy * vec2(depth * depthScale)/vec2(PARALLAX_REFINE_STEPS*2);
  130. for (int i = 0; i < PARALLAX_REFINE_STEPS; i++)
  131. {
  132. depth = (depth + texture(texMap, texCoord + offset).r)/(PARALLAX_REFINE_STEPS*2);
  133. offset = negViewTS.xy * vec2(depth * depthScale)/vec2(PARALLAX_REFINE_STEPS*2);
  134. }
  135. return offset;
  136. }
  137. /// The maximum value for 16bit per component integer HDR encoding.
  138. const float HDR_RGB16_MAX = 100.0;
  139. /// The maximum value for 10bit per component integer HDR encoding.
  140. const float HDR_RGB10_MAX = 4.0;
  141. /// Encodes an HDR color for storage into a target.
  142. vec3 hdrEncode( vec3 _sample )
  143. {
  144. #if defined( TORQUE_HDR_RGB16 )
  145. return _sample / HDR_RGB16_MAX;
  146. #elif defined( TORQUE_HDR_RGB10 )
  147. return _sample / HDR_RGB10_MAX;
  148. #else
  149. // No encoding.
  150. return _sample;
  151. #endif
  152. }
  153. /// Encodes an HDR color for storage into a target.
  154. vec4 hdrEncode( vec4 _sample )
  155. {
  156. return vec4( hdrEncode( _sample.rgb ), _sample.a );
  157. }
  158. /// Decodes an HDR color from a target.
  159. vec3 hdrDecode( vec3 _sample )
  160. {
  161. #if defined( TORQUE_HDR_RGB16 )
  162. return _sample * HDR_RGB16_MAX;
  163. #elif defined( TORQUE_HDR_RGB10 )
  164. return _sample * HDR_RGB10_MAX;
  165. #else
  166. // No encoding.
  167. return _sample;
  168. #endif
  169. }
  170. /// Decodes an HDR color from a target.
  171. vec4 hdrDecode( vec4 _sample )
  172. {
  173. return vec4( hdrDecode( _sample.rgb ), _sample.a );
  174. }
  175. /// Returns the luminance for an HDR pixel.
  176. float hdrLuminance( vec3 _sample )
  177. {
  178. // There are quite a few different ways to
  179. // calculate luminance from an rgb value.
  180. //
  181. // If you want to use a different technique
  182. // then plug it in here.
  183. //
  184. ////////////////////////////////////////////////////////////////////////////
  185. //
  186. // Max component luminance.
  187. //
  188. //float lum = max( _sample.r, max( _sample.g, _sample.b ) );
  189. ////////////////////////////////////////////////////////////////////////////
  190. // The perceptual relative luminance.
  191. //
  192. // See http://en.wikipedia.org/wiki/Luminance_(relative)
  193. //
  194. const vec3 RELATIVE_LUMINANCE = vec3( 0.2126, 0.7152, 0.0722 );
  195. float lum = dot( _sample, RELATIVE_LUMINANCE );
  196. ////////////////////////////////////////////////////////////////////////////
  197. //
  198. // The average component luminance.
  199. //
  200. //const vec3 AVERAGE_LUMINANCE = vec3( 0.3333, 0.3333, 0.3333 );
  201. //float lum = dot( _sample, AVERAGE_LUMINANCE );
  202. return lum;
  203. }
  204. #ifdef TORQUE_PIXEL_SHADER
  205. /// Called from the visibility feature to do screen
  206. /// door transparency for fading of objects.
  207. void fizzle(vec2 vpos, float visibility)
  208. {
  209. // NOTE: The magic values below are what give us
  210. // the nice even pattern during the fizzle.
  211. //
  212. // These values can be changed to get different
  213. // patterns... some better than others.
  214. //
  215. // Horizontal Blinds - { vpos.x, 0.916, vpos.y, 0 }
  216. // Vertical Lines - { vpos.x, 12.9898, vpos.y, 78.233 }
  217. //
  218. // I'm sure there are many more patterns here to
  219. // discover for different effects.
  220. mat2x2 m = mat2x2( vpos.x, vpos.y, 0.916, 0.350 );
  221. if( (visibility - fract( determinant( m ) )) < 0 ) //if(a < 0) discard;
  222. discard;
  223. }
  224. #endif //TORQUE_PIXEL_SHADER
  225. /// Basic assert macro. If the condition fails, then the shader will output color.
  226. /// @param condition This should be a bvec[2-4]. If any items is false, condition is considered to fail.
  227. /// @param color The color that should be outputted if the condition fails.
  228. /// @note This macro will only work in the void main() method of a pixel shader.
  229. #define assert(condition, color) { if(!any(condition)) { OUT_col = color; return; } }
  230. // Deferred Shading: Material Info Flag Check
  231. bool getFlag(float flags, float num)
  232. {
  233. float process = round(flags * 255);
  234. float squareNum = pow(2.0, num);
  235. return (mod(process, pow(2.0, squareNum)) >= squareNum);
  236. }
  237. // #define TORQUE_STOCK_GAMMA
  238. #ifdef TORQUE_STOCK_GAMMA
  239. // Sample in linear space. Decodes gamma.
  240. vec4 toLinear(vec4 tex)
  241. {
  242. return tex;
  243. }
  244. // Encodes gamma.
  245. vec4 toGamma(vec4 tex)
  246. {
  247. return tex;
  248. }
  249. vec3 toLinear(vec3 tex)
  250. {
  251. return tex;
  252. }
  253. // Encodes gamma.
  254. vec3 toGamma(vec3 tex)
  255. {
  256. return tex;
  257. }
  258. #else
  259. // Sample in linear space. Decodes gamma.
  260. vec4 toLinear(vec4 tex)
  261. {
  262. return vec4(pow(abs(tex.rgb), vec3(2.2)), tex.a);
  263. }
  264. // Encodes gamma.
  265. vec4 toGamma(vec4 tex)
  266. {
  267. return vec4(pow(abs(tex.rgb), vec3(1.0/2.2)), tex.a);
  268. }
  269. // Sample in linear space. Decodes gamma.
  270. vec3 toLinear(vec3 tex)
  271. {
  272. return pow(abs(tex), vec3(2.2));
  273. }
  274. // Encodes gamma.
  275. vec3 toGamma(vec3 tex)
  276. {
  277. return pow(abs(tex), vec3(1.0/2.2));
  278. }
  279. #endif //
  280. #endif // _TORQUE_GLSL_