HeatVision.cg
上传用户:xhbjoy
上传日期:2014-10-07
资源大小:38068k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. //////////////////////////////////////////////
  2. //  CASTER PASS //
  3. //     HEAT //
  4. //////////////////////////////////////////////
  5. // vs_1_1
  6. void HeatCaster_vp(
  7.               // in
  8.               float4 vPos: POSITION,
  9.               float4 vNormal: NORMAL,
  10.               
  11.               // out
  12.               out float4 oPos: POSITION,
  13.               out float2 oNDotV: TEXCOORD0,
  14.               
  15.               // parameters
  16.               uniform float4x4 worldViewProj,
  17.               uniform float3 eyePosition        // object space
  18.             )
  19. {
  20.    float4 eyeDir = float4(eyePosition - vPos.xyz, 0);
  21.    eyeDir = normalize(eyeDir);
  22.    oPos   = mul( worldViewProj, vPos );
  23.    oNDotV = clamp( dot( vNormal, eyeDir ), 0, 1 );
  24. }
  25. // ps_2_0
  26. float4 HeatCaster_fp(
  27.                         // input from vp
  28.                         float2 iNDotV: TEXCOORD0
  29.                      ) : COLOR0
  30. {
  31.    return iNDotV.x;
  32. }
  33. //////////////////////////////////////////////
  34. //  CASTER PASS //
  35. //     COLD //
  36. //////////////////////////////////////////////
  37. // vs_1_1
  38. void ColdCaster_vp(
  39.               // in
  40.               float4 vPos: POSITION,
  41.               float4 vNormal: NORMAL,
  42.               
  43.               // out
  44.               out float4 oPos: POSITION,
  45.               out float2 oNDotV: TEXCOORD0,
  46.               
  47.               // parameters
  48.               uniform float4x4 worldViewProj,
  49.               uniform float3 eyePosition        // object space
  50.             )
  51. {
  52.    float4 eyeDir = float4(eyePosition - vPos.xyz, 0);
  53.    eyeDir = normalize(eyeDir);
  54.    oPos   = mul( worldViewProj, vPos );
  55.    oNDotV = clamp( dot( vNormal, eyeDir ), 0, 1 );
  56. }
  57. // ps_2_0
  58. float4 ColdCaster_fp(
  59.                         // input from vp
  60.                         float2 iNDotV: TEXCOORD0
  61.                      ) : COLOR0
  62. {
  63.    return iNDotV.x / 2;
  64. }
  65. //////////////////////////////////////////////
  66. //    PASS 1 - Light to heat conversion //
  67. //////////////////////////////////////////////
  68. // vs_1_1
  69. void LightToHeat_vp(
  70.                     // in
  71.                     float4 inPos: POSITION,
  72.                     
  73.                     uniform float flipping,
  74.                     // out
  75.                     out float4 Pos: POSITION,
  76.                     out float2 uv0: TEXCOORD0
  77.                    )
  78. {
  79.     Pos = float4(inPos.x, flipping * inPos.y, 0.0f, 1.0f);
  80.     inPos.xy = sign(inPos.xy);
  81.     uv0  = (float2(inPos.x, -inPos.y) + 1.0f)/2.0f;
  82. }
  83. // ps_2_0
  84. void LightToHeat_fp(
  85.                         // input from vp
  86.                         float4 inDiffuse: COLOR0,
  87.                         float2 inUV0: TEXCOORD0,
  88.                         
  89.                         // out
  90.                         out float4 outColor: COLOR0,
  91.                         
  92.                         // params
  93.                         uniform float4 random_fractions,
  94.                         uniform float4 heatBiasScale,
  95.                         uniform float4 depth_modulator,
  96.                         uniform sampler2D Input,         // output of HeatVisionCaster_fp (NdotV)
  97.                         uniform sampler2D NoiseMap,
  98.                         uniform sampler2D HeatLookup
  99.                      )
  100. {
  101.    float  depth, heat, interference;
  102.    //  Output constant color:
  103.    depth = tex2D( Input, inUV0 );
  104.    depth *= (depth * depth_modulator);
  105.    heat  = (depth * heatBiasScale.y);
  106. //   if (depth > 0)
  107.    {
  108. interference = -0.5 + tex2D( NoiseMap, inUV0 + float2( random_fractions.x, random_fractions.y ) );
  109. interference *= interference;
  110. interference *= 1 - heat;
  111. heat += interference;//+ heatBiasScale.x;
  112.    }
  113. /*
  114. heatBias isn't used for now
  115.    if (heat > 0)
  116.        heat += heatBiasScale.x;
  117. */
  118.    // Clamp UVs
  119.    heat  = max( 0.005, min( 0.995, heat ) );
  120.    outColor = tex2D( HeatLookup, float2( heat, 0.f ) );
  121. }
  122. //////////////////////////////////////////////
  123. // PASS 2 - add simple blur (final pass) //
  124. //////////////////////////////////////////////
  125. // vs_1_1
  126. void Blur_vp(
  127.                     // in
  128.                     float4 inPos: POSITION,
  129.                     
  130.                     uniform float flipping,
  131.                     // out
  132.                     out float4 Pos: POSITION,
  133.                     out float2 uv0: TEXCOORD0
  134.                    )
  135. {
  136.     Pos = float4(inPos.x, flipping * inPos.y, 0.0f, 1.0f);
  137.     inPos.xy = sign(inPos.xy);
  138.     uv0  = (float2(inPos.x, -inPos.y) + 1.0f)/2.0f;
  139. }
  140. // ps_2_0
  141. void Blur_fp(
  142.                     // input from vp
  143.                     float4 inDiffuse: COLOR0,
  144.                     float2 inUV0: TEXCOORD0,
  145.                     
  146.                     // out
  147.                     out float4 outColor: COLOR0,
  148.                     
  149.                     // parameters
  150.                     uniform sampler2D Input,             // output of HeatVision_fp1 (HeatRenderTexture)
  151.                     uniform float4 blurAmount
  152.                     )
  153. {
  154.    int i;
  155.    float4 tmpOutColor;
  156.    float  diffuseGlowFactor;
  157.    const float2 offsets[4] = 
  158.    {
  159. /*
  160. // hazy blur
  161. -1.8, -1.8,
  162. -1.8, 1.8,
  163. 1.8, -1.8,
  164. 1.8, 1.8
  165. */
  166. /*
  167. // less-hazy blur
  168.   -1.0,  2.0,
  169.   -1.0, -1.0,
  170.    1.0, -1.0,
  171.    1.0,  1.0
  172. */
  173. /*
  174.       -0.326212, -0.405805,
  175.       -0.840144, -0.073580,
  176.       -0.695914,  0.457137,
  177.       -0.203345,  0.620716
  178. */
  179.   -0.3,  0.4,
  180.   -0.3, -0.4,
  181.    0.3, -0.4,
  182.    0.3,  0.4
  183.    };
  184.    tmpOutColor = tex2D( Input, inUV0 ); // UV coords are in image space
  185.    // calculate glow amount
  186.    diffuseGlowFactor = 0.0113f * (2.0 - max( tmpOutColor.r, tmpOutColor.g ));
  187.    // basic blur filter
  188.    for (i = 0; i < 4; i++) {
  189.       tmpOutColor += tex2D( Input, inUV0 + blurAmount.x * diffuseGlowFactor * offsets[i] );
  190.    }
  191.    tmpOutColor *= 0.25;
  192.    // TIPS (old-skool strikes again!)
  193.    // Pay attention here! If you use the "out float4 outColor" directly
  194.    // in your steps while creating the output color (like you remove
  195.    // the "tmpOutColor" var and just use the "outColor" directly)
  196.    // your pixel-color output IS CHANGING EACH TIME YOU DO AN ASSIGNMENT TOO!
  197.    // A temporary variable, instead, acts like a per-pixel double buffer, and
  198.    // best of all, lead to better performance.
  199.    outColor = tmpOutColor;
  200. }