summaryrefslogtreecommitdiffstats
path: root/fog.cginc
blob: db90f8ce3ef798c19b96790e417ceaa6632201a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __FOG_INC
#define __FOG_INC

#include "cnlohr.cginc"
#include "interpolators.cginc"
#include "globals.cginc"

#if defined(_RAYMARCHED_FOG)

struct FogParams {
    float steps;
    float density;
    float y_cutoff;
    texture2D dithering_noise;
    texture3D density_noise;
    float4 density_noise_scale;
};

struct FogResult {
    float4 color;
    float depth;
};

FogResult raymarched_fog(v2f i, FogParams p)
{
  float3 ro = _WorldSpaceCameraPos;
  float3 rd = normalize(i.eyeVec.xyz);

  ro += rd * 1E-3;

  float2 screen_uv = (i.pos.xy + 0.5) / _ScreenParams.xy;
  float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv);
  float linearZ =
    GetLinearZFromZDepth_WorksWithMirrors(zDepthFromMap, screen_uv);
  linearZ = min(1E3, linearZ);

  // Get intersection with plane at elevation y.
  float plane_y = -10;
  float distance_to_y = 1E3;
  if (abs(rd.y) > 1E-6) {
    float t = (plane_y - ro.y) / rd.y;
    if (t > 0) {
      distance_to_y = min(t, 1E3);
    }
  }
  linearZ = min(linearZ, distance_to_y);

  float step_size = linearZ / p.steps;
  float3 pp = ro;
  float d = 0;
  for (uint ii = 0; ii < p.steps; ++ii) {
    pp += step_size * rd;
    float cur_d = p.density_noise.SampleLevel(linear_repeat_s,
        pp * p.density_noise_scale.xyz, 0);
    cur_d *= p.density * step_size;

    d = d + (1 - d) * cur_d;
  }

  FogResult r;
  r.color.rgb = 1;
  r.color.rgb = saturate(log(linearZ) / 5.0);
  //r.color.a = saturate(d);
  r.color.a = 1;
  r.depth = 0.0001;  // Very small depth value to render in front
  return r;
}

#endif  // _RAYMARCHED_FOG
#endif  // __FOG_INC