mrDIMAS

Volume fog new

Nov 13th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include "common.h"
  2.  
  3. sampler depthSampler : register(s0);
  4. sampler fogTexture : register(s1);
  5.  
  6. float4x4 invViewProj : register(c0);
  7. float3 boundingBox[2] : register(c4);
  8. float3 cameraPos : register(c6);
  9. float3 fogColor : register(c7);
  10. float fogDensity : register(c8);
  11. float3 fogOffset : register(c9);
  12.  
  13. // original code of ray-box intersection
  14. // https://github.com/hpicgs/cgsee/wiki/Ray-Box-Intersection-on-the-GPU
  15. struct Ray {
  16.     float3 origin;
  17.     float3 direction;
  18.     float3 invDirection;
  19.     int sign[3];
  20. };
  21.  
  22. Ray MakeRay(float3 origin, float3 direction) {
  23.     Ray ray;
  24.     ray.origin = origin;
  25.     ray.direction = direction;
  26.     ray.invDirection = float3(1.0, 1.0, 1.0) / direction;
  27.     ray.sign[0] = (ray.invDirection.x < 0) ? 1 : 0;
  28.     ray.sign[1] = (ray.invDirection.y < 0) ? 1 : 0;
  29.     ray.sign[2] = (ray.invDirection.z < 0) ? 1 : 0;
  30.     return ray;
  31. }
  32.  
  33. void RayBoxIntersection(in Ray ray, in float3 aabb[2],out float tmin, out float tmax) {
  34.     float tymin, tymax, tzmin, tzmax;
  35.     tmin = (aabb[ray.sign[0]].x - ray.origin.x) * ray.invDirection.x;
  36.     tmax = (aabb[1-ray.sign[0]].x - ray.origin.x) * ray.invDirection.x;
  37.     tymin = (aabb[ray.sign[1]].y - ray.origin.y) * ray.invDirection.y;
  38.     tymax = (aabb[1-ray.sign[1]].y - ray.origin.y) * ray.invDirection.y;
  39.     tzmin = (aabb[ray.sign[2]].z - ray.origin.z) * ray.invDirection.z;
  40.     tzmax = (aabb[1-ray.sign[2]].z - ray.origin.z) * ray.invDirection.z;
  41.     tmin = max(max(tmin, tymin), tzmin);
  42.     tmax = min(min(tmax, tymax), tzmax);
  43. }
  44.  
  45. float4 main( float2 texCoord : TEXCOORD0 ) : COLOR0 {
  46.     float tmin, tmax;
  47.     float4 outColor = float4(fogColor, 0);
  48.     float3 p = RestorePositionFromDepth(texCoord, invViewProj, depthSampler);
  49.     float3 direction = p - cameraPos;
  50.     float distance = length(direction);
  51.     Ray ray = MakeRay(cameraPos, direction);
  52.    
  53.     RayBoxIntersection(ray, boundingBox, tmin, tmax);
  54.    
  55.     // got intersection
  56.     if(tmin <= tmax) {     
  57.         if(tmin > 0) { // camera is outside the fog
  58.             float3 pMin = cameraPos + tmin * direction;
  59.             float3 pMax = cameraPos + tmax * direction;
  60.             float3 v = cameraPos - pMin;           
  61.             if(distance > length(v)) { // "z-cull"
  62.                 float thickness = length(pMax - pMin);     
  63.                 float dTarget = length(p - pMin);          
  64.                 outColor.a = fogDensity * (dTarget / thickness);
  65.                 // modulate density
  66.                 for(float k = 0; k < 0.5; k += 0.05) {
  67.                     float3 trace = 0.05f * (cameraPos + (tmin + k) * direction);
  68.                     float4 density = tex3Dlod(fogTexture, float4(fogOffset + trace, 0));
  69.                     outColor.a += 0.05 * dot(float4(1, 0.5, 0.25, 0.125), density);
  70.                 }
  71.             }          
  72.         } else if(tmin <= 0 && tmax > 0) { // camera is inside the fog
  73.             float thickness = length(tmax * direction);    
  74.             float dTarget = length(p - cameraPos);         
  75.             outColor.a = fogDensity * (dTarget / thickness);   
  76.  
  77.             // modulate density
  78.             for(float k = 0; k < 0.5; k += 0.05) {
  79.                 float3 trace = 0.05f * ((tmax + k) * direction);
  80.                 float4 density = tex3Dlod(fogTexture, float4(fogOffset + trace, 0));
  81.                 outColor.a += 0.05 * dot(float4(1, 0.5, 0.25, 0.125), density);
  82.             }      
  83.         }
  84.     }
  85.    
  86.  
  87.    
  88.     return outColor;
  89. };
Advertisement
Add Comment
Please, Sign In to add comment