Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Raymarching Terrain Basics
- */
- #define MARCH_STEPS 62
- #define MIN_DIST .0
- #define MAX_DIST 4.
- #define EPSILON .001
- #define NUM_OCTAVES 8
- // toggle for cave
- #define CAVE 1
- /*
- Random, Noise and FBM
- */
- float random(in vec2 st) {
- return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.1);
- }
- float noise(vec2 p) {
- vec2 i = ceil(p);
- vec2 f = fract(p);
- vec2 u = f * f * (3. - 2. * f);
- float a = random(i);
- float b = random(i + vec2(1., 0.));
- float c = random(i + vec2(0., 1.));
- float d = random(i + vec2(1., 1.));
- return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
- }
- float fbm(in vec2 p) {
- float s = 0.0;
- float m = 0.0;
- float a = 0.5;
- for(int i = 0; i < NUM_OCTAVES; i++) {
- s += a * noise(p);
- m += a;
- a *= 0.5;
- p *= 2.0;
- }
- return s / m;
- }
- /*
- Raymarch functions
- */
- // get position along dist
- vec3 get_position(vec3 o, vec3 d, float t) {
- return o + d * t;
- }
- // terrain map function
- float map(vec3 p) {
- // y-offset based on fbm
- float yd = .2 + (
- (.8 * fbm(p.xz)) +
- (.1 * fbm(p.xz * 10.))
- );
- float y = p.y + yd;
- return dot(
- vec3(p.x, y, p.z),
- vec3(0., 1., 0.)
- );
- #endif
- }
- // find the closest point along ray
- float trace_ray(vec3 origin, vec3 direction) {
- float depth = MIN_DIST;
- for (int i = 0; i < MARCH_STEPS; i++) {
- float dist = map(get_position(origin, direction, depth));
- depth += dist;
- }
- return depth;
- }
- // projected ray
- vec3 ray_direction(vec2 uv, float fov) {
- float z = 2. / tan(radians(fov) / 2.0);
- return normalize(vec3(uv, z));
- }
- /*
- Main shader
- */
- void mainImage(out vec4 O, in vec2 I) {
- vec2 R = iResolution.xy;
- vec2 uv = (2. * I - R) / R.y;
- //uv.y *= -1.;
- vec3 eye = vec3(0., -.05, iTime);
- vec3 dir = ray_direction(uv, 90.);
- //float dist = trace_ray(eye, dir);
- float dist = trace_ray(eye, dir);
- O = vec4(vec3(1. / dist), 1.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement