Space Sim
Loading...
Searching...
No Matches
clouds.frag
Go to the documentation of this file.
1
2#version 430 core
3
7uniform samplerCube uCubemap;
10uniform float uInnerRadius;
11uniform float uOuterRadius;
12uniform sampler3D fbm_tex;
13uniform sampler3D perlin_tex;
14uniform sampler3D worley_tex;
15uniform float uPlanetRadius;
16const int samples = 2;
17layout(std140) uniform CameraData {
18 vec4 cam_pos;
19 vec4 cam_vel;
21 mat4 view;
22 mat4 inv_projection;
23 mat4 inv_view;
24};
25
26#define MAX_LAYERS 24
27struct WarpLayer
28{
30 float freq0;
32 float freq1;
34 float freq2;
35 float amp;
36 float pad0;
37 float pad1;
38 float pad3;
39};
40
41struct PerlinLayer
42{
43 float freq;
44 float amp;
45 float pad0;
46 float pad1;
47};
48
49struct FBMLayer
50{
51 float freq;
52 float amp;
53 float pad0;
54 float pad1;
55};
56
57layout(std430, binding=13) buffer PlanetData {
58
59 int warp_count;
60 int perlin_count;
61 int fbm_count;
62 int pad0;
65
66 WarpLayer warp_layers[MAX_LAYERS];
67 PerlinLayer perlin_layers[MAX_LAYERS];
68 FBMLayer fbm_layers[MAX_LAYERS];
69
70};
71void main()
72{
73 vec3 scaled_cam = uCamPosWorld;
74 vec3 dir = vWorldPos - cam_pos.xyz;
75 float fFar = length(dir);
76 dir /= fFar;
77 float b = 2.0 * (dot(scaled_cam.xyz, dir));
78 float c = dot(scaled_cam.z, scaled_cam.z) - uOuterRadius * uOuterRadius;
79 float det = max(0.0, b * b - 4.0 * c);
80 float fNear = 0.5 * (-b - sqrt(det));
81 fFar = 0.5 * (-b + sqrt(det));
82
83 fNear = max(fNear, 0.0);
84
85 vec3 start = scaled_cam.xyz + dir * fNear;
86 fFar -= fNear;
87
88
89 float sample_length = fFar / samples;
90
91
92 // swap start to end
93 start = start + dir * fFar;
94 dir = -dir;
95 vec3 ray = dir * sample_length;
96 vec3 point = start + ray * 0.5;
97 vec3 offset = vec3(cam_pos.xy / 10000, 0);
98 point += offset;
99
100 float diff = uOuterRadius - uInnerRadius;
101 vec3 color = vec3(0,0,0);
102 float transmittance = 1;
103 for (int i = 0; i < samples; i++)
104 {
105
106
107 float len = length(point - offset);
108 float h = len - uInnerRadius;
109 h = h / diff;
110 float thickness = smoothstep(0.1, 0.2, h) * (1-smoothstep(0.7, 0.9, h));
111 vec4 s = texture(worley_tex, (point * 3 * 0.5 + 0.5)).rgba;
112 float f = texture(fbm_tex, (point * 50 * 0.5 + 0.5)).r * 2 - 1;
113 float d = 0;
114 d += (s.r + s.g * 0.5) * 0.5;
115 d += f * 0.2;
116 d -= (s.b + s.a) * 0.2;
117
118 d = max(d, 0.01) * thickness;
119
120 float absorb = (exp(-d * (1 / sample_length) * 2));
121 color += d * transmittance /* * dot(dir, sun_dir.xyz) */;
122 transmittance *= absorb;
123
124
125
126 point += ray;
127 }
128 color /= samples;
129
130
131
132 mat3 rotation = mat3(
133 vec3(1, 0, 0),
134 vec3(0, 0, 1),
135 vec3(0, -1, 0)
136 );
137 vec3 pos = vPos * rotation;
138
139
140 fragColor = vec4((color.rgb + texture(uCubemap, pos).rgb) / 1.25, 1.0);
141}
out vec4 fragColor
in vec3 vPos
uniform float uOuterRadius
uniform vec3 uCamPosWorld
const int samples
uniform float uInnerRadius
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
in vec3 vColor
Definition clouds.frag:4
uniform float uPlanetRadius
Definition clouds.frag:15
in vec3 vWorldPos
Definition clouds.frag:9
uniform samplerCube uCubemap
Definition clouds.frag:7
vec3 sun_dir
Definition main.c:237
vec3 sun_color
Definition main.c:238
float * perlin_tex
Definition init.c:610
float * fbm_tex
Definition init.c:346
vec4 * worley_tex
Definition init.c:377
int main(int argc, char *argv[])
Definition main.c:1726
bool s
Definition menu.c:6
uniform mat4 projection
Definition shadow.vert:8
uniform mat4 view
Definition shadow.vert:9
float z
Definition main.h:5
#define MAX_LAYERS
Definition types.h:407
Definition types.h:51
Definition main.h:5
Definition types.h:47
vec3 offset2
Definition types.h:378
float pad1
Definition types.h:403
float amp
Definition types.h:401
float freq0
Definition types.h:375
vec3 offset1
Definition types.h:376
float freq
Definition types.h:400
float amp
Definition types.h:391
float pad3
Definition types.h:383
float freq1
Definition types.h:377
float pad1
Definition types.h:382
float pad0
Definition types.h:392
float freq2
Definition types.h:379
vec3 offset0
Definition types.h:374
float pad1
Definition types.h:393
float pad0
Definition types.h:381
float amp
Definition types.h:380
float freq
Definition types.h:390
float pad0
Definition types.h:402