alien orb ball

https://www.shadertoy.com/view/M3yGzh

Shader code
shader_type canvas_item;

#define iResolution 1.0/SCREEN_PIXEL_SIZE
#define iTime TIME
uniform vec2 camera  = vec2(0.5);
// "RayMarching starting point" 
// by Martijn Steinrucken aka The Art of Code/BigWings - 2020
// The MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Email: countfrolic@gmail.com
// Twitter: @The_ArtOfCode
// YouTube: youtube.com/TheArtOfCodeIsCool
// Facebook: https://www.facebook.com/groups/theartofcode/
//
// You can use this shader as a template for ray marching shaders

#define MAX_STEPS 100
#define MAX_DIST 100.
#define SURF_DIST .001
#define TAU 6.283185
#define PI 3.141592
#define S smoothstep
#define T iTime / 2.

mat2 Rot(float a) {
    float s=sin(a), c=cos(a);
    return mat2(vec2(c, -s), vec2(s, c));
}

float sdBox(vec3 p, vec3 s) {
    p = abs(p)-s;
	return length(max(p, 0.))+min(max(p.x, max(p.y, p.z)), 0.);
}

float BallGyroid(vec3 p){
    p.yz *= Rot(T);
    p *= 10.;
    return abs(0.7*dot(sin(p), cos(p.yzx))/10.) - .02;
}

float smin(float a, float b, float k) {
    float h = clamp(.5+.5*(b-a)/k, 0., 1.);
    return mix(b, a, h) - k*h*(1.-h);
}

float GetDist(vec3 p) {
    float ball = length(p) - 1.;
    ball = abs(ball)-.03;
    float g = BallGyroid(p);
    
    ball = smin(ball, g, -.01);
    
    float ground = p.y + 1.;
    p.z -= T;
    p *= 5.;
    p.y = sin(p.z);
    float y = abs(dot(sin(p), cos(p.yzx)))*.1;
    ground += y;
    
    float m = min(ball, ground);
    return m;
}

float RayMarch(vec3 ro, vec3 rd) {
	float dO=0.;
    
    for(int i=0; i<MAX_STEPS; i++) {
    	vec3 p = ro + rd*dO;
        float dS = GetDist(p);
        dO += dS;
        if(dO>MAX_DIST || abs(dS)<SURF_DIST) break;
    }
    
    return dO;
}

vec3 GetNormal(vec3 p) {
    vec2 e = vec2(.001, 0);
    vec3 n = GetDist(p) - 
        vec3(GetDist(p-e.xyy), GetDist(p-e.yxy),GetDist(p-e.yyx));
    
    return normalize(n);
}

vec3 GetRayDir(vec2 uv, vec3 p, vec3 l, float z) {
    vec3 
        f = normalize(l-p),
        r = normalize(cross(vec3(0,1,0), f)),
        u = cross(f,r),
        c = f*z,
        i = c + uv.x*r + uv.y*u;
    return normalize(i);
}

float Hash21(vec2 p) {
    p = fract(p*vec2(52.432, 34.155));
    p += dot(p, p+76.48);
    return fract(p.x * p.y);
}

float Glitter(vec2 p, float a) {
    p *= 10.;
    vec2 id = floor(p);
    p = fract(p) - .5;
    float n = Hash21(id);
    
    float d = length(p);
    float m = S(.5*n, 0., d);
    
    m *= pow(sin(a + fract(n*10.)*TAU)*0.5+0.5, 100.);
    return m;
}

vec3 RayPlane(vec3 ro, vec3 rd, vec3 p, vec3 n){
    float c = max(0., dot(p-ro, n)/dot(rd, n));
    return ro + rd*c;
}

void fragment()
{
    vec2 uv = UV - 0.5;
	uv.y = -uv.y;
	uv.x /= SCREEN_PIXEL_SIZE.x / SCREEN_PIXEL_SIZE.y;
	vec2 m = camera.xy/iResolution.xy;
    float cds = dot(uv, uv);

    vec3 ro = vec3(0, 3, -3)*.6;
    ro.yz *= Rot(-m.y*PI+1.);
    ro.y = max(ro.y, -0.9);
    ro.xz *= Rot(-m.x*TAU+T*.5);
    
        if (m.x > 0. || m.y > 0.) {
        m -= .5;
    }
    
    vec3 rd = GetRayDir(uv, ro, vec3(0,0.,0), 1.);
    vec3 col = vec3(0);
   
    float d = RayMarch(ro, rd);

    if(d<MAX_DIST) {
        vec3 p = ro + rd * d;
        vec3 n = GetNormal(p);
        vec3 r = reflect(rd, n);
        vec3 lightDir = -normalize(p);
        float dif = dot(n, lightDir)*.5+.5;
        col = vec3(dif);
        
        float cd = length(p);
        float w = cd * .02;
        if (cd > 1.04) {
            float s = BallGyroid(normalize(p));
            float shadow = S(-w, w, s);
            col *= shadow;
            
            p.z -= T;
            col += Glitter(p.xz * 6., dot(ro, vec3(1.))-T*4.)*3.*shadow;
            col /= cd*cd;
        } else { // hit the ball
            // sub surface scattering
            float sss = S(.2, 0., cds);
            sss = min(2., sss);
            float s = BallGyroid(p+sin(p*10.+T)*.02);
            sss *= S(-0.02, 0., s);
            col += sss*vec3(1., .2, .3);
        }
    }
    
    
    vec3 lightCol = vec3(1., 0.7, 0.7);
    float light = .005/cds;
    col += light*S(0., .5, d - 2.)*lightCol;
    
    float s = BallGyroid(normalize(ro));
    col += light*S(0., .03, s)*lightCol;
    
    // volumetrics
    //float a = atan(uv.x, uv.y); // -pi to pi
    //float sb = sin(a*11.-T)*sin(a*7.+T)*sin(a*5.+T);
    //sb *= S(.0, .4, cd);
    //col += max(0., sb);
    vec3 pp = RayPlane(ro, rd, vec3(0), normalize(ro));
    float sb = BallGyroid(normalize(pp));
    sb *= S(.0, .4, cds);
    col += max(0., sb*2.);
    
    col *= 1. - cds;
    col = pow(col, vec3(.4545));	// gamma correction
    
    COLOR = vec4(col,1.0);
}
This shader is a port from an existing Shadertoy project. Shadertoy shaders are by default protected under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) license unless anything else has been stated by the author. For more info, see our License terms.

More from RayL019

starfield angled

cartoon explosion effect

BUBBLE TEA TIME

Related shaders

Mana Resource Orb Shader

electric ball canvas item

fireball fire ball with light

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments