Debian Codegraph @

Shaders/thrustflame-ALS-detailed.frag

raw
1 // -*-C++-*-
2
3 #version 120
4
5 varying vec3 vertex;
6 varying vec3 viewDir;
7
8 uniform float osg_SimulationTime;
9 uniform float thrust_collimation;
10 uniform float flame_radius_fraction;
11 uniform float thrust_density;
12 uniform float base_flame_density;
13 uniform float shock_frequency;
14 uniform float noise_strength;
15 uniform float noise_scale;
16 uniform float deflection_coeff;
17
18 uniform float flame_color_low_r;
19 uniform float flame_color_low_g;
20 uniform float flame_color_low_b;
21
22 uniform float flame_color_high_r;
23 uniform float flame_color_high_g;
24 uniform float flame_color_high_b;
25
26 uniform float base_flame_r;
27 uniform float base_flame_g;
28 uniform float base_flame_b;
29
30 uniform int use_shocks;
31 uniform int use_noise;
32
33 float Noise3D(in vec3 coord, in float wavelength);
34
35
36 const int n_steps = 25;
37
38 float spherical_smoothstep (in vec3 pos)
39 {
40
41 float l = length(vec3 (pos.x/2.0, pos.y,pos.z) );
42
43 return 10.0 * thrust_density * base_flame_density * (1.0 - smoothstep(0.5* flame_radius_fraction, flame_radius_fraction, l));
44
45 }
46
47
48
49 float thrust_flame (in vec3 pos)
50 {
51
52
53 float noise = 0.0;
54
55 pos.z +=8.0 * deflection_coeff;
56
57 float d_rad = length(pos.yz - vec2 (0.0, deflection_coeff * pos.x * pos.x));
58 float longFade = pos.x/5.0;
59
60 float density = 1.0 - longFade;
61 float radius = flame_radius_fraction + thrust_collimation * 1.0 * pow((pos.x+0.1),0.5);
62
63 if (d_rad > radius) {return 0.0;}
64
65
66 if (use_noise ==1)
67 {
68 noise = Noise3D(vec3(pos.x - osg_SimulationTime * 30.0 , pos.y, pos.z), noise_scale);
69 }
70
71 density *= (1.0 - smoothstep(0.125, radius, d_rad)) * (1.0 - noise_strength + noise_strength* noise);
72
73 if (use_shocks == 1)
74 {
75 float shock = sin(pos.x * 10.0 * shock_frequency);
76 density += shock * shock * shock * shock * (1.0 - longFade) * (1.0 - smoothstep(0.25*flame_radius_fraction, 0.5*flame_radius_fraction, d_rad)) * (1.0 - smoothstep(0.0, 1.0, thrust_collimation));
77 }
78
79
80 return 10.0 * thrust_density * density / (radius/0.2);
81 }
82
83
84
85 void main()
86 {
87
88 vec3 vDir = normalize(viewDir);
89
90 float x_E, y_E, z_E;
91
92 if (vDir.x > 0.0) {x_E = 5.0;} else {x_E = 0.0;}
93 if (vDir.y > 0.0) {y_E = 1.0;} else {y_E = -1.0;}
94 if (vDir.z > 0.0) {z_E = 1.0;} else {z_E = -1.0;}
95
96 float t_x = (x_E - vertex.x) / vDir.x;
97 float t_y = (y_E - vertex.y) / vDir.y;
98 float t_z = (z_E - vertex.z) / vDir.z;
99
100 float t_min = min(t_x, t_y);
101 t_min = min(t_min, t_z);
102
103
104 float dt = t_min / float(n_steps);
105
106 vec3 step = viewDir * dt;
107 vec3 pos = vertex;
108
109 float density1 = 0.0;
110 float density2 = 0.0;
111
112
113
114 for (int i = 0; i < n_steps; i++)
115 {
116 pos = pos + step;
117 density1 += spherical_smoothstep(pos) * dt;
118 density2 += thrust_flame(pos) * dt;
119 }
120
121
122
123
124 float density = density1 + density2;
125 density = 1.0 - exp(-density);
126
127 density1 = 1.0 - exp(-density1);
128 density2 = 1.0 - exp(-density2);
129
130
131 vec3 flame_color_low = vec3 (flame_color_low_r, flame_color_low_g, flame_color_low_b);
132 vec3 flame_color_high = vec3 (flame_color_high_r, flame_color_high_g, flame_color_high_b);
133
134 vec3 color = mix(flame_color_low, flame_color_high, density2);
135 color = mix(color, vec3(0.8, 1.0, 1.0), density1);
136
137 vec4 finalColor = vec4 (color.rgb, density);
138
139
140 gl_FragColor = finalColor;
141 }