Author Topic: Old conversion topics merged  (Read 29807 times)

0 Members and 1 Guest are viewing this topic.

Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
The Cave converted
« Reply #20 on: June 06, 2015, 01:46:54 PM »
The Cave converted to GLSL Hacker format

Copy the code and save as The_Cave_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/The Cave"
          width="800" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
uniform sampler2D iChannel2;



//

// constants for the camera tunnel
const vec2 cama=vec2(-2.6943,3.0483);
const vec2 camb=vec2(0.2516,0.1749);
const vec2 camc=vec2(-3.7902,2.4478);
const vec2 camd=vec2(0.0865,-0.1664);

const vec2 lighta=vec2(1.4301,4.0985);
const vec2 lightb=vec2(-0.1276,0.2347);
const vec2 lightc=vec2(-2.2655,1.5066);
const vec2 lightd=vec2(-0.1284,0.0731);

// calculates the position of a single tunnel
vec2 Position(float z, vec2 a, vec2 b, vec2 c, vec2 d)
{
    return sin(z*a)*b+cos(z*c)*d;
}

// calculates 3D positon of a tunnel for a given time
vec3 Position3D(float time, vec2 a, vec2 b, vec2 c, vec2 d)
{
    return vec3(Position(time,a,b,c,d),time);
}

// 2d distance field for a slice of a single tunnel
float Distance(vec3 p, vec2 a, vec2 b, vec2 c, vec2 d, vec2 e, float r)
{
    vec2 pos=Position(p.z,a,b,c,d);   
    float radius=max(5.0,r+sin(p.z*e.x)*e.y)/10000.0;
    return radius/dot(p.xy-pos,p.xy-pos);
}

// 2d distance field for a slice of the tunnel network
float Dist2D(vec3 pos)
{
    float d=0.0;
   
    d+=Distance(pos,cama,camb,camc,camd,vec2(2.1913,15.4634),70.0000);
    d+=Distance(pos,lighta,lightb,lightc,lightd,vec2(0.3814,12.7206),17.0590);
    d+=Distance(pos,vec2(2.7377,-1.2462),vec2(-0.1914,-0.2339),vec2(-1.3698,-0.6855),vec2(0.1049,-0.1347),vec2(-1.1157,13.6200),27.3718);
    d+=Distance(pos,vec2(-2.3815,0.2382),vec2(-0.1528,-0.1475),vec2(0.9996,-2.1459),vec2(-0.0566,-0.0854),vec2(0.3287,12.1713),21.8130);
    d+=Distance(pos,vec2(-2.7424,4.8901),vec2(-0.1257,0.2561),vec2(-0.4138,2.6706),vec2(-0.1355,0.1648),vec2(2.8162,14.8847),32.2235);
    d+=Distance(pos,vec2(-2.2158,4.5260),vec2(0.2834,0.2319),vec2(4.2578,-2.5997),vec2(-0.0391,-0.2070),vec2(2.2086,13.0546),30.9920);
    d+=Distance(pos,vec2(0.9824,4.4131),vec2(0.2281,-0.2955),vec2(-0.6033,0.4780),vec2(-0.1544,0.1360),vec2(3.2020,12.2138),29.1169);
    d+=Distance(pos,vec2(1.2733,-2.4752),vec2(-0.2821,-0.1180),vec2(3.4862,-0.7046),vec2(0.0224,0.2024),vec2(-2.2714,9.7317),6.3008);
    d+=Distance(pos,vec2(2.6860,2.3608),vec2(-0.1486,0.2376),vec2(2.0568,1.5440),vec2(0.0367,0.1594),vec2(-2.0396,10.2225),25.5348);
    d+=Distance(pos,vec2(0.5009,0.9612),vec2(0.1818,-0.1669),vec2(0.0698,-2.0880),vec2(0.1424,0.1063),vec2(1.7980,11.2733),35.7880);
   
    return d;
}

vec3 nmap(vec2 t, sampler2D tx, float str)
{
    float d=1.0/1024.0;

    float xy=texture2D(tx,t).x;
    float x2=texture2D(tx,t+vec2(d,0)).x;
    float y2=texture2D(tx,t+vec2(0,d)).x;
   
    float s=(1.0-str)*1.2;
    s*=s;
    s*=s;
   
    return normalize(vec3(x2-xy,y2-xy,s/8.0));///2.0+0.5;
}

void main()
{
    float time=iGlobalTime/3.0+291.0;//+43.63/3.0;

    //calculate camera by looking ahead in the tunnel
   
    vec2 p1=Position(time+0.05,cama,camb,camc,camd); //position ahead
    vec3 Pos=Position3D(time,cama,camb,camc,camd); //current position
    vec3 oPos=Pos;
   
    vec3 CamDir=normalize(vec3(p1.x-Pos.x,-p1.y+Pos.y,0.1));
    vec3 CamRight=normalize(cross(CamDir,vec3(0,1,0)));
    vec3 CamUp=normalize(cross(CamRight,CamDir));   
    mat3 cam=mat3(CamRight,CamUp,CamDir);

    //ray calculation   
    vec2 uv=2.0*gl_FragCoord.xy/iResolution.xy-1.0;
    float aspect=iResolution.x/iResolution.y;
   
    vec3 Dir=normalize(vec3(uv*vec2(aspect,1.0),1.0))*cam;
   
    //raymarching
    float fade=0.0;
   
    const float numit=75.0; //raymarch precision
    const float threshold=1.20; //defines the thickness of tunnels
    const float scale=1.5; //tunnel z depth
   
    vec3 Posm1=Pos;
   
    //calculate first hit
    for (float x=0.0; x<numit; x++)
    {
        if (Dist2D(Pos)<threshold)
        {
            fade=1.0-x/numit;
            break;
        }
        Posm1=Pos;
        Pos+=Dir/numit*scale;//*(1.0+x/numit);
    }

    //track back to get better resolution
    for (int x=0; x<6; x++)
    {
        vec3 p2=(Posm1+Pos)/2.0;
        if (Dist2D(p2)<threshold)
            Pos=p2;
        else
            Posm1=p2;
    }   

    //lighting   
    vec3 n=normalize(vec3(Dist2D(Pos+vec3(0.01,0,0))-Dist2D(Pos+vec3(-0.01,0,0)),
                          Dist2D(Pos+vec3(0,0.01,0))-Dist2D(Pos+vec3(0,-0.01,0)),
                          Dist2D(Pos+vec3(0,0,0.01))-Dist2D(Pos+vec3(0,0,-0.01))));
   
    //triplanar blend vector
    vec3 tpn=normalize(max(vec3(0.0),(abs(n.xyz)-vec3(0.2))*7.0))*0.5;
   
    //position of the light - uncomment the second line to get a more interesting path
    vec3 lp=Position3D(time+0.5,cama,camb,camc,camd); //current light position
    //lp=Position3D(time+0.3,lighta,lightb,lightc,lightd);
   
    vec3 ld=lp-Pos;    //light direction
    float lv=1.0;
   
    const float ShadowIT=15.0; //shadow precision
   
    //shadow calc
    for (float x=1.0; x<ShadowIT; x++)
        if (Dist2D(Pos+ld*(x/ShadowIT))<threshold)
        {
            lv=0.0;
            break;
        }

    vec3 tuv=Pos*vec3(3.0,3.0,1.5);    //texture coordinates
   
    //normal mapping
    float nms=0.19;
    vec3 nmx=nmap(tuv.yz,iChannel0,nms)+nmap(-tuv.yz,iChannel0,nms);
    vec3 nmy=nmap(tuv.xz,iChannel1,nms)+nmap(-tuv.xz,iChannel1,nms);
    vec3 nmz=nmap(tuv.xy,iChannel2,nms)+nmap(-tuv.xy,iChannel2,nms);
   
    vec3 nn=normalize(nmx*tpn.x+nmy*tpn.y+nmz*tpn.z);
   
    float dd;
    //normalmapped version:
    dd=max(0.0,dot(nn,normalize(ld*mat3(vec3(1,0,0),vec3(0,0,1),n))));
    //standard version:
    //dd=max(0.0,dot(n,normalize(ld)));
   
    vec4 diff=vec4(dd*1.2*lv)+vec4(0.2);

    //wisp
    float w=pow(dot(normalize(Pos-oPos),normalize(lp-oPos)),5000.0);
    if (length(Pos-oPos)<length(lp-oPos)) w=0.0;   

    //texturing
    //double sampling to fix seams on texture edges
    vec4 tx=texture2D(iChannel0,tuv.yz)+texture2D(iChannel0,-tuv.yz);
    vec4 ty=texture2D(iChannel1,tuv.xz)+texture2D(iChannel1,-tuv.xz);
    vec4 tz=texture2D(iChannel2,tuv.xy)+texture2D(iChannel2,-tuv.xy);
   
    vec4 col=tx*tpn.x+ty*tpn.y+tz*tpn.z;
   
    gl_FragColor = col*diff*min(1.0,fade*10.0)+w;
}

//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

tex0 = moon3d.image.load2d("./data/tex06.jpg")
tex1 = moon3d.image.load2d("./data/tex01.jpg")
tex2 = moon3d.image.load2d("./data/tex09.jpg")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex1, 1)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex2, 2)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/The Cave", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>



Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Pyroclastic explosion converted
« Reply #21 on: June 06, 2015, 01:49:09 PM »
Pyroclastic explosion converted to GLSL Hacker format

Copy the code and save as Pyroclastic_explosion_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/pyroclastic explosion"
          width="800" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0;



//

// volume explosion shader
// simon green / nvidia 2012
// http://developer.download.nvidia.com/assets/gamedev/files/gdc12/GDC2012_Mastering_DirectX11_with_Unity.pdf

// sorry, port from HLSL!
#define float3 vec3
#define float4 vec4

// parameters
// be nice if we had sliders for these!
const int _MaxSteps = 64;
const float _StepDistanceScale = 0.5;
const float _MinStep = 0.001;
const float _DistThreshold = 0.005;

const int _VolumeSteps = 32;
const float _StepSize = 0.02;
const float _Density = 0.1;

const float _SphereRadius = 0.5;
const float _NoiseFreq = 4.0;
const float _NoiseAmp = -0.5;
const float3 _NoiseAnim = float3(0, -1, 0);

// iq's nice integer-less noise function

// matrix to rotate the noise octaves
mat3 m = mat3( 0.00,  0.80,  0.60,
              -0.80,  0.36, -0.48,
              -0.60, -0.48,  0.64 );

float hash( float n )
{
    return fract(sin(n)*43758.5453);
}


float noise( in vec3 x )
{
    vec3 p = floor(x);
    vec3 f = fract(x);

    f = f*f*(3.0-2.0*f);

    float n = p.x + p.y*57.0 + 113.0*p.z;

    float res = mix(mix(mix( hash(n+  0.0), hash(n+  1.0),f.x),
                        mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),
                    mix(mix( hash(n+113.0), hash(n+114.0),f.x),
                        mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);
    return res;
}

float fbm( vec3 p )
{
    float f;
    f = 0.5000*noise( p ); p = m*p*2.02;
    f += 0.2500*noise( p ); p = m*p*2.03;
    f += 0.1250*noise( p ); p = m*p*2.01;
    f += 0.0625*noise( p );
    p = m*p*2.02; f += 0.03125*abs(noise( p ));   
    return f/0.9375;
}


// distance field stuff
float sphereDist(float3 p, float4 sphere)
{
    return length(p - sphere.xyz) - sphere.w;
}

// returns signed distance to nearest surface
// displace is displacement from original surface (0, 1)
float distanceFunc(float3 p, out float displace)
{   
    //float d = length(p) - _SphereRadius;    // distance to sphere
    float d = length(p) - (sin(iGlobalTime*0.25)+0.5);    // animated radius
   
    // offset distance with pyroclastic noise
    //p = normalize(p) * _SphereRadius;    // project noise point to sphere surface
    displace = fbm(p*_NoiseFreq + _NoiseAnim*iGlobalTime);
    d += displace * _NoiseAmp;
   
    return d;
}

// calculate normal from distance field
float3 dfNormal(float3 pos)
{
    float eps = 0.001;
    float3 n;
    float s;
#if 0
    // central difference
    n.x = distanceFunc( float3(pos.x+eps, pos.y, pos.z), s ) - distanceFunc( float3(pos.x-eps, pos.y, pos.z), s );
    n.y = distanceFunc( float3(pos.x, pos.y+eps, pos.z), s ) - distanceFunc( float3(pos.x, pos.y-eps, pos.z), s );
    n.z = distanceFunc( float3(pos.x, pos.y, pos.z+eps), s ) - distanceFunc( float3(pos.x, pos.y, pos.z-eps), s );
#else
    // forward difference (faster)
    float d = distanceFunc(pos, s);
    n.x = distanceFunc( float3(pos.x+eps, pos.y, pos.z), s ) - d;
    n.y = distanceFunc( float3(pos.x, pos.y+eps, pos.z), s ) - d;
    n.z = distanceFunc( float3(pos.x, pos.y, pos.z+eps), s ) - d;
#endif

    return normalize(n);
}

// color gradient
// this should be in a 1D texture really
float4 gradient(float x)
{
    const float4 c0 = float4(4, 4, 4, 1);    // hot white
    const float4 c1 = float4(1, 1, 0, 1);    // yellow
    const float4 c2 = float4(1, 0, 0, 1);    // red
    const float4 c3 = float4(0.4, 0.4, 0.4, 4);    // grey
   
    float t = fract(x*3.0);
    float4 c;
    if (x < 0.3333) {
        c =  mix(c0, c1, t);
    } else if (x < 0.6666) {
        c = mix(c1, c2, t);
    } else {
        c = mix(c2, c3, t);
    }
    //return float4(x);
    //return float4(t);
    return c;
}

// shade a point based on position and displacement from surface
float4 shade(float3 p, float displace)
{   
    // lookup in color gradient
    displace = displace*1.5 - 0.2;
    displace = clamp(displace, 0.0, 0.99);
    float4 c = gradient(displace);
    //c.a *= smoothstep(1.0, 0.8, length(p));
   
    // lighting
    float3 n = dfNormal(p);
    float diffuse = n.z*0.5+0.5;
    //float diffuse = max(0.0, n.z);
    c.rgb = mix(c.rgb, c.rgb*diffuse, clamp((displace-0.5)*2.0, 0.0, 1.0));
   
    //return float4(float3(displace), 1);
    //return float4(dfNormal(p)*float3(0.5)+float3(0.5), 1);
    //return float4(diffuse);
    //return gradient(displace);
    return c;
}

// procedural volume
// maps position to color
float4 volumeFunc(float3 p)
{
    float displace;
    float d = distanceFunc(p, displace);
    float4 c = shade(p, displace);
    return c;
}

// sphere trace
// returns hit position
float3 sphereTrace(float3 rayOrigin, float3 rayDir, out bool hit, out float displace)
{
    float3 pos = rayOrigin;
    hit = false;
    displace = 0.0;   
    float d;
    //float3 hitPos;
    float disp;
    for(int i=0; i<_MaxSteps; i++) {
        d = distanceFunc(pos, disp);
            if (d < _DistThreshold) {
            hit = true;
            displace = disp;
            //hitPos = pos;
                //break;    // early exit from loop doesn't work in ES?
            }
        //d = max(d, _MinStep);
        pos += rayDir*d*_StepDistanceScale;
    }
   
    return pos;
    //return hitPos;
}


// ray march volume from front to back
// returns color
float4 rayMarch(float3 rayOrigin, float3 rayStep, out float3 pos)
{
    float4 sum = float4(0, 0, 0, 0);
    pos = rayOrigin;
    for(int i=0; i<_VolumeSteps; i++) {
        float4 col = volumeFunc(pos);
        col.a *= _Density;
        col.a = min(col.a, 1.0);
       
        // pre-multiply alpha
        col.rgb *= col.a;
        sum = sum + col*(1.0 - sum.a);   
#if 0
        // exit early if opaque
            if (sum.a > _OpacityThreshold)
                    break;
#endif       
        pos += rayStep;
    }
    return sum;
}

void main()
{
    vec2 q = gl_FragCoord.xy / iResolution.xy;
    vec2 p = q*2.0-1.0;
    p.x *= iResolution.x / iResolution.y;
   
    float rotx = (iMouse.y / iResolution.y)*4.0;
    float roty = 0.2*iGlobalTime - (iMouse.x / iResolution.x)*4.0;
   
    // camera
    vec3 ro = 2.0*normalize(vec3(cos(roty), cos(rotx), sin(roty)));
    vec3 ww = normalize(vec3(0.0,0.0,0.0) - ro);
    vec3 uu = normalize(cross( vec3(0.0,1.0,0.0), ww ));
    vec3 vv = normalize(cross(ww,uu));
    vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );

    // sphere trace distance field
    bool hit;
    float displace;
    vec3 hitPos = sphereTrace(ro, rd, hit, displace);

    vec4 col = vec4(0, 0, 0, 1);
    if (hit) {
        // shade
           col = shade(hitPos, displace);    // opaque version
        //col = rayMarch(hitPos, rd*_StepSize, hitPos);    // volume render
    }

    gl_FragColor = col;
}


//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

-- tex0 = moon3d.image.load2d("./data/tex16.png")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
--moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/pyroclastic explosion", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Raymarching primitives converted
« Reply #22 on: June 06, 2015, 01:53:05 PM »
Raymarching primitives converted to GLSL Hacker format

Copy the code and save as Raymarching_primitives_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Raymarching primitives"
          width="800" height="400"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0;



//

// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// A list of usefull distance function to simple primitives, and an example on how to
// do some interesting boolean operations, repetition and displacement.
//
// More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm

float sdPlane( vec3 p )
{
    return p.y;
}

float sdSphere( vec3 p, float s )
{
    return length(p)-s;
}

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

float udRoundBox( vec3 p, vec3 b, float r )
{
  return length(max(abs(p)-b,0.0))-r;
}

float sdTorus( vec3 p, vec2 t )
{
  return length( vec2(length(p.xz)-t.x,p.y) )-t.y;
}

float sdHexPrism( vec3 p, vec2 h )
{
    vec3 q = abs(p);
#if 0
    return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);
#else
    float d1 = q.z-h.y;
    float d2 = max((q.x*0.866025+q.y*0.5),q.y)-h.x;
    return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
#endif
}

float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{
    vec3 pa = p-a, ba = b-a;
    float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
    return length( pa - ba*h ) - r;
}

float sdTriPrism( vec3 p, vec2 h )
{
    vec3 q = abs(p);
#if 0
    return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);
#else
    float d1 = q.z-h.y;
    float d2 = max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5;
    return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
#endif
}

float sdCylinder( vec3 p, vec2 h )
{
  vec2 d = abs(vec2(length(p.xz),p.y)) - h;
  return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}


float sdCone( in vec3 p, in vec3 c )
{
    vec2 q = vec2( length(p.xz), p.y );
#if 0
    return max( max( dot(q,c.xy), p.y), -p.y-c.z );
#else
    float d1 = -p.y-c.z;
    float d2 = max( dot(q,c.xy), p.y);
    return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
#endif   
}

float length2( vec2 p )
{
    return sqrt( p.x*p.x + p.y*p.y );
}

float length6( vec2 p )
{
    p = p*p*p; p = p*p;
    return pow( p.x + p.y, 1.0/6.0 );
}

float length8( vec2 p )
{
    p = p*p; p = p*p; p = p*p;
    return pow( p.x + p.y, 1.0/8.0 );
}

float sdTorus82( vec3 p, vec2 t )
{
  vec2 q = vec2(length2(p.xz)-t.x,p.y);
  return length8(q)-t.y;
}

float sdTorus88( vec3 p, vec2 t )
{
  vec2 q = vec2(length8(p.xz)-t.x,p.y);
  return length8(q)-t.y;
}

float sdCylinder6( vec3 p, vec2 h )
{
  return max( length6(p.xz)-h.x, abs(p.y)-h.y );
}

//----------------------------------------------------------------------

float opS( float d1, float d2 )
{
    return max(-d2,d1);
}

vec2 opU( vec2 d1, vec2 d2 )
{
    return (d1.x<d2.x) ? d1 : d2;
}

vec3 opRep( vec3 p, vec3 c )
{
    return mod(p,c)-0.5*c;
}

vec3 opTwist( vec3 p )
{
    float  c = cos(10.0*p.y+10.0);
    float  s = sin(10.0*p.y+10.0);
    mat2   m = mat2(c,-s,s,c);
    return vec3(m*p.xz,p.y);
}

//----------------------------------------------------------------------

vec2 map( in vec3 pos )
{
    vec2 res = opU( vec2( sdPlane(     pos), 1.0 ),
                    vec2( sdSphere(    pos-vec3( 0.0,0.25, 0.0), 0.25 ), 46.9 ) );
    res = opU( res, vec2( sdBox(       pos-vec3( 1.0,0.25, 0.0), vec3(0.25) ), 3.0 ) );
    res = opU( res, vec2( udRoundBox(  pos-vec3( 1.0,0.25, 1.0), vec3(0.15), 0.1 ), 41.0 ) );
    res = opU( res, vec2( sdTorus(     pos-vec3( 0.0,0.25, 1.0), vec2(0.20,0.05) ), 25.0 ) );
    res = opU( res, vec2( sdCapsule(   pos,vec3(-1.3,0.20,-0.1), vec3(-1.0,0.20,0.2), 0.1  ), 31.9 ) );
    res = opU( res, vec2( sdTriPrism(  pos-vec3(-1.0,0.25,-1.0), vec2(0.25,0.05) ),43.5 ) );
    res = opU( res, vec2( sdCylinder(  pos-vec3( 1.0,0.30,-1.0), vec2(0.1,0.2) ), 8.0 ) );
    res = opU( res, vec2( sdCone(      pos-vec3( 0.0,0.50,-1.0), vec3(0.8,0.6,0.3) ), 55.0 ) );
    res = opU( res, vec2( sdTorus82(   pos-vec3( 0.0,0.25, 2.0), vec2(0.20,0.05) ),50.0 ) );
    res = opU( res, vec2( sdTorus88(   pos-vec3(-1.0,0.25, 2.0), vec2(0.20,0.05) ),43.0 ) );
    res = opU( res, vec2( sdCylinder6( pos-vec3( 1.0,0.30, 2.0), vec2(0.1,0.2) ), 12.0 ) );
    res = opU( res, vec2( sdHexPrism(  pos-vec3(-1.0,0.20, 1.0), vec2(0.25,0.05) ),17.0 ) );

    res = opU( res, vec2( opS(
                     udRoundBox(  pos-vec3(-2.0,0.2, 1.0), vec3(0.15),0.05),
                     sdSphere(    pos-vec3(-2.0,0.2, 1.0), 0.25)), 13.0 ) );
    res = opU( res, vec2( opS(
                     sdTorus82(  pos-vec3(-2.0,0.2, 0.0), vec2(0.20,0.1)),
                     sdCylinder(  opRep( vec3(atan(pos.x+2.0,pos.z)/6.2831,
                                              pos.y,
                                              0.02+0.5*length(pos-vec3(-2.0,0.2, 0.0))),
                                         vec3(0.05,1.0,0.05)), vec2(0.02,0.6))), 51.0 ) );
    res = opU( res, vec2( 0.7*sdSphere(    pos-vec3(-2.0,0.25,-1.0), 0.2 ) +
                                       0.03*sin(50.0*pos.x)*sin(50.0*pos.y)*sin(50.0*pos.z),
                                       65.0 ) );
    res = opU( res, vec2( 0.5*sdTorus( opTwist(pos-vec3(-2.0,0.25, 2.0)),vec2(0.20,0.05)), 46.7 ) );

    return res;
}

vec2 castRay( in vec3 ro, in vec3 rd )
{
    float tmin = 1.0;
    float tmax = 20.0;
   
#if 0
    float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
    float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 );
                                                 else           tmax = min( tmax, tp2 ); }
#endif
   
    float precis = 0.002;
    float t = tmin;
    float m = -1.0;
    for( int i=0; i<50; i++ )
    {
        vec2 res = map( ro+rd*t );
        if( res.x<precis || t>tmax ) break;
        t += res.x;
        m = res.y;
    }

    if( t>tmax ) m=-1.0;
    return vec2( t, m );
}


float softshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{
    float res = 1.0;
    float t = mint;
    for( int i=0; i<16; i++ )
    {
        float h = map( ro + rd*t ).x;
        res = min( res, 8.0*h/t );
        t += clamp( h, 0.02, 0.10 );
        if( h<0.001 || t>tmax ) break;
    }
    return clamp( res, 0.0, 1.0 );

}

vec3 calcNormal( in vec3 pos )
{
    vec3 eps = vec3( 0.001, 0.0, 0.0 );
    vec3 nor = vec3(
        map(pos+eps.xyy).x - map(pos-eps.xyy).x,
        map(pos+eps.yxy).x - map(pos-eps.yxy).x,
        map(pos+eps.yyx).x - map(pos-eps.yyx).x );
    return normalize(nor);
}

float calcAO( in vec3 pos, in vec3 nor )
{
    float occ = 0.0;
    float sca = 1.0;
    for( int i=0; i<5; i++ )
    {
        float hr = 0.01 + 0.12*float(i)/4.0;
        vec3 aopos =  nor * hr + pos;
        float dd = map( aopos ).x;
        occ += -(dd-hr)*sca;
        sca *= 0.95;
    }
    return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );   
}




vec3 render( in vec3 ro, in vec3 rd )
{
    vec3 col = vec3(0.8, 0.9, 1.0);
    vec2 res = castRay(ro,rd);
    float t = res.x;
    float m = res.y;
    if( m>-0.5 )
    {
        vec3 pos = ro + t*rd;
        vec3 nor = calcNormal( pos );
        vec3 ref = reflect( rd, nor );
       
        // material       
        col = 0.45 + 0.3*sin( vec3(0.05,0.08,0.10)*(m-1.0) );
       
        if( m<1.5 )
        {
           
            float f = mod( floor(5.0*pos.z) + floor(5.0*pos.x), 2.0);
            col = 0.4 + 0.1*f*vec3(1.0);
        }

        // lighting       
        float occ = calcAO( pos, nor );
        vec3  lig = normalize( vec3(-0.6, 0.7, -0.5) );
        float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
        float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
        float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
        float dom = smoothstep( -0.1, 0.1, ref.y );
        float fre = pow( clamp(1.0+dot(nor,rd),0.0,1.0), 2.0 );
        float spe = pow(clamp( dot( ref, lig ), 0.0, 1.0 ),16.0);
       
        dif *= softshadow( pos, lig, 0.02, 2.5 );
        dom *= softshadow( pos, ref, 0.02, 2.5 );

        vec3 brdf = vec3(0.0);
        brdf += 1.20*dif*vec3(1.00,0.90,0.60);
        brdf += 1.20*spe*vec3(1.00,0.90,0.60)*dif;
        brdf += 0.30*amb*vec3(0.50,0.70,1.00)*occ;
        brdf += 0.40*dom*vec3(0.50,0.70,1.00)*occ;
        brdf += 0.30*bac*vec3(0.25,0.25,0.25)*occ;
        brdf += 0.40*fre*vec3(1.00,1.00,1.00)*occ;
        brdf += 0.02;
        col = col*brdf;

        col = mix( col, vec3(0.8,0.9,1.0), 1.0-exp( -0.0005*t*t ) );

    }

    return vec3( clamp(col,0.0,1.0) );
}

mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
    vec3 cw = normalize(ta-ro);
    vec3 cp = vec3(sin(cr), cos(cr),0.0);
    vec3 cu = normalize( cross(cw,cp) );
    vec3 cv = normalize( cross(cu,cw) );
    return mat3( cu, cv, cw );
}

void main()
{
    vec2 q = gl_FragCoord.xy/iResolution.xy;
    vec2 p = -1.0+2.0*q;
    p.x *= iResolution.x/iResolution.y;
    vec2 mo = iMouse.xy/iResolution.xy;
         
    float time = 15.0 + iGlobalTime;

    // camera   
    vec3 ro = vec3( -0.5+3.2*cos(0.1*time + 6.0*mo.x), 1.0 + 2.0*mo.y, 0.5 + 3.2*sin(0.1*time + 6.0*mo.x) );
    vec3 ta = vec3( -0.5, -0.4, 0.5 );
   
    // camera-to-world transformation
    mat3 ca = setCamera( ro, ta, 0.0 );
   
    // ray direction
    vec3 rd = ca * normalize( vec3(p.xy,2.5) );

    // render   
    vec3 col = render( ro, rd );

    col = pow( col, vec3(0.4545) );

    gl_FragColor=vec4( col, 1.0 );
}

//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

-- tex0 = moon3d.image.load2d("./data/tex16.png")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
--moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Raymarching primitives", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Schrödinger's cat converted
« Reply #23 on: June 06, 2015, 02:02:58 PM »
Schrödinger's cat converted to GLSL Hacker format.

If you never heard about Schrödinger's cat, check Wikipedia article

Copy the code and save as Schroedingers_cat _gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Schrödinger's cat"
          width="800" height="400"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0;



//

// "Schroedinger's Cat" by dr2 - 2014
// License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

const vec4 cHashA4 = vec4 (0., 1., 57., 58.);
const vec3 cHashA3 = vec3 (1., 57., 113.);
const float cHashM = 43758.54;

float Hashfv2 (vec2 p)
{
  return fract (sin (dot (p, cHashA3.xy)) * cHashM);
}

vec4 Hashv4f (float p)
{
  return fract (sin (p + cHashA4) * cHashM);
}

float Noisefv2 (vec2 p)
{
  vec2 i = floor (p);
  vec2 f = fract (p);
  f = f * f * (3. - 2. * f);
  vec4 t = Hashv4f (dot (i, cHashA3.xy));
  return mix (mix (t.x, t.y, f.x), mix (t.z, t.w, f.x), f.y);
}

float Fbm2 (vec2 p)
{
  float s = 0.;
  float a = 1.;
  for (int i = 0; i < 5; i ++) {
    s += a * Noisefv2 (p);
    a *= 0.5;
    p *= 2.;
  }
  return s;
}

float Fbmn (vec3 p, vec3 n)
{
  vec3 s = vec3 (0.);
  float a = 1.;
  for (int i = 0; i < 5; i ++) {
    s += a * vec3 (Noisefv2 (p.yz), Noisefv2 (p.zx), Noisefv2 (p.xy));
    a *= 0.5;
    p *= 2.;
  }
  return dot (s, abs (n));
}

vec3 VaryNf (vec3 p, vec3 n, float f)
{
  vec3 e = vec3 (0.2, 0., 0.);
  float s = Fbmn (p, n);
  vec3 g = vec3 (Fbmn (p + e.xyy, n) - s,
     Fbmn (p + e.yxy, n) - s, Fbmn (p + e.yyx, n) - s);
  return normalize (n + f * (g - n * dot (n, g)));
}

float SmoothMin (float a, float b, float r)
{
  float h = clamp (0.5 + 0.5 * (b - a) / r, 0., 1.);
  return mix (b, a, h) - r * h * (1. - h);
}

float SmoothBump (float lo, float hi, float w, float x)
{
  return (1. - smoothstep (hi - w, hi + w, x)) * smoothstep (lo - w, lo + w, x);
}

float Length4 (vec2 p)
{
  p *= p;
  p *= p;
  return pow (p.x + p.y, 1./4.);
}

float Length8 (vec2 p)
{
  p *= p;
  p *= p;
  p *= p;
  return pow (p.x + p.y, 1./8.);
}

vec3 RgbToHsv (vec3 c)
{
  vec4 p = mix (vec4 (c.bg, vec2 (-1., 2./3.)), vec4 (c.gb, vec2 (0., -1./3.)),
     step (c.b, c.g));
  vec4 q = mix (vec4 (p.xyw, c.r), vec4 (c.r, p.yzx), step (p.x, c.r));
  float d = q.x - min (q.w, q.y);
  const float e = 1.e-10;
  return vec3 (abs (q.z + (q.w - q.y) / (6. * d + e)), d / (q.x + e), q.x);
}

vec3 HsvToRgb (vec3 c)
{
  vec3 p = abs (fract (c.xxx + vec3 (1., 2./3., 1./3.)) * 6. - 3.);
  return c.z * mix (vec3 (1.), clamp (p - 1., 0., 1.), c.y);
}

float PrSphDf (vec3 p, float r)
{
  return length (p) - r;
}

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

float PrCylDf (vec3 p, float r, float h)
{
  return max (length (p.xy) - r, abs (p.z) - h);
}

float PrCapsDf (vec3 p, float r, float h)
{
  return length (p - vec3 (0., 0., h * clamp (p.z / h, -1., 1.))) - r;
}

float PrTorus88Df (vec3 p, float ri, float rc)
{
  vec2 q = vec2 (Length8 (p.xy) - rc, p.z);
  return Length8 (q) - ri;
}

int idObj;
mat3 bodyMat, headMat, tailMat, boxMat, boxMatR;
vec3 catPos, qHit, ltDir;
float bdLen, boxSize, tCur, tSeq, nSeq;
bool isLive;
const float dstFar = 150.;
const float pi = 3.14159;
const int idBody = 11, idLegs = 12, idTail = 13, idHead = 14, idEars = 15,
   idTongue = 16, idEyes = 17, idNose = 18, idWhisk = 19, idFloor = 20,
   idWall = 21, idWallR = 22, idHinge = 23, idJar = 24;

float CatBodyDf (vec3 p, float dHit)
{
  vec3 q, qh;
  float h, d, w, a, ca, sa;
  q = p;
  w = q.z / bdLen;
  d = PrCapsDf (q * vec3 (1.3, 1., 1.), 0.7 * bdLen * (1. - 0.07 * w * w), bdLen);
  if (d < dHit) {
    dHit = d;  idObj = idBody;  qHit = q;
  }
  q = p - bdLen * vec3 (0., -0.8, 0.);
  vec3 qo = q;
  q.xz = abs (q.xz) - bdLen * vec2 (0.5, 0.9);
  q.xz += q.y * vec2 (0.1, 0.3);
  h = 0.6 * bdLen;
  w = q.y / h;
  d = PrCapsDf (q.xzy, 0.15 * bdLen * (1. - 0.3 * w * w), h);
  if (d < dHit + 0.2) {
    dHit = SmoothMin (dHit, d, 0.2);  idObj = idLegs;  qHit = q * sign (qo.zyx);
  }
  q = p - bdLen * vec3 (0., 0., -1.;
  w = q.z / bdLen;
  if (isLive) q.y += bdLen * (w * (1.1 - 0.3 * w) - 1.1);
  h = 0.7 * bdLen;
  if (isLive) a = 0.8 * sin (0.7 * 2. * pi * tCur);
  else a = 0.;
  ca = cos (a);
  sa = sin (a);
  tailMat = mat3 (ca, 0., - sa, 0., 1., 0., sa, 0., ca);
  q.z -= h;
  q = tailMat * q;
  q.z += h;
  d = PrCapsDf (q, 0.12 * bdLen * (1. - 0.1 * w), h);
  if (d < dHit + 0.2) {
    dHit = SmoothMin (dHit, d, 0.2);  idObj = idTail;  qHit = q;
  }
  return dHit;
}

float CatHeadDf (vec3 p, float dHit)
{
  vec3 q, qh;
  float r, h, d, w, rw, a, ca, sa;
  qh = p - bdLen * vec3 (0., 0.9, 1.5);
  if (isLive) a = 0.8 * sin (0.7 * 2. * pi * tCur);
  else a = 0.;
  ca = cos (a);
  sa = sin (a);
  headMat = mat3 (ca, 0., - sa, 0., 1., 0., sa, 0., ca);
  qh = headMat * qh;
  q = qh;
  q.y += 0.4 * q.z;
  d = PrCapsDf (q * vec3 (1., 1.2, 1.), 0.65 * bdLen, 0.05 * bdLen);
  d = max (d, - PrCylDf (q * vec3 (1., 2., 1.) - bdLen * vec3 (0., -0.42, 0.7),
     0.15 * bdLen, 0.2 * bdLen));
  if (d < dHit + 0.1) {
    dHit = SmoothMin (dHit, d, 0.1);  idObj = idHead;  qHit = q;
  }
  q.y += 0.22 * bdLen;
  if (isLive) a = 0.15 * sin (0.9 * 2. * pi * tCur);
  else a = -0.15;
  q.z -= bdLen * (0.5 + a);
  d = PrCapsDf (q * vec3 (1., 2., 1.), 0.12 * bdLen, 0.17 * bdLen);
  if (d < dHit) {
    dHit = d;  idObj = idTongue;  qHit = q;
  }
  vec3 qe = qh - bdLen * vec3 (0., 0.75, -0.1);
  vec3 qo = qe;
  qe.x = abs (q.x) - 0.4 * bdLen;
  r = 0.3 * bdLen;
  w = qe.x / r;
  rw = r * (1. - 0.5 * w * w);
  q = qe;
  q.z -= 0.5 * q.x;
  float d1 = max (PrCylDf (q.yxz, rw, 0.03 * bdLen), - q.x);
  q = qe;
  q.z += 0.1 * q.x;
  float d2 = max (PrCylDf (q.yxz, rw, 0.03 * bdLen), q.x);
  d = min (d1, d2);
  if (d < dHit + 0.1) {
    dHit = SmoothMin (dHit, d, 0.1);  idObj = idEars;  qHit = q * sign (qo.zyx);
  }
  q = qh - bdLen * vec3 (0., 0., 0.37);
  q.x = abs (q.x) - 0.3 * bdLen;
  d = PrSphDf (q * vec3 (1., 1.5, 1.), 0.2 * bdLen);
  if (d < dHit) {
    dHit = d;  idObj = idEyes;  qHit = q;
  }
  q = qh - bdLen * vec3 (0., -0.2, 0.65);
  q.z += 0.5 * q.y;
  d = PrCapsDf (q, 0.1 * bdLen, 0.03 * bdLen);
  if (d < dHit + 0.05) {
    dHit = SmoothMin (dHit, d, 0.05);  idObj = idNose;  qHit = q;
  }
  q = qh - bdLen * vec3 (0., -0.3, 0.65);
  q.xy = abs (q.xy) - bdLen * vec2 (0.1, -0.005);
  q.yz += 0.1 * q.x * vec2 (-1., 1.);
  d = PrCylDf (q.zyx, 0.01 * bdLen, 0.6 * bdLen);
  if (d < dHit) {
    dHit = d;  idObj = idWhisk;  qHit = q;
  }
  return dHit;
}

float CatDf (vec3 p, float dHit)
{
  vec3 q = p;
  if (! isLive) {
    q.x -= 1.05 * bdLen;
    q *= vec3 (1.5, 1., 1.);
  }
  dHit = CatBodyDf (q, dHit);
  dHit = CatHeadDf (q, dHit);
  return 0.5 * dHit;
}

vec3 FurCol (vec3 p, vec3 n)
{
  const vec3 c1 = vec3 (0.7, 0.6, 0.), c2 = vec3 (0.1), c3 = vec3 (0.9);
  p *= 2.5;
  float s = Fbmn (p, n);
  return mix (mix (c1, c2, smoothstep (0.8, 1.2, s)), c3,
     smoothstep (1.4, 1.7, s));
}

vec4 CatCol (vec3 n)
{
  vec3 col = vec3 (0.);
  float spec = 1.;
  const vec3 wCol = vec3 (0.9);
  vec3 q = 2. * qHit / bdLen;
  if (idObj >= idBody && idObj <= idEars) {
    if (idObj == idLegs || idObj == idHead) q *= 1.5;
    else if (idObj == idTail || idObj == idEars) q *= 2.;
    if (idObj == idTail) n = tailMat * n;
    else if (idObj == idHead || idObj == idEars) n = headMat * n;
    if (idObj == idEars && n.z > 0.4) col = vec3 (0.8, 0.6, 0.6);
    else {
      vec3 anis = vec3 (1.);
      if (idObj == idBody) anis = vec3 (1., 0.7, 1.);
      else if (idObj == idHead) anis = vec3 (1., 1., 1.3);
      col = FurCol (q * anis, n);
    }
    qHit /= bdLen;
    if (idObj == idBody) col = mix (mix (wCol, col,
       smoothstep (-0.65, -0.35, qHit.y)),
       wCol, (1. - smoothstep (-1.15, -0.95, qHit.z)) *
       smoothstep (0.3, 0.5, qHit.y));
    else if (idObj == idHead)
       col = mix (col, wCol, smoothstep (0.25, 0.45, qHit.z));
    else if (idObj == idTail)
      col = mix (col, wCol, smoothstep (0.25, 0.45, qHit.z));
    spec = 0.1;
  } else if (idObj == idTongue) {
    col = vec3 (0.9, 0.4, 0.4);
  } else if (idObj == idEyes) {
    n = headMat * n;
    col = vec3 (0., 0.7, 0.2);
    if (length (qHit - bdLen * vec3 (0.16, 0.12, 0.3)) < 0.4) {
      col = vec3 (0.4, 0., 0.);
      spec = 5.;
    }
  } else if (idObj == idNose) {
    col = vec3 (0.3, 0.2, 0.1);
  } else if (idObj == idWhisk) {
    col = vec3 (0.9, 0.7, 0.);
    spec = 5.;
  }
  if (! isLive && idObj != idTongue)
     col = HsvToRgb (RgbToHsv (col) * vec3 (1., 0.3, 0.);
  return vec4 (col, spec);
}

float BoxDf (vec3 p, float dHit)
{
  vec3 q, qb, qh;
  float d;
  float bb = 4. * boxSize;
  float bh = 2.5 * boxSize;
  float bt = 0.08 * boxSize;
  q = p - vec3 (0., -0.5 * bt, 0.);
  d = PrBoxDf (q, vec3 (bb - bt, bt, bb - bt));
  if (d < dHit) {
    dHit = d;  idObj = idFloor;  qHit = q;
  }
  qb = q;
  int nx = 0;
  if (qb.x < 0.) nx = 1;
  qb.x = abs (qb.x) - 0.5 * bb;
  qh = qb;
  float a = 0.;
  if (tSeq > 1. && tSeq < 2.) a = - pi * (tSeq - 1.);
  else if (tSeq > 8. && tSeq < 9.) a = - pi * (9. - tSeq);
  else if (tSeq >= 2. && tSeq <= 8.) a = - pi;
  float ca = cos (a);
  float sa = sin (a);
  boxMat = mat3 (ca, - sa, 0., sa, ca, 0., 0., 0., 1.);
  boxMatR = mat3 (ca, sa, 0., - sa, ca, 0., 0., 0., 1.);
  qb.x -= 0.5 * bb;
  qb = boxMat * qb;
  qb.x += 0.5 * bb;
  q = qb - vec3 (0.5 * bb, bh - bt, 0.);
  d = PrBoxDf (q, vec3 (bt, bh, bb - bt));
  if (d < dHit) {
    dHit = d;  idObj = idWall + nx;  qHit = q;
  }
  q = qb - vec3 (0., bh - bt, 0.);
  q.z = abs (q.z) - bb;
  d = PrBoxDf (q, vec3 (0.5 * bb + bt, bh, bt));
  if (d < dHit) {
    dHit = d;  idObj = idWall;  qHit = q;
  }
  q = qb - vec3 (0., 2. * bh - 0.5 * bt, 0.);
  d = PrBoxDf (q, vec3 (0.5 * bb + bt, bt, bb + bt));
  if (d < dHit) {
    dHit = d;  idObj = idWall;  qHit = q;
  }

  q = qb - vec3 (-0.1 * bb, bt + 2. * bh - 0.5 * bt, 0.);
  d = PrTorus88Df (q.yzx, 1.5 * bt, 0.2 * bb);
  d = max (d, - q.y);
  if (d < dHit) {
    dHit = d;  idObj = idWall;  qHit = q;
  }
  q = qh - vec3 (0.5 * bb, 0., 0.);
  q.z = abs (q.z) - 0.5 * bb;
  d = PrCylDf (q.yxz, 1.8 * bt, 0.25 * bb);
  if (d < dHit) {
    dHit = d;  idObj = idHinge;  qHit = q;
  }
  return dHit;
}

vec3 WoodCol (vec3 p, vec3 n)
{
  p *= 4.;
  float f = dot (vec3 (Fbm2 (p.yz * vec2 (1., 0.1)),
     Fbm2 (p.zx * vec2 (1., 0.1)), Fbm2 (p.yx * vec2 (1., 0.1))), abs (n));
  return 2. * mix (vec3 (0.8, 0.4, 0.2), vec3 (0.45, 0.25, 0.1), f);
}

vec4 BoxCol (vec3 n)
{
  vec3 col = vec3 (0.);
  float spec = 1.;
  if (idObj == idFloor) {
    col = WoodCol (qHit, n);
  } else if (idObj == idWall) {
    col = WoodCol (qHit, boxMat * n);
  } else if (idObj == idWallR) {
    col = WoodCol (qHit, boxMatR * n);
  } else if (idObj == idHinge) {
    col = vec3 (0.7, 0.5, 0.1);
    spec = 10.;
  }
  return vec4 (col, spec);
}

float JarDf (vec3 p, float dHit)
{
  vec3 q;
  float d;
  q = p - boxSize * vec3 (-3., 0.5, 3.);
  d = PrCylDf (q.xzy, 0.5 * boxSize, 0.5 * boxSize);
  if (d < dHit) {
    dHit = d;  idObj = idJar;  qHit = q;
  }
  return dHit;
}

vec4 JarCol (vec3 n)
{
  vec3 col;
  float spec = 1.;
  vec3 q = qHit;
  if (n.y < 0.95) {
    float a = abs (atan (qHit.x, qHit.z));
    a = min (a, abs (pi - a));
    col = vec3 (1., 1., 0.) *
       (1. - SmoothBump (-0.25, 0.25, 0.03, a - 1.3 * abs (qHit.y))) *
       (1. - SmoothBump (-0.1, 0.25, 0.03, Length4 (vec2 (a, qHit.y -
       0.3 * boxSize))));
  } else {
    if (isLive) col = vec3 (0., 1., 0.);
    else col = vec3 (1., 0., 0.);
    col *= 0.6 * (1. + cos (3. * 2. * pi * tSeq));
  }
  return vec4 (col, spec);
}

float ObjDf (vec3 p)
{
  float dHit = dstFar;
  dHit = CatDf (bodyMat * (p - catPos), dHit);
  dHit = BoxDf (p, dHit);
  dHit = JarDf (p, dHit);
  return dHit;
}

float ObjRay (vec3 ro, vec3 rd)
{
  const float dTol = 0.001;
  float d;
  float dHit = 0.;
  for (int j = 0; j < 150; j ++) {
    d = ObjDf (ro + dHit * rd);
    dHit += d;
    if (d < dTol || dHit > dstFar) break;
  }
  return dHit;
}

vec3 ObjNf (vec3 p)
{
  const vec3 e = vec3 (0.001, -0.001, 0.);
  float v0 = ObjDf (p + e.xxx);
  float v1 = ObjDf (p + e.xyy);
  float v2 = ObjDf (p + e.yxy);
  float v3 = ObjDf (p + e.yyx);
  return normalize (vec3 (v0 - v1 - v2 - v3) + 2. * vec3 (v1, v2, v3));
}

float ObjSShadow (vec3 ro, vec3 rd)
{
  float sh = 1.;
  float d = 0.1;
  for (int i = 0; i < 100; i++) {
    float h = ObjDf (ro + rd * d);
    sh = min (sh, 20. * h / d);
    d += 0.1;
    if (h < 0.001) break;
  }
  return clamp (sh, 0., 1.);
}

vec4 ObjCol (vec3 n)
{
  vec4 col4 = vec4 (0.);
  if (idObj >= idBody && idObj <= idWhisk) col4 = CatCol (bodyMat * n);
  else if (idObj >= idFloor && idObj <= idHinge) col4 = BoxCol (n);
  else if (idObj == idJar) col4 = JarCol (n);
  return col4;
}

void CatPM (float t)
{
  float frq = 0.44;
  float rl = 0.;
  float az = 0.;
  float el = 0.;
  if (isLive) {
    catPos = vec3 (0., bdLen * (1.94 + 0.4 * sin (2. * pi * frq * tCur)), 0.);
    az += 0.7 * sin (pi * frq * tCur);
    el -= 0.4 * (1. + sin (2. * pi * frq * tCur));
  } else {
    float ps = 2. * mod (nSeq, 2.) - 1.;
    catPos = vec3 (0., bdLen * (0.45 + 1.05 * ps), 0.);
    rl -= 0.5 * pi * ps;
  }
  vec3 ca = cos (vec3 (el, az, rl));
  vec3 sa = sin (vec3 (el, az, rl));
  bodyMat = mat3 (ca.z, - sa.z, 0., sa.z, ca.z, 0., 0., 0., 1.) *
     mat3 (1., 0., 0., 0., ca.x, - sa.x, 0., sa.x, ca.x) *
     mat3 (ca.y, 0., - sa.y, 0., 1., 0., sa.y, 0., ca.y);
}

vec3 ShowScene (vec3 ro, vec3 rd)
{
  vec3 vn, objCol;
  float dstHit;
  vec3 col = vec3 (0., 0., 0.02);
  idObj = -1;
  dstHit = ObjRay (ro, rd);
  int idObjT = idObj;
  if (idObj < 0) dstHit = dstFar;
  if (dstHit < dstFar) {
    ro += rd * dstHit;
    vn = ObjNf (ro);
    idObj = idObjT;
    if (idObj >= idBody && idObj <= idTongue) vn = VaryNf (20. * qHit, vn, 0.4);
    vec4 col4 = ObjCol (vn);
    objCol = col4.xyz;
    float spec = col4.w;
    float dif = max (dot (vn, ltDir), 0.);
    vec3 vl = 30. * ltDir - ro;
    float di = 1. / length (vl);
    float br = min (1.1, 40. * di);
    float f = dot (ltDir, vl) * di;
    col = (0.1 + pow (f, 16.)) * br * objCol * (0.2 * (1. +
       max (dot (vn, - normalize (vec3 (ltDir.x, 0., ltDir.z))), 0.)) +
       max (0., dif) * ObjSShadow (ro, ltDir) *
       (dif + spec * pow (max (0., dot (ltDir, reflect (rd, vn))), 64.)));
  }
  col = sqrt (clamp (col, 0., 1.));
  return col;
}

void main()
{
  vec2 uv = 2. * gl_FragCoord.xy / iResolution.xy - 1.;
  uv.x *= iResolution.x / iResolution.y;
  float zmFac = 4.8;
  tCur = iGlobalTime;
  tCur = max (tCur - 2., 0.);
  const float tPer = 10.;
  nSeq = floor (tCur / tPer);
  float tBase = tPer * nSeq;
  tSeq = tCur - tBase;
  isLive = (sin (tBase) + sin (1.7 * tBase) + sin (2.7 * tBase)) > -0.5;
  boxSize = 2.;
  bdLen = boxSize;
  if (! isLive) {
    float s = tSeq / tPer;
    bdLen *= 1. - 0.95 * s * s * s;
  }
  float dist = 50.;
  float el = 0.4 + 0.2 * sin (0.042 * tCur);
  float az = pi + 0.6 * sin (0.093 * tCur);
  float cEl = cos (el);
  float sEl = sin (el);
  float cAz = cos (az);
  float sAz = sin (az);
  mat3 vuMat = mat3 (1., 0., 0., 0., cEl, - sEl, 0., sEl, cEl) *
     mat3 (cAz, 0., sAz, 0., 1., 0., - sAz, 0., cAz);
  vec3 rd = normalize (vec3 (uv, zmFac)) * vuMat;
  vec3 ro = - vec3 (0., 0., dist) * vuMat;
  ro.y += 0.08 * dist;
  ltDir = normalize (vec3 (0.5, 1., -1.));
  ltDir *= vuMat;
  CatPM (tCur);
  vec3 col = ShowScene (ro, rd);
  gl_FragColor = vec4 (col, 1.);
}


//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

-- tex0 = moon3d.image.load2d("./data/tex16.png")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
--moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Schrödinger's cat", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Audrey converted
« Reply #24 on: June 06, 2015, 02:09:09 PM »
Audrey converted to GLSL Hacker format

Copy the code and save as Audrey_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Audrey"
          width="800" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

// https://www.shadertoy.com/view/4df3D8

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
//uniform sampler2D iChannel0;



//

// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// These are 316 gaussian points (201 brushes by using local symmetry) forming the picture
// of Audrey Hepburn. They pack down to 800 bytes (34 bits per point) before huffman/arithmetic
// compression.
//
// The points were chosen by (very quickly) runing this
//
// http://www.iquilezles.org/www/articles/genetic/genetic.htm
//
// with some importance sampling for the eyes, nouse and mouth.

float brush( float col, vec2 p, in vec4 b, float an )
{
    p += an*cos( iGlobalTime + 100.0*b.yz );
               
    vec2 dd = p - b.yz;
    col = mix( col, b.x, exp( -b.w*b.w*dot(dd,dd) ) );
    if( abs(b.z-0.5)<0.251 )
    {
        dd.x = p.x - 1.0 + b.y;
        col =  mix( col, b.x, exp( -b.w*b.w*dot(dd,dd) ) );
    }
    return col;
}

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.y;
    uv.x -=  0.5*(iResolution.x / iResolution.y - 1.0);

    float an = smoothstep( 0.0, 1.0, cos(iGlobalTime) );

    float col = 0.0;

    col = brush( col, uv, vec4(1.000,0.371,0.379,11.770), an );
    col = brush( col, uv, vec4(0.992,0.545,0.551,8.359), an );
    col = brush( col, uv, vec4(0.749,0.623,0.990,36.571), an );
    col = brush( col, uv, vec4(1.000,0.510,0.395,11.315), an );
    col = brush( col, uv, vec4(1.000,0.723,0.564,15.170), an );
    col = brush( col, uv, vec4(0.953,0.729,0.750,14.629), an );
    col = brush( col, uv, vec4(0.706,0.982,0.033,16.254), an );
    col = brush( col, uv, vec4(1.000,0.855,0.652,26.256), an );
    col = brush( col, uv, vec4(1.000,0.664,0.623,81.920), an );
    col = brush( col, uv, vec4(0.000,0.881,0.750,8.031), an );
    col = brush( col, uv, vec4(0.686,0.682,0.900,27.676), an );
    col = brush( col, uv, vec4(1.000,0.189,0.684,18.618), an );
    col = brush( col, uv, vec4(0.000,0.904,0.750,8.031), an );
    col = brush( col, uv, vec4(1.000,0.422,0.195,44.522), an );
    col = brush( col, uv, vec4(1.000,0.779,0.750,16.787), an );
    col = brush( col, uv, vec4(1.000,0.645,0.330,14.222), an );
    col = brush( col, uv, vec4(1.000,0.197,0.648,22.505), an );
    col = brush( col, uv, vec4(0.702,0.512,0.393,35.310), an );
    col = brush( col, uv, vec4(1.000,0.744,0.621,14.949), an );
    col = brush( col, uv, vec4(0.671,0.885,0.092,24.675), an );
    col = brush( col, uv, vec4(0.000,0.344,0.750,8.031), an );
    col = brush( col, uv, vec4(1.000,0.760,0.465,40.960), an );
    col = brush( col, uv, vec4(0.008,0.908,0.311,8.031), an );
    col = brush( col, uv, vec4(0.016,0.959,0.750,10.039), an );
    col = brush( col, uv, vec4(0.004,0.930,0.750,12.800), an );
    col = brush( col, uv, vec4(1.000,0.555,0.250,19.883), an );
    col = brush( col, uv, vec4(1.000,0.770,1.018,15.876), an );
    col = brush( col, uv, vec4(0.000,0.828,0.756,36.571), an );
    col = brush( col, uv, vec4(0.580,0.566,0.424,89.043), an );
    col = brush( col, uv, vec4(0.988,0.162,0.691,40.157), an );
    col = brush( col, uv, vec4(0.000,0.314,0.750,8.031), an );
    col = brush( col, uv, vec4(0.000,0.947,0.125,32.000), an );
    col = brush( col, uv, vec4(0.914,0.844,0.725,52.513), an );
    col = brush( col, uv, vec4(1.000,0.313,0.762,42.667), an );
    col = brush( col, uv, vec4(0.996,0.676,0.689,85.333), an );
    col = brush( col, uv, vec4(0.980,0.346,0.559,24.675), an );
    col = brush( col, uv, vec4(1.000,0.553,0.250,18.789), an );
    col = brush( col, uv, vec4(0.004,0.258,0.248,8.031), an );
    col = brush( col, uv, vec4(1.000,0.420,0.742,30.567), an );
    col = brush( col, uv, vec4(0.906,0.543,0.250,22.756), an );
    col = brush( col, uv, vec4(0.863,0.674,0.322,20.078), an );
    col = brush( col, uv, vec4(0.753,0.357,0.686,78.769), an );
    col = brush( col, uv, vec4(0.906,0.795,0.705,37.236), an );
    col = brush( col, uv, vec4(0.933,0.520,0.365,38.642), an );
    col = brush( col, uv, vec4(0.996,0.318,0.488,14.734), an );
    col = brush( col, uv, vec4(0.337,0.486,0.281,81.920), an );
    col = brush( col, uv, vec4(0.965,0.691,0.516,16.650), an );
    col = brush( col, uv, vec4(0.808,0.582,0.973,52.513), an );
    col = brush( col, uv, vec4(0.012,0.240,0.928,8.063), an );
    col = brush( col, uv, vec4(1.000,0.496,0.217,31.508), an );
    col = brush( col, uv, vec4(0.000,0.658,0.953,34.133), an );
    col = brush( col, uv, vec4(0.871,0.582,0.172,62.061), an );
    col = brush( col, uv, vec4(0.855,0.346,0.342,17.504), an );
    col = brush( col, uv, vec4(0.878,0.787,0.648,28.845), an );
    col = brush( col, uv, vec4(0.000,0.984,0.111,35.310), an );
    col = brush( col, uv, vec4(0.855,0.514,0.965,66.065), an );
    col = brush( col, uv, vec4(0.561,0.613,0.350,81.920), an );
    col = brush( col, uv, vec4(0.992,0.818,0.902,21.558), an );
    col = brush( col, uv, vec4(0.914,0.746,0.615,40.157), an );
    col = brush( col, uv, vec4(0.557,0.580,0.125,60.235), an );
    col = brush( col, uv, vec4(0.475,0.547,0.414,70.621), an );
    col = brush( col, uv, vec4(0.843,0.680,0.793,20.277), an );
    col = brush( col, uv, vec4(1.000,0.230,0.758,56.889), an );
    col = brush( col, uv, vec4(1.000,0.299,0.691,68.267), an );
    col = brush( col, uv, vec4(0.737,0.518,0.100,68.267), an );
    col = brush( col, uv, vec4(0.996,0.227,0.514,41.796), an );
    col = brush( col, uv, vec4(0.929,0.850,0.770,62.061), an );
    col = brush( col, uv, vec4(0.682,0.834,0.111,30.118), an );
    col = brush( col, uv, vec4(0.996,0.854,0.793,58.514), an );
    col = brush( col, uv, vec4(0.490,0.736,0.889,19.321), an );
    col = brush( col, uv, vec4(0.980,0.465,0.725,16.126), an );
    col = brush( col, uv, vec4(0.992,0.484,1.010,23.273), an );
    col = brush( col, uv, vec4(0.008,0.949,0.727,23.540), an );
    col = brush( col, uv, vec4(0.012,0.086,0.086,8.031), an );
    col = brush( col, uv, vec4(1.000,0.121,0.750,44.522), an );
    col = brush( col, uv, vec4(0.427,0.617,0.891,27.676), an );
    col = brush( col, uv, vec4(0.804,0.693,0.633,78.769), an );
    col = brush( col, uv, vec4(0.012,0.711,0.084,13.745), an );
    col = brush( col, uv, vec4(0.082,0.584,0.338,107.789), an );
    col = brush( col, uv, vec4(0.929,0.613,0.268,19.692), an );
    col = brush( col, uv, vec4(0.200,0.549,0.420,128.000), an );
    col = brush( col, uv, vec4(1.000,0.402,0.717,26.947), an );
    col = brush( col, uv, vec4(0.000,0.551,0.168,45.511), an );
    col = brush( col, uv, vec4(0.992,0.627,0.621,56.889), an );
    col = brush( col, uv, vec4(0.902,0.361,0.748,40.960), an );
    col = brush( col, uv, vec4(0.984,0.344,0.754,38.642), an );
    col = brush( col, uv, vec4(0.902,0.203,0.818,51.200), an );
    col = brush( col, uv, vec4(1.000,0.230,0.803,52.513), an );
    col = brush( col, uv, vec4(0.922,0.738,0.691,47.628), an );
    col = brush( col, uv, vec4(0.000,0.385,0.797,43.574), an );
    col = brush( col, uv, vec4(0.000,0.725,0.305,62.061), an );
    col = brush( col, uv, vec4(0.000,0.150,0.750,45.511), an );
    col = brush( col, uv, vec4(1.000,0.742,0.408,47.628), an );
    col = brush( col, uv, vec4(0.000,0.645,0.643,60.235), an );
    col = brush( col, uv, vec4(1.000,0.645,0.438,35.310), an );
    col = brush( col, uv, vec4(0.510,0.564,0.789,18.450), an );
    col = brush( col, uv, vec4(0.863,0.211,0.781,30.567), an );
    col = brush( col, uv, vec4(0.106,0.508,0.328,89.043), an );
    col = brush( col, uv, vec4(0.012,0.410,0.875,14.629), an );
    col = brush( col, uv, vec4(1.000,0.871,0.877,48.762), an );
    col = brush( col, uv, vec4(1.000,0.258,0.779,37.926), an );
    col = brush( col, uv, vec4(0.000,0.436,0.807,28.845), an );
    col = brush( col, uv, vec4(0.918,0.861,0.836,49.951), an );
    col = brush( col, uv, vec4(1.000,0.291,0.770,40.960), an );
    col = brush( col, uv, vec4(0.000,0.750,0.283,27.676), an );
    col = brush( col, uv, vec4(0.965,0.596,0.572,28.055), an );
    col = brush( col, uv, vec4(0.902,0.803,0.953,24.976), an );
    col = brush( col, uv, vec4(0.957,0.498,0.600,16.126), an );
    col = brush( col, uv, vec4(0.914,0.322,0.432,15.634), an );
    col = brush( col, uv, vec4(0.008,0.025,0.621,17.809), an );
    col = brush( col, uv, vec4(0.000,0.916,0.713,56.889), an );
    col = brush( col, uv, vec4(0.914,0.547,0.971,47.628), an );
    col = brush( col, uv, vec4(0.000,0.207,0.432,37.926), an );
    col = brush( col, uv, vec4(0.875,0.176,0.793,46.545), an );
    col = brush( col, uv, vec4(0.000,0.646,0.668,41.796), an );
    col = brush( col, uv, vec4(1.000,0.721,0.691,51.200), an );
    col = brush( col, uv, vec4(0.451,0.559,0.754,49.951), an );
    col = brush( col, uv, vec4(0.969,0.846,0.750,58.514), an );
    col = brush( col, uv, vec4(0.000,0.900,0.146,36.571), an );
    col = brush( col, uv, vec4(1.000,0.613,0.635,85.333), an );
    col = brush( col, uv, vec4(0.596,0.807,0.150,58.514), an );
    col = brush( col, uv, vec4(0.898,0.330,0.760,40.157), an );
    col = brush( col, uv, vec4(0.694,0.594,0.012,51.200), an );
    col = brush( col, uv, vec4(0.698,0.592,0.055,53.895), an );
    col = brush( col, uv, vec4(0.902,0.268,0.773,39.385), an );
    col = brush( col, uv, vec4(0.925,0.838,0.660,58.514), an );
    col = brush( col, uv, vec4(0.843,0.670,0.242,28.444), an );
    col = brush( col, uv, vec4(0.243,0.465,0.285,85.333), an );
    col = brush( col, uv, vec4(0.816,0.588,0.674,44.522), an );
    col = brush( col, uv, vec4(0.008,0.283,0.115,8.031), an );
    col = brush( col, uv, vec4(0.247,0.414,0.691,60.235), an );
    col = brush( col, uv, vec4(1.000,0.104,0.781,60.235), an );
    col = brush( col, uv, vec4(0.000,0.619,0.660,60.235), an );
    col = brush( col, uv, vec4(0.584,0.650,0.994,46.545), an );
    col = brush( col, uv, vec4(0.000,0.219,0.393,36.571), an );
    col = brush( col, uv, vec4(1.000,0.307,0.645,97.524), an );
    col = brush( col, uv, vec4(0.953,0.639,0.771,38.642), an );
    col = brush( col, uv, vec4(0.000,0.238,0.357,34.712), an );
    col = brush( col, uv, vec4(0.922,0.713,0.352,53.895), an );
    col = brush( col, uv, vec4(0.965,0.387,0.748,43.574), an );
    col = brush( col, uv, vec4(0.000,0.898,0.633,41.796), an );
    col = brush( col, uv, vec4(0.941,0.352,0.488,14.734), an );
    col = brush( col, uv, vec4(0.933,0.439,0.725,30.567), an );
    col = brush( col, uv, vec4(0.310,0.541,0.906,47.628), an );
    col = brush( col, uv, vec4(0.941,0.502,0.689,24.094), an );
    col = brush( col, uv, vec4(0.094,0.527,0.330,85.333), an );
    col = brush( col, uv, vec4(0.000,0.090,0.688,55.351), an );
    col = brush( col, uv, vec4(0.000,0.652,0.713,75.852), an );
    col = brush( col, uv, vec4(0.949,0.320,0.623,107.789), an );
    col = brush( col, uv, vec4(0.890,0.775,0.750,22.505), an );
    col = brush( col, uv, vec4(0.012,0.918,0.490,14.322), an );
    col = brush( col, uv, vec4(1.000,0.871,0.967,58.514), an );
    col = brush( col, uv, vec4(0.000,0.324,0.676,64.000), an );
    col = brush( col, uv, vec4(0.008,0.141,0.248,8.031), an );
    col = brush( col, uv, vec4(0.000,0.633,0.707,75.852), an );
    col = brush( col, uv, vec4(0.910,0.385,0.207,44.522), an );
    col = brush( col, uv, vec4(0.012,0.703,0.182,31.508), an );
    col = brush( col, uv, vec4(0.000,0.617,0.703,73.143), an );
    col = brush( col, uv, vec4(0.890,0.352,0.225,45.511), an );
    col = brush( col, uv, vec4(0.933,0.826,0.604,44.522), an );
    col = brush( col, uv, vec4(0.914,0.777,0.574,25.924), an );
    col = brush( col, uv, vec4(0.631,0.781,0.182,68.267), an );
    col = brush( col, uv, vec4(1.000,0.873,0.916,48.762), an );
    col = brush( col, uv, vec4(0.694,0.520,0.113,81.920), an );
    col = brush( col, uv, vec4(0.000,0.900,0.926,58.514), an );
    col = brush( col, uv, vec4(0.184,0.598,0.344,146.286), an );
    col = brush( col, uv, vec4(0.863,0.678,0.250,35.310), an );
    col = brush( col, uv, vec4(0.090,0.566,0.332,78.769), an );
    col = brush( col, uv, vec4(0.420,0.445,0.301,56.889), an );
    col = brush( col, uv, vec4(0.973,0.617,0.516,18.124), an );
    col = brush( col, uv, vec4(0.000,0.191,0.500,39.385), an );
    col = brush( col, uv, vec4(0.000,0.240,0.326,31.508), an );
    col = brush( col, uv, vec4(0.000,0.264,0.322,55.351), an );
    col = brush( col, uv, vec4(0.000,0.604,0.699,70.621), an );
    col = brush( col, uv, vec4(0.000,0.113,0.604,43.574), an );
    col = brush( col, uv, vec4(0.894,0.760,0.697,49.951), an );
    col = brush( col, uv, vec4(0.914,0.725,0.383,55.351), an );
    col = brush( col, uv, vec4(0.000,0.199,0.467,48.762), an );
    col = brush( col, uv, vec4(0.000,0.904,0.660,52.513), an );
    col = brush( col, uv, vec4(0.922,0.611,0.191,45.511), an );
    col = brush( col, uv, vec4(0.059,0.789,0.869,30.118), an );
    col = brush( col, uv, vec4(0.976,0.641,0.213,40.960), an );
    col = brush( col, uv, vec4(0.918,0.402,0.742,47.628), an );
    col = brush( col, uv, vec4(0.945,0.717,0.582,40.157), an );
    col = brush( col, uv, vec4(0.000,0.299,0.672,58.514), an );
    col = brush( col, uv, vec4(0.000,0.719,0.666,48.762), an );
    col = brush( col, uv, vec4(0.882,0.697,0.271,58.514), an );
    col = brush( col, uv, vec4(0.929,0.752,0.436,64.000), an );
    col = brush( col, uv, vec4(1.000,0.867,0.813,56.889), an );
    col = brush( col, uv, vec4(0.643,0.588,0.090,64.000), an );
    col = brush( col, uv, vec4(0.012,0.063,0.922,10.952), an );
    col = brush( col, uv, vec4(0.878,0.186,0.750,31.508), an );
    col = brush( col, uv, vec4(0.953,0.648,0.613,120.471), an );
    col = brush( col, uv, vec4(0.973,0.180,0.576,45.511), an );
    col = brush( col, uv, vec4(0.741,0.943,0.076,52.513), an );
    col = brush( col, uv, vec4(0.059,0.545,0.332,89.043), an );
    col = brush( col, uv, vec4(0.094,0.295,0.734,85.333), an );
    col = brush( col, uv, vec4(0.008,0.676,0.721,85.333), an );
    col = brush( col, uv, vec4(0.550,0.350,0.650,85.000), an );

    gl_FragColor = vec4(col,col,col,1.0);
}


//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

-- tex0 = moon3d.image.load2d("./data/tex16.png")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
--moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Audrey", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Clover tree converted
« Reply #25 on: June 07, 2015, 07:43:33 AM »
Clover tree converted to GLSL Hacker format.

I used the only cubemap that comes with MadShaders.
If you don't like that, study the code - i prepared it for random 2D textures.

Copy the code and save as Clover_tree_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Clover tree"
          width="800" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

// https://www.shadertoy.com/view/4dlXR4

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
uniform sampler2D iChannel0;
// uniform sampler2D iChannel1;
// uniform sampler2D iChannel2;
uniform samplerCube iChannel1;
uniform samplerCube iChannel2;

//

// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

// Domain repetition. A (simple) clover shape is repeated over space.

// Clearly NOT the way to do it (super slow). I run out of instructions (and speed), so
// I couldn't add variation or detail or lighting really. But the basic technique is there.

float hash1( vec3 p )
{
    return fract(sin(dot(p,vec3(1.0,57.0,113.0)))*43758.5453);
}
vec3 hash3( float n )
{
    return fract(sin(vec3(n,n+1.0,n+2.0))*vec3(43758.5453123,22578.1459123,19642.3490423));
}
vec3 hash3( vec3 p )
{
    return fract(sin(vec3( dot(p,vec3(1.0,57.0,113.0)),
                           dot(p,vec3(57.0,113.0,1.0)),
                           dot(p,vec3(113.0,1.0,57.0))))*43758.5453);
}

// simple clover shape
float shape( in vec3 p, in float s )
{
    float a = atan( p.x, p.y );
    float r = length( p.xy );
   
    float ra = 0.2 + 0.3*sqrt(0.5+0.5*sin( 3.0*a ));
    ra *= s;
    return min( max(length(p.xy)-0.04*(0.5+0.5*p.z),-p.z), max( length(p.xy)-ra, abs(p.z-0.2*r)-0.06*s*clamp(1.0-1.5*r,0.0,1.0) ) );
}

// df
vec4 map( vec3 p )
{
    p.x += 0.1*sin( 3.0*p.y );
   
    float rr = length(p.xz);
    float ma = 0.0;
    vec2 uv = vec2(0.0);
   
    float d1 = rr - 1.5;
    if( d1<1.8 )
    {
       
        float siz = 6.0;
        vec3 x = p*siz + 0.5;
        vec3 xi = floor( x );
        vec3 xf = fract( x );

        vec2 d3 = vec2( 1000.0, 0.0 );
        for( int k=-1; k<=1; k++ )
        for( int j=-1; j<=1; j++ )
        for( int i=-1; i<=1; i++ )
        {
            vec3 b = vec3( float(i), float(j), float(k) );
            vec3 c = xi + b;
           
            float ic = dot(c.xz,c.xz)/(siz*siz);
           
            float re = 1.5;
           
            if( ic>(1.0*1.0) && ic < (re*re) )
            {
            vec3 r = b - xf + 0.5 + 0.4*(-1.0+2.0*hash3( c ));
            //vec3 r = c + 0.5 - x;

            vec3 ww = normalize( vec3(c.x,0.0,c.z) );
            ww.y += 1.0; ww = normalize(ww);
            ww += 0.25 * (-1.0+2.0*hash3( c+123.123 ));
               
            vec3 uu = normalize( cross( ww, vec3(0.0,1.0,0.0) ) );
            vec3 vv = normalize( cross( uu, ww ) );
            r = mat3(  uu.x, vv.x, ww.x,
                       uu.y, vv.y, ww.y,
                       uu.z, vv.z, ww.z )*r;
            float s = 0.75 + 0.5*hash1( c+167.7 );               
            float d = shape(r,s)/siz;
            if( d < d3.x )
            {
                d3 = vec2( d, 1.0 );
                ma = hash1( c.yzx+712.1 );
                uv = r.xy;
            }
            }
        }
        d1 = mix( rr-1.5, d3.x, d3.y );
    }
   
    d1 = min( d1, rr - 1.0 );

    return vec4(d1, ma, uv );
   
}


vec4 intersect( in vec3 ro, in vec3 rd )
{
    const float maxd = 10.0;
   
    float h = 1.0;
    float t = 0.0;
    float m = -1.0;
    vec2  u = vec2(0.0);
    for( int i=0; i<50; i++ )
    {
        if( h<0.001||t>maxd ) continue;//break;
        t += h;
        vec4 res = map( ro+rd*t );
        h = res.x;
        m = res.y;
        u = res.zw;
    }

    if( t>maxd ) m=-1.0;
    return vec4( t, m, u );
}

vec3 calcNormal( in vec3 pos )
{
    vec2 eps = vec2(0.001,0.0);

    return normalize( vec3(
           map(pos+eps.xyy).x - map(pos-eps.xyy).x,
           map(pos+eps.yxy).x - map(pos-eps.yxy).x,
           map(pos+eps.yyx).x - map(pos-eps.yyx).x ) );
}


void main()
{
    vec2 q = gl_FragCoord.xy / iResolution.xy;
    vec2 p = -1.0 + 2.0 * q;
    p.x *= iResolution.x/iResolution.y;
    vec2 m = iMouse.xy/iResolution.xy;
   
    // camera
    float an = 20.0 + 0.15*iGlobalTime - 7.0*m.x;
    vec3  ro = 3.1*normalize(vec3(sin(an),0.5-0.4*m.y, cos(an)));
    vec3  ta = vec3( 0.0, 0.8, 0.0 );
    float rl = 0.5*sin(0.35*an);
    vec3  ww = normalize( ta - ro );
    vec3  uu = normalize( cross(ww,vec3(sin(rl),cos(rl),0.0) ) );
    vec3  vv = normalize( cross(uu,ww));
    vec3  rd = normalize( p.x*uu + p.y*vv + 1.5*ww );

    // render
    // vec3 col = texture2D( iChannel2, rd ).xyz; col = col*col;
    //
    vec3 col = textureCube( iChannel2, rd ).xyz; col = col*col;
   
    // raymarch
    vec4 tmat = intersect(ro,rd);
    if( tmat.y>-0.5 )
    {
        // geometry
        vec3 pos = ro + tmat.x*rd;
        vec3 nor = calcNormal(pos);
        vec3 ref = reflect( rd, nor );

        // material
        vec3 mate = vec3(0.3,0.5,0.1);
        mate = mix( mate, vec3(0.5,0.25,0.1), smoothstep( 0.9,0.91, tmat.y) );
        mate += 0.1*sin( tmat.y*10.0  + vec3(0.0,2.0,2.0));
        mate *= 0.8+0.4*tmat.y;
        vec2 uv = tmat.zw;
        float r = length(uv);
        float a = atan(uv.y,uv.x);
        mate += vec3(0.2,0.15,0.1)*smoothstep(0.8,1.0,-cos(3.0*a))*(1.0-1.5*r);
        mate *= 0.2+r;
       
        // lighting
        float amb = clamp(0.5+0.5*nor.y,0.0,1.0);
        amb *= 0.1 + 0.9*pow( clamp( (length(pos.xz)-1.0)/(1.5-1.0), 0.0, 1.0 ), 2.0 );
        vec3 snor = normalize( nor + normalize( vec3(pos.x,0.0,pos.y) ) );
        // vec3 lin = 1.0*texture2D( iChannel1, snor ).xyz*amb;
        //
        vec3 lin = 1.0*textureCube( iChannel1, snor ).xyz*amb;
        col = mate*lin;
        float kd = pow(clamp(1.0+dot(rd,nor),0.0,1.0),3.0);
        // col += 0.2*kd*pow( texture2D( iChannel2, ref ).xyz, vec3(2.2) )*amb;
        //
        col += 0.2*kd*pow( textureCube( iChannel2, ref ).xyz, vec3(2.2) )*amb;
    }

    // gamma
    col = pow( col, vec3(0.45) );
   
    // vigneting
    col *= 0.5 + 0.5*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.15 );
   
    gl_FragColor = vec4( col,1.0 );
}

//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

tex0 = moon3d.image.load2d("./data/noise_texture_0010_256x256.jpg")
-- tex1 = moon3d.image.load2d("./data/tex10.png")
-- tex2 = moon3d.image.load2d("./data/ground013.jpg")
cubemap = moon3d.image.loadCubeMap("./data/cubemap01_posx.jpg", "./data/cubemap01_negx.jpg", "./data/cubemap01_posy.jpg", "./data/cubemap01_negy.jpg", "./data/cubemap01_posz.jpg", "./data/cubemap01_negz.jpg")
moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
-- moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex1, 1)
-- moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex2, 2)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, cubemap, 1)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, cubemap, 2)

moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Clover tree", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Trampoline converted
« Reply #26 on: June 07, 2015, 08:23:52 AM »
Trampoline converted to GLSL Hacker format

I used the only cubemap that comes with MadShaders.
If you don't like that, study the code - i prepared it for random 2D textures.

Texture for the net tex08.jpg is somehow broken, load and save in e.g. IrfanView to fix.

Copy the code and save as Trampoline_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Trampoline"
          width="800" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click
uniform sampler2D iChannel0;
uniform sampler2D iChannel1; // Texture for the net tex08.jpg is somehow broken, load and save in e.g. IrfanView to fix.
//uniform sampler2D iChannel2;
uniform samplerCube iChannel2;

//

#define time iGlobalTime
#define EPSILON .0001
#define PI 3.14159265

const int MAX_ITER = 80;
vec3 lightDir    = normalize(vec3(-1.6, 0.7, -0.5));
float speedJump = 0.8;  //???
float heightJump = 7.;  //
float ballSize = 1.7;   //??

struct mat
{
  float typeMat; 
  bool  ref;     
     
};
mat materialMy = mat(0.0, false);
//-----------------------------
vec3 getNormal(in vec3 p);
float renderFunction(in vec3 pos);
float render(in vec3 posOnRay, in vec3 rayDir);
vec3 getColorPixel(inout vec3 ro, vec3 rd, inout vec3 normal, float dist, float typeColor);
float distMat(inout float curDist, float dist, in float typeMat, in bool refl);
//-----------------------------
vec3 rotationCoord(vec3 n, in float t, float paramRotate)
{
 vec3 result;
 //--------------------------------------------
   vec2 sc = vec2(sin(t), cos(t));
   mat3 rotate;
   if(paramRotate <= 0.1)
   {

      rotate = mat3(  1.0,  0.0,  0.0,
                      0.0,  1.0,  0.0,
                      0.0,  0.0,  1.0);   
   }
   else if(paramRotate <= 1.0)
   {
      rotate = mat3(  1.0,  0.0,  0.0,
                      0.0, sc.y,-sc.x,
                      0.0, sc.x, sc.y);
   }
   else if(paramRotate <= 2.0)
   {
       rotate = mat3(  1.0,  0.0,  0.0,
                       0.0, sc.y,sc.x,
                       0.0, -sc.x, sc.y); 
   }
   else if (paramRotate <= 3.0)
   {
      rotate = mat3( sc.y,  0.0, -sc.x,
                     0.0,   1.0,  0.0,
                     sc.x,  0.0, sc.y);   
   }
   else if (paramRotate <= 4.0)
   {
      rotate = mat3( sc.y,  0.0, sc.x,
                     0.0,   1.0,  0.0,
                    -sc.x,  0.0, sc.y);   
   }   
   else if (paramRotate <= 5.0)
   {
       rotate = mat3( sc.y,sc.x,  0.0,
                     -sc.x, sc.y, 0.0,
                      0.0,  0.0,  1.0); 
   }   
   else if (paramRotate <= 6.0)
   {
       rotate = mat3( sc.y,-sc.x, 0.0,
                      sc.x, sc.y, 0.0,
                      0.0,  0.0,  1.0); 
   }     
   else
   {
   mat3 rotate_x = mat3(  1.0,  0.0,  0.0,
                          0.0, sc.y,-sc.x,
                          0.0, sc.x, sc.y);
   mat3 rotate_y = mat3( sc.y,  0.0, -sc.x,
                         0.0,   1.0,  0.0,
                         sc.x,  0.0,  sc.y);
   mat3 rotate_z = mat3( sc.y, sc.x,  0.0,
                        -sc.x, sc.y,  0.0,
                         0.0,  0.0,   1.0);
   rotate = rotate_z * rotate_y * rotate_z;               
   }
  result = n * rotate;
  return result;
}
//----------------------------------------------------

//------------------------------------------
vec2 rot(vec2 p,float r){
  vec2 ret;
  ret.x=p.x*cos(r)-p.y*sin(r);
  ret.y=p.x*sin(r)+p.y*cos(r);
  return ret;
}
//------------------------------------------
vec2 rotsim(vec2 p,float s)
{
  vec2 ret=p;
  ret=rot(p,-PI/(s*2.0));
  ret=rot(p,floor(atan(ret.x,ret.y)/PI*s)*(PI/s));
  return ret;
}
//???
//------------------------------------------
float udRoundBox( vec3 p, vec3 b, float r )
{
  return length(max(abs(p)-b,0.0))-r;
}
//----------------------------------------------------
float dTorus(vec3 p, vec2 t)
{
   vec2 q = vec2(length(p.xz) - t.x, p.y);
   return length(q) - t.y;
}
//----------------------------------------------------
float dSphere(vec3 p, float r)
{
   return length(p) - r;
}
//----------------------------------------------------
float yCylinder(vec3 p, vec2 h)
{
    return max( length(p.xz)-h.x, abs(p.y)-h.y );
}
//--------------------------------------------------
// capsule in Y axis
float capsuleY(vec3 p, float r, float h)
{
    p.y -= clamp(p.y, 0.0, h);
    return length(p) - r;
}
//--------------------------------------------------
float sdCapsule( vec3 p, vec3 a, vec3 b, float r ) {
   vec3 pa = p - a, ba = b - a;
   float h = clamp( dot(pa, ba) / dot(ba , ba), 0.0, 1.0 );
       return length( pa - ba * h ) - r;
}
//---------------------------------------------------
float zCylinder(vec3 p, vec2 h)
{
    return max( length(p.xy)-h.x, abs(p.z)-h.y );
}
//------------------------------------------
//?? https://www.shadertoy.com/view/ltXGRj
float rays(in vec3 p, in vec2 size, in float r)
{
   float rad=length(p);
   return (length(p.xz)- size.x + size.y * (rad - r));
}
float astra(in vec3 p, in vec2 kol, in vec2 size, in float r)
{
   p.xy = rotsim(p.xy, kol.x);
   p.zy = rotsim(p.zy, kol.y);
   return rays(p, size, r);
}
//--------------------------------------------------
//-------------------------------------------------
//http://glslsandbox.com/e#20289.0
float dSegment(vec3 p,  vec3 a, vec3 b, float r1, float r2)
{
   vec3 pa = p - a;
   vec3 ba = b - a;
   float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
   return length( pa - ba*h ) - r1 + r2*h;
}
//--------------------------------------------------
float smin( float a, float b, float k )
{
   float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
   return mix( b, a, h ) - k*h*(1.0-h);
}
//
//--------------------------------------------------???
float jumper(in vec3 pos, in float r,in float d)
{
   float d1 = 1.;
   vec3 p = pos;
   float angle = 0.;
   float r25 = r*0.25, r15 =r*0.15, r30 = r* 0.3, r50 = r*0.5, r40 = r*0.4,
         r60 = r*0.6, r80 = r*0.8, r70 = r*0.7, r90 = r*0.9;   
  d1 = dSphere(p , r90); //
  d1 = smin(d1, dSphere(p +  vec3(0., r25, -r80),r25), 0.1);
  p.x = abs(p.x);
  p -= vec3(r70, 0., r80);
  p.yz = rot(p.yz, 1.);
  d1 = max(d1, -dTorus(p , vec2(-r15, r40)));
 //--------//?
  p = pos;
  float t = fract(speedJump * time);//??
  t *= 4.0  * (1.0 - t);
  angle = clamp(2.7 * t, 0.2, 2.7);   //??? 
 
 vec3 offY = vec3(0., r + r50, 0.);
 d1 = min(d1,capsuleY(p + offY, r25, r50)); //
 p.x = abs(p.x);
 p -= vec3(r60, -(r+r90), 0.);
 p.xy = rot(p.xy, angle);
 d1 = min(d1,  capsuleY(p, r25,r*2.)); //?
 d1 = min(d1,  dSphere(p - vec3(0., r*2., 0.), r30));
 d =  distMat(d,  d1,2.0, false );
//----------- //??
  p = pos;
  p.x = abs(p.x);
  p -= vec3(r60, 0., r60);
  d =  distMat(d,  dSphere(p , r15) ,  4.0, false) ;
//----------//??
 p = pos;
 p.yz = rot(p.yz, -0.;
 d1 = min(d1, astra(p , vec2(20.), vec2(r25, 0.15 ), 0.));
 d1 = max(d1, -dSphere(p -  vec3(0., 0., r*2.),r*2.));
 d =  distMat(d,  d1   ,  5.0, false ); 
 //--------//?
 p = pos;
 offY = vec3(0., r*3.+r25, 0.); 
 d1 = capsuleY(p + offY, r90, r+r25);             //?
 //--------
 offY = vec3(r60, -(r+r80), 0.);
 p.x = abs(p.x);
 p -= offY;
 p.xy = rot(p.xy, angle);
 d1 = min(d1,  capsuleY(p, r40, r80));   //
 d =  distMat(d,  d1   ,  3.0, false );
 //-----------
 d =  distMat(d,  dSphere(p , r70) ,  4.0, false );
 //-------- //??
 angle = clamp(-1.5 * t, -2., 0.); 
 //------------------
 offY = vec3(0., r*3.+r60, 0.); 
 p = pos + offY;
 d1 = capsuleY(p, r80, r25); //?
//------------------
 p = pos;
 p.x = abs(p.x);
 offY = vec3(r50, -(r*4.), 0.);
 p -= offY;
//=================================================
  vec3 p1 = vec3(0.);
  vec3 p2 = vec3(0., -(r+r50), 0.);
  vec3 p3 = vec3(0., -(r+r50), 0.);
  p2.yz = rot(p2.yz, angle); 
  p3.xy += p2.xy;
   
  d1 = smin(d1, dSegment(p, p1, p2, r50, 0.), 0.1);  //???
  d =  distMat(d,  d1   ,  4.0, false ); 
  d1 = dSegment(p, p2, p3, r30, 0.);        //?
  d =  distMat(d,  d1   ,  2.0, false );
  p += vec3(0., r+r50, -r30);
  p.xy -= p2.xy;
  d1 = dSphere(p * vec3(1.5, 1.5, 0.6), r50);            //??
  d =  distMat(d,  d1   ,  5.0, false); 
//=================================================

  return d;
}

//--------------------------------------------------
float sinc(float x)
{
   return sin(x) / x;
}
//--------------------------------------------------
float getY(in vec2 p, float push, float ballz)
{
   float stiffness = 0.4;
   float dotP =dot(p,p);
   float punch = exp2(-stiffness * dotP);
   ballz -= (ballSize * ballSize - dotP);
   return min(punch * push, ballz);
}
//--------------------------------------------------
float  dTrampoline(in vec3 pos, in float r, in float d)
{
  vec3 p = pos;
  float d1 = 1.; 
  float t = fract(speedJump * time);
  float push = -ballSize * sinc(42. * t); //?
 
   float y = getY(p.xz, push, heightJump);
   p.y -= y ;
   d1=  yCylinder(p, vec2(r*7., 0.02));      //??
   d =  distMat(d,  d1   ,  7.0, false );   
   d1 = dTorus(p, vec2(r*7., r*0.4));         //?
   d =  distMat(d,  d1   ,  4.0, false );
   //----------
   p = pos + vec3(0.,r*2., 0.);

   p.xz = rotsim( p.xz, 6.);
   p.xz = abs(p.xz);
   p.z -= r*7.;
   p.xy = rot(p.xy, 0.5);   
   d1 = capsuleY(p, r*0.4, r*2.5);       //??
   d =  distMat(d,  d1   ,  1.0, false);     
   //---------- 
  return d;
}
//------------------------------------------
float distMat(inout float curDist, float dist, in float typeMat, in bool refl)
{
   float res = curDist;
   if (dist < curDist)
   {
      materialMy.typeMat     = typeMat;
      materialMy.ref         = refl;
      res                    = dist;
     return res;
   }
   return curDist;
}

//--------------------------------------------------
float myObject(in vec3 p)
{
   float d =  1.0;
   float r = 0.7;
   materialMy.typeMat = 0.0;
   materialMy.ref = false;
   vec3 pos = p;
   pos += vec3(0., 8., 0.);

  d =  distMat(d,   udRoundBox( pos, vec3(10, 0.3, 10. ), 0.5),  1.0, true);   
//======================================??
  pos = p + vec3(0., 6.,  0.);
  d =  dTrampoline(pos, r,  d);
//======================================???
  pos.y -= r*7.;
  float t = fract(speedJump * time);//??
  t *= 4.0  * (1.0 - t);
  pos.y -= heightJump * t ;
  pos = rotationCoord(pos,t * 4., 3.);   
//  pos = rotationCoord(pos,t * 8., 1.);   
  d = jumper(pos,r,d);

   return d;
}
//-------------------------------------------------
//???
float renderFunction(in vec3 pos)
{
    return  myObject(pos);   
}
//-------------------------------------------------
vec3 getNormal(in vec3 p)
{

const float precis = 0.00001;
    vec3  eps = vec3(precis,0.0,0.0);
    vec3 nor;
    nor.x = renderFunction(p+eps.xyy) - renderFunction(p-eps.xyy);
    nor.y = renderFunction(p+eps.yxy) - renderFunction(p-eps.yxy);
    nor.z = renderFunction(p+eps.yyx) - renderFunction(p-eps.yyx);
    return normalize(nor);

}
//???,? -??  ? IQ
//https://www.shadertoy.com/view/Xds3zN
//-------------------------------------------------
float calcAO( in vec3 pos, in vec3 nor )
{
   float occ = 0.0;
    float sca = 1.0;
    for( int i=0; i<5; i++ )
    {
        float hr = 0.01 + 0.12*float(i)/4.0;
        vec3 aopos =  nor * hr + pos;
        float dd = renderFunction( aopos );
        occ += -(dd-hr)*sca;
        sca *= 0.95;
    }
    return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );   
}
//-------------------------------------------------
float softshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{
   float res = 1.0;
    float t = mint;
    for( int i=0; i<16; i++ )
    {
      float h = renderFunction( ro + rd*t );
        res = min( res, 8.0*h/t );
        t += clamp( h, 0.02, 0.10 );
        if( h<0.001 || t>tmax ) break;
    }
    return clamp( res, 0.0, 1.0 );
}

//-------------------------------------------------
vec3 getLighting1(in vec3 ro, in vec3 rd ,in vec3 norm, in vec3 lightDir, in vec3 color, in float dist)
{
   vec3 col = color;
   float occ = calcAO( ro, norm );
   vec3 ref = reflect( rd, norm );
   float amb = clamp( 0.5+0.5*norm.y, 0.0, 1.0 );
   float dif = clamp( dot( norm, lightDir ), 0.0, 1.0 );
   float bac = clamp( dot( norm, normalize(vec3(-lightDir.x,0.0,-lightDir.z))), 0.0, 1.0 )*clamp( 1.0-ro.y,0.0,1.0);
   float dom = smoothstep( -0.1, 0.1, ref.y );
   float fre = pow( clamp(1.0+dot(norm,rd),0.0,1.0), 2.0 );
   float spe = pow(clamp( dot( ref, lightDir ), 0.0, 1.0 ),16.0);
   dif *= softshadow( ro, lightDir, 0.02, 2.5 );
   dom *= softshadow( ro, ref, 0.02, 2.5 );
   vec3 brdf = vec3(0.0);
   brdf += 1.20*dif*vec3(1.00,0.90,0.60);
   brdf += 1.20*spe*vec3(1.00,0.90,0.60)*dif;
   brdf += 0.30*amb*vec3(0.50,0.70,1.00)*occ;
   brdf += 0.40*dom*vec3(0.50,0.70,1.00)*occ;
   brdf += 0.30*bac*vec3(0.25,0.25,0.25)*occ;
   brdf += 0.40*fre*vec3(1.00,1.00,1.00)*occ;
   if(materialMy.ref)
         //col += pow(texture2D( iChannel2, ref ).xyz,vec3(2.2));
         col += pow(textureCube( iChannel2, ref ).xyz,vec3(2.2));
   col = col*brdf;
//   col = mix( col, vec3(0.8,0.9,1.0), 1.0-exp( -0.0005*dist*dist ) );
   return col ;
}

//----------------------------------------------------------------------
vec3 getColorPixel(inout vec3 ro, vec3 rd, inout vec3 normal, float dist, float typeColor)
{

  //vec3 color = texture2D( iChannel2, rd ).xyz;//vec4(1.);
  vec3 color = textureCube( iChannel2, rd ).xyz;//vec4(1.);
  vec3 hitPos = ro + rd * dist;
  normal = normalize(getNormal(hitPos)); 
//----------------------------------

  if (materialMy.typeMat == 0.0)
  {
    //color = texture2D( iChannel2, rd ).xyz;
    color = textureCube( iChannel2, rd ).xyz;
   }
 else if (materialMy.typeMat == 1.0)   
        color.rgb = texture2D( iChannel0, 0.15 * hitPos.xz ).xyz;
 else if (materialMy.typeMat == 2.0)   
        color = vec3(1., 0.61, 0.43);     
 else if (materialMy.typeMat == 3.0)   
        color = vec3(0.3, 0.7, 1.);
 else if (materialMy.typeMat == 4.0)   
        color = vec3(0.07, 0.08, 0.46);
 else if (materialMy.typeMat == 5.0)   
        color = vec3(0.38, 0.23, 0.67);
 else if (materialMy.typeMat == 7.)
      color = texture2D( iChannel1, 0.15 * hitPos.xz ).xyz;
 else
       color = vec3(0.5);

   if(materialMy.typeMat !=0. )
   {
       color = getLighting1(hitPos, rd, normal, lightDir, color,dist);
   }   
    ro = hitPos;

  return color;
}

//-------------------------------------------------
float render(in vec3 posOnRay, in vec3 rayDir)
{
  float t = 0.0;
  float maxDist = 40.;
  float d = 0.1; 

  for(int i=0; i<MAX_ITER; ++i)
  {
    if (abs(d) <EPSILON || t > maxDist)
         break;
    t += d;
    vec3 ro = posOnRay + t*rayDir;
    d = renderFunction(ro);     
  }

   return t;
}
//------------------------------------------
void main()
{
    vec2 pos     =  gl_FragCoord.xy / iResolution.xy * 2. - 1.;
    pos.x *= iResolution.x / iResolution.y; 
    vec2 m = vec2(0.);
    if( iMouse.z>0.0 )m = iMouse.xy/iResolution.xy*PI*2.;
   
    vec3 camPos = vec3(0., 3., 17.);
    camPos.yz=rot(camPos.yz, m.y);
    camPos.xz=rot(camPos.yz, m.x+ 0.1*time+1.);
    vec3 camP = rotationCoord(camPos,time * 0.1, 3.);
    vec3 camUp = vec3(0. , 1., 0.);
    vec3 camDir = normalize(-camP);
    vec3 u = normalize(cross(camUp,camDir));
    vec3 v = cross(camDir,u);
    vec3 rayDir = normalize(camDir * 2. + pos.x * u + pos.y * v); 

   vec4 color    = vec4(1.0);
    vec3 normal   = vec3(1.0);
    vec3 posOnRay = camP;
    float path = 0.;
  //---------------------------
     path =  render(posOnRay, rayDir); 
     color.rgb = getColorPixel(posOnRay, rayDir, normal, path, materialMy.typeMat);
    // Gamma correct
     color.rgb = pow(color.rgb , vec3(0.45));
    // Contrast adjust - cute trick learned from iq
     color.rgb  = mix( color.rgb , vec3(dot(color.rgb ,vec3(0.333))), -0.6 );
     color.a = 1.;
     gl_FragColor =  color;

}

//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

tex0 = moon3d.image.load2d("./data/tex06.jpg")
tex1 = moon3d.image.load2d("./data/tex08.jpg")
-- tex2 = moon3d.image.load2d("./data/cubemap01_posz.jpg")
cubemap = moon3d.image.loadCubeMap("./data/cubemap01_posx.jpg", "./data/cubemap01_negx.jpg", "./data/cubemap01_posy.jpg", "./data/cubemap01_negy.jpg", "./data/cubemap01_posz.jpg", "./data/cubemap01_negz.jpg")
moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex0, 0)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex1, 1)
--moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex2, 2)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, cubemap, 2)

moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/trampoline", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
Flappy Bird converted
« Reply #27 on: June 09, 2015, 12:04:47 AM »
Flappy Bird converted to GLSL Hacker format

iChannelTime[0] needs to be replaced with iGlobalTime

Copy the code and save as Flappy_Bird_gl2.xml in demo folder of MadShaders.

Code: [Select]
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Flappy Bird"
          width="800" height="400"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="shadertoy_prog" >
    <raw_data_vs><![CDATA[     
void main()
{   
    gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;       
}
  ]]></raw_data_vs>
 
    <raw_data_ps><![CDATA[     

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

uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click

//iChannelTime[0] replaced with iGlobalTime

// Flappy Bird (tribute), fragment shader by movAX13h, Feb.2014

float rand(float n)
{
    return fract(sin(n * 12.9898) * 43758.5453)-0.5;
}

void pipe(inout vec3 col, vec2 p, float h)
{
    vec2 ap = abs(p);
    if (ap.y > h)
    {
        float dy = ap.y - h;
        if (dy < 60.0) ap.x *= 0.93;
        col = mix(col, vec3(0.322, 0.224, 0.290), step(ap.x, 65.0)); // outline
        if (dy > 60.0 || mod(dy, 55.0) > 5.0)
        {
            float gradient = 0.0;
            if (abs(dy - 57.5) > 7.5) gradient = max(0.0, 0.5*cos(floor((p.x+25.0)/5.0)*5.0*(0.026 - 0.006*step(dy, 10.0))));
            col = mix(col, vec3(0.322, 0.506, 0.129) + gradient, step(ap.x, 60.0)); // pipe
        }
    }
}

// constant-array-index workaround ---
float slice(int id)
{
    // flappy bird character (no worries, I have a tool)
    if (id == 0) return 2359296.0;
    if (id == 1) return 585.0;
    if (id == 2) return 4489216.0;
    if (id == 3) return 46674.0;
    if (id == 4) return 4751360.0;
    if (id == 5) return 2995812.0;
    if (id == 6) return 8945664.0;
    if (id == 7) return 3003172.0;
    if (id == return 9469963.0;
    if (id == 9) return 7248164.0;
    if (id == 10) return 2359385.0;
    if (id == 11) return 10897481.0;
    if (id == 12) return 6554331.0;
    if (id == 13) return 9574107.0;
    if (id == 14) return 2134601.0;
    if (id == 15) return 9492189.0;
    if (id == 16) return 3894705.0;
    if (id == 17) return 9474632.0;
    if (id == 18) return 2396785.0;
    if (id == 19) return 9585152.0;
    if (id == 20) return 14380132.0;
    if (id == 21) return 8683521.0;
    if (id == 22) return 2398500.0;
    if (id == 23) return 1.0;
    if (id == 24) return 4681.0;   
    return 0.0;   
}

vec3 color(int id)
{
    // flappy bird colors
    if (id == 0) return vec3(0.0);
    if (id == 1) return vec3(0.320,0.223,0.289);
    if (id == 2) return vec3(0.996,0.449,0.063);
    if (id == 3) return vec3(0.965,0.996,0.965);
    if (id == 4) return vec3(0.996,0.223,0.000);
    if (id == 5) return vec3(0.836,0.902,0.805);
    return vec3(0.965,0.707,0.191);
}
// ---

int sprite(vec2 p)
{
    // this time it's 3 bit/px (8 colors) and 8px/slice, 204px total
    int d = 0;
    p = floor(p);
    p.x = 16.0 - p.x;
   
    if (clamp(p.x, 0.0, 16.0) == p.x && clamp(p.y, 0.0, 11.0) == p.y)
    {
        float k = p.x + 17.0*p.y;
        float s = floor(k / 8.0);
        float n = slice(int(s));
        k = (k - s*8.0)*3.0;
        if (int(mod(n/(pow(2.0,k)),2.0)) == 1)         d += 1;
        if (int(mod(n/(pow(2.0,k+1.0)),2.0)) == 1)     d += 2;
        if (int(mod(n/(pow(2.0,k+2.0)),2.0)) == 1)     d += 4;
    }
    return d;
}

void hero(inout vec3 col, vec2 p, float angle)
{
    p = vec2(p.x * cos(angle) - p.y * sin(angle), p.y * cos(angle) + p.x * sin(angle));
    int i = sprite(p*0.2);
    col = mix(col, color(i), min(1.0, float(i)));
}

void ground(inout vec3 col, vec2 p)
{
    p = floor(p);
    if (p.y > -280.0) return;
    if (p.y < -285.0) col = color(1);
    if (p.y < -290.0) col = vec3(0.902, 1.000, 0.549);
    if (p.y < -295.0) col = mix(vec3(0.612, 0.906, 0.353), vec3(0.451, 0.745, 0.192), step(mod(p.x-floor(p.y/5.0)*5.0, 60.0), 30.0));
    if (p.y < -325.0) col = vec3(0.322, 0.506, 0.129);
    if (p.y < -330.0) col = vec3(0.839, 0.667, 0.290);
    if (p.y < -335.0) col = vec3(0.871, 0.843, 0.580);
}

void sky(inout vec3 col, vec2 p)
{
    col = mix(col, vec3(1.0), 0.3*sin(p.y*0.01));
}

float hAt(float i)
{
    return 250.0*rand(i*1.232157);
}

void main()
{
    float s = 2000.0/iResolution.x;
    vec2 p = max(1.6666667, s)*(gl_FragCoord.xy - iResolution.xy * 0.5);
    float dx = iGlobalTime * 320.0;
    p.x += dx;
   
    vec3 col = vec3(0.322, 0.745, 0.808);
    sky(col, vec2(0.0, -100.0)-p);
   
    pipe(col, vec2(mod(p.x, 400.0)-200.0, p.y + hAt(floor(p.x / 400.0)) - 80.0), 110.0);
   
    float hx = dx - 200.0; // hero x
    float sx = hx - 300.0; // sample x
    float i = floor(sx/400.0); // instance
    float ch = hAt(i); // current height
    float nh = hAt(i+1.0); // next height
    //    float bh = abs(60.0*sin(iChannelTime[0]*6.0)); // bounce height
    float bh = abs(60.0*sin(iGlobalTime*6.0)); // bounce height
    float hy = bh - mix(ch, nh, min(1.0, mod(sx, 400.0)*0.005)) + 80.0; // hero y
    float angle = -min(0.1, 0.002*(bh));
    hero(col, vec2(hx, hy)-p, angle);
   
    ground(col, p);
   
    gl_FragColor = vec4(col,1.0);
}



//

]]></raw_data_ps>

</gpu_program>

 
    <script name="init_scene" run_mode="INIT" >
        <raw_data><![CDATA[   

app_dir = gh_utils.get_scripting_libs_dir()         
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

 
    <script name="update_scene" run_mode="FRAME" >
        <raw_data><![CDATA[   

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Flappy Bird", global_time, 1, 1, 1, 1)

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

    <script name="resize_scene" run_mode="SIZE" >
        <raw_data><![CDATA[   

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

        ]]></raw_data>
    </script>
 
</glsl_hacker>

« Last Edit: June 09, 2015, 05:14:37 PM by Stefan »