Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
render
clouds.frag
Go to the documentation of this file.
1
2
#version 430 core
3
4
in
vec3
vColor
;
5
out
vec4
fragColor
;
6
in
vec3
vPos
;
7
uniform samplerCube
uCubemap
;
8
uniform
vec3
uCamPosWorld
;
9
in
vec3
vWorldPos
;
10
uniform
float
uInnerRadius
;
11
uniform
float
uOuterRadius
;
12
uniform sampler3D
fbm_tex
;
13
uniform sampler3D
perlin_tex
;
14
uniform sampler3D
worley_tex
;
15
uniform
float
uPlanetRadius
;
16
const
int
samples
= 2;
17
layout
(std140) uniform CameraData {
18
vec4
cam_pos;
19
vec4
cam_vel;
20
mat4
projection
;
21
mat4
view
;
22
mat4
inv_projection;
23
mat4
inv_view;
24
};
25
26
#define MAX_LAYERS 24
27
struct
WarpLayer
28
{
29
vec3
offset0
;
30
float
freq0
;
31
vec3
offset1
;
32
float
freq1
;
33
vec3
offset2
;
34
float
freq2
;
35
float
amp
;
36
float
pad0
;
37
float
pad1
;
38
float
pad3
;
39
};
40
41
struct
PerlinLayer
42
{
43
float
freq
;
44
float
amp
;
45
float
pad0
;
46
float
pad1
;
47
};
48
49
struct
FBMLayer
50
{
51
float
freq
;
52
float
amp
;
53
float
pad0
;
54
float
pad1
;
55
};
56
57
layout
(std430, binding=13) buffer
PlanetData
{
58
59
int
warp_count;
60
int
perlin_count;
61
int
fbm_count;
62
int
pad0;
63
vec4
sun_dir
;
64
vec4
sun_color
;
65
66
WarpLayer
warp_layers[
MAX_LAYERS
];
67
PerlinLayer
perlin_layers[
MAX_LAYERS
];
68
FBMLayer
fbm_layers[
MAX_LAYERS
];
69
70
};
71
void
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
}
fragColor
out vec4 fragColor
Definition
atmosphere.frag:9
vPos
in vec3 vPos
Definition
atmosphere.frag:10
uOuterRadius
uniform float uOuterRadius
Definition
atmosphere.vert:78
uCamPosWorld
uniform vec3 uCamPosWorld
Definition
atmosphere.vert:75
samples
const int samples
Definition
atmosphere.vert:103
uInnerRadius
uniform float uInnerRadius
Definition
atmosphere.vert:76
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
vColor
in vec3 vColor
Definition
clouds.frag:4
uPlanetRadius
uniform float uPlanetRadius
Definition
clouds.frag:15
vWorldPos
in vec3 vWorldPos
Definition
clouds.frag:9
uCubemap
uniform samplerCube uCubemap
Definition
clouds.frag:7
sun_dir
vec3 sun_dir
Definition
main.c:237
sun_color
vec3 sun_color
Definition
main.c:238
perlin_tex
float * perlin_tex
Definition
init.c:610
fbm_tex
float * fbm_tex
Definition
init.c:346
worley_tex
vec4 * worley_tex
Definition
init.c:377
main
int main(int argc, char *argv[])
Definition
main.c:1726
s
bool s
Definition
menu.c:6
projection
uniform mat4 projection
Definition
shadow.vert:8
view
uniform mat4 view
Definition
shadow.vert:9
vec3::z
float z
Definition
main.h:5
MAX_LAYERS
#define MAX_LAYERS
Definition
types.h:407
PlanetData
Definition
types.h:409
mat4
Definition
types.h:51
vec3
Definition
main.h:5
vec4
Definition
types.h:47
WarpLayer::offset2
vec3 offset2
Definition
types.h:378
FBMLayer::pad1
float pad1
Definition
types.h:403
FBMLayer::amp
float amp
Definition
types.h:401
WarpLayer::freq0
float freq0
Definition
types.h:375
WarpLayer::offset1
vec3 offset1
Definition
types.h:376
FBMLayer::freq
float freq
Definition
types.h:400
PerlinLayer::amp
float amp
Definition
types.h:391
WarpLayer::pad3
float pad3
Definition
types.h:383
WarpLayer::freq1
float freq1
Definition
types.h:377
WarpLayer::pad1
float pad1
Definition
types.h:382
PerlinLayer::pad0
float pad0
Definition
types.h:392
WarpLayer::freq2
float freq2
Definition
types.h:379
WarpLayer::offset0
vec3 offset0
Definition
types.h:374
PerlinLayer::pad1
float pad1
Definition
types.h:393
WarpLayer::pad0
float pad0
Definition
types.h:381
WarpLayer::amp
float amp
Definition
types.h:380
PerlinLayer::freq
float freq
Definition
types.h:390
FBMLayer::pad0
float pad0
Definition
types.h:402
FBMLayer
Definition
types.h:398
PerlinLayer
Definition
types.h:388
WarpLayer
Definition
types.h:372
Generated by
1.18.0