본문 바로가기
Shader/HLSL

HLSL_04

by BroJune 2021. 8. 19.

 

const float PI = 3.14159265;

 

float map(vec3 p)

{

    return length(p) - .4;

}

 

// aka calc gradient

vec3 calcNormal(vec3 p)

{

    return normalize(vec3(

        map(p + vec3(.001, .0, .0)) - map(p - vec3(.001, .0, .0)),

        map(p + vec3(.0, .001, .0)) - map(p - vec3(.0, .001, .0)),

        map(p + vec3(.0, .0, .001)) - map(p - vec3(.0, .0, .001))

    ));



}

 

void mainImage( out vec4 fragColor, in vec2 fragCoord )

{

    vec2 screen_pos = ((2.0 * fragCoord.xy) - iResolution.xy) / iResolution.y;

    

    float cam_an = iTime;

    

    vec3 cam_orig = vec3(sin(cam_an), .0, cos(cam_an));

    vec3 cam_target = vec3(.0, .0, .0);

    

    vec3 cam_forward = normalize(cam_target - cam_orig);

    vec3 cam_right = normalize(cross(cam_forward, vec3(.0, 1., .0)));

    vec3 cam_up = normalize(cross(cam_right, cam_forward));

    

    vec3 ro = cam_orig;

    vec3 rd = normalize(vec3(

        screen_pos.x * cam_right +

        screen_pos.y * cam_up +

        1. * cam_forward

    ));

    

    vec3 col = vec3(.0);

    

    float t = .0;

    

    for (int step_march = 0; step_march < 100; ++step_march)

    {

        vec3 p = ro + t * rd;

        float h = map(p);

        if(h < .001)

        {

            col = calcNormal(p);

            break;

        }

        t += h;

    }

 

   

    

    fragColor = vec4(col,1.);

}

 

 

 

const float PI = 3.14159265;

 

// iquilez

float smin(float a, float b, float k)

{

    float h = max(k - abs(a-b), .0) / k;

    return min(a, b) - h * h * k * (1./4.);

 }

 

// d1 = sphere, d2 = tube

float map(vec3 p)

{

    float d1 = length(p - vec3(3. * cos(iTime), .0, .0)) - .8;

    

    float an = 2. * sin(iTime);

    mat2 rot = mat2(

        cos(an), -sin(an), 

        sin(an), cos(an)

    );

    vec3 q = p;

    q.yz = rot * q.yz;

        

    float d2 = length(vec2(q.y, length(q.xz) - 1.)) - .4;

    

    float d = smin(d1, d2, .5);

    

    d += .05 * sin(4. * p.x + 5. * iTime) + .07 * sin(3. * p.y + 5. * iTime);

    

    return d;

}

 

// aka calc gradient

vec3 calcNormal(vec3 p)

{

    vec2 eps = vec2(.001, .0);

    return normalize(vec3(

        map(p + eps.xyy) - map(p - eps.xyy),

        map(p + eps.yxy) - map(p - eps.yxy),

        map(p + eps.yyx) - map(p - eps.yyx)

    ));



}

 

void mainImage( out vec4 fragColor, in vec2 fragCoord )

{

    vec2 screen_pos = ((2.0 * fragCoord.xy) - iResolution.xy) / iResolution.y;

    

    float cam_an = .0; // iTime;

    

    vec3 cam_orig = vec3(5. * sin(cam_an), 4., 5. * cos(cam_an));

    vec3 cam_target = vec3(.0, .0, .0);

    

    vec3 cam_forward = normalize(cam_target - cam_orig);

    vec3 cam_right = normalize(cross(cam_forward, vec3(.0, 1., .0)));

    vec3 cam_up = normalize(cross(cam_right, cam_forward));

    

    vec3 ro = cam_orig;

    vec3 rd = normalize(vec3(

        screen_pos.x * cam_right +

        screen_pos.y * cam_up +

        2. * cam_forward

    ));

    

    vec3 col = vec3(.0);

    

    float t = .0;

    

    for (int step_march = 0; step_march < 100; ++step_march)

    {

        vec3 p = ro + t * rd;

        float h = map(p);

        if(h < .001)

        {

            col = calcNormal(p);

            break;

        }

        t += h;

    }   

    

    fragColor = vec4(col,1.);

}

 

참고 자료 : https://www.udemy.com/

 

온라인 강의 - 자신의 일정에 맞춰 뭐든지 배워 보세요 | Udemy

Udemy는 155,000개 이상의 강의와 4천만명 이상의 수강생이 있는 온라인 학습 및 교수 마켓플레이스입니다. 프로그래밍, 마케팅, 데이터 과학 및 그 밖의 분야에 대해 배워 보세요.

www.udemy.com

 

'Shader > HLSL' 카테고리의 다른 글

HLSL_03  (0) 2021.08.19
HLSL_02  (0) 2021.08.19
HLSL_01  (0) 2021.08.19