Selaa lähdekoodia

raymarching! (it's a circle, 'doh)

Lucas Stadler 10 vuotta sitten
vanhempi
commit
5290c50662
1 muutettua tiedostoa jossa 45 lisäystä ja 0 poistoa
  1. 45 0
      glsl/raymarching.frag

+ 45 - 0
glsl/raymarching.frag

@ -0,0 +1,45 @@
1
/*
2
 * Ray marching in Fragmentarium.
3
 *
4
 * Based on the `trace` function from [1], runnable in
5
 * Fragmentarium [2]. Inspired by a talk about signed
6
 * distance functions [3], Fractal Lab [4] and lots of
7
 * other things [citation needed].
8
 *
9
 * [1]: http://blog.hvidtfeldts.net/index.php/2011/06/distance-estimated-3d-fractals-part-i/
10
 * [2]: http://syntopia.github.io/Fragmentarium/
11
 * [3]: https://www.youtube.com/watch?v=s8nFqwOho-s
12
 * [4]: http://sub.blue/fractal-lab
13
 */
14
15
// 3D.frag includes camera support and only requires
16
// us to define a `color` function.  (See below.)
17
#include "3D.frag"
18
19
#group DistranceEstimator
20
uniform float MinimumDistance; slider[0.0,0.01,10.0]
21
uniform int MaximumRaySteps; slider[10,0,100]
22
23
// Adapted with minimal changes from [1].
24
float trace(vec3 from, vec3 direction) {
25
	float totalDistance = 0.0;
26
	int steps;
27
	for (steps=0; steps < MaximumRaySteps; steps++) {
28
		vec3 p = from + totalDistance * direction;
29
		float distance = DistanceEstimator(p);
30
		totalDistance += distance;
31
		if (distance < MinimumDistance) break;
32
	}
33
	return 1.0-float(steps)/float(MaximumRaySteps);
34
}
35
36
// This is where the actual calculation takes place!
37
float DistanceEstimator(vec3 pos) {
38
  return length(pos) - 1.0;
39
}
40
41
// Simple gray-scalish rendering.
42
vec3 color(vec3 pos, vec3 direction) {
43
  float dist = trace(pos, direction);
44
  return direction + vec3(dist, dist, dist);
45
}