particlesP.hlsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "torque.hlsl"
  23. #include "shaderModel.hlsl"
  24. // With advanced lighting we get soft particles.
  25. #ifdef TORQUE_LINEAR_DEPTH
  26. #define SOFTPARTICLES
  27. #endif
  28. #ifdef SOFTPARTICLES
  29. #include "shaderModelAutoGen.hlsl"
  30. uniform float oneOverSoftness;
  31. uniform float oneOverFar;
  32. TORQUE_UNIFORM_SAMPLER2D(prepassTex, 1);
  33. //uniform float3 vEye;
  34. uniform float4 prePassTargetParams;
  35. #endif
  36. #define CLIP_Z // TODO: Make this a proper macro
  37. struct Conn
  38. {
  39. float4 hpos : TORQUE_POSITION;
  40. float4 color : TEXCOORD0;
  41. float2 uv0 : TEXCOORD1;
  42. float4 pos : TEXCOORD2;
  43. };
  44. TORQUE_UNIFORM_SAMPLER2D(diffuseMap, 0);
  45. TORQUE_UNIFORM_SAMPLER2D(paraboloidLightMap, 2);
  46. float4 lmSample( float3 nrm )
  47. {
  48. bool calcBack = (nrm.z < 0.0);
  49. if ( calcBack )
  50. nrm.z = nrm.z * -1.0;
  51. float2 lmCoord;
  52. lmCoord.x = (nrm.x / (2*(1 + nrm.z))) + 0.5;
  53. lmCoord.y = 1-((nrm.y / (2*(1 + nrm.z))) + 0.5);
  54. // If this is the back, offset in the atlas
  55. if ( calcBack )
  56. lmCoord.x += 1.0;
  57. // Atlasing front and back maps, so scale
  58. lmCoord.x *= 0.5;
  59. return TORQUE_TEX2D(paraboloidLightMap, lmCoord);
  60. }
  61. uniform float alphaFactor;
  62. uniform float alphaScale;
  63. float4 main( Conn IN ) : TORQUE_TARGET0
  64. {
  65. float softBlend = 1;
  66. #ifdef SOFTPARTICLES
  67. float2 tc = IN.pos.xy * float2(1.0, -1.0) / IN.pos.w;
  68. tc = viewportCoordToRenderTarget(saturate( ( tc + 1.0 ) * 0.5 ), prePassTargetParams);
  69. float sceneDepth = TORQUE_PREPASS_UNCONDITION(prepassTex, tc).w;
  70. float depth = IN.pos.w * oneOverFar;
  71. float diff = sceneDepth - depth;
  72. #ifdef CLIP_Z
  73. // If drawing offscreen, this acts as the depth test, since we don't line up with the z-buffer
  74. // When drawing high-res, though, we want to be able to take advantage of hi-z
  75. // so this is #ifdef'd out
  76. //clip(diff);
  77. #endif
  78. softBlend = saturate( diff * oneOverSoftness );
  79. #endif
  80. float4 diffuse = TORQUE_TEX2D( diffuseMap, IN.uv0 );
  81. //return float4( lmSample(float3(0, 0, -1)).rgb, IN.color.a * diffuse.a * softBlend * alphaScale);
  82. // Scale output color by the alpha factor (turn LerpAlpha into pre-multiplied alpha)
  83. float3 colorScale = ( alphaFactor < 0.0 ? IN.color.rgb * diffuse.rgb : ( alphaFactor > 0.0 ? IN.color.a * diffuse.a * alphaFactor * softBlend : softBlend ) );
  84. return hdrEncode( float4( IN.color.rgb * diffuse.rgb * colorScale,
  85. IN.color.a * diffuse.a * softBlend * alphaScale ) );
  86. }