128 float pscale = 1 / 10000;
129 vec3 scaled_cam = cam_pos.xyz - p[0].p_m.xyz;
130 vec3 pos = aPosition;
131 float size = pow(p[0].p_m.w, 1.0/3.0);
132 float inner =
size * 0.99;
135 float fscale = 1 / (
size - inner);
136 float fscale_over_depth = fscale / 4.0f;
139 vec3 sphere_ray_to_cam = pos - scaled_cam.xyz;
140 float fFar = length(sphere_ray_to_cam);
141 sphere_ray_to_cam /= fFar;
142 float b = 2.0 * (dot(scaled_cam.xyz, sphere_ray_to_cam));
143 float c = dot(scaled_cam.xyz, scaled_cam.xyz) -
size *
size;
144 float det = max(0.0, b * b - 4.0 * c);
147 float fNear = 0.5 * -b - sqrt(det)* 0.5;
149 fNear = max(fNear, 0.1);
151 vec3 start = scaled_cam.xyz + sphere_ray_to_cam * fNear;
154 float start_height = length(start);
155 float start_depth = exp(fscale_over_depth * (inner - start_height));
156 float start_angle = 0;
157 if (start_height > 0) start_angle = dot(sphere_ray_to_cam, start) / start_height;
158 float start_offset = start_depth *
scale(start_angle);
160 float sample_length = fFar /
samples;
161 float scaled_length = sample_length * fscale;
162 vec3 sample_ray = sphere_ray_to_cam * sample_length;
166 for (
int i = 0; i <
samples; i++)
169 float ti = (i + 0.5f) /
float(
samples);
171 vec3 sample_point = start + sphere_ray_to_cam* ti;
173 float height = length(sample_point);
174 float depth = exp(fscale_over_depth * (inner - height));
175 float light_angle = 0;
176 if (height > 0) light_angle =
clamp(dot(
L, sample_point) / height, -1, 1);
177 float camera_angle = 0;
178 if (height > 0) camera_angle =
clamp(dot(sphere_ray_to_cam, sample_point) / height, -1, 1);
179 float scatter = (start_offset +
depth * (
scale(light_angle) -
scale(camera_angle)));
181 color += att * (
depth * scaled_length);
188 t0 = (-sphere_ray_to_cam);
194 gl_Position = worldPos;
int main(int argc, char *argv[])