Debian Codegraph @

Shaders/thrustflame-ALS.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 uniform float random_seed;
18
19 uniform float flame_color_low_r;
20 uniform float flame_color_low_g;
21 uniform float flame_color_low_b;
22
23 uniform float flame_color_high_r;
24 uniform float flame_color_high_g;
25 uniform float flame_color_high_b;
26
27 uniform float base_flame_r;
28 uniform float base_flame_g;
29 uniform float base_flame_b;
30
31 uniform int use_shocks;
32 uniform int use_noise;
33
34 float Noise2D(in vec2 coord, in float wavelength);
35
36 const int n_steps = 15;
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 = smoothstep(0.0, 5.0, pos.x) ;
59 float longFade = pos.x/5.0;
60
61 float density = 1.0 - longFade;
62 float radius = flame_radius_fraction + thrust_collimation * 1.0 * pow((pos.x+0.1),0.5);
63
64 if (d_rad > radius) {return 0.0;}
65
66
67 if (use_noise ==1)
68 {
69 noise = Noise2D(vec2(pos.x - osg_SimulationTime * 30.0 + random_seed , d_rad), noise_scale);
70 }
71
72 density *= (1.0 - smoothstep(0.125, radius, d_rad)) * (1.0 - noise_strength + noise_strength* noise);
73
74 if (use_shocks == 1)
75 {
76 float shock = sin(pos.x * 10.0 * shock_frequency);
77 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)) * (1.0 + 0.5 * base_flame_density);
78 }
79
80
81 return 10.0 * thrust_density * density / (radius/0.2);
82 }
83
84
85
86 void main()
87 {
88
89 vec3 vDir = normalize(viewDir);
90
91 float x_E, y_E, z_E;
92
93 if (vDir.x > 0.0) {x_E = 5.0;} else {x_E = 0.0;}
94 if (vDir.y > 0.0) {y_E = 1.0;} else {y_E = -1.0;}
95 if (vDir.z > 0.0) {z_E = 1.0;} else {z_E = -1.0;}
96
97 float t_x = (x_E - vertex.x) / vDir.x;
98 float t_y = (y_E - vertex.y) / vDir.y;
99 float t_z = (z_E - vertex.z) / vDir.z;
100
101 float t_min = min(t_x, t_y);
102 t_min = min(t_min, t_z);
103
104
105 float dt = t_min / float(n_steps);
106
107 vec3 step = viewDir * dt;
108 vec3 pos = vertex;
109
110 float density1 = 0.0;
111 float density2 = 0.0;
112
113
114
115 for (int i = 0; i < n_steps; i++)
116 {
117 pos = pos + step;
118 density1 += spherical_smoothstep(pos) * dt;
119 density2 += thrust_flame(pos) * dt;
120 }
121
122
123
124
125 float density = density1 + density2;
126 //density = clamp(density,0.0,1.0);
127 density = 1.0 - exp(-density);
128
129 density1 = 1.0 - exp(-density1);
130 density2 = 1.0 - exp(-density2);
131
132
133 vec3 flame_color_low = vec3 (flame_color_low_r, flame_color_low_g, flame_color_low_b);
134 vec3 flame_color_high = vec3 (flame_color_high_r, flame_color_high_g, flame_color_high_b);
135
136 vec3 color = mix(flame_color_low, flame_color_high, density2);
137 color = mix(color, vec3(base_flame_r, base_flame_g, base_flame_b), density1);
138
139 vec4 finalColor = vec4 (color.rgb, density);
140
141 gl_FragColor = finalColor;
142 }