Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
help with shader distortion sdf
Post Body

Hi all,

im having an issue where my shader is being distorted. i know its an issue with a true sdf calculation and raymarching, and i may need to implement a more robust sdf calculation, but am unsure how. heres my code (supposed to be desert rock formations):

#define MAX_DIST 100.0
#define MAX_STEPS 100
#define THRESHOLD 0.01

#include "lygia/math/rotate3dX.glsl"
#include "lygia/generative/snoise.glsl"

struct Light {
    vec3 pos;
    vec3 color;
};

struct Material {
    vec3 ambientColor;
    vec3 diffuseColor;
    vec3 specularColor;
    float shininess;
};

Material dirt() {
    vec3 aCol = 0.4 * vec3(0.5, 0.35, 0.2);
    vec3 dCol = 0.7 * vec3(0.55, 0.4, 0.25);
    vec3 sCol = 0.3 * vec3(1.0);
    float a = 16.0;
    return Material(aCol, dCol, sCol, a);
}

float fbm(vec3 p) {
    float f = 0.0;
    float amplitude = 0.5;
    float frequency = 0.5; 
    for(int i = 0; i < 6; i  ) { 
        f  = amplitude * snoise(p * frequency);
        p *= 2.0;
        amplitude *= 0.5;
        frequency *= 1.5; 
    }
    return f;
}

float rockHeight(vec2 p) {
    float base = 1.2 * fbm(vec3(p.x * 0.3, 0.0, p.y * 0.3)) - 0.4;
    float spikes = abs(snoise(vec3(p.x * 0.4, 0.0, p.y * 0.4)) * 2.0) - 0.6;
    return base   spikes;
}

float sdPlane(vec3 p, vec3 n, float h) {
    return dot(p, n)   h;
}

vec2 scene(vec3 p) {
    vec2 horizontalPos = vec2(p.x, p.z);
    float terrainHeight = rockHeight(horizontalPos);
    float d = p.y - terrainHeight;
    return vec2(d, 0.0);
}

vec3 calcNormal(vec3 p) {
    const float h = 0.0001; 
    return normalize(vec3(
        scene(p   vec3(h, 0.0, 0.0)).x - scene(p - vec3(h, 0.0, 0.0)).x,
        scene(p   vec3(0.0, h, 0.0)).x - scene(p - vec3(0.0, h, 0.0)).x,
        scene(p   vec3(0.0, 0.0, h)).x - scene(p - vec3(0.0, 0.0, h)).x
    ));
}

float shadows(vec3 rayOrigin, vec3 lightDir) {
    float d = 0.0;
    float shadow = 1.0;
    for(int i = 0; i < MAX_STEPS; i  ) {
        vec3 p = rayOrigin   d * lightDir;
        float sd = scene(p).x;
        if(sd < THRESHOLD) {
            shadow = 0.0;
            break;
        }
        d  = sd;
        if(d > MAX_DIST) {
            break;
        }
    }
    return shadow;
}

vec3 lighting(vec3 p) {
    vec3 layerColor1 = vec3(0.8, 0.4, 0.2);
    vec3 layerColor2 = vec3(0.7, 0.3, 0.1);
    vec3 layerColor3 = vec3(0.9, 0.5, 0.3);

    float layerHeight1 = 0.0;
    float layerHeight2 = 0.5;
    float layerHeight3 = 1.0;

    vec3 baseColor;
    if (p.y < layerHeight1) {
        baseColor = layerColor1;
    } else if (p.y < layerHeight2) {
        baseColor = layerColor2;
    } else if (p.y < layerHeight3) {
        baseColor = layerColor3;
    } else {
        baseColor = layerColor1;
    }

    vec3 lightDir = normalize(vec3(-0.5, 0.8, 0.6));
    vec3 ambient = vec3(0.2); 

    vec3 norm = calcNormal(p);
    float diffuse = max(dot(norm, lightDir), 0.0); 

    vec3 color = ambient * baseColor   diffuse * baseColor; 

    return color;
}

vec3 rayMarch(vec3 rayOrigin, vec3 rayDir) {
    float d = 0.0;
    for(int i = 0; i < MAX_STEPS; i  ) {
        vec3 p = rayOrigin   d * rayDir;
        vec2 march = scene(p);
        float sd = march.x;

        if(sd < THRESHOLD) {
            return lighting(p);
        }
        d  = sd;
        if(d > MAX_DIST) {
            break;
        }
    }
    return vec3(0.53, 0.81, 0.92);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    uv = uv * 2.0 - 1.0;
    float aspectRatio = iResolution.x / iResolution.y;
    uv.x *= aspectRatio;
    float fov = 45.0;
    float scale = tan(radians(fov * 0.5));
    vec3 rd = normalize(vec3(uv.x * scale, uv.y * scale, -1.0));

    float engine = iTime * 0.5;
    vec3 ro = vec3(0.0, 2.0, 5.0 - engine);

    vec3 col = rayMarch(ro, rd);

    fragColor = vec4(col, 1.0);
}

Author
Account Strength
80%
Account Age
4 years
Verified Email
Yes
Verified Flair
No
Total Karma
258
Link Karma
41
Comment Karma
217
Profile updated: 4 days ago
Posts updated: 3 weeks ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
1 month ago