Ellesar

Volumetric light pixel shader

Apr 26th, 2011
321
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void PS_Phong(in float3 inNormal             : TEXCOORD,
  2.               in float3 inCameraDir          : TEXCOORD1,
  3.               in float3 inWorldPos           : TEXCOORD2,
  4.               out float4 outColor            : COLOR)
  5. {
  6.     outColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
  7.  
  8.     //renormalize interpolated vectors
  9.     inNormal = normalize(inNormal);
  10.     inCameraDir = normalize(inCameraDir);
  11.  
  12.     const float shininess = 2;
  13.     const float4 material_ambient = float4(0.05f, 0.05f, 0.05f, 1.0f);
  14.     const float4 material_diffuse = float4(0.30f, 0.30f, 0.30f, 1.0f);
  15.     const float4 material_specular = float4(0.20f, 0.20f, 0.20f, 1.0f);
  16.     int i;
  17.  
  18.     const float opacity = 0.01;
  19.  
  20.     const float3 direction = inWorldPos-cameraPos;
  21.     const float distance_to_wall = length(direction);
  22.  
  23.     float alpha_sum = 0;
  24.     const float3 rayDirection = normalize(direction);
  25.  
  26.     for (i=0; i<4; i++)
  27.     {
  28.         if (cameraId == i)
  29.             continue;
  30.  
  31.         float3 position = cameraPos + normalize(direction)*0.1;
  32.  
  33.         int playerIndex = i;
  34.  
  35.         float3 value = float3(0, 0, 0);
  36.         float alpha = 1;
  37.         float distance_from_nearplane = 0;
  38.  
  39.         while(distance_from_nearplane < distance_to_wall)
  40.         {
  41.             //float3 distance_2 = length(lightPos[playerIndex] - position);
  42.             //float lightAttenuation = 1 / pow(distance_2, cameraId+1);
  43.             float3 distance = lightPos[playerIndex] - position;
  44.             float lightAttenuation = 1 / dot(distance, distance);
  45.  
  46.             float shadow = 1.0f;
  47.             float lightIntensity=1.1337f;
  48.  
  49.             alpha = pow(1-opacity, distance_from_nearplane);
  50.             alpha_sum += alpha * opacity;
  51.             value.rgb = (alpha * opacity)*(lightColors[playerIndex] * lightIntensity * lightAttenuation * shadow);
  52.  
  53.             distance_from_nearplane = length(position - cameraPos) - 0.1;
  54.             outColor += float4(value, 0);
  55.  
  56.             float rayLength = length(lightPos[playerIndex] - position)/5;
  57.             position += rayDirection*(rayLength + 0.1);
  58.         }
  59.     }
  60.  
  61.     for (i = 0; i < MaxLights; ++i)
  62.     {
  63.         float3 lightDirection = (lightPos[i] - inWorldPos);
  64.         float atten = 1.0f / (length(lightDirection)*linearLightFactor + dot(lightDirection, lightDirection)*quadraticLightFactor + constantLightFactor);
  65.        
  66.         lightDirection = normalize(lightDirection);
  67.         float3 h = normalize(lightDirection + inCameraDir);
  68.        
  69.         float nDotL = saturate(dot(inNormal, lightDirection));
  70.         float nDotH = saturate(dot(inNormal, h));
  71.  
  72.         float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, shininess);
  73.  
  74.         float4 color = (material_ambient) + (material_diffuse * nDotL);// + (material_specular * power);
  75.        
  76.         color *= atten;
  77.         color *= shadowBrightness(inWorldPos, i);
  78.         //color *= lightRad[i];
  79.  
  80.         outColor += color*(1-alpha_sum)*float4(lightColors[i], 1);
  81.     }
  82.  
  83.     outColor.a = 1;
  84. }
RAW Paste Data