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



0 Members and 1 Guest are viewing this topic.

Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4222
    • View Profile
Old conversion topics merged
« on: May 30, 2015, 02:55:38 PM »
EvilSeed @ Shadertoy converted to GLSL Hacker format

My first try with conversion and for no reason a big screenshot :P

Copy the code and save as evilseed_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/EvilSeed"
          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/llBGRd

 
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 vec3 iChannelResolution0;



const float pi = 3.14159;

mat3 xrot(float t)
{
    return mat3(1.0, 0.0, 0.0,
                0.0, cos(t), -sin(t),
                0.0, sin(t), cos(t));
}

mat3 yrot(float t)
{
    return mat3(cos(t), 0.0, -sin(t),
                0.0, 1.0, 0.0,
                sin(t), 0.0, cos(t));
}

mat3 zrot(float t)
{
    return mat3(cos(t), -sin(t), 0.0,
                sin(t), cos(t), 0.0,
                0.0, 0.0, 1.0);
}

float btime = 1.0;
float bspeed = 10.0;

float map(vec3 pos)
{
    float dir = 1.0;
    vec3 id = floor(pos);
    pos.y += sin(iGlobalTime+id.x*10.0+id.z*10.0);
    pos = fract(pos) * 2.0 - 1.0;
    pos *= yrot(iGlobalTime*btime*4.0*dir);
    float s = length(pos.xz);
    float k = atan(pos.z, pos.x);
    float mr = 0.9;
    float r = mix(0.8, 1.0, 0.5 + 0.5 * sin(s/mr*dir*bspeed+k*10.0));
    float p = mr * r;
    float e = 1.0 - s/p;
    vec3 c = vec3(pos.x, clamp(pos.y,-0.02*e, 0.02*e), pos.z);
    return min(max(length(c-pos), s-p), s-0.1);
}

vec3 normal(vec3 p)
{
    vec3 o = vec3(0.01, 0.0, 0.0);
    return normalize(vec3(map(p+o.xyy) - map(p-o.xyy),
                          map(p+o.yxy) - map(p-o.yxy),
                          map(p+o.yyx) - map(p-o.yyx)));
}

float trace(vec3 o, vec3 r)
{
     float t = 0.0;
    for (int i = 0; i < 16; ++i) {
         vec3 p = o + r * t;
        float d = map(p);
        t += d * 0.5;
    }
    return t;
}

float pshade(vec3 p)
{
    vec3 id = floor(p);
    p.y += sin(iGlobalTime+id.x*10.0+id.z*10.0);
    p = fract(p) * 2.0 - 1.0;
    p *= yrot(iGlobalTime*btime*4.0+length(p.xz)*4.0);
    p += id;
    float ac = texture2D(iChannel0, vec2(p.y,p.z)).x;
    float bc = texture2D(iChannel0, vec2(p.x,p.z)).x;
    float cc = texture2D(iChannel0, vec2(p.x,p.y)).x;
    float s = ((ac + bc + cc) / 3.0) * 2.0 - 1.0;
    return s;
}

vec4 surf(vec3 r, vec3 w, vec3 sn, float t)
{
    float prod = max(dot(sn,-r), 0.0);
    float off = 0.5 + 0.5 * sin(pshade(w)*pi*5.0);
    float ao = map(w) * 20.0;
    float fog = prod / (1.0 + t * t + off + ao);
    return vec4(vec3(fog),off);
}

vec3 shade(vec3 r, float time)
{
    r *= xrot(iGlobalTime) * zrot(time);
   
    vec3 o = vec3(0.0, 0.0, 0.0);
    o.z += time;
   
    float t = trace(o, r);
    vec3 w = o + r * t;
    vec3 sn = normal(w);
   
    vec4 ac = surf(r, w, sn, t);
   
    vec3 from = vec3(0.8, 0.2, 0.1);
    vec3 to = vec3(1.0, 1.0, 1.0);
   
    float fx = 1.0 - ac.w;
   
    vec3 mixed = ac.xyz * mix(from, to, fx);
   
    return mixed;
}

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
    uv = uv * 2.0 - 1.0;
    uv.x *= iResolution.x / iResolution.y;
   
    vec3 r = normalize(vec3(uv, 1.0 - dot(uv,uv) * 0.33));

    btime = 2.0;
    bspeed = 10.0;
    vec3 sa = shade(r, iGlobalTime);
   
    float of = 0.01;
   
    btime = 4.0;
    bspeed = 20.0;
    vec3 sb = shade(r, iGlobalTime+of);

    btime = 6.0;
    bspeed = 20.0;
    vec3 sc = shade(r, iGlobalTime+of*2.0);
   
    vec3 f = sa * 0.2 + sb * 0.5 + sc * 0.3;
   
    gl_FragColor = vec4(f,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/EvilSeed", 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 11, 2015, 05:25:41 PM by Stefan »

Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4222
    • View Profile
38911 BASIC BYTES FREE converted
« Reply #1 on: May 30, 2015, 03:37:12 PM »
38911 BASIC Bytes free converted to GLSL Hacker format

The original resolution of the C64 was 320x200, but that looks too crappy nowadays.

Copy the code and save as 38911_basic_bytes_free_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/38911 BASIC Bytes free"
          width="640" 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/MljGWG

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 vec3      iResolution;     // viewport resolution (in pixels)



//

//-------------------------------------------------------------------------
// general utilities

float dtoa(float d, float amount)
{
    return clamp(1.0 / (clamp(d, 1.0/amount, 1.0)*amount), 0.,1.);
}

// returns 0.0 or 1.0
float extractBit(float fv, float bitIndex)
{
    fv = floor(fv / pow(2., bitIndex));// shift right bitIndex and remove unwanted bits to the right
    fv /= 2.;// shift one more to put our value into decimal portion
    fv = fract(fv);// our value is now isolated. fv is now exactly 0.0 or approx.0.5
    return sign(fv);
}

// not exactly perfectly perfect, but darn close
float pointRectDist(vec2 p, vec2 rectTL, vec2 rectBR)
{
  float dx = max(max(rectTL.x - p.x, 0.), p.x - rectBR.x);
  float dy = max(max(rectTL.y - p.y, 0.), p.y - rectBR.y);
  return max(dx, dy);
}


// warps (0,0)-(1,1) coords
vec2 tvWarp(vec2 uv) {
    uv = (uv - 0.5) * 2.0;// uv is now -1 to 1
    uv *= 1.1;
    uv.x *= 1.0 + pow((abs(uv.y) / 4.0), 2.5);
    uv.y *= 1.0 + pow((abs(uv.x) / 3.5), 2.5);
    uv = (uv / 2.0) + 0.5;// back to 0-1 coords
    uv = uv * 0.92 + 0.04;
    return uv;
}

vec2 getuv(vec2 gl_FragCoord, vec2 newTL, vec2 newSize, out float distanceToVisibleArea, out float vignetteAmt)
{
    vec2 ret = vec2(gl_FragCoord.x / iResolution.x, (iResolution.y - gl_FragCoord.y) / iResolution.y);// ret is now 0-1 in both dimensions
   
    // warp
    ret = tvWarp(ret / 2.) * 2.;// scale it by 2.
    distanceToVisibleArea = pointRectDist(ret, vec2(0.0), vec2(1.));

    // vignette
    vec2 vignetteCenter = vec2(0.5, 0.7);
    vignetteAmt = 1.0 - distance(ret, vignetteCenter);
    vignetteAmt = pow(vignetteAmt, 0.4);// strength
   
    ret *= newSize;// scale up to new dimensions
    float aspect = iResolution.x / iResolution.y;
    ret.x *= aspect;// orig aspect ratio
    float newWidth = newSize.x * aspect;
    return ret + vec2(newTL.x - (newWidth - newSize.x) / 2.0, newTL.y);
}



//-------------------------------------------------------------------------
// font drawing code ...

const int g_glyphCount = 38;
void getGlyphAtIndex(int gi, out vec4 scan0123, out vec4 scan4567)
{
    if(gi==0){scan0123=vec4(0x18,0x3C,0x66,0x7E);scan4567=vec4(0x66,0x66,0x66,0x00);return;}
    if(gi==1){scan0123=vec4(0x7C,0x66,0x66,0x7C);scan4567=vec4(0x66,0x66,0x7C,0x00);return;}
    if(gi==2){scan0123=vec4(0x3C,0x66,0x60,0x60);scan4567=vec4(0x60,0x66,0x3C,0x00);return;}
    if(gi==3){scan0123=vec4(0x78,0x6C,0x66,0x66);scan4567=vec4(0x66,0x6C,0x78,0x00);return;}
    if(gi==4){scan0123=vec4(0x7E,0x60,0x60,0x78);scan4567=vec4(0x60,0x60,0x7E,0x00);return;}
    if(gi==5){scan0123=vec4(0x7E,0x60,0x60,0x78);scan4567=vec4(0x60,0x60,0x60,0x00);return;}
    if(gi==6){scan0123=vec4(0x3C,0x66,0x60,0x6E);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==7){scan0123=vec4(0x66,0x66,0x66,0x7E);scan4567=vec4(0x66,0x66,0x66,0x00);return;}
    if(gi==8){scan0123=vec4(0x3C,0x18,0x18,0x18);scan4567=vec4(0x18,0x18,0x3C,0x00);return;}
    if(gi==9){scan0123=vec4(0x1E,0x0C,0x0C,0x0C);scan4567=vec4(0x0C,0x6C,0x38,0x00);return;}
    if(gi==10){scan0123=vec4(0x66,0x6C,0x78,0x70);scan4567=vec4(0x78,0x6C,0x66,0x00);return;}
    if(gi==11){scan0123=vec4(0x60,0x60,0x60,0x60);scan4567=vec4(0x60,0x60,0x7E,0x00);return;}
    if(gi==12){scan0123=vec4(0x63,0x77,0x7F,0x6B);scan4567=vec4(0x63,0x63,0x63,0x00);return;}
    if(gi==13){scan0123=vec4(0x66,0x76,0x7E,0x6E);scan4567=vec4(0x66,0x66,0x66,0x00);return;}
    if(gi==14){scan0123=vec4(0x3C,0x66,0x66,0x66);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==15){scan0123=vec4(0x7C,0x66,0x66,0x66);scan4567=vec4(0x7C,0x60,0x60,0x00);return;}
    if(gi==16){scan0123=vec4(0x3C,0x66,0x66,0x66);scan4567=vec4(0x66,0x3C,0x0E,0x00);return;}
    if(gi==17){scan0123=vec4(0x7C,0x66,0x66,0x7C);scan4567=vec4(0x78,0x6C,0x66,0x00);return;}
    if(gi==18){scan0123=vec4(0x3C,0x66,0x60,0x3C);scan4567=vec4(0x06,0x66,0x3C,0x00);return;}
    if(gi==19){scan0123=vec4(0x7E,0x18,0x18,0x18);scan4567=vec4(0x18,0x18,0x18,0x00);return;}
    if(gi==20){scan0123=vec4(0x66,0x66,0x66,0x66);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==21){scan0123=vec4(0x66,0x66,0x66,0x66);scan4567=vec4(0x66,0x3C,0x18,0x00);return;}
    if(gi==22){scan0123=vec4(0x63,0x63,0x63,0x6B);scan4567=vec4(0x7F,0x77,0x63,0x00);return;}
    if(gi==23){scan0123=vec4(0x66,0x66,0x3C,0x18);scan4567=vec4(0x3C,0x66,0x66,0x00);return;}
    if(gi==24){scan0123=vec4(0x66,0x66,0x66,0x3C);scan4567=vec4(0x18,0x18,0x18,0x00);return;}
    if(gi==25){scan0123=vec4(0x7E,0x06,0x0C,0x18);scan4567=vec4(0x30,0x60,0x7E,0x00);return;}
    if(gi==26){scan0123=vec4(0x3C,0x66,0x6E,0x76);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==27){scan0123=vec4(0x18,0x18,0x38,0x18);scan4567=vec4(0x18,0x18,0x7E,0x00);return;}
    if(gi==28){scan0123=vec4(0x3C,0x66,0x06,0x0C);scan4567=vec4(0x30,0x60,0x7E,0x00);return;}
    if(gi==29){scan0123=vec4(0x3C,0x66,0x06,0x1C);scan4567=vec4(0x06,0x66,0x3C,0x00);return;}
    if(gi==30){scan0123=vec4(0x06,0x0E,0x1E,0x66);scan4567=vec4(0x7F,0x06,0x06,0x00);return;}
    if(gi==31){scan0123=vec4(0x7E,0x60,0x7C,0x06);scan4567=vec4(0x06,0x66,0x3C,0x00);return;}
    if(gi==32){scan0123=vec4(0x3C,0x66,0x60,0x7C);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==33){scan0123=vec4(0x7E,0x66,0x0C,0x18);scan4567=vec4(0x18,0x18,0x18,0x00);return;}
    if(gi==34){scan0123=vec4(0x3C,0x66,0x66,0x3C);scan4567=vec4(0x66,0x66,0x3C,0x00);return;}
    if(gi==35){scan0123=vec4(0x3C,0x66,0x66,0x3E);scan4567=vec4(0x06,0x66,0x3C,0x00);return;}
    if(gi==36){scan0123=vec4(0x00,0x00,0x00,0x00);scan4567=vec4(0x00,0x18,0x18,0x00);return;}
    if(gi==37){scan0123=vec4(0x00,0x66,0x3C,0xFF);scan4567=vec4(0x3C,0x66,0x00,0x00);return;}   
    scan0123 = vec4(0.);scan4567 = vec4(0.);
}

// stringIndex lets you use the same pos for a string of chars, just incrementing stringIndex.
// this is pretty fast, but is binary. a prettier version might return a distance function but will suffer perf problems because of the complex geometry.
vec4 drawCharacter(vec4 inpColor, vec4 glyphColor, vec2 uv, vec2 pos, vec2 charSize, float stringIndex, int glyphIndex)
{
    vec2 element = floor(((uv - pos) / (charSize / 8.)));// convert uv to pixel indices
    element.x -= stringIndex * 8.0;
    element.x = floor(7.0 - element.x);// flip X. maybe my encoding method is wrong?
    // bounds check; most of the time uv will not land on the character so important to optimize this.
    if(element.y < 0. || element.y > 7.) return inpColor;
    if(element.x < 0. || element.x > 7.) return inpColor;

    vec4 scan0123;
    vec4 scan4567;
    getGlyphAtIndex(glyphIndex, scan0123, scan4567);
   
    int scanLineI = int(element.y);
    float scanLine;
   
    if(scanLineI == 0) scanLine = scan0123[0];
    else if(scanLineI == 1) scanLine = scan0123[1];
    else if(scanLineI == 2) scanLine = scan0123[2];
    else if(scanLineI == 3) scanLine = scan0123[3];
    else if(scanLineI == 4) scanLine = scan4567[0];
    else if(scanLineI == 5) scanLine = scan4567[1];
    else if(scanLineI == 6) scanLine = scan4567[2];
    else if(scanLineI == 7) scanLine = scan4567[3];

    float a = extractBit(scanLine, element.x);
    return vec4(mix(inpColor.rgb, glyphColor.rgb, a * glyphColor.a), inpColor.a);
}




//-------------------------------------------------------------------------
vec4 hardRect(vec4 inpColor, vec4 rectColor, vec2 uv, vec2 tl, vec2 br)
{
    if(uv.x < tl.x)
        return inpColor;
    if(uv.x > br.x)
        return inpColor;
    if(uv.y < tl.y)
        return inpColor;
    if(uv.y > br.y)
        return inpColor;
    return rectColor;
}


float nsin(float x)
{
    return (sin(x) + 1.0) / 2.;
}


void main()
{
    float distanceToVisibleArea;
    float vignetteAmt;
    vec2 uv = getuv(gl_FragCoord, vec2(1.08,-0.1), vec2(2.6), distanceToVisibleArea, vignetteAmt);

    vec4 darkBlue = vec4(0.21,0.16,0.47,1.0);
    vec4 lightBlue = vec4(0.42,0.37,0.71,1.0);

    // main background
    gl_FragColor = darkBlue;
   
    // border
    vec2 charAreaTL = vec2(0.6, 0.3);
    if(uv.x < charAreaTL.x)
        gl_FragColor = lightBlue;
    if(uv.y < charAreaTL.y)
        gl_FragColor = lightBlue;

    // ready.
    vec4 charColor = lightBlue;
    vec2 charSize = vec2(0.2);
    charSize.x *= 0.936;// c64 aspect ratio
    vec2 stringPos = charAreaTL + vec2(0, 1.0 * charSize.y);// line 1
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 4., 37);// *
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 5., 37);// *
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 6., 37);// *
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 7., 37);// *

    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 9., 2);// C
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 10., 14);// O
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 11., 12);// M
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 12., 12);// M
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 13., 14);// O
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 14., 3);// D
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 15., 14);// O
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 16., 17);// R
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 17., 4);// E

    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 19., 32);// 6
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 20., 30);// 4
   
    stringPos = charAreaTL + vec2(0, 3.0 * charSize.y);// line 3
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 1., 32);// 6
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 2., 30);// 4
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 3., 10);// K
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 5., 17);// R
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 6., 0);// A
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 7., 12);// M

    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 9., 18);// S
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 10., 24);// Y
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 11., 18);// S
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 12., 19);// T
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 13., 4);// E
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 14., 12);// M
   
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 17., 29);// 3
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 18., 34);// 8
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 19., 35);// 9
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 20., 27);// 1
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 21., 27);// 1
   
    stringPos = charAreaTL + vec2(0, 5.0 * charSize.y);// line 5
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 0., 17);// R
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 1., 4);// E
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 2., 0);// A
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 3., 3);// D
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 4., 24);// Y
    gl_FragColor = drawCharacter(gl_FragColor, charColor, uv, stringPos, charSize, 5., 36);// .
   
    if(mod(iGlobalTime, 1.) < 0.5)
    {
        vec2 tl = vec2(stringPos.x, stringPos.y + charSize.y);
        gl_FragColor = hardRect(gl_FragColor, charColor, uv, tl, tl + charSize);
    }

   
    // black out warped area
    gl_FragColor = vec4(mix(vec3(0.), gl_FragColor.rgb, dtoa(distanceToVisibleArea, 200.)), 1.0);
       
    gl_FragColor.rgb *= vignetteAmt;

    //Draws the horizontal scan lines across the screen
    float scanLineFX = nsin(uv.y * 500.);
    scanLineFX = clamp(pow(scanLineFX, 4.0), 0.3, 1.0);
    gl_FragColor.rgb *= 1.0 + scanLineFX;
    gl_FragColor.rgb = clamp(gl_FragColor.rgb, 0.0, 1.0);

    //gl_FragColor = gridOverlay(gl_FragColor, uv);
}



//

]]></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/38911 BASIC Bytes free", 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: 4222
    • View Profile
Magnetismic converted
« Reply #2 on: May 30, 2015, 09:11:13 PM »
Magnetismic converted to GLSL Hacker format

Note: i stretched iResolution.y

Copy the code and save as Magnetismic _gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Magnetismic"
          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/XlB3zV

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;



//

//Magnetismic by nimitz (twitter: @stormoid)

//Getting 60fps here at high quality
#define HIGH_QUALITY

#ifdef HIGH_QUALITY
#define STEPS 100
#define ALPHA_WEIGHT 0.025
#define BASE_STEP 0.05
#else
#define STEPS 50
#define ALPHA_WEIGHT 0.05
#define BASE_STEP 0.1
#endif

#define time iGlobalTime
vec2 mo;
vec2 rot(in vec2 p, in float a){float c = cos(a), s = sin(a);return p*mat2(c,s,-s,c);}
float hash21(in vec2 n){ return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); }
float noise(in vec3 p) //iq's ubiquitous 3d noise
{
    vec3 ip = floor(p), f = fract(p);
    #ifdef HIGH_QUALITY
    f = f*f*f*(f*(f*6. - 15.) + 10.); //Quintic smoothing
    #else
    f = f*f*(3.0-2.0*f); //Cubic smoothing
    #endif
    vec2 uv = (ip.xy+vec2(37.0,17.0)*ip.z) + f.xy;
    vec2 rg = texture2D( iChannel0, (uv+ 0.5)/256.0, -100.0 ).yx;
    return mix(rg.x, rg.y, f.z);
}

float fbm(in vec3 p, in float sr)
{
    p *= 3.5;
    float rz = 0., z = 1.;
    for(int i=0;i<4;i++)
    {
        float n = noise(p-time*.6);
        rz += (sin(n*4.4)-.45)*z;
        z *= .47;
        p *= 3.5;
    }
    return rz;
}

vec4 map(in vec3 p)
{
    float dtp = dot(p,p);
    p = .5*p/(dtp + .2);
    p.xz = rot(p.xz, p.y*2.5);
    p.xy = rot(p.xz, p.y*2.);
   
    float dtp2 = dot(p, p);
    p = (mo.y + .6)*3.*p/(dtp2 - 5.);
    float r = clamp(fbm(p, dtp*0.1)*1.5-dtp*(.35-sin(time*0.3)*0.15), 0. ,1.);
    vec4 col = vec4(.5,1.7,.5,.96)*r;
   
    float grd = clamp((dtp+.7)*0.4,0.,1.);
    col.b += grd*.6;
    col.r -= grd*.5;   
    vec3 lv = mix(p,vec3(0.3),2.);
    grd = clamp((col.w - fbm(p+lv*.05,1.))*2., 0.01, 1.5 );
    col.rgb *= vec3(.5, 0.4, .6)*grd + vec3(4.,0.,.4);
    col.a *= clamp(dtp*2.-1.,0.,1.)*0.07+0.93;
   
    return col;
}

vec4 vmarch(in vec3 ro, in vec3 rd)
{
    vec4 rz = vec4(0);
    float t = 2.5;
    t += 0.03*hash21(gl_FragCoord.xy);
    for(int i=0; i<STEPS; i++)
    {
        if(rz.a > 0.99 || t > 6.)break;
        vec3 pos = ro + t*rd;
        vec4 col = map(pos);
        float den = col.a;
        col.a *= ALPHA_WEIGHT;
        col.rgb *= col.a*1.7;
        rz += col*(1. - rz.a);
        t += BASE_STEP - den*(BASE_STEP-BASE_STEP*0.015);
    }
    return rz;
}

void main()
{
    vec2 p = gl_FragCoord.xy/iResolution.xy*2. - 1.;
    p.x *= iResolution.x/iResolution.y*.3;  // y stretched
    mo = 2.0*iMouse.xy/iResolution.xy;
    mo = (mo==vec2(.0))?mo=vec2(0.5,1.):mo;
   
    vec3 ro = 4.*normalize(vec3(cos(2.75-2.0*(mo.x+time*0.05)), sin(time*0.22)*0.2, sin(2.75-2.0*(mo.x+time*0.05))));
    vec3 eye = normalize(vec3(0) - ro);
    vec3 rgt = normalize(cross(vec3(0,1,0), eye));
    vec3 up = cross(eye,rgt);
    vec3 rd = normalize(p.x*rgt + p.y*up + (3.3-sin(time*0.3)*.7)*eye);
   
    vec4 col = vmarch(ro, rd);
    gl_FragColor = vec4(col.rgb, 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_0002_256x256.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/Magnetismic", 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: 4222
    • View Profile
raymarching is so awesome - converted
« Reply #3 on: May 30, 2015, 09:15:21 PM »
raymarching is so awesome converted to GLSL Hacker format

Copy the code and save as raymarching_is_so_awesome _gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/raymarching is so awesome"
          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/4lB3WK
// copy canvas.png for iChannel0 into /data/folder

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;



//

#define FOV_SCALE 1.
#define EPSILON .008
const vec3 ne = vec3(.0004435,0.,0.);
vec3 repeat = vec3(20.,0.,20.);
vec3 repeat2 = repeat/2.;

float df(in vec3 p) {
    vec3 ap = abs(p);
    vec3 r = mod(ap,repeat)-repeat2;
    float d = min(1.+p.y,
                  length(max(abs(r)-vec3(2.,40.,2.),0.)));
   
    float dp = cos(ap.x+ap.z)*.5+.5;
    ap.y += dp;
    d = max(-(length(mod(ap,vec3(5.))-vec3(2.5))-2.), d);

    return d;
}
vec3 nrm(in vec3 p) {
    return normalize(vec3(df(p+ne)-df(p-ne),
                     df(p+ne.yxz)-df(p-ne.yxz),
                     df(p+ne.yzx)-df(p-ne.yzx)));
}

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy - vec2(.5);
    uv.y *= iResolution.y/iResolution.x;
   
    vec3 ld = normalize(vec3(500.,100.,200.));
    float ct = iGlobalTime*10.;
    vec3 rp = vec3(0.,20.,ct);
    vec3 rd = normalize(vec3(0.,-2.,10.));
    vec3 srd = rd;
    vec3 rr = cross(vec3(0.,1.,0.),rd);
    rd = normalize(rd*FOV_SCALE + uv.x*rr + uv.y*cross(rd,rr));
    //rr = vec3(0.,0.,0.)*.001;
   
    float d,s = 0.;
    for (int i = 0; i < 256; i++) {
        d = df(rp);
        if (d < EPSILON) break;
       
        rp += d*rd;
        s += d*.04;
        //rd = normalize(rd+d*rr);
        rr = cross(vec3(sin(s),cos(s),0.)+.5,rd);
        rd = normalize(srd*FOV_SCALE + uv.x*rr + uv.y*cross(rd,rr));
    }
   
    vec3 c;
    if (d < EPSILON) {
        vec3 n = nrm(rp);
        c = texture2D(iChannel0,(rp.xy+rp.zx*.5)*.1).xyz;
       
        rd = n;
        rp -= rd*d;
        for (int i = 0; i < 4; i++) {
            d = df(rp);
            if (d < EPSILON) break;
        }
       
        c = .14*c +
            max(0.,dot(ld,n))*c;
    } else {
        c = vec3(.9);
    }
   
    gl_FragColor = vec4(pow(c,vec3(1.2,1.2,1.3)),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/tex06.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.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/raymarching is so awesome", 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: 4222
    • View Profile
MachineRoom converted
« Reply #4 on: May 30, 2015, 09:55:12 PM »
MachineRoom converted to GLSL Hacker format

Copy the code and save as MachineRoom_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/MachineRoom"
          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/4lB3W3

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;



//

const float pi = 3.14159;

mat3 xrot(float t)
{
    return mat3(1.0, 0.0, 0.0,
                0.0, cos(t), -sin(t),
                0.0, sin(t), cos(t));
}

mat3 yrot(float t)
{
    return mat3(cos(t), 0.0, -sin(t),
                0.0, 1.0, 0.0,
                sin(t), 0.0, cos(t));
}

mat3 zrot(float t)
{
    return mat3(cos(t), -sin(t), 0.0,
                sin(t), cos(t), 0.0,
                0.0, 0.0, 1.0);
}

vec3 paxis(vec3 p)
{ /* thanks to eiffie */
    vec3 a=abs(p),r = vec3(1.0,0.0,0.0);
    if(a.z>=max(a.x,a.y))r=r.yzx;
    else if(a.y>=a.x)r=r.zxy;
    return r*sign(p);
}

float udBox( vec3 p, vec3 b )
{ /* thanks to iq */
  return length(max(abs(p)-b,0.0));
}

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

float sdCylinderInfinite(vec3 p, float r)
{
    return length(p.xz) - r;
}

vec3 func(vec3 p, float s)
{
    vec3 off = paxis(p) * s * 1.5;
    p -= off;
    p -= sign(p) * s * 2.75;
    return p;
}

vec2 map(vec3 p)
{
    p.x += sin(p.z);
   
    vec3 op = p;
   
    float k = 16.0;
    p.z = (fract(p.z/k) * 2.0 - 1.0) * k * 0.5;
   
    vec3 ip = p;
   
    float bs = 1.0;
    float r = 0.0;
    float d = 1000.0;

    for (int i = 0; i < 5; ++i) {
       
        ip = func(ip, bs);

        float fd = udBox(ip, vec3(bs));
        if (fd < d) {
            d = fd;
            r = float(i);
        }
       
        bs *= 0.5;
    }
   
    d = max(d, -sdBoxInfinite(p,vec3(1.0)));
   
    float ck = 8.0;
    vec3 pc = vec3(p.x, p.y, (fract(op.z/ck)*2.0-1.0)*ck*0.5);
    d = max(d, -sdCylinderInfinite(pc, 2.0));
   
    float ground = p.y + 0.9;
    if (ground < d) {
        d = ground;
        r = 6.0;
    }

    return vec2(d,r);
}

vec3 normal(vec3 p)
{
    vec3 o = vec3(0.01, 0.0, 0.0);
    return normalize(vec3(map(p+o.xyy).x - map(p-o.xyy).x,
                          map(p+o.yxy).x - map(p-o.yxy).x,
                          map(p+o.yyx).x - map(p-o.yyx).x));
}

float trace(vec3 o, vec3 r)
{
     float t = 0.0;
    for (int i = 0; i < 32; ++i) {
        vec3 p = o + r * t;
        float d = map(p).x;
        t += d * 0.5;
    }
    return t;
}

vec3 texture(vec3 p)
{
    vec3 ta = texture2D(iChannel0, vec2(p.y,p.z)).xyz;
    vec3 tb = texture2D(iChannel0, vec2(p.x,p.z)).xyz;
    vec3 tc = texture2D(iChannel0, vec2(p.x,p.y)).xyz;
    return (ta + tb + tc) / 3.0;
}

float aoc(vec3 origin, vec3 ray) {
    float delta = 0.1;
    const int samples = 6;
    float r = 0.0;
    for (int i = 1; i <= samples; ++i) {
        float t = delta * float(i);
         vec3 pos = origin + ray * t;
        float dist = map(pos).x;
        float len = abs(t - dist);
        r += len * pow(2.0, -float(i));
    }
    return r;
}

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
    uv = uv * 2.0 - 1.0;
    uv.x *= iResolution.x / iResolution.y;
   
    vec3 o = vec3(0.0, 0.0, 0.0);
    o.z += iGlobalTime * 0.5;
    o.x = sin(-o.z);
    vec3 r = normalize(vec3(uv, 1.3));
    r *= yrot(o.x);
   
    float t = trace(o, r);
    vec3 w = o + r * t;
    vec2 mp = map(w);
    float fd = mp.x;
    float it = mp.y;
    vec3 sn = normal(w);

    float fog = 1.0 / (1.0 + t * t * 0.1 + fd * 100.0);
   
    vec3 diff = texture(w);
   
    if (it == 3.0) {
        diff *= 0.5;
    } else if (it == 2.0) {
        diff = diff.xxx * 1.5;
    } else if (it == 1.0) {
        diff *= vec3(1.0, 1.0, 0.0);
    }
   
    float sz = w.x + sin(w.z);
   
    if (it == 6.0) {
        float m = 0.5+0.5*sign(fract(w.z*10.0+abs(sz)*10.0)-0.5);
        float k = 0.5+0.5*sign(abs(sz)-0.;
        float ik = 0.5+0.5*sign(abs(sz)-0.9);
        float cm = k*(1.0-ik);
        vec3 tape = vec2(m*k,0.0).xxy;
        diff = mix(diff*0.5, tape, cm);
    }
   
    vec3 lighting = vec3(0.6);
    for (int i = -2; i <= 2; ++i) {
        float lz = floor(w.z+float(i)+0.5);
        vec3 lpos = vec3(-sin(lz), 0.0, lz);
        vec3 lcol = vec3(1.0);
        float lmod = mod(lz,3.0);
        if (lmod == 0.0) {
            lcol = vec3(0.0,0.0,1.0) * (0.5+0.5*sin(iGlobalTime));
            lpos.y = 1.0;
        } else if (lmod == 2.0) {
            lcol = vec3(0.0, 1.0, 0.0);
            lpos.y = 0.25;
            lpos.x += cos(lz);
        } else {
             lcol = vec3(1.0, 0.0, 0.0);
            lpos.y = -0.25;
            lpos.x -= cos(lz);
        }
        vec3 ldel = lpos - w;
        float ldist = length(ldel);
        ldel /= ldist;
        float lprod = max(dot(sn,ldel),0.0);
        float latten = 1.0 / (1.0 + ldist * ldist);
        lighting += lprod * latten * lcol;
    }
   
    diff *= lighting * fog;

    gl_FragColor = vec4(diff,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/tex02.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.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/MachineRoom", 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: 4222
    • View Profile
AncientMars converted
« Reply #5 on: May 30, 2015, 10:07:56 PM »
AncientMars converted to GLSL Hacker format

Copy the code and save as AncientMars_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/AncientMars"
          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/Mtj3DG

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;



//

// Ancient Ruins
// Scene from Karla Quintero's modern dance performance "If Mars"
//
// Mashup of heavy RMF, Kali's fractal (and stars I believe) and Nimitz's fog trick.
// Added IQ's noise for posting on shadertoy.
// Apologies for the brute force detail and hence slowness.
//
// @rianflo


float smin(float a, float b, float k)
{
    return -(log(exp(k*-a)+exp(k*-b))/k);
}

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

#if 1
float noise( in vec3 x )
{
    vec3 p = floor(x);
    vec3 f = fract(x);
    f = f*f*(3.0-2.0*f);
   
    vec2 uv = (p.xy+vec2(37.0,17.0)*p.z) + f.xy;
    vec2 rg = texture2D( iChannel0, (uv+ 0.5)/256.0, -100.0 ).yx;
    return mix( rg.x, rg.y, f.z );
}
#else
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;
    return 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);
}
#endif
mat3 rotationMatrix(vec3 axis, float angle)
{
    axis = normalize(axis);
    float s = sin(angle);
    float c = cos(angle);
    float oc = 1.0 - c;
   
    return mat3(oc * axis.x * axis.x + c,           oc * axis.x * axis.y - axis.z * s,  oc * axis.z * axis.x + axis.y * s,
                oc * axis.x * axis.y + axis.z * s,  oc * axis.y * axis.y + c,           oc * axis.y * axis.z - axis.x * s,
                oc * axis.z * axis.x - axis.y * s,  oc * axis.y * axis.z + axis.x * s,  oc * axis.z * axis.z + c);
}


const float PI = 3.1415926535897932384626433832795;


// ----------------------------------------------------------EDIT ME----------------------------------------------------------------------
float rmf(vec3 p)
{
    float signal = 0.0;
    float value  = 0.0;
    float weight = 1.0;
    float h = 1.0;
    float f = 1.0;

    for (int curOctave=0; curOctave < 11; curOctave++)
    {
        signal = noise(p)*2.0-0.4;
        signal = pow(1.0 - abs(signal), 2.0) * weight;
        weight = clamp(0.0, 1.0, signal * 16.0);
        value += (signal * pow(f, -1.0));
        f *= 2.0;
        p *= 2.0;
    }
   
    return (value * 1.25) - 1.0;
}

const int Iterations=15;

const float Scale=2.1;

float de(vec3 pos)
{
    vec3 p=pos;
    p.y +=4.76-iGlobalTime*0.005;
    p.xz=abs(.5-mod(pos.xz,1.))+.01;
    float DEfactor=1.;
    float ot=1000.;
    for (int i=0; i<Iterations; i++)
    {
        p = abs(p)-vec3(0.,2.,0.); 
        float r2 = dot(p, p);
        ot = min(ot,abs(length(p)));
        float sc=Scale/clamp(r2,0.4,1.);
        p*=sc;
        DEfactor*=sc;
        p = p - vec3(0.5,1.,0.5);
    }
    float rr=length(pos+vec3(0.,-3.03,1.85))-.017;
    float d=length(p)/DEfactor-.0005;
   
    return d;
}

float sdf(vec3 p)
{
    float sd = p.y;
   
   
    sd = smin(sd, de(p), 60.0);
    //sd = min(sd, length(p)-1.0-rmf(p+time*0.01, 8.0, 1.8, coneR)*0.06);
    sd -= rmf(p*8.0+2.0)*0.015;       // ceiling
   
    return sd;
}

vec4 getColorAndRoughness(vec3 p, vec3 N, float ambo)
{
    return vec4(1.0);
}

vec3 grad(vec3 p, float coneR)
{
    coneR*=3.0;
    vec3 f = vec3(sdf(p));
    vec3 g = vec3(sdf(p+vec3(coneR, 0.0, 0.0)),
                  sdf(p+vec3(0.0, coneR, 0.0)),
                  sdf(p+vec3(0.0, 0.0, coneR)));
    return (g-f) / coneR;
}

vec3 setupRayDirection(float camFov)
{
    vec2 coord = vec2(gl_FragCoord.xy);
    vec2 v = vec2(coord / iResolution.xy) * 2.0 - 1.0;
    float camAspect = iResolution.x/iResolution.y;
    float fov_y_scale = tan(camFov/2.0);
    vec3 raydir = vec3(v.x*fov_y_scale*camAspect, v.y*fov_y_scale, -1.0);
    return normalize(raydir);
}

float ambientOcclusion( in vec3 pos, in vec3 nor, float coneR )
{
  float occ = 0.0;
  float sca = 1.1;
  for ( int i=0; i<5; i++ )
  {
    float hr = 0.02 + 0.11*float(i) / 4.0;
    vec3 aopos =  nor * hr + pos;
    float dd = sdf(aopos);
    occ += -(dd-hr)*sca;
    sca *= 0.95;
  }
  return clamp( 1.0 - 1.5*occ, 0.0, 1.0 );
}


vec3 shade(vec3 ro, vec3 rd, float t, float coneR, vec3 ld)
{
    vec3 g = grad(ro+rd*t, coneR);
    vec3 N = normalize(g);
    float ambo = ambientOcclusion(ro+rd*t, N, coneR);
    float ndl = clamp(dot(N, ld), 0.0, 1.0);
    vec3 color = mix(vec3(0.782, 0.569, 0.45), vec3(0.8, 0.678, 0.569), clamp(1.5-length(g), 0.0, 1.0));
    return vec3(mix(ambo*color, ambo*color*ndl, 0.5));
}

float fogmap(in vec3 p, in float d)
{
    vec3 q = p;
    q.z-=0.0;
    p.x -= iGlobalTime*0.05;
    vec3 turb = vec3(noise(80.0*p.xyz+iGlobalTime*0.51)*0.5, noise(160.0*p.xzy+iGlobalTime*0.2)*0.3, noise(60.0*p.zyx+iGlobalTime*0.1)*0.2);
    p += turb;
    float fog = (max(noise(p*64.0+0.1)-0.1, 0.0)*noise(p*16.0))*0.03;
   
    return fog;
}


#define iterations 14
#define formuparam 0.530

#define volsteps 3
#define stepsize 0.2

#define zoom   1.20
#define tile   0.850
#define speed  0.1

#define brightness 0.0015
#define darkmatter 0.400
#define distfading 0.160
#define saturation 0.400

vec3 space(vec2 uv)
{
    vec3 v=vec3(0.4);
    vec3 dir=vec3(uv*zoom,1.);
   
    //float a2=2.0;
    float a1=4.0;//+time*0.001;
    mat2 rot1=mat2(cos(a1),sin(a1),-sin(a1),cos(a1));
    mat2 rot2=rot1;//mat2(cos(a2),sin(a2),-sin(a2),cos(a2));
    dir.xz*=rot1;
    dir.xy*=rot2;
   
    //from.x-=time;
    //mouse movement
    vec3 from=vec3(170.0, 170.2,0.01);
    from+=vec3(0.0,0.0,-2.);
   
    //from.x-=iMouse.x;
    //from.y-=iMouse.y;
   
    from.xz*=rot1;
    from.xy*=rot2;
   
    //volumetric rendering
    float s=.4,fade=.2;
   
   
    for (int r=0; r<volsteps; r++)
    {
        vec3 q = vec3(0.3, 0.5, -3.0)+s*dir;
       
        vec3 p=from+s*dir*.5;
        p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
        float pa,a=pa=0.;
        for (int i=0; i<iterations; i++)
        {
            p=abs(p)/dot(p,p)-formuparam; // the magic formula
            a+=abs(length(p)-pa); // absolute sum of average change
            pa=length(p);
        }
       
        float dm=max(0.,darkmatter-a*a*.001); //dark matter
        a*=a*a*2.; // add contrast
        if (r>3) fade*=1.-dm; // dark matter, don't render near
        //v+=vec3(dm,dm*.5,0.);
        v+=fade;
        v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
        fade*=distfading; // distance fading
        s+=stepsize;

    }
    v=mix(vec3(length(v)),v,saturation); //color adjust
    return v;
}

void main()
{
    vec2 p = (-iResolution.xy + 2.0 * gl_FragCoord.xy) / iResolution.y;
   
    vec3 lightDir = normalize(vec3(0.0, 0.4, -0.3));
    //float v = mouse.y;
    vec3 rayOrigin = vec3(0.0, 0.01, 0.7);
    mat3 rm = rotationMatrix(vec3(0.0, 0.8, -0.15), -2.1);
    vec3 rayDir = rm*setupRayDirection(radians(60.0));
   

    float travel = 0.0;
   
    vec3 sp = vec3(0.0);
    float coneR;
    vec3 col = space(p)*0.01;
    float fog = 0.0;
   
    for (int i=0; i<256; i++)
    {
       
        coneR = travel * tan(0.5*radians(60.0)/iResolution.y);
        float sd = sdf(rayOrigin+rayDir*travel);
       
        if (sd < coneR)
        {
           
            col = shade(rayOrigin, rayDir, travel, coneR, lightDir);
            col = mix(col, vec3(0.4, 0.4, 0.5), smoothstep(3.0, 4.0, travel));
            break;
        }
       
        fog += fogmap(rayOrigin+rayDir*travel, travel);
        if (travel > 4.0 )
        {
            col = mix(col, vec3(0.4, 0.4, 0.5), smoothstep(1.0, 0.0, p.y));
            break;
        }
       
        travel += min(sd + travel * .001, 8.0);
       
       
    }
   
    fog = min(fog, 1.0);
   
   
   
    col = mix(col, vec3(0.97, .95, .9), fog);
    col = pow(col, vec3(1.2));
   
    vec2 q = gl_FragCoord.xy/iResolution.xy;
    col *= pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.1);
   
    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_0002_256x256.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/AncientMars", 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: 4222
    • View Profile
Anti menger sphere sponge converted
« Reply #6 on: May 31, 2015, 02:51:31 PM »
Anti menger sphere sponge converted to GLSL Hacker format

I changed iResolution a bit.

Move mouse to lower right corner if you see only the checker board.

Copy the code and save as Anti_menger_sphere_sponge_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Anti menger sphere sponge"
          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/MtBGWK

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;



//


//---------------------------------------------------------
// Shader:   RayMarchingPrimitivesV2.glsl
// original: https://www.shadertoy.com/view/Xds3zN   colored
//           http://glslsandbox.com/e#20839          gray scaled
// 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 (animated), 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
//---------------------------------------------------------

#ifdef GL_ES
precision highp float;
#endif

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

#define ANIMATE true
#define ROTATE true
#define flag true

float aTime = 0.0;
float sinTime = 0.0;
//---------------------------------------------------------
vec3 rotateX(vec3 p, float a)
{
  float sa = sin(a);
  float ca = cos(a);
  return vec3(p.x, ca * p.y - sa * p.z, sa * p.y + ca * p.z);
}
vec3 rotateY(vec3 p, float a)
{
  float sa = sin(a);
  float ca = cos(a);
  return vec3(ca * p.x + sa * p.z, p.y, -sa * p.x + ca * p.z);
}
vec3 rotateZ(vec3 p, float a)
{
  float sa = sin(a);
  float ca = cos(a);
  return vec3(ca * p.x - sa * p.y, sa * p.x + ca * p.y, p.z);
}

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

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

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

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

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

float sdWaveSphere(vec3 p, float radius, int waves, float waveSize)
{
  // deformation of radius
  float d = waveSize*(radius-length(p.y));
//  float d = waveSize*(radius*radius-(p.y*p.y));
  radius += d * cos(atan(p.x,p.z) * float(waves));
  return length(p) - radius;
}

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;
}

// t.x = torus radius,  t.y = ring radius
float sdTorus( vec3 p, vec2 t )
{
  return length(vec2(length(p.xz)-t.x, p.y)) - t.y;
}

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 sdBlob (in vec3 pos, in float r)
{
  vec3 v1 = pos * 6.0;
  return 0.05*(r + 0.5* (length(dot(v1, v1)) -0.51*(cos(4.*v1.x) +cos(4.*v1.y) +cos(4.*v1.z))));
}

// Capsule:  a,b = end points, r = cylinder radius
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;
}

// Triangle prism:
float sdTriPrism( vec3 p, float radius, float height )
{
  vec3 q = abs(p);
  #ifdef flag
    return max(q.z-height, max(q.x*0.866025 +p.y*0.5, -p.y) - radius*0.5);
  #else
    float d1 = q.z-height;
    float d2 = max(q.x*0.866025+p.y*0.5, -p.y) - radius*0.5;
    return length(max(vec2(d1, d2), 0.0)) + min(max(d1, d2), 0.0);
  #endif
}

// hexagonal prism:r
float sdHexPrism( vec3 p, float radius, float height)
{
  vec3 q = abs(p);
  #ifdef flag
    return max(q.z-height, max((q.x*0.866025 +q.y*0.5), q.y) - radius);
  #else
    float d1 = q.z-heighty;
    float d2 = max((q.x*0.866025 +q.y*0.5), q.y)-radius;
    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 sdCylinder( vec3 p, vec3 h )
{
  return length(p.xz - h.xy) - h.z;
}
// h.xy = base rectangle size,  h.z = height
float sdCylinder6( vec3 p, vec3 h )
{
  return max( length6(p.xz) - h.x, abs(p.y) - h.z );
}

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
}

//----------------------------------------------------------------------
// distance operations
//----------------------------------------------------------------------

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

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

//----------------------------------------------------------------------
// domain operations
//----------------------------------------------------------------------

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

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

//----------------------------------------------------------------------
// sphere cutted out from a rounded box
//----------------------------------------------------------------------
float sdBoxMinusSphere( in vec3 pos, in float radius )
{
  return opS( udRoundBox( pos, vec3(0.15), 0.05)
            , sdSphere(   pos, radius - 0.012 + 0.02*sinTime));
}
//----------------------------------------------------------------------
// rack-wheel with holes
//----------------------------------------------------------------------
float sdRackWheel( in vec3 pos)
{
  return 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 + 0.1*aTime,
                                     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)
                       )
            );
}
//----------------------------------------------------------------------
float sdBallyBall( in vec3 pos)
{
  return 0.7 * sdSphere(pos, 0.2 )
         + 0.03*sin(50.0*pos.x)*sin(50.0*pos.y+8.0*aTime)*sin(50.0*pos.z);
}
//----------------------------------------------------------------------
float sdTwistedTorus( in vec3 pos, float angle)
{
  return 0.5*sdTorus( opTwist(pos,angle), vec2(0.20, 0.05));
}
//----------------------------------------------------------------------
// animated fractal Anti sphere sponge
float AntiSphereSponge(vec3 w)
{
  const int ITER = 10;
  float sphereHoles = 8.0 + 5.*sin(0.4123*aTime);
  float sphereScale = 1.5 + 0.2*sin(0.345*aTime);
  float scale = 1.5;
  float k = scale*.5;
  float d = 10000.0;
  float r, d1;
  float sph=length(w * vec3(0.8, 0.8, 0.5)) - 0.6 ;
 
  if ( sph < .1)
  for (int i = 0; i < ITER; i++)
  {
    vec3 zz = mod(w * k, sphereHoles) - vec3(0.5 * sphereHoles);
    r = length(zz);
    d1 = -(sphereScale - r) / k;
    k *= scale;
    d = min(d, d1);
  }
    else d = 0.;
  d = max(d,sph);
  return d;
}
//----------------------------------------------------------------------
vec2 map( in vec3 pos )
{
  vec3 r1, r2;
  float sphy = 0.35 + 0.1 * sinTime;
  vec3 sp = pos - vec3( 1.0, sphy, 0.0);
  vec2 res = vec2( sdPlane( pos), 1.0 );
  float color = 46.9 + 40.0 * cos(0.2 * iGlobalTime);
  res = opU(res,vec2(AntiSphereSponge(pos - vec3(0.0, 0.7, 0.0)), color));
  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<14; 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 )
{
  aTime = ANIMATE ? iGlobalTime : 0.0;
  sinTime = sin(aTime);
  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.005*t*t ) );
  }
  return vec3( clamp(col, 0.0, 1.0) );
}


void main()
{
    vec2 p = 1.2*(gl_FragCoord.xy / iResolution.xy) - 0.6;
  p.x *= 0.5 * iResolution.x / iResolution.y;

  // camera 
  float angle = ROTATE ? 0.02*iGlobalTime : 0.0;
  float rx = 0.5 + 3.0*cos(angle + 6.0*iMouse.x);
  float rz = 0.5 + 3.0*sin(angle + 6.0*iMouse.x);
  vec3 ro = vec3( rx, 1.0 + 1.0*iMouse.y, rz );
  vec3 ta = vec3( 0.0, 0.5, 0.0 );

  // camera tx
  vec3 cw = normalize( ta-ro );
  vec3 cp = vec3( 0.0, 1.0, 0.0 );
  vec3 cu = normalize( cross(cw, cp) );
  vec3 cv = normalize( cross(cu, cw) );
  vec3 rd = normalize( p.x*cu + p.y*cv + 3.0*cw );

  // pixel color
  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/Anti menger sphere sponge", 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: 4222
    • View Profile
Hyper dodecahedron converted
« Reply #7 on: May 31, 2015, 03:02:26 PM »
Hyper dodecahedron converted to GLSL Hacker format.

Looks best in a square window.

Copy the code and save as Hyper_dodecahedron_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Hyper dodecahedron"
          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/4lS3DK

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;



//

//---------------------------------------------------------
// Shader:   HyperDodecahedron.glsl 
// original: 'Famous solid' by nimitz (@stormoid)
//           https://www.shadertoy.com/view/ltlGWM
// modified: use mouse to select different shape types now
// tags:     hyperbolic, dodecahedron, kaleidoscope, raymarcher
//---------------------------------------------------------
/*
    Quick laydown of what's going on:
        -knighty's folding technique to get dodecahedron distance (see: https://www.shadertoy.com/view/XlX3zB)
        -Linear extrapolation of sphere to "hyberbolize" the dodecahedron
        -Fold symmetries are used to displace, shade and color
        -Cheap analytic curvature for shading (see: https://www.shadertoy.com/view/Xts3WM)
        -Wave noise for bump mapping (generalized triangle noise: https://www.shadertoy.com/view/XtX3DH)
        -eiffie's auto-overstep raymarching method: https://www.shadertoy.com/view/ldSSDV
        -Lighting mostly from iq
*/

#define ROTATE true
#define ITR 120
#define FAR 7.
#define time iGlobalTime
#define TYPES 5.

int solidType = 2;   // use 0 to 4 for starting shape.

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

vec3 fold(in vec3 p)
{
    const vec3 nc = vec3(-0.5,-0.809017,0.309017);
    for(int i=0;i<5;i++)
    {
        p.xy = abs(p.xy);
        float t = 2.*min(0.,dot(p,nc));
        p -= t*nc;
    }
    return p;
}

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

float tri(in float x){return abs(fract(x)-0.5)*2.;}

float map(in vec3 p)
{
    vec3 fp = fold(p) - vec3(0.,0.,1.275);
    float d = mix(dot(fp,vec3(.618,0,1.)), length(p)-1.15,-3.6);
   
    if (solidType == 1)
    {
      d += tri(fp.x*8.+fp.z*3.)*0.05+tri(fp.x*fp.y*40.+time*0.2)*0.07-0.17;
      d += tri(fp.y*5.)*0.04;
      d *= 0.9;
    }
    else if (solidType == 2)
    {
      d *= 0.7;
      d += sin(time+fp.z*5.+sin(fp.x*20.*fp.y*8.)+1.1)*0.05-0.08;
      d += sin(fp.x*20.*sin(fp.z*8.+time*0.2))*0.05;
      d += sin(fp.x*20.*sin(fp.z*8.-time*0.3)*sin(fp.y*10.))*0.05;
    }
    else if (solidType == 3)
    {
      d = smax(d+.5, -(d+sin(fp.y*20.+time+fp.z*10.)+1.5)*0.3)*.55;
      d += sin(max(fp.x*1.3,max(fp.z*.5,fp.y*1.))*35.+time)*0.03;
    }
    else if (solidType == 4)
      d = smax(d+.5, -(d+sin(fp.z*10.+sin(fp.x*20.*fp.y*9.)+1.1)*0.3-0.3))*.5;
    else
    {
      d = smax(d+.2, -(d+cos(fp.z*13.+cos(fp.x*18.*fp.y*2.)+2.1)*0.5-0.5))*.8;
      d += tri(fp.x*3.+fp.z*6.)*0.1+tri(fp.x*fp.y*4.+time*0.2)*0.1-0.17;
      d *= 0.5;
    }
   
    return d*0.25;
}

float march(in vec3 ro, in vec3 rd)
{
    float t=0.,stp=0.0,os=0.0,pd=10.0, d =0.;
    for(int i=0;i<ITR;i++)
    {
        t+=stp;
        d=map(ro+rd*t);
        if (t>FAR || abs(d) <0.0005)break;
        if(d>=os)
        {       
            os=.9*d*d/pd;
            stp=d+os;
            pd=d;
        }
        else
        {
            stp=-os;
            pd=1.;
            os=.001;
        }
    }
    return t;
}

vec3 normal(in vec3 p)

    vec2 e = vec2(-1., 1.)*0.0001;
    return normalize(e.yxx*map(p + e.yxx) + e.xxy*map(p + e.xxy) +
                     e.xyx*map(p + e.xyx) + e.yyy*map(p + e.yyy) );   
}

//Cheap analytic curvature: https://www.shadertoy.com/view/Xts3WM
float curv(in vec3 p)
{
    vec2 e = vec2(-1., 1.)*0.03;
   
    float t1 = map(p + e.yxx), t2 = map(p + e.xxy);
    float t3 = map(p + e.xyx), t4 = map(p + e.yyy);
   
    return .15/(e.x*e.x) *(t1 + t2 + t3 + t4 - 4. * map(p));
}

float wav(in float x){return sin(x*6.283)*0.25+0.25;}
vec2 wav2(in vec2 p){return vec2(wav(p.x+wav(p.y*1.5)),wav(p.y+wav(p.x*1.5)));}

float wavenoise(in vec2 p)
{
    float z=2.;
    float z2=1.;
    float rz = 0.;
    vec2 bp = p;
    rz+= (wav(-time*0.5+p.x*(sin(-time)*0.3+.9)+wav(p.y-time*0.2)))*.7/z;
    for (float i=0.; i<=3.; i++ )
    {
        vec2 dg = wav2(bp*2.)*.8;
        dg *= mm2(time*.2);
        p += dg/z2;

        bp *= 2.4;
        z2 *= 1.05;
        z *= 2.4;
        p *= 1.4;
       
        rz+= (wav(p.x+wav(p.y)))/z;
    }
    return rz;
}

vec3 tex(in vec3 p)
{   
    float rz;
    vec3 col;
    if (solidType == 1)
    {
        rz= p.y*15.+p.x*30.+p.z*5.;
        col = (sin(vec3(.7,2.,.1-rz*0.2)+rz*.1+0.45))*0.5+0.5;
    }
    else if (solidType == 2)
    {
        rz= (sin(p.x*0.+p.z*20.)-p.y*20.);
        col = (sin(vec3(2.1,.7,.1)+rz*.09+4.15))*0.5+0.5;
    }
    else if (solidType == 3)   
    {
        rz= sin(p.z*3.+p.x*6.)*0.5+0.5;
        col = mix(vec3(.7,0.1,0.),vec3(1,.5,0.4),rz)*0.5+0.05;
    }
    else if (solidType == 4)
    {
        rz= p.z*13.+p.x*30.;
        col = (sin(vec3(2.2,.7,.1)+rz*.1+4.2))*1.3+1.3;
    }
    else
    {
        rz= sin(p.z*13.) + p.x*23.;
        col = (sin(vec3(0.2,.9,.5)+rz*0.4+2.2))*1.1+1.1;
    }
    return col;
}

//Bump mapping
float bumptex(in vec3 p)
{
    if      (solidType == 1) return wavenoise(mix(p.zy,p.yx,1.)*0.55);
    else if (solidType == 2) return wavenoise(mix(p.yz,p.xy,.5)*0.55);
    else if (solidType == 3) return wavenoise(mix(p.zy,p.xy,.5)*0.44);
    else if (solidType == 4) return wavenoise(mix(p.zy,p.xy,.1)*0.55);
    else                     return wavenoise(mix(p.zy,p.xy,.2)*0.50);
}

vec3 bump(in vec3 p, in vec3 n)
{
    vec2 e = vec2(.01,0);
    float n0 = bumptex(p);
    vec3 d = vec3(bumptex(p+e.xyy)-n0, bumptex(p+e.yxy)-n0, bumptex(p+e.yyx)-n0)/e.x;
    n = normalize(n-d*.3);
    return n;
}

float shadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
{
    float res = 1.0;
    float t = mint;
    for( int i=0; i<15; i++ )
    {
        float h = map(ro + rd*t);
        res = min( res, 4.*h/t );
        t += clamp( h, 0.01, .1 );
        if(h<0.001 || t>tmax) break;
    }
    return clamp( res, 0.0, 1.0 );
}

float ao( 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 );
        occ += -(dd-hr)*sca;
        sca *= .95;
    }
    return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );   
}

vec3 rotx(vec3 p, float a)
{
    float s = sin(a), c = cos(a);
    return vec3(p.x, c*p.y - s*p.z, s*p.y + c*p.z);
}
vec3 roty(vec3 p, float a)
{
    float s = sin(a), c = cos(a);
    return vec3(c*p.x + s*p.z, p.y, -s*p.x + c*p.z);
}

void main()
{   
    vec2 q = gl_FragCoord.xy / iResolution.xy;
    vec2 p = q-0.5;
    float ratio = iResolution.x / iResolution.y;
    p.x *= ratio;
    vec2 mo = iMouse.xy / iResolution.xy*1.5 -.75;
    mo.x *= ratio;
    if (ROTATE)
        mo += vec2(time*0.03, time*0.004);
    solidType = int( iMouse.x / iResolution.x * TYPES);
   
    vec3 ro = vec3(.0,0.0,-6.5);
    vec3 rd = vec3(p,1.2);
    ro = rotx(ro, -mo.y*3.0);ro = roty(ro, mo.x*3.0);
    rd = rotx(rd, -mo.y*3.0);rd = roty(rd ,mo.x*3.0);
   
    float rz = march(ro,rd);
    vec3 col = vec3(0.9);
   
    if ( rz < FAR )
    {
        //setup
        vec3 pos = ro+rz*rd;
        float crv= curv(pos);
        vec3 nor = normal(pos);
           vec3 fpos = fold(pos);
        vec3 lgt = normalize(vec3(.0, 1., 0.9));
        float shd = shadow( pos, lgt, 0.02, 3.0 );
        nor = bump(fpos, nor);
       
        //components
        float dif = max(dot(nor,lgt),0.0)*shd;
        float bac = max(0.2 + 0.8*dot(nor,vec3(-lgt.x,lgt.y,-lgt.z)),0.0);
        float fre = clamp(pow(1.0+dot(nor,rd),3.),0.,10.)*shd;
        vec3 haf = normalize(lgt - rd);
        float spe = pow(clamp(dot(nor,haf),0.0,1.0),50.0)*shd;
        float occ= crv*0.25+0.75;
       
        //compose
        col  = 0.2*occ + dif*vec3(1.0,0.8,0.6)
            + 0.4*bac*vec3(1.0)*occ;
        col *= 0.5*pow(tex(fpos),vec3(.5));
        col += .4*fre*vec3(1.0) + .35*spe*vec3(1.0);
        col *= ao(pos,nor);
        col = pow(col,vec3(.75))*1.3;
    }
   
    //vignetting from iq
    col *= pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.12 )*0.8+0.4;
   
    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/Hyper dodecahedron", 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: 4222
    • View Profile
Physically based rendering test converted
« Reply #8 on: May 31, 2015, 03:09:41 PM »
Physically based rendering test converted to GLSL Hacker format

Copy the code and save as pbr_test_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Physically based rendering test"
          width="800" height="600"
          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/MlB3DV

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;



//

/*
* References :
*
* http://renderwonk.com/publications/s2010-shading-course/hoffman/s2010_physically_based_shading_hoffman_b_notes.pdf
*
* http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
*
* http://graphicrants.blogspot.fr/2013/08/specular-brdf-reference.html
*
* http://www.filmicworlds.com/2014/04/21/optimizing-ggx-shaders-with-dotlh/
*
* http://blog.selfshadow.com/publications/s2013-shading-course/#course_content
*
*    Ray marching code from iq
*/


#define NB_LIGHTS 3

// Metals values in linear space
#define GOLD vec3(1.0, 0.71, 0.29)
#define COPPER vec3(0.95, 0.64, 0.54)
#define IRON vec3(0.56, 0.57, 0.58)
#define ALUMINIUM vec3(0.91, 0.92, 0.92)
#define SILVER vec3(0.95, 0.93, 0.88)

float time = 0.25*iGlobalTime;

float fPlane( vec3 p, vec4 n )
{
  // n must be normalized
  return dot(p,n.xyz) + n.w;
}

float fSphere( vec3 p, float s )
{
    return length(p)-s;
}
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;
}
// ---- Scene definition
vec2 fScene(vec3 p) {
   
    vec3 pSphere = p/*opRep(p, vec3( 2.0, 0.0, 2.0))*/;
   
     vec2 sphere0 = vec2(fSphere(p, 1.0), 0.5);     
    vec2 sphere1 = vec2(fSphere(p+vec3(2.1, 0.0, 2.0), 1.0), 2.5);
    vec2 sphere2 = vec2(fSphere(p+vec3(-2.1, 0.0, 2.0), 1.0), 3.5);
    vec2 sphere3 = vec2(fSphere(p+vec3(2.1, 0.0, -2.0), 1.0), 4.5);
    vec2 sphere4 = vec2(fSphere(p+vec3(-2.1, 0.0, -2.0), 1.0), 5.5);
   


    vec2 plane = vec2(fPlane(p, vec4(0, 1, 0, 1.0)), 1.5);
   
    return opU(opU(opU(opU(opU(plane, sphere0), sphere1), sphere2), sphere3), sphere4);
}
// -----

vec2 castRay( in vec3 ro, in vec3 rd )
{
    float tmin = 1.0;
    float tmax = 100.0;
   
   
    float precis = 0.00001;
    float t = tmin;
    float m = -1.0;
    for( int i=0; i<50; i++ )
    {
        vec2 res = fScene( 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 = fScene( 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(
        fScene(pos+eps.xyy).x - fScene(pos-eps.xyy).x,
        fScene(pos+eps.yxy).x - fScene(pos-eps.yxy).x,
        fScene(pos+eps.yyx).x - fScene(pos-eps.yyx).x );
    return normalize(nor);
}


struct Light {
     vec3 pos;
    vec3 color;
};
Light lights[NB_LIGHTS];


float G1V ( float dotNV, float k ) {
    return 1.0 / (dotNV*(1.0 - k) + k);
}

vec3 computePBRLighting ( in Light light, in vec3 position, in vec3 N, in vec3 V, in vec3 albedo, in float roughness, in vec3 F0 ) {

    float alpha = roughness*roughness;
    vec3 L = normalize(light.pos.xyz - position);
    vec3 H = normalize (V + L);

    float dotNL = clamp (dot (N, L), 0.0, 1.0);
    float dotNV = clamp (dot (N, V), 0.0, 1.0);
    float dotNH = clamp (dot (N, H), 0.0, 1.0);
    float dotLH = clamp (dot (L, H), 0.0, 1.0);

    float D, vis;
    vec3 F;

    // NDF : GGX
    float alphaSqr = alpha*alpha;
    float pi = 3.1415926535;
    float denom = dotNH * dotNH *(alphaSqr - 1.0) + 1.0;
    D = alphaSqr / (pi * denom * denom);

    // Fresnel (Schlick)
    float dotLH5 = pow (1.0 - dotLH, 5.0);
    F = F0 + (1.0 - F0)*(dotLH5);

    // Visibility term (G) : Smith with Schlick's approximation
    float k = alpha / 2.0;
    vis = G1V (dotNL, k) * G1V (dotNV, k);

    vec3 specular = /*dotNL **/ D * F * vis;

    vec3 ambient = vec3(.01);

    float invPi = 0.31830988618;
    vec3 diffuse = (albedo /** invPi*/);


    return ambient + (diffuse + specular) * light.color.xyz * dotNL ;
}

vec3 addPBR( in vec3 position, in vec3 N, in vec3 V, in vec3 baseColor, in float metalMask, in float smoothness, in float reflectance) {
    vec3 color = vec3(0.0);

    float roughness = 1.0 - smoothness*smoothness;
    vec3 F0 = 0.16*reflectance*reflectance * (1.0-metalMask) + baseColor*metalMask;
    vec3 albedo = baseColor;
   
    float s = 0.0;
   
   
    for ( int i = 0; i < NB_LIGHTS; ++i ) {
        vec3 col = computePBRLighting ( lights[i], position, N, V, albedo, roughness, F0);
        color += col;   
        s += softshadow( position, normalize(lights[i].pos.xyz - position), 0.02, 2.5 );
    }

    return color*s;
}

vec3 render( in vec3 ro, in vec3 rd )
{
    vec3 col = vec3(0.8, 0.9, 1.0)*8.0; // Sky color
   
    vec2 res = castRay( ro, rd );
    float t = res.x;
    float m = res.y;
    vec3 p = ro + t*rd;
   
    if(m>-0.5) { // Intersection found
        if( m < 1.0 ) {
           // float f = mod( floor( 5.0*p.z ) + floor( 5.0*p.x ), 2.0 );
            vec3 sur = vec3(1.0,1.0,1.0)*smoothstep(-1.0,-0.6,sin(16.0*p.x));
            col = addPBR( p, calcNormal( p ), -rd, GOLD*sur, sur.x, 0.3+0.6*sur.x, 0.5 );
        }
        else if( m < 2.0 ) {
            float f = mod( floor( 5.0*p.z ) + floor( 5.0*p.x ), 2.0 );
            col = addPBR(p, calcNormal( p ), -rd, vec3(0.5), 0.0, 0.3+0.6*f, 0.5 );
        }
        else if( m < 3.0 ) {
            vec3 sur = vec3(1.0,1.0,1.0)*smoothstep(-1.0,-0.4,sin(18.0*p.x));
            col = addPBR( p, calcNormal( p ), -rd, COPPER*sur, sur.x, 0.3+0.6*sur.x, 0.5 );
        }
        else if( m < 4.0 ) {
            vec3 sur = vec3(1.0,1.0,1.0)*smoothstep(-1.0,-0.0995,sin(106.0*p.x))*smoothstep(-1.0,-0.9,sin(47.0*p.z));
            col = addPBR( p, calcNormal( p ), -rd, vec3(0.2), 1.0-sur.x, 0.9*sur.x, 0.5 );
        }
        else if( m < 5.0 ) {
            vec3 sur = vec3(1.0)*smoothstep(-1.0,-0.765,sin(24.0*p.x))*smoothstep(-1.0,-0.4,sin(70.9*p.z));
            col = addPBR( p, calcNormal( p ), -rd, GOLD*(1.0-sur), sur.x, 0.3+0.6*sur.x, 0.5 );
        }
        else if( m < 6.0 ) {
            vec3 sur = vec3(1.0,1.0,1.0)*smoothstep(-1.0,-0.4,sin(18.0*p.x));
            col = addPBR( p, calcNormal( p ), -rd, ALUMINIUM*sur, sur.x, 0.3+0.6*sur.x, 0.5 );
        }
    }
   
    return col;
}

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 );
}

vec4 hejlToneMapping (in vec4 color) {
     vec4 x = max(vec4(0.0), color-vec4(0.004));
    return (x * ((6.2*x)+vec4(0.5))) / max(x * ((6.2*x)+vec4(1.7))+vec4(0.06), vec4(1e-8));
}

void main()
{
   
   
    lights[0] = Light(vec3(0.0, 5.0, .0), vec3(1.0));   
    lights[1] = Light(vec3(12.0*sin(iGlobalTime), 8.0, 12.0*cos(iGlobalTime)), vec3(1.0));       
    lights[2] = Light(vec3(-12.0*cos(-iGlobalTime), 8.0, 12.0*sin(-iGlobalTime)), vec3(.05));   

   

    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;
         
   

    // camera   
    vec3 ro = vec3( 7.0*sin(time), 3.6 , -7.0*cos(time) );
    vec3 ta = vec3( 0.0 );
   
    // 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 );

    #if 0
        col = pow( col, vec3(0.4545) );
        gl_FragColor=vec4( col, 1.0 );
    #else
        float exposure = 0.032 + 0.023*sin(time-3.14);
           gl_FragColor = hejlToneMapping(vec4(col, 1.0) * exposure) ;
    #endif
}

//

]]></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/Physically based rendering test", 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: 4222
    • View Profile
Heartwing Angel converted
« Reply #9 on: May 31, 2015, 03:22:33 PM »
Heartwing Angel converted to GLSL Hacker format

Copy the code and save as Heartwing_Angel_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Heartwing Angel"
          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/4l23zc

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;



//

// @christinacoffin
// 2015-05-07: 1st version, need to optim and do some proper AA

vec3 JuliaFractal(vec2 c, vec2 c2, float animparam, float anim2 ) {   
    vec2 z = c;
   
    float ci = 0.0;
    float mean = 0.0;
   
    for(int i = 0;i < 64; i++)
    {
        vec2 a = vec2(z.x,abs(z.y));
       
        float b = atan(a.y*(0.99+animparam*9.0), a.x+.110765432+animparam);
       
        if(b > 0.0) b -= 6.303431307+(animparam*3.1513);
       
        z = vec2(log(length(a*(0.98899-(animparam*2.70*anim2)))),b) + c2;

        if (i>0) mean+=length(z/a*b);

        mean+=a.x-(b*77.0/length(a*b));

        mean = clamp(mean, 111.0, 99999.0);
    }
   
    mean/=131.21;
    ci =  1.0 - fract(log2(.5*log2(mean/(0.57891895-abs(animparam*141.0)))));

    return vec3( .5+.5*cos(6.*ci+0.0),.5+.75*cos(6.*ci + 0.14),.5+.5*cos(6.*ci +0.7) );
}


void main()
{
    float animWings = 0.004 * cos(iGlobalTime*0.5);
    float animFlap = 0.011 * sin(iGlobalTime*1.0);   
    float timeVal = 56.48-20.1601;
    vec2 uv = gl_FragCoord.xy - iResolution.xy*.5;
    uv /= iResolution.x*1.5113*abs(sin(timeVal));
    uv.y -= animWings*5.0;
    vec2 tuv = uv*125.0;
    float rot=3.141592654*0.5;
 
    uv.x = tuv.x*cos(rot)-tuv.y*sin(rot);
    uv.y =1.05* tuv.x*sin(rot)+tuv.y*cos(rot);
    float juliax = tan(timeVal) * 0.011 + 0.02/(gl_FragCoord.y*0.19531*(1.0-animFlap));
    float juliay = cos(timeVal * 0.213) * (0.022+animFlap) + 5.66752-(juliax*1.5101);//+(gl_FragCoord.y*0.0001);// or 5.7
   
 
    float tapU = (1.0/ float(iResolution.x))*25.5;//*cos(animFlap);
    float tapV = (1.0/ float(iResolution.y))*25.5;//*cos(animFlap);
   
 
    gl_FragColor = vec4( JuliaFractal(uv+vec2(0.0,0.0), vec2(juliax, juliay), animWings, animFlap ) ,1.0);
   
    gl_FragColor += vec4( JuliaFractal(uv+vec2(tapU,tapV), vec2(juliax, juliay), animWings, animFlap ) ,1.0);
//    gl_FragColor += vec4( JuliaFractal(uv+vec2(tapU,-tapV), vec2(juliax, juliay), animWings, animFlap ) ,1.0);
//    gl_FragColor += vec4( JuliaFractal(uv+vec2(-tapU,tapV), vec2(juliax, juliay), animWings, animFlap ) ,1.0);
    gl_FragColor += vec4( JuliaFractal(uv+vec2(-tapU,-tapV), vec2(juliax, juliay), animWings, animFlap ) ,1.0); 
    gl_FragColor *= 0.3333;
   
    gl_FragColor.xyz = gl_FragColor.zyx;
    gl_FragColor.xyz = vec3(1)-gl_FragColor.xyz;

}

//

]]></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/Heartwing Angel", 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: 4222
    • View Profile
Manta Ray converted
« Reply #10 on: May 31, 2015, 05:41:41 PM »
Manta Ray converted to GLSL Hacker format

Copy the code and save as Manta_Ray_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Manta ray"
          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/4ls3zM

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 iChannel3;



//

// version 1.5 = trying to make the water surface and the sea floor more interesting
// version 1.3 = changed wing primitives and adding front flaps
// version 1.2 = working on improving the shape
// version 1.1 = multiple mantas. Added scattering attributes
// Version 1.0 = creation. Added a background. Single manta swim loop


const vec3 sun = vec3(-0.6, 0.4,-0.3);
float time = iGlobalTime+32.2;
vec3 lightDir = normalize(vec3(.1,.7, .2));
#define csb(f, con, sat, bri) mix(vec3(.5), mix(vec3(dot(vec3(.2125, .7154, .0721), f*bri)), f*bri, sat), con)
#define MATERIAL_SKIN 1.0
#define MATERIAL_NONE 0.0
#define SPACING 4.
#define ISPACING 0.25
#define BBSIZE 2.0
#define MAXDIST 25.

//--------------------------------------------------------------------------------------
// Utilities....
float hash( float n )
{
    return fract(sin(n)*43758.5453123);
}

vec3 Rotate_Y(vec3 v, float angle)
{
    vec3 vo = v; float cosa = cos(angle); float sina = sin(angle);
    v.x = cosa*vo.x - sina*vo.z;
    v.z = sina*vo.x + cosa*vo.z;
    return v;
}

// stolen from IQ.
float softMin(float a, float b)
{
    float k = 0.5;
    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 difference(float a, float b)
{
    return max(a, -b);
}

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

float wings(vec3 p)
{   
    p*=vec3(0.7,3.5,1.2);
    p.z -= p.x*p.x*0.8;
    p.y -= 0.1;
    float d = p.x+p.y*p.y+pow(abs(p.z),1.4);
   
    return d-1.0;
}

float mantabody(vec3 p)
{
    vec3 origP = p;
    // thinner wings
    p*=vec3(2.4,2.6,1.3);
    float d = sphere(p, 1.0);
    d = softMin(d, wings(origP));
   
    // body hole, scale with body
    vec3 holeP = p;
    holeP *= vec3(1.2,1.4,0.;
    holeP += vec3(-0.2,0.13,0.6);
    d = difference(d, length(holeP) - 0.9);
   
    vec3 flapsP = origP;
    flapsP += vec3(-0.3,0.3-origP.x*0.5,0.95-origP.x*1.);
    flapsP *= vec3(6.0,12.0,6.0);
    d = softMin(d, sphere(flapsP,1.0));
   
    return d;
}


//--------------------------------------------------------------------------------------
vec2 Scene(vec3 p)
{
    p.z+= time;
    float mat = MATERIAL_SKIN;
   
   
    // Repeat
    vec3 loopP = p;
    loopP.x = mod(loopP.x+BBSIZE, SPACING)-BBSIZE;
    loopP.z = mod(loopP.z+BBSIZE, SPACING)-BBSIZE;

    //scramble
    float rowId = floor((p.x+2.)*ISPACING);
    float columnId = floor((p.z+2.)*ISPACING);
    //loopP.y += hash(7.*rowId + 11.* columnId)*4.-1.5; // starting height
    float size = 0.6 + 0.6*hash(7.*rowId + 11.* columnId);
    //float size = 1.0;
    float timeloop = time * 2.5 / (size-0.25) + // loop speed
        hash(rowId+3.*columnId) * 10.; // random offset
    loopP.y+= -sin(timeloop-0.5)*.25 * size;
    loopP.y+= sin(time*0.5 + hash(37.*rowId+11.*columnId)*17.) * 2.5;
   
    // Mirror
    loopP.x = abs(loopP.x);
    loopP/=size;
   
    float d = 3.;
   
   
    //////////////////
    // manta body
    //////////////////
    vec3 mantap = loopP;
   
    // tail
    vec3 tailp = loopP;
   
    // animate wings
    float animation = sin(timeloop-3. - 1.3*loopP.z);
    mantap.y += animation * (0.3*loopP.x*loopP.x + 0.10);
    tailp.y += animation* (0.1 + smoothstep(1.,3.,tailp.z)) - 0.1;
   
    d = min(d, mantabody(mantap));
    //d = min(d, sphere(mantap,1.0));
   
   
    float taild = length(tailp.xy)*0.4 +
        smoothstep(0.,1.,-tailp.z) * 2.+
        smoothstep(3.,5.,tailp.z) * 10.;
   
    // soft min?
    d = softMin(d,taild);

    return vec2(d, mat);
}

//--------------------------------------------------------------------------------------
vec4 Trace(vec3 rayOrigin, vec3 rayDirection, out float hit)
{
    const float minStep = 0.005;
    hit = 0.0;
   
    vec2 ret = vec2(0.0, 0.0);
    vec3 pos = rayOrigin;
    float dist = 0.0;
    for(int i=0; i < 200; i++){
        if (hit == 0.0 && dist < MAXDIST && pos.y<10.0 && pos.y > -10.0 )
        {
            pos = rayOrigin + dist * rayDirection;
            ret = Scene(pos);
            // ret.x = distance
            // ret.y = material type
            if (ret.x < 0.01)
                hit = ret.y;

            // increment
            if (ret.y >= 2.0)
                dist += ret.x * 10.0;
            else
                dist += max(ret.x * 0.2, minStep);
        }
       
    }
    return vec4(pos, ret.y);
}

//--------------------------------------------------------------------------------------
vec3 GetNormal(vec3 p)
{
    vec3 eps = vec3(0.01,0.0,0.0);
    return normalize(vec3(Scene(p+eps.xyy).x-Scene(p-eps.xyy).x,
                          Scene(p+eps.yxy).x-Scene(p-eps.yxy).x,
                          Scene(p+eps.yyx).x-Scene(p-eps.yyx).x ));
}

//--------------------------------------------------------------------------------------
vec3 GetColour(vec4 p, vec3 n, vec3 org, vec3 dir)
{
    vec3 localP = vec3(p) + vec3(0.,0.,time);
    vec3 loopP = localP.xyz;
    loopP.x = mod(loopP.x+BBSIZE, SPACING)-BBSIZE;
    loopP.z = mod(loopP.z+BBSIZE, SPACING)-BBSIZE;
   
   
    vec3 colour = vec3(0.0);
    if (p.w < 1.5)
    {
        float v = clamp(-(n.y-.1)*6.2, 0.3, 1.0);
        v+=.35;
        colour = vec3(v*.8, v*.9, v*1.0);
    }
   
    vec2 coord = loopP.xz;
    vec3 colorUp = colour;
    float stainsUp = texture2D(iChannel1, coord).x;
    stainsUp *= stainsUp * 2.;
    stainsUp = 1.-stainsUp;
    stainsUp += smoothstep(2.,-2.,loopP.z);
    stainsUp = clamp(stainsUp, 0., 1.);
    colorUp *= stainsUp;
   
    vec3 colorDown = colour;
    float stainsDown = texture2D(iChannel1, coord*0.4).x;
    stainsDown += smoothstep(0.,-3.,loopP.z);
    stainsDown *= stainsDown;
    stainsDown = clamp(stainsDown, 0., 1.);
    colorDown *= vec3(stainsDown);
   
    colour = mix(colorUp, colorDown, smoothstep(-0.4,0.4,n.y));   
    {
        // Water caustics
        vec2 wat = p.xz*1.;
        wat +=  (texture2D(iChannel0, (wat*5.0+time*.04)*.1, 3.0).z -
             texture2D(iChannel0, wat*.3-time*.03, 2.0).y) * .4;
        float causticLight = texture2D(iChannel0, wat* .04, 0.0).x;
        causticLight = pow(max(0.0, causticLight-.2), 1.0) * 20. * smoothstep(-5.,3.,p.y);
        colour *= vec3(1.0) + vec3(causticLight*.5, causticLight, causticLight)*max(n.y, 0.0);
    }
   
    // shadow
    float diff = dot(n,lightDir);
    // light properties
    vec3 brightLight = vec3(0.7,0.7,0.;
    vec3 shade = vec3(0.12,0.15,0.22);
    colour *= mix(shade,brightLight,max(diff*0.5+0.5,0.0));
   
    return colour;
}


//--------------------------------------------------------------------------------------
void main()
{
    vec3 col;   
    vec2 uv = (gl_FragCoord.xy / iResolution.xy) - vec2(.5);
    uv.x*=iResolution.x/iResolution.y;
    vec3 dir = normalize(vec3(uv, -1.0));
   
    //vec3 pos = vec3(1.3, sin(time+4.3)*.18-.05, sin(-time*.15)*5.0-1.35);
    vec3 pos = (sin(time*0.14)*2.+4.5)*vec3(sin(time*.5), 0.0, cos(time*.5));
    pos.z -= time;
    pos.y += 0.7 * sin(time * 0.2);
    float rot = -time*0.5;
    dir = Rotate_Y(dir, rot);

    // Sun...
    float i = max(0.0, 1./(length(sun-dir)+1.0));
    col = vec3(pow(i, 1.9), pow(i, 1.0), pow(i, .) * 1.3;
   
    // Water depth colour...
    col = mix(col, vec3(0.0, .25, .45), ((1.0-uv.y)*.45) * 1.;
   
    float d;
    if (uv.y >= 0.0)
    {
        // Add water ripples...
        d = (3.0-pos.y) / -uv.y;
       
        vec2 wat = (dir * d).xz-pos.xz;
        d += 1.*sin(wat.x + time);
        wat = (dir * d).xz-pos.xz;
        wat = wat * 0.1 + 0.2* texture2D(iChannel0, wat, 0.0).xy;
       
        i = texture2D(iChannel3, wat, 0.0).x;
       
        col += vec3(i) * max(2./-d, 0.0);
    }
    else       
    {
        // Do floor stuff...
        d = (-3.0-pos.y) / uv.y;
        vec2 coord = pos.xz+(dir.xz * d);
        vec3 sand = texture2D(iChannel3, coord* .1).rgb * 1.5  +
                    texture2D(iChannel3, coord* .23).rgb;
        sand *= 0.5;
       
        float f = ((-uv.y-0.3 +sin(time*0.1)*0.2)*2.45) * .4;
        f = clamp(f, 0.0, 1.0);
       
        col = mix(col, sand, f);
    }

    float hit = 0.0;
    vec4 loc = Trace(pos, dir, hit);
    if (hit > 0.0)
    {
        vec3 norm = GetNormal(loc.xyz);
        vec3 foundColor = GetColour(loc, norm, pos, dir);
        vec3 backgroundColor = col;
   
        // total water reflection
        float facing = -dot(norm,dir);
        float upfacing = clamp(norm.y, 0.,1.);
        float fresnel = 1.0-facing;
        fresnel = clamp(pow(fresnel, 1.0), 0.0,1.0);
        foundColor = mix(foundColor, backgroundColor*2.0, 0.5 * (0.5 + upfacing*upfacing) * fresnel);
       
        // atmos
        float dis = length(pos-loc.xyz);
        float fogAmount = clamp(max((dis-.5),0.0)/MAXDIST, 0.0, 1.0);
       
        col = mix(foundColor, backgroundColor, fogAmount );
    }
   
    // Contrast, saturation and brightness...
    col = csb(col, 1.1, 1.05, 1.22);
   
    // Fade in...
    //col *= smoothstep( 0.0, 2.5, iGlobalTime );
    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/tex03.jpg")
tex3 = moon3d.image.load2d("./data/tex02.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, tex3, 3)

moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Manta Ray", 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: 4222
    • View Profile
Sakura revisited converted
« Reply #11 on: May 31, 2015, 05:44:48 PM »
Sakura revisited converted to GLSL Hacker format

Copy the code and save as Sakura_revisited_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Sakura revisited"
          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/Xt23Dz

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;



//

#define PI 3.1415926
#define V vec2(0.,1.)

#define t iGlobalTime
#define r iResolution.xy
#define c gl_FragCoord
#define cl(i) clamp(i,0.,1.)

#define MAT_NULL -1.
#define MAT_ANTHER 0.
#define MAT_FILAMENT 1.
#define MAT_PISTIL 2.
#define MAT_PETAL 3.
#define MAT_CALYX 4.
#define MAT_TREE 5.

float hash(vec2 _v)
{
    return fract(sin(dot(_v,vec2(89.44,19.36)))*22189.22);
}

float iHash(vec2 _v,vec2 _r)
{
    float h00 = hash(vec2(floor(_v*_r+V.xx)/_r));
    float h10 = hash(vec2(floor(_v*_r+V.yx)/_r));
    float h01 = hash(vec2(floor(_v*_r+V.xy)/_r));
    float h11 = hash(vec2(floor(_v*_r+V.yy)/_r));
    vec2 ip = vec2(smoothstep(V.xx,V.yy,mod(_v*_r,1.)));
    return (h00*(1.-ip.x)+h10*ip.x)*(1.-ip.y)+(h01*(1.-ip.x)+h11*ip.x)*ip.y;
}

float noise(vec2 _v)
{
    float sum = 0.;
    for(int i=1; i<9; i++)
    {
        sum += iHash(_v+vec2(i),vec2(2.*pow(2.,float(i))))/pow(2.,float(i));
    }
    return sum;
}

mat2 rotate( float _th )
{
    return mat2( cos(_th), -sin(_th), sin(_th), cos(_th) );
}

float pole( vec3 _p, float _r, float _l )
{
    vec2 d = abs( vec2( length( _p.xz ), _p.y ) ) - vec2( _r, _l );
    return min( max( d.x, d.y ), 0. ) + length( max( d, 0. ) );
}

float pole( vec3 _p, float _r )
{
    return length( _p.xz ) - _r;
}

float smin( float _a, float _b, float _k )
{
    float h = cl( 0.5+0.5*( _b-_a )/_k );
    return mix( _b, _a, h ) - _k*h*( 1.-h );
}

vec2 stamen( vec3 _p )
{
    vec2 dist = vec2( 1E2 );
    vec2 distC = dist;
    float phase = floor( atan( _p.z, _p.x )/PI/2.*13.-1. );
    vec3 offset = vec3( .04, -.0, .04 ) * ( 1. + sin( t )*.1 );
    offset.xz *= .6 + hash( vec2( phase*1.9873, 821.122 ) )*.6;
    vec3 p = _p - V.xyx*.01;
    p.xz = rotate( floor( phase )*PI*2./13. ) * p.xz;
    vec3 pa = p + ( sin(p.x*200.)*sin(p.y*200.)*sin(p.z*200.) ) * .003 - offset - V.xyx*.1;
    distC = vec2( length( pa ) - .005, MAT_ANTHER );
    if( distC.x < dist.x ){ dist = distC; }
    pa = p + vec3( sin(p.y*20.), 0., cos(p.y*20.) )*.003 - offset * ( .5+.5*sin( p.y/.1*PI/2. ) );
    distC = vec2( pole( pa-V.xyx*.025,  sin( pa.y/.1*PI )*.001, .075 ), MAT_FILAMENT );
    if( distC.x < dist.x ){ dist = distC; }
    return dist;
}

vec2 pistil( vec3 _p )
{
    vec3 p = _p;
    float pistil = pole( p - V.xyx*.01 + vec3( cos(p.y*50.), 0., sin(p.y*50.) )*.001, .004, .06 );
    pistil = smin( pistil, length( vec2( length( p.xz )-.007, p.y-.07 ) )-.001, .01 );
    return vec2( pistil, MAT_PISTIL );
}

vec2 petals( vec3 _p )
{
    float dist = 1E2;
    vec3 p = _p;
    p.y -= pow( length( p.xz ), .5)*.2-.055;
    p.xz = rotate( floor( atan( p.z, p.x )/PI/2.*5.-2. )*PI*2./5. ) * p.xz;
    p.xy = rotate( -.3 + sin( t )*.1 ) * p.xy;
    p.x += .14;
    p.x *= ( 1. - pow( abs( sin( atan( p.z, p.x ) ) ), .1 )*.3 )*1.4;
    p += ( sin(_p.x*20.)*sin(_p.y*20.)*sin(_p.z*20.) ) * .018;
    dist = min( dist, pole( p, .1, .001 ) );
    return vec2( dist, MAT_PETAL + cl( length( _p.xz ) ) );
}

vec2 calyx( vec3 _p )
{
    float dist = 1E2;
   
    vec3 p = _p;
    p.y -= pow( length( p.xz ), .2)*.2-.13;
    p.xz = rotate( floor( atan( p.z, p.x )/PI/2.*5.-2. )*PI*2./5. ) * p.xz;
    p.xy = rotate( -.3 ) * p.xy;

    vec3 ptemp = p;
    p.x += .04;
    p.x *= max( pow( abs( sin( atan( p.z, p.x ) ) ), .1 ), .6 );
    p += ( sin(_p.x*20.)*sin(_p.y*20.)*sin(_p.z*20.) ) * .018;
    dist = smin( dist, pole( p, .03, .001 ), .02 );

    p = ptemp + V.xyx*.15;
    p.x -= .02;
    p.x *= max( pow( abs( sin( atan( p.z, p.x ) ) ), .1 ), .6 );
    p += ( sin(_p.x*20.)*sin(_p.y*20.)*sin(_p.z*20.) ) * .018;
    dist = smin( dist, pole( p, .01, .001 ), .02 );
   
    dist = smin( dist, pole( _p + vec3( cos(_p.y*20.), 0., sin(_p.y*20.) )*.004 + V.xyx*.15, .01, .09 ), .02 );
   
    return vec2( dist, MAT_CALYX + cl( -_p.y-.05 ) );
}

vec2 blossom( vec3 _p, float _h )
{
    vec2 dist = vec2( 1E2 );
    if( length( _p ) < .28 )
    {
        dist = stamen( _p );
        vec2 distC = pistil( _p );
        if( distC.x < dist.x ){ dist = distC; }
        distC = petals( _p );
        if( distC.x < dist.x ){ dist = distC; }
        distC = calyx( _p );
        if( distC.x < dist.x ){ dist = distC; }
    }
    else
    {
        dist = vec2( length( _p )-.27, MAT_NULL );
    }
    return dist;
}

vec2 branch( vec3 _p )
{
    vec2 dist = vec2( 1E2 );
    vec3 p = _p;
    p.xy = rotate( -1. )*p.xy;
    vec3 pt = p + sin( p.x*10. )*sin( p.y*10. )*sin( p.z*10. ) * .03;
    dist = vec2( pole( pt, .1 ), MAT_TREE );
    p.zx = rotate( floor( p.y*2.+.5 )*.7 - .5 )*p.zx;
    float th = atan( p.z, p.x );
    if( th < 0. ){ p.zx = rotate( PI )*p.zx; }
    p.yz = rotate( PI/2. )*p.yz;
    p.y -= .3;
    p.z = mod( p.z+.25, .5 )-.25;
    vec2 distC = blossom( p, 12.4 );
    if( distC.x < dist.x ){ dist = distC; }
    return dist;
}

vec2 scene( vec3 _p )
{
    vec2 dist = branch( _p );
    return dist;
}

vec3 sceneNormal( vec3 _p )
{
    vec2 d = V*1E-4;
    return normalize( vec3(
        scene( _p + d.yxx ).x - scene( _p - d.yxx ).x,
        scene( _p + d.xyx ).x - scene( _p - d.xyx ).x,
        scene( _p + d.xxy ).x - scene( _p - d.xxy ).x
    ) );
}

void main()
{
    vec2 p = (c*2.-r)/r.x;
   
    vec3 camPos = vec3( .8, 0., 1. );
    vec3 camTar = vec3( sin(t*.72), sin(t*.83), sin(t*.37) )*.01 - vec3( sin( t*.21 )*.2, -.2, .0 );
    vec3 camDir = normalize( camTar - camPos );
    vec3 camAir = V.xyx;
    vec3 camSid = normalize( cross( camDir, camAir ) );
    vec3 camTop = normalize( cross( camSid, camDir ) );
   
    vec3 rayDir = normalize( camSid * p.x + camTop * p.y + camDir );
    vec3 rayPos = camPos;
    float rayLen = 0.;
    vec2 dist = V.xx;
    for( int i=0; i<64; i++ )
    {
        dist = scene( rayPos )*vec2( .6, 1. );
        rayLen += dist.x;
        rayPos = camPos + rayDir * rayLen;
        if( dist.x < 2E-3 || 1E2 < rayLen ){ break; }
    }
   
    vec3 col = V.xxx;
    if( dist.x < 4E-3 )
    {
        vec3 ligPos = vec3( 0., 1., 11. );
        vec3 nor = normalize( sceneNormal( rayPos ) );
        float dif = cl( dot( normalize( rayPos-ligPos ), -nor ) )*.3;
        float amb = .7;
        float speDot = cl( dot( normalize( normalize( rayPos-ligPos ) + normalize( rayPos-camPos ) ), -nor ) );
        float spe = cl( pow( speDot, 10. ) )*.1;
        vec3 matCol = V.xxx;
        if( floor( dist.y ) == MAT_NULL ){ amb = 1.; matCol = vec3( 1. ); }
        else if( floor( dist.y ) == MAT_ANTHER ){ matCol = vec3( .9, .9, .5 ); }
        else if( floor( dist.y ) == MAT_FILAMENT ){ matCol = vec3( .9, .9, .8 ); }
        else if( floor( dist.y ) == MAT_PISTIL ){ matCol = vec3( .8, .9, .6 ); }
        else if( floor( dist.y ) == MAT_PETAL ){ matCol = vec3( .9, .4, .8 ) + vec3( .1, .6, .2 ) * ( 1.-exp( -fract( dist.y )*16. ) ); }
        else if( floor( dist.y ) == MAT_CALYX ){ matCol = vec3( .7, .1, .3 ) + vec3( -.4, .4, -.4 ) * cl( fract( dist.y )*8. ); }
        else if( floor( dist.y ) == MAT_TREE ){
            nor += vec3(
                noise(vec2((rayPos.x+rayPos.z)*8.,rayPos.y*8.+27.1982))-.5,
                noise(vec2((rayPos.x+rayPos.z)*8.,rayPos.y*8.+28.1982))-.5,
                noise(vec2((rayPos.x+rayPos.z)*8.,rayPos.y*8.+29.1982))-.5
            )*2.;
            nor = normalize( nor );
            dif = cl( dot( normalize( rayPos-ligPos ), -nor ) )*.6;
            float speDot = cl( dot( normalize( normalize( rayPos-ligPos ) + normalize( rayPos-camPos ) ), -nor ) );
            float spe = cl( pow( speDot, 10. ) )*.1;
            matCol = vec3( .4, .3, .1 );
        }
           col = ( dif + amb ) * matCol + spe;
    }
    else
    {
        col = vec3( .5, .7, .9 );
        for( float i=12.; i<39.; i+=1. )
        {
            vec2 pos = ( vec2( hash(vec2(i,198.33)), hash(vec2(i,298.33)) ) - .5 )*r/r.x*2.;
            pos += vec2( sin( t*.2*hash(vec2(i,19.233)) ), sin( t*.2*hash(vec2(i,29.233)) ) )*.01;
            col += cl( 12.-length(p-pos)*50. ) * vec3( 1., .7, .9 )*.02;
        }
    }
   
    col -= length( p )*.4;
   
    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/Sakura revisited", 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: 4222
    • View Profile
Dawn of the tentacle converted
« Reply #12 on: June 01, 2015, 05:18:19 PM »
Dawn of the tentacle converted to GLSL Hacker format

Copy the code and save as dawn_of_the_tentacle_gl2.xml

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

<glsl_hacker>
   
  <window name="win3d01" title="MadShaders - Shadertoy/Dawn of the tentacle"
          width="1024" height="1024"
          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/Xtf3Rj

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;



//

/*
---------------------------------------------------------------------------------------

 "Dawn of the Tentacle" by Pablo Roman Andrioli (Kali)
 
 Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

---------------------------------------------------------------------------------------
*/


//smooth version by iq
#define smoothversion

//remove if you see a flat shaded tentacle
//(it happens to me in firefox)
#define underwater


// Raymarching
const float zoom=.95;
const float detail=.017;    // distance to stop tracing
const float maxdist=14.;    // scene depth
const int maxsteps=55;      // max ray steps

// Light
const vec3 lightdir=vec3(.6,-1.,0.);
const float diffuse=.7;
const float specular=.7;
const float specularexp=5.;

// Tentacle Shape
const float maxiter=28.;    // Max iterations
const float width=.9;
const float height=0.52;
const float scaling=0.92;       // Scaling factor at each iteration
const vec3 rotvector=vec3(1.,0.,0.); // Base vector for rotation
const float anglevary=.2;    // Angle variation at each iteration

// Animation
const float amplitude=0.55;   
const float speed=0.6;
const float dynscaling=0.9; // Scaling factor for the dynamic fractal animation


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

vec3 dir;
vec2 pix;
vec2 coord;


// Rotation function included in MathUtils.frag of Syntopia's Fragmentarium
mat3 rotationMat(vec3 v, float angle)
{
    float c = cos(angle);
    float s = sin(angle);
   
    return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
        (1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
        (1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
        );
}


// Water
float water(vec3 p){
    //p.z-=15.;
    float l=length(p.xz);
    p.x+=l*3.;
    p.x-=iGlobalTime*1.5;
    float h=(sin(p.x)+cos(p.z))*.05;
    p.xz*=vec2(3,2.5);
    p.xz+vec2(1.,2.5);
    h+=(cos(p.x)+sin(p.z))*.05;
    h+=texture2D(iChannel0,p.xz*.2).z*max(0.1,.2-l*l*.01); //texture displacement
    return p.y-h;
}


float smin( float a, float b )
{
float k = 0.1; float h = clamp( 0.5 + 0.5*(b-a)/k, 0.0, 1.0 );
return mix( b, a, h ) - k*h*(1.0-h);
}


// Scene DE
vec2 DE(vec3 pos){

    float angle=0.;
    vec3 p=pos;
    int n = 0;
    float sc=1.;
    float time=iGlobalTime*speed+100.;
    float amp=sin(time/2.)*amplitude;
    float dtent=1e10;
    float dd=1e10;
    float minit=maxiter;
    vec3 minp=pos;
    for (float n=0.; n < maxiter; n++) {
        float d1=length(p)-width*sc;
        float d2=length(p+vec3(width*sc*.75,0.,0.))-width*sc*.4;
        float d=min(d1,d2);
        if (n<1.) dtent=d;
        if (d<=dtent) { //test min distance and save iteration & vector
            dtent=d;
            minit=n;
            minp=p;
        }
        #ifdef smoothversion
        dd=smin(dd,d);
        #endif
       
        p*=rotationMat(normalize(rotvector+vec3(0.,sin(iGlobalTime)*.7,0.)),sin(time)*amp+angle);
        p.y-=height*sc; // go up
        sc*=scaling; // scale size
    //    amp*=dynscaling; // scale amplitude
        time/=dynscaling; // scale time
        angle+=radians(anglevary); // vary rotation
    }
        #ifdef smoothversion
        dtent=dd;
        #endif   
   
    dtent+=length(texture2D(iChannel1,vec2(minp.y*2.,atan(minp.x,minp.z)*.).xyz)*.025;
   
    float dwat=water(pos);
    float de=min(dwat,dtent);
    float col=0.;
    if (de!=dwat) col=minit+1.; // return coloring parameter
    return vec2(de,col);
}


// finite difference normal
vec3 normal(vec3 pos) {
    vec3 e = vec3(0.0,detail,0.0);
   
    return normalize(vec3(
            DE(pos+e.yxx).x-DE(pos-e.yxx).x,
            DE(pos+e.xyx).x-DE(pos-e.xyx).x,
            DE(pos+e.xxy).x-DE(pos-e.xxy).x
            )
        );   
}

// coloring
vec3 color(float obj, vec3 p) {

    if (obj<1.) { //water
        vec3 tex=texture2D(iChannel0,p.xz*.1).xyz;
        return (tex+.2)*vec3(1.7,1.2,1.2);
    }
    else if (obj>0.) {//tentacle
        float g=(maxiter-obj)/maxiter;
        return vec3(.9,g+.2,0.6);
    }
   
}

//lighting
vec3 light(vec3 p) {
vec3 ldir=normalize(lightdir);
vec3 n=normal(p);
float diff=max(0.0,dot(-n, ldir))*diffuse;
vec3 r = reflect(ldir,n);
float spec=max(0.,dot(dir,-r));
return vec3(1.,.8,.5)*(diff+pow(spec,specularexp)*specular);   
}

//raymarching
vec3 raymarch(vec3 from, vec3 dir) {
    vec3 p,p2;
    float totdist=0., totdist2=0.;
    vec3 col;
    vec2 d=vec2(0.);
    float d2=100000.;
    //march
    for (int i=1; i<maxsteps; i++) {
        p=from+totdist*dir;
        d=DE(p);
        if (d.x<detail || totdist>maxdist) break;
        #ifdef smoothversion
            totdist+=max(.1,d.x);
        #else
            totdist+=max(.01,d.x);
        #endif
    }

    //underwater
    if (d.x<detail && d.y<.5) {
        totdist2=totdist+detail*3.;
        for (int i=1; i<maxsteps; i++) {
            p2=from+totdist2*dir;
            d2=length(p2.xz)-width/scaling+p2.y*.2;
            if (d2<detail || totdist2>maxdist) break;
            totdist2+=d2;
        }
    }
   
   
    vec3 back=mix(vec3(1.3,.95,.7),vec3(.52,.5,.56) //background gradient
    *1.25,clamp((dir.y+.13)*2.5,0.,1.35));
    back+=vec3(1.,1.,.5)*(.04+dot(lightdir,-dir)*.4); //dawn
   
    if (d.x<detail) {
        col=color(d.y,p)*light(p-detail*dir*.5); //apply color+light
        col=mix(col,back,smoothstep(0.,2.2,totdist*.17)); //fog
    } else { //background-sky
        col=back+.85*vec3(1.,.85,.7)*texture2D(iChannel1,
        vec2(dir.y*4.+.5,atan(dir.z,dir.x)*1.*.15+vec2(iGlobalTime*.002)).b*(dir.y*.75+.15)
        *vec3(max(0.1,.5-coord.y*2.5))*1.4;
        col-=vec3(max(0.,coord.y*.3));
    }
    #ifdef underwater
    if (d2<detail) {
        vec3 col2=color(1.1,p2)*light(p+p2-detail*dir*.5);
        col=mix(col,col2,.3); //underwater
    }
    #endif
   
    col+=vec3(1.,.7,.5)*max(0.,9.-totdist)*.04; //camlight
    return col;
}

void main()
{
    //Camera
    pix=gl_FragCoord.xy / iResolution.xy;
    float viewangle=-135.+(iMouse.x/iResolution.x)*100.;
    mat3 rotview=rotationMat(vec3(0.,1.,0.),radians(viewangle));
    coord = pix-vec2(.5);
    coord.y*=iResolution.y/iResolution.x;
    vec3 from=vec3(0.,2.85,8.)*rotview*zoom;
    dir=normalize(vec3(coord*1.8,-1.))*rotview;
   
    //Draw scene
    vec3 col=vec3(0.);
    col=raymarch(from,dir);
   
    //adjust levels
    col*=pow(length(col),1.*.41;
    col+=vec3(0.02,-.01,0.05);
    col=mix(col,vec3(length(col))*.6,.3);
   
    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/tex07.jpg")
tex1 = moon3d.image.load2d("./data/tex03.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.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Dawn of the tentacle", 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: 4222
    • View Profile
Shmup shader converted
« Reply #13 on: June 03, 2015, 05:06:51 PM »
Shmup Shader  by Katsuomi Kobayashi (@KatsuomiK) [via] converted to GLSL Hacker format.

Shmup (Shoot them up) looks best vertikal.

Copy the code and save as shmup_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 - GLSLSandbox/Shmup Shader"
          width="400" height="800"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="glslsandbox_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[     


// http://glslsandbox.com/e#23161.0

// Shmup Shader
// by Katsuomi Kobayashi (@KatsuomiK)
// http://framesynthesis.com/

#ifdef GL_ES
precision mediump float;
#endif

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

#define SCREEN_W 0.7
#define SCROLL_SPEED 0.1

#define BLINK_INTERVAL 0.06
float blink = mod(time, BLINK_INTERVAL) / BLINK_INTERVAL;

vec2 p;

vec2 myfighter_pos;

float rand(vec2 co)
{
    return fract(sin(dot(co.xy ,vec2(12.9898, 78.233))) * 43758.5453);
}

float linear_map(float value, float s0, float s1, float d0, float d1)
{
    return d0 + (value - s0) * (d1 - d0) / (s1 - s0);
}

void set_myfighter_pos()
{
    myfighter_pos = mouse.xy - vec2(0.5, 0.5);

    float n = floor(time * 2.0);
    float t = fract(time * 2.0);

    float x0 = linear_map(rand(vec2(n, 0)), 0.0, 1.0, -0.3, 0.3);
    float x1 = linear_map(rand(vec2(n + 1.0, 0)), 0.0, 1.0, -0.3, 0.3);
    float y0 = linear_map(rand(vec2(0, n)), 0.0, 1.0, -0.4, -0.1);
    float y1 = linear_map(rand(vec2(0, n + 1.0)), 0.0, 1.0, -0.4, -0.1);

    float x = linear_map(t, 0.0, 1.0, x0, x1);
    float y = linear_map(t, 0.0, 1.0, y0, y1);

    myfighter_pos.x = x;
    myfighter_pos.y = y;
}

vec3 background()
{
    float interval = 0.1;
    float thick = 0.005;
    vec3 color = vec3(0.0, 0.2, 0.4);

    if (mod(p.y + time * SCROLL_SPEED, interval) < thick || mod(p.x, interval) < thick) {
        return color;
    }
    return vec3(0);
}

vec2 get_boss_pos(float offset)
{
    float x = sin(time - offset) * 0.2;

    return vec2(x, 0.35);
}

vec3 bullet(vec2 v, float t)
{
    vec2 pos = get_boss_pos(t) + v * t;

    if (length(p - pos) < 0.006) {
        return vec3(1);
    }
    if (length(p - pos) < 0.01) {
        return vec3(1, 0.4, 0.3);
    }
    return vec3(0);
}

vec3 myfighter(vec2 pos)
{
    float radius = 0.03;

    float dx = pos.x - p.x;
    float dy = pos.y - p.y;

    int px = int(floor(abs(dx) / 0.02));
    int py = int(floor(dy / 0.02));

    bool pixel = false;

    if (px == 0) {
        if (py >= -1 && py <= 1) {
            pixel = true;
        }
    }
    if (px == 1) {
        if (py >= 1 && py <= 2) {
            pixel = true;
        }
    }

    if (pixel) {
        return vec3(1, 1, 1);
    }
    return vec3(0);
}

float get_laser_hit_y()
{
    if (abs(myfighter_pos.x - get_boss_pos(0.0).x) < 0.09) {
        return 0.3;
    }
    return 2.0;
}

vec3 laser(vec2 pos)
{
    vec2 boss_pos = get_boss_pos(0.0);

    float a = 0.003 + blink * 0.004;

    float d;

    if (p.y > myfighter_pos.y && p.y < get_laser_hit_y()) {
        d = abs(p.x - pos.x);
    } else {
        d = length(p - pos);
    }
    float n = a / (d * 3.0);

    return vec3(2, 2, 5) * n;
}

vec3 laser_hit_effect()
{
    vec2 pos = vec2(myfighter_pos.x, get_laser_hit_y());

    float a = 0.02 + blink * 0.02;

    float d = length(p - pos);
    float n = a / (d * 5.0);

    return vec3(2, 2, 5) * n;
}

vec3 boss()
{
    vec2 pos = get_boss_pos(0.0);

    bool pixel = false;

    float dx = pos.x - p.x;
    float dy = pos.y - p.y;

    int px = int(floor(abs(dx) / 0.02));
    int py = int(floor(dy / 0.02));

    if (px >= -5 && px <= 5) {
        if (py >= -5 && py <= 3) {
            if (rand(vec2(px, py)) > 0.5) {
                pixel = true;
            }
        }
    }

    if (pixel) {
        if (abs(myfighter_pos.x - pos.x) < 0.08 && blink > 0.5) {
            return vec3(1);
        }
        return vec3(1, 0.5, 0);
    }
    return vec3(0);
}

void main(void)
{
    p = (gl_FragCoord.xy - resolution * 0.5) / resolution.y;

    if (abs(p.x) > SCREEN_W / 2.0) {
        discard;
    }

    set_myfighter_pos();

    vec3 c = background();

    c += boss();

    for (int i = -4; i <= 2; i++) {
        float t = fract(time * 0.5 + float(i) * 0.03);
        vec2 v = normalize(vec2(float(i) * 0.1, -1.0));
        c += bullet(v, t);
    }

    for (int i = -2; i <= 4; i++) {
        float t = fract(time * 0.5 + 0.5 + float(i) * -0.03);
        vec2 v = normalize(vec2(float(i) * 0.1, -1.0));
        c += bullet(v, t);
    }

    c += myfighter(myfighter_pos);
    c += laser(myfighter_pos);
    c += laser_hit_effect();

    gl_FragColor = vec4(c, 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)
glslsandbox_prog = moon3d.graphics.getGpuProgram("glslsandbox_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.bindPersp()

moon3d.graphics.bindGpuProgram(glslsandbox_prog)
moon3d.madshaders.updateGLSLSandboxCommonParams(glslsandbox_prog, global_time)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("GLSLSandbox/Shmup shader", 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: 4222
    • View Profile
Descent converted
« Reply #14 on: June 03, 2015, 05:14:35 PM »
Descent converted to GLSL Hacker format.

Illegal variable name "char" replaced with "hurz".
Why is it illegal in GLSL Hacker but not in Shadertoy?

Copy the code and save as Descent_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/Descent"
          width="800" height="600"
          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/XtSGWD

// illegal variable name "char" replaced with "hurz"

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;



//

const int buildingCount = 40;
const float buildingLifeTime = 5.0;
const float buildingSpread = 1.1;

const float topCurve = 0.75;
const float topOvershoot = 2.0;

const vec3 backgroundFog = vec3(0.21, 0.2, 0.3);
const vec3 streetGlow = vec3(0.3, 0.15, 0.4);

const float buildingScale = buildingLifeTime / (float(buildingCount) - 1.0);
const float buildingLeft = -0.5 * (buildingSpread - 1.0);

float hash11(float p) {
    vec2 p2 = fract(p * vec2(5.3983, 5.4427));
    p2 += dot(p2.yx, p2.xy +  vec2(21.5351, 14.3137));
    return fract(p2.x * p2.y * 95.4337);
}

float hash12(vec2 p) {
    p = fract(p * vec2(5.3983, 5.4427));
    p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
    return fract(p.x * p.y * 95.4337);
}

float hash13(vec3 p) {
    p = fract(p * vec3(5.3983, 5.4427, 6.9371));
    p += dot(p.zxy, p.xyz + vec3(21.5351, 14.3137, 15.3219));
    return fract(p.x * p.y * p.z * 95.4337);
}

vec2 hash21(float p) {
    vec2 p2 = fract(p * vec2(5.3983, 5.4427));
    p2 += dot(p2.yx, p2.xy +  vec2(21.5351, 14.3137));
    return fract(vec2(p2.x * p2.y * 95.4337, p2.x * p2.y * 97.597));
}

vec3 hash31(float p) {
    vec3 p2 = fract(p * vec3(5.3983, 5.4427, 6.9371));
    p2 += dot(p2.zxy, p2.xyz + vec3(21.5351, 14.3137, 15.3219));
    return fract(vec3(p2.x * p2.y * 95.4337, p2.y * p2.z * 97.597, p2.z * p2.x * 93.8365));
}

float noise12(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return 1.0 - 2.0 * mix(mix(hash12(i + vec2(0.0, 0.0)),
                               hash12(i + vec2(1.0, 0.0)), u.x),
                           mix(hash12(i + vec2(0.0, 1.0)),
                               hash12(i + vec2(1.0, 1.0)), u.x), u.y);
}

void main()
{
    float screenWidth = iResolution.x / iResolution.y;
    vec2 uv = gl_FragCoord.xy / iResolution.y;
   
    float idOffset = floor(iGlobalTime / buildingScale);
   
    vec3 color = backgroundFog;
    for (int i = 0; i < buildingCount; ++i) {
        float id = idOffset + float(i);
        float time = (iGlobalTime - buildingScale * id) / buildingLifeTime + 1.0;
       
       
        //    Building
        vec2 hash = hash21(id);       
        float top = topOvershoot * (topCurve * (time / topOvershoot - time * time) + time);
        float center = screenWidth * (buildingLeft + buildingSpread * hash.x);
        vec3 buildingColor = (top - uv.y) * streetGlow;
       
        vec2 border = 0.02 + 0.03 * hash21(id);
        vec2 outerWindow = vec2(0.01, 0.015) + vec2(0.02, 0.005) * hash21(id + 0.1);
        vec2 innerWindow = 0.25 * hash21(id + 0.2);
        float innerWidth = outerWindow.x * floor(0.2 * (0.5 + hash.y) / outerWindow.x);
        float outerWidth = innerWidth + border.x;
       
        vec2 pos = (uv - vec2(center, top - border.y)) / outerWindow;
        vec2 local = mod(pos, 1.0);
        vec2 index = floor(pos);
       
        vec3 windowColor = vec3(0.85) + 0.15 * hash31(id);
        float window = hash13(vec3(index, id)) - 0.2 * hash11(id + 0.3);
        window = smoothstep(0.62, 0.68, window);
        window *= step(innerWindow.x, local.x) * step(local.x, 1.0 - innerWindow.x);
        window *= step(innerWindow.y, local.y) * step(local.y, 1.0 - innerWindow.y);
       
        window *= step(index.y, -0.5);
        window *= step(uv.x, center + innerWidth) * step(center - innerWidth, uv.x);
        buildingColor = mix(buildingColor, windowColor, window);
       
        buildingColor = mix(buildingColor, backgroundFog, time);
       
        float inside = step(uv.y, top);
        inside *= step(uv.x, center + outerWidth);
        inside *= step(center - outerWidth, uv.x);
       
        color = mix(color, buildingColor, inside);
       
        //    Sign
        hash = hash21(id + 0.5);
        vec2 signCenter = vec2(center + outerWidth * (2.0 * hash.x - 1.0), top - 0.2 - 0.2 * hash.y);

        hash = hash21(id + 0.6);
        float hurzSize = 0.01 + 0.04 * hash.x;
        float hurzCount = floor(1.0 + 8.0 * hash.y);
       
        vec2 halfSize = 0.5 * vec2(hurzSize, hurzSize * hurzCount);
        float outline = length(max(abs(uv - signCenter) - halfSize, 0.0));
       
        vec3 signColor = hash31(id + 0.1);
        signColor = signColor / max(max(signColor.r, signColor.g), signColor.b);
        signColor = clamp(vec3(0.2) + signColor, 0.0, 1.0);
        signColor = mix(signColor, backgroundFog, time * time);
       
        vec2 hurzPos = (uv - signCenter + halfSize) / hurzSize;
        float hurz = 1.5 + 4.0 * noise12(id + 6.0 * hurzPos);
        hurzPos = fract(hurzPos);
        hurz *= smoothstep(0.0, 0.4, hurzPos.x) * smoothstep(1.0, 0.6, hurzPos.x);
        hurz *= smoothstep(0.0, 0.4, hurzPos.y) * smoothstep(1.0, 0.6, hurzPos.y);
        hurz *= step(outline, 0.001);
        signColor = mix(backgroundFog * time, signColor, clamp(hurz, 0.0, 1.0));
        color = mix(color, signColor, step(outline, 0.01));
       
        vec3 outlineColor = hash31(id + 0.2);
        outlineColor = outlineColor / max(max(outlineColor.r, outlineColor.g), outlineColor.b);
        outlineColor = clamp(vec3(0.2) + outlineColor, 0.0, 1.0);
        outlineColor = mix(outlineColor, backgroundFog, time * time);
       
        outline = smoothstep(0.0, 0.01, outline) * smoothstep(0.02, 0.01, outline);
        color = mix(color, outlineColor, outline);
       
        //    Balls
        hash = hash21(id + 0.;
        float radius = 0.005 + 0.01 * hash.y;
        float gap = radius + 0.015 * hash.x;
       
        hash = hash21(id + 0.9);
        float ballX = gap * (floor(uv.x / gap) + 0.5);
        float ballOffset = ballX - center;
        float ballY = top - 0.4 - 0.7 * hash.y + (0.03 + 0.05 * hash.x) * ballOffset * ballOffset;
        float ball = length(uv - vec2(ballX, ballY)) / radius;
        color = mix(color, outlineColor, smoothstep(1.0, 0.0, ball));
    }
    gl_FragColor = vec4(color, 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/Descent", 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: 4222
    • View Profile
Doom 2 shader converted
« Reply #15 on: June 03, 2015, 05:31:15 PM »
Doom 2 converted to GLSL Hacker format.

This is not a game.

Copy the code and save as doom_2_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/Doom 2"
          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/lsB3zD

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;



//

// Doom 2. Reinder Nijhoff 2013
// @reindernijhoff
//
// https://www.shadertoy.com/view/lsB3zD
//

#define COL(r,g,b) vec3(r/255.,g/255.,b/255.)

float time = iGlobalTime;

//----------------------------------------------------------------------
// Math functions

float hash( const float n ) {
    return fract(sin(n*14.1234512)*51231.545341231);
}
float hash( const vec2 x ) {
    float n = dot( x, vec2(14.1432,1131.15532) );
    return fract(sin(n)*51231.545341231);
}
float crossp( const vec2 a, const vec2 b ) { return a.x*b.y - a.y*b.x; }
vec3 rotate(vec3 r, float v){ return vec3(r.x*cos(v)+r.z*sin(v),r.y,r.z*cos(v)-r.x*sin(v));}

//----------------------------------------------------------------------
// Intersection functions

bool intersectWall(const vec3 ro, const vec3 rd, const vec2 a, const vec2 b, const float height,
                      inout float dist, inout vec2 uv ) {
    vec2 p = ro.xz;    vec2 r = rd.xz;
    vec2 q = a-p;    vec2 s = b-a;
    float rCrossS = crossp(r, s);
   
    if( rCrossS == 0.) {
        return false;
    }
    float d = crossp(q, s) / rCrossS;
    float u = crossp(q, r) / rCrossS;
    float he = ro.y+rd.y*d;
   
    if(0. <= d && d < dist && 0. <= u && u <= 1. && he*sign(height) < height ) {
        dist = d;
        uv = vec2( -u*length(s), height-he );
        return true;
    }
    return false;
}
bool intersectFloor(const vec3 ro, const vec3 rd, const float height,
                    inout float dist, inout vec2 uv ) {   
    if (rd.y==0.0) {
        return false;
    }
       
    float d = -(ro.y - height)/rd.y;
    d = min(100000.0, d);
    if( d > 0. && d < dist) {
        dist = d;
        uv = ro.xz+dist*rd.xz;
        return true;
    }
    return false;
}

//----------------------------------------------------------------------
// Material helper functions

float sat( const float a ) { return clamp(a,0.,1.); }
float onCircleAA( const vec2 c, const vec2 centre, const float radius, const float aa ) {
    return sat( aa*(radius - distance(c,centre)) );
}
float onLineX( const vec2 c, const float x ) {
    return step(x,c.x)*step(c.x,x);
}
float onLineY( const vec2 c, const float y ) {
    return step(y,c.y)*step(c.y,y);
}
float onBand( const float c, const float mi, const float ma ) {
    return step(mi,c)*step(c,ma);
}
float onRect( const vec2 c, const vec2 lt, const vec2 rb ) {
    return onBand( c.x, lt.x, rb.x )*onBand( c.y, lt.y, rb.y );
}
vec3 addKnobAA( const vec2 c, const vec2 centre, const float radius, const float strength, const vec3 col ) {
    vec2 lv = normalize( centre-c );
    return mix( col, col*(1.0+strength*dot(lv,vec2(-0.7071,0.7071))), onCircleAA(c, centre, radius, 4. ) );
}
float onBandAA( const float c, const float mi, const float ma ) {
    return sat( (ma-c+1.) )*sat( (c-mi+1.) );
}
float onRectAA( const vec2 c, const vec2 lt, const vec2 rb ) {
    return onBandAA( c.x, lt.x, rb.x )*onBandAA( c.y, lt.y, rb.y );
}
vec3 addBevel( const vec2 c, const vec2 lt, const vec2 rb, const float size, const float strength, const float lil, const float lit, const vec3 col ) {
    float xl = sat( (c.x-lt.x)/size);
    float xr = sat( (rb.x-c.x)/size);   
    float yt = sat( (c.y-lt.y)/size);
    float yb = sat( (rb.y-c.y)/size);
    return mix( col, col*clamp(1.0+strength*(lil*(xl-xr)+lit*(yb-yt)), 0., 2.), onRectAA( c, lt, rb ) );
}

//----------------------------------------------------------------------
// Generate materials!

void getMaterialColor( const int material, in vec2 uv, out vec3 col ) {   
    uv = floor( uv );
    float huv = hash(uv), huvx = hash(uv.x);
   
    if( material == 0 ) { // ceiling GRNLITE1
        uv = mod(uv, vec2(64.)); vec2 centre = mod(uv,vec2(32.,16.));
        col = mix( COL(90.,98.,69.),COL(152.,149.,125.),(0.75*huv+0.25*mod(uv.x,2.)) );
        col = mix( col, mix(vec3(243./255.),vec3(169./255.), distance(centre,vec2(16.,8.))/6.5), onCircleAA(centre, vec2(16.,8.), 6.25, 0.75) );
    }
    else if( material == 1 ) { // ceiling FLOOR_1
        uv = mod(uv, vec2(64.)); vec2 uv8 = mod(uv, vec2(32.,7.7));
        float h = huv*huvx;
        col = mix( COL(136.,114.,95.), COL(143.,122.,92.), sat(4.*h) );   
        col = mix( col, COL(175.,126.,89.), sat( 2.*(hash(floor(uv*0.125))+huv-1.35) ) );
        col = mix( col, COL(121.,103.,83.), sat( onLineX(uv,0.)+onLineY(uv,63.)) );
        col = mix( col, COL(121.,103.,83.), onLineX(uv,31.)*huv );
        uv8.x = abs(16.-uv8.x);
        float d = min( max( uv8.x-8.,abs(uv8.y-4.) ), abs(distance(uv8,vec2(11.,4.))) )+huv;
        vec3 fgcol = mix( col, col*sat(((16.-uv8.y)/12.)), step(d,3.) );
        col = mix( mix( fgcol, COL(114.,94.,78.), sat(d*(3.5-d)/4.)*step(2.,d) ), col, onRect(uv, vec2(32.,23),vec2(63.,39.) ) );
    }
    else if( material == 2 ) { // wall TEKGREN2 & TEKGREN5
        uv = mod(uv, vec2(128.,128)); vec2 uv64 = mod(uv, vec2(64.,65.) ); vec2 uv24 = mod(uv64, vec2(64.,24.) );
        float h = huv*huvx;
        col = mix( vec3(114./255.), vec3(98./255.), sat(2.*h) );
        col = mix( col, mix( COL(111.,114.,87.), COL(90.,98.,69.), sat(2.*h) ), sat( 100.*(hash(uv+vec2(523.,53.))*hash(150.-uv.x)-0.15)) );   
        col = addKnobAA( mod( uv24, vec2(3.,32.) ), vec2(0.,4.5), 1.1, 0.4, col );
        col = mix( col, COL(137.,141.,115.), 0.7*sat( onLineX(uv64,1.)+onLineY(uv,1.)+onLineY(uv24,0.)+onLineY(uv24,19.)+onLineY(uv64,59.) ) );
        col = mix( col, COL(73.,81.,55.), sat( onLineX(uv64,0.)+onLineX(uv64,62.) ) );
        col = mix( col, mix(COL(73.,81.,55.),vec3(38./255.),uv24.y-22.), onBand(uv24.y,22.,23.) );
        col = mix( col, mix(COL(73.,81.,55.),vec3(38./255.),uv64.y-63.), onBand(uv64.y,63.,64.) );
        col = mix( col, vec3(38./255.), sat( onLineY(uv,0.)+onLineX(uv64,63.) ) );
        col = mix( col, COL(137.,141.,115.), onRect(uv,vec2(3.),vec2(60.,12.)) );
        col = mix( col, mix( vec3(1.), COL(255.,253.,110.), sat( abs(uv.x-32.)/20.)-0.25*mod(uv.x,2.)), onRect(uv,vec2(4.),vec2(59.,11.)) );
    }   
    else if( material == 3 ) { // wall BRONZE2
        uv = mod(uv, vec2(64.,128)); float s = sin(31.15926*uv.x/64.);
        col = mix( vec3(75./255.), vec3(64./255.), huv );
        col = mix( col, COL(106.,86.,51.),  sat( 5.*(huv+(s+1.2)*(1.-(uv.y+44.)/64.))) * onBand(uv.y, 0., 30. ) );
        col = mix( col, COL(123.,105.,85.), sat( 2.*(0.5*huvx+huv+(s+1.7)*(1.-(uv.y+44.)/64.)-0.5) ) * onBand(uv.y, 0., 30. ) );
        col = mix( col, COL(106.,86.,51.),  sat( 5.*(huv+(s+0.7)*(1.-(uv.y+14.)/64.))) * onBand(uv.y, 30., 98. ) );
        col = mix( col, COL(123.,105.,85.), sat( 2.*(1.1*huvx+(s+1.7)*(1.-(uv.y+14.)/64.)-0.5) ) * onBand(uv.y, 30., 98. ) );
        col = mix( col, COL(7.,59.,20.), sat( huv*uv.y/96.-0.5) );
        col = mix( col, COL(106.,86.,51.),  sat( 5.*(huv+(s+1.2)*(1.-(uv.y-40.)/64.))) * onBand(uv.y, 98., 128. ) );
        col = mix( col, COL(123.,105.,85.), sat( 2.*(huvx+(s+1.7)*(1.-(uv.y-40.)/64.)-0.5) ) * onBand(uv.y, 98., 128. ) );   
        col = mix( col, mix(COL(110.,89.,70.),COL(130.,112.,92.),sat((uv.y-3.)/18.)), onRectAA(mod(uv,vec2(16.,128.)),vec2(6.5,1.5),vec2(12.5,21.5)) );
        col = addBevel( mod(uv,vec2(16.,128.)),vec2(5.5,-2.5),vec2(12.5,21.5), 2.3, 1., 0.1, 0.7, col );
        col = mix( col, addBevel( abs(mod(uv+vec2(0.,-85.),vec2(64.))-vec2(32.,0.)), vec2(15.5,0.5), vec2(34.5,52.5), 1.2, 1., 0.5, -0.7, col ), onBand(uv.y, 30.,97.));
        col = mix( col, 0.7*col, sat( onLineY(uv,127.)+onLineX(uv,0.)+onBand(uv.y, 97.,98.)+onBand(uv.y, 29.,30.)) );
        col = mix( col, 1.2*col, sat( onBand(uv.y, 98.,99.)+onBand(uv.y, 0.,1.)+onLineX(uv, 63.)) );
        col = mix( col, 0.75*col*uv.x, onBand(uv.x, 0., 1.)*onBand(uv.y, 30.,97.) );
        col *= 1.0-0.1*huv;
    }   
    else if( material == 4 ) { // wall STEP2
        uv = mod(uv, vec2(64.,16.));
        col = mix( COL(182.,133.,93.), COL(132.,98.,66.), sat(huv-0.5) );
        col = mix( col, COL(129.,111.,79.), sat(1.-(uv.y-4.)/8.) );
        col = mix( col, COL(102.,82.,50.), sat((huv+1.)*onRectAA(mod(uv,vec2(32.,16.)), vec2(1.5,9.7), vec2(29.5,13.5))) );
        col = mix( col, COL(102.,82.,50.), 0.6*sat((huv+1.)*onRectAA(mod(uv,vec2(8.,16.)), vec2(2.5,3.5), vec2(5.5,6.2))) );
        col = mix( col, COL(143.,122.,92.), onLineY(uv,0.) );
        col = mix( col, COL(106.,86.,61.), onLineY(uv,2.) );
        col *= 1.-0.2*onLineY(uv,3.);
    }
    else if( material == 5 ) { // wall PIPE4
        uv = mod(uv, vec2(128.,64.)); float huv2 = hash( uv*5312. );
        col = mix( mix(COL(184.,165.,144.),COL(136.,102.,67.),uv.x/128.),
                   mix(COL(142.,122.,104.),COL(93.,77.,50.),uv.x/128.), sat(huv+huvx) );
        col *= 1.+0.5*sat(hash(uv.y)-0.7);
        col *= 1.-0.2*sat(hash(uv.y-1.)-0.7);
        col = mix( col, COL(102.,82.,50.), sat(0.2*huv2+3.*(huvx-0.7)) );
        col = mix( col, COL(165.,122.,85.), (0.75+0.5*huv2)*sat( onBandAA(uv.x,122.5,123.5)+onBandAA(uv.x,117.5,118.5)+onBandAA(uv.x,108.5,109.5) ) );
        col = mix( col, mix(  (1.-sat(0.2*abs(2.8-mod(uv.x,6.))))*mix(COL(175.,126.,89.),COL(143.,107.,71.),0.4*distance( mod(uv,vec2(6.)), vec2 (1.5))), COL(77.,68.,40.), onBandAA(mod(uv.x+1.,6.),0.,1.5)),
                                   (0.75+0.5*huv2)*sat( onBandAA(uv.x,6.5,11.5)+onBandAA(uv.x,54.5,59.5)+onBandAA(uv.x,66.5,70.5)+onBandAA(uv.x,72.5,78.5) ) );
        col = mix( col, mix( COL(82.,90.,64.), 1.2*COL(118.,125.,99.), huv*(sat(abs(uv.x-14.)-huv)+sat(abs(uv.x-62.)-huv)) ), onBandAA(uv.x,12.8,13. + onBandAA(uv.x,60.8,61.);
        col = mix( col, vec3(0.), 0.3*(onBandAA(uv.y,18.8,21.*onBandAA(uv.x,40.8,52. + onBandAA(uv.x,0.1,3.7) + onBandAA(uv.x,41.3,44.2) + onBandAA(uv.x,48.9,51.+0.6*onBandAA(uv.x,80.1,81.6)));
        col = mix( col, mix( 1.2*COL(205.,186.,167.), COL(143.,122.,92.), 0.3*(sat(abs(uv.x-2.)+huv)+sat(abs(uv.x-43.)+huv)+sat(abs(uv.x-51.)+huv)) ), onBandAA(uv.x,0.8,2. + onBandAA(uv.x,42.1,43.3) + onBandAA(uv.x,49.8,51.2)+0.6*onBandAA(uv.x,80.8,81.5));
        col = mix( col, mix( 1.2*COL(205.,186.,167.), COL(154.,133.,105.), (sat(abs(uv.y-20.5)+huv)) ), onBandAA(uv.y,19.3,21.2)*onBandAA(uv.x,40.8,52.1));
        float d = min( min( min( min( min( min( distance(uv,vec2(6.,39.)), 0.8*distance(uv,vec2(23.,45.)) ), 1.2*distance(uv,vec2(39.,30.)) )
                      , 1.5*distance(uv,vec2(48.,42.)) ), distance(uv,vec2(90.,32.)) ), 0.8*distance(uv,vec2(98.,50.)) ), 1.15*distance(uv,vec2(120.,44.)) );;
        d *= (1.-0.8*(sat(hash(uv.x+uv.y)-0.6)+sat(huvx-0.6)));
        col = mix( col,COL(93.,77.,50.), sat((7.-d)/8.) );
        col = mix( col, vec3(0.), pow(sat((5.-d)/6.),1.5) );
    }
    else if( material == 6 ) { // floor FLOOR_3_3
        uv = mod(uv, vec2(64.));
        col = mix( COL(147.,126.,108.), COL(175.,152.,134.), sat( 1.5*(huv+hash(uv.x-uv.y)-0.95-uv.y/128.)) );
        col = mix( col, COL(175.,152.,134.), sat( 1.5*(huv+hash(uv.x-uv.y*1.1+5.)-1.8+uv.y/64.)) );
        col = mix( col, COL(130.,133.,108.), sat( 10.*(huv+hash(uv.x*1.1-uv.y+3.)-1.25)) );
        col = mix( col, mix( COL(118.,125.,99.), COL(130.,133.,108.), 1.-huv), sat(5.*(huv-1.5+uv.y/64.)) );
        col = mix( col, COL(129.,111.,91.), sat( onLineX(uv,0.)+onLineY(uv,63.) ) );
        col *= sat(0.92+huv);       
    }
    else if( material == 7 ) { // floor FLOOR_0_1
        uv = mod(uv, vec2(64.));
        float h = hash(3.*uv.x+uv.y);
        col = mix( COL(136.,114.,95.), COL(143.,122.,104.), sat(4.*h*huv) );
        col = mix( col, COL(129.,111.,91.), sat(h-0.5) );   
        col *= 1.+0.05*sat( 0.3+mod(uv.x,2.)*cos(uv.y*0.2)*huv );
        col = mix( col, COL(175.,126.,89.), sat( 2.*(hash(floor(uv*0.125))+huv-1.5) ) );
        vec3 ncol = mix( col, COL(114.,94.,78.), sat(
            (0.4*huv+0.4)*onRectAA( mod(uv+vec2(0.,33.),vec2(64.)), vec2(6.5,0.5), vec2(36.5,58.5) )
                         -onRectAA( mod(uv+vec2(0.,33.),vec2(64.)), vec2(9.5,3.5), vec2(33.5,55.5) ) ));
        ncol = mix( ncol, COL(114.,94.,78.), sat( (0.6*huv+0.3)*onRectAA( mod(uv+vec2(0.,5.),vec2(64.)), vec2(33.5,0.5), vec2(59.5,60.5) ) ));
        ncol = mix( ncol, col, sat(               0.8*onRectAA( mod(uv+vec2(0.,5.),vec2(64.)), vec2(35.5,2.5), vec2(57.5,58.5) ) ));
        ncol = mix( ncol, COL(121.,103.,81.), sat( (0.8*huv+0.9)*onRectAA( mod(uv+vec2(0.,53.),vec2(64.)), vec2(18.5,0.5), vec2(41.5,22.5) ) ));
        ncol = mix( ncol, col, sat(               onRectAA( mod(uv+vec2(0.,53.),vec2(64.)), vec2(19.5,1.5), vec2(40.5,21.5) ) ));
        ncol = mix( ncol, COL(114.,94.,78. ), sat( (0.8*huv+0.6)*onRectAA( mod(uv+vec2(8.,46.),vec2(64.)), vec2(0.5,0.5), vec2(20.5,36.5) ) ));
        col  = mix( ncol, col, sat(               onRectAA( mod(uv+vec2(8.,46.),vec2(64.)), vec2(1.5,1.5), vec2(19.5,35.5) ) ));
    } else  {
        col = vec3(0.5);
    }
}

//----------------------------------------------------------------------
// Render MAP functions

struct lineDef { vec2 a, b; float h; float l; int m; };

vec3 castRay( const vec3 ro, const vec3 rd ) {
    lineDef ldfs[14];
    ldfs[0]  = lineDef(vec2(192.,-448.), vec2(320.,-320.), 264., 128., 5 );
    ldfs[1]  = lineDef(vec2(320.,-320.), vec2(256.,0.),    264., 128., 5 );
    ldfs[2]  = lineDef(vec2(256.,0.),    vec2(64.,0.),     264., 128., 5 );
    ldfs[4]  = lineDef(vec2(64.,0.),     vec2(0.,0.),       56., 208., 4 );
    ldfs[3]  = lineDef(vec2(0.,448.),    vec2(320.,448.),  128., 224., 2 );
    ldfs[5]  = lineDef(vec2(64.,0.),     vec2(-64.,0.),   -128., 208., 5 );
    ldfs[6]  = lineDef(vec2(192.,-320.), vec2(128.,-320.), 264., 128., 3 );
    ldfs[7]  = lineDef(vec2(128.,-320.), vec2(128.,-256.), 264., 128., 3 );
    ldfs[8]  = lineDef(vec2(192.,0.),    vec2(0.,-320.),    16., 144., 4 );
    ldfs[9]  = lineDef(vec2(160.,0.),    vec2(0.,-256.),    24., 160., 4 );
    ldfs[10] = lineDef(vec2(128.,0.),    vec2(0.,-192.),    32., 176., 4 );
    ldfs[11] = lineDef(vec2(96.,0.),     vec2(0.,-128.),    40., 192., 4 );
    ldfs[12] = lineDef(vec2(64.,0.),     vec2(0.,-64.),     48., 208., 4 );
    ldfs[13] = lineDef(vec2(64.,0.),     vec2(64.,320.),   128., 224., 2 );
   
    float dist = 999999., curdist; vec2 uv, curuv;
    vec3 col = vec3( 0. ); float lightning = 128.; int mat = 0;

    // check walls
    for( int i=0; i<14; i++ ) {
        vec2 a = ldfs[i].a, b = ldfs[i].b; float h=ldfs[i].h;         
        if( intersectWall(ro, rd, a, b, h, dist, uv) ||
            intersectWall(ro, rd, b*vec2(-1.,1.), a*vec2(-1.,1.), h, dist, uv) ) {
            mat = ldfs[i].m;
            lightning = ldfs[i].l * (1.-0.2*abs( normalize( (a-b).yx ).y ));
        }
    }
    if( mat == 5 ) { // fix large texture on wall above portal
        vec3 intersection = ro + rd*dist;
        if( intersection.z > -0.1 ) {
            uv = -intersection.xy+vec2(64.,0.);
            lightning = 0.8*max(128., min(208., 248.-20.*floor(abs(intersection.x)/32.)));
        }
        uv *= 0.5;
    }
   
    // check floor and ceiling
    if( intersectFloor(ro, rd, 264., dist, uv ) ) {
        mat = 1;
        lightning =128.;
        float c1=320., c2=196.;
        for( int i=4; i>=0; i-- ) {
            if( abs(uv.x)*(c1/c2)-uv.y < c1 ) {
                lightning = float(208-i*16);
            }
            c1-=64.; c2-=32.;
        }
    }
    if( intersectFloor(ro, rd, 8., dist, uv ) ) {
        mat = 7;
        lightning =128.;
    }       
    float c1=64., c2=64., c3=48.;
    for( int i=0; i<5; i++ ) {
        curdist = dist;
        if( intersectFloor(ro, rd, c3, curdist, curuv ) && abs(curuv.x)*(c1/c2)-curuv.y < c1 ) {
            uv = curuv;
            mat = 7;
            dist = curdist;
            lightning = float(208-i*16);
        }
        c3-=8.; c1+=64.; c2+=32.;
    }
    // and hall   
    curdist = dist;
    if( (intersectFloor(ro, rd, 56., curdist, curuv ) || intersectFloor(ro, rd, 128., curdist, curuv ) ) && curuv.y > 0. ) {
        dist = curdist;
        uv = curuv;
        mat = rd.y>0.?0:6;
        lightning = 224.;
    }
   
    getMaterialColor( mat, uv, col );
       
    col *= 0.3*pow(2.*lightning/255., 2.5)*sat( 1.-curdist/2000. );   
    // fake 8-bit pallete
    col = floor((col)*64.+vec3(0.5))/64.;
    return col;
}

//----------------------------------------------------------------------
// Camera path

float getPathHeight( const float z, const float t ) {
    return max( 0.+step(0.,z)*56.+step(z,-448.)*56.+
        mix(56.,8.,(448.+z)/32.)*step(-448.,z)*step(z,-416.)+
        mix(8.,56.,(320.+z)/320.)*step(z,0.)*step(-320.,z), 8.) + 56.;
}
vec2 path( const float t ) {
    return vec2(32.*sin(t*0.21), -200.-249.*cos( max(0.,mod(t,30.)-10.)*(3.1415936/10.) ) );
}


//----------------------------------------------------------------------
// Main

void main()
 {
    vec2 q = gl_FragCoord.xy/iResolution.xy;
    vec2 p = -1.0 + 2.0*q;
    p.x *= iResolution.x/ iResolution.y;

    vec3 ro; ro.xz = path(time);
    vec3 ta; ta.xz = path(time+0.1) + vec2(0.,20.);
    ta.y = ro.y = getPathHeight(ro.z, time);
   
    vec3 rdcenter =  rotate( normalize(ta - ro), 0.5*cos(time*0.5) );
    vec3 uu = normalize(cross( vec3(0.,1.,0.), rdcenter ));
    vec3 vv = normalize(cross(rdcenter,uu));
    vec3 rd = normalize( p.x*uu + p.y*vv + 1.25*rdcenter );
   
    vec3 col = castRay( ro, rd );
       
    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/Doom 2", 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: 4222
    • View Profile
Piggy converted
« Reply #16 on: June 04, 2015, 10:47:51 AM »
piggy at Revision 2012 by Floppy converted to GLSL Hacker format

Download the code (too long for this forum) and copy into  demo folder of MadShaders.


Stefan

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4222
    • View Profile
CeramicGlassMosaic converted
« Reply #17 on: June 04, 2015, 10:50:24 AM »
CeramicGlassMosaic converted to GLSL Hacker format

Copy the code and save as CeramicGlassMosaic_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/CeramicGlassMosaic"
          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/XlSGWt

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;



//

// Created by Christina Coffin - @christinacoffin
// 2015-06-03 - 1st version, needs AA
//
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

void main()
{
    vec2 p = gl_FragCoord.xy / iResolution.xy;
    vec2 uv = p*0.841;   
    vec2 mo = iMouse.xy/iResolution.xy;
   
    //---------------------------------------------   
    // layered time animation on the x-axis so we can see the fx change when
    //    the mouse isnt being moved   
    float anim_A= sin(iGlobalTime);
    anim_A *= anim_A*anim_A;
    mo.x+= abs(anim_A)*0.01;

    //---------------------------------------------   
    mo.x *= 0.25;    // mouse axis to control the mosaic size
   
    float mosaicScaler = 0.15 + mo.x*1.0;// 0.1 = big!  , 0.9 = tiny
    float glassTileScale = 1.0;//mo.y;// 0.2 = ceramicTile , 0.95 = glassblockTile
   
    float textureResolution = 64.0 * mosaicScaler;
    uv = uv*textureResolution + 0.5;
    vec2 iuv = floor( uv );
    vec2 fuv = fract( uv )*glassTileScale;
    vec2 fuv_2 = fuv; 
   
       fuv = fuv*fuv*(3.0-2.0*fuv);

    vec2    uv_X = (iuv + fuv - 0.5)/textureResolution;
    vec3     col_X = texture2D( iChannel1, uv_X ).xyz;   
   
    fuv = fuv*fuv*(3.0-2.0*fuv);
    // even smootherstep to get a nice beveled tile shape
    vec2 xsq = fuv *fuv;
    vec2 xsqsq = xsq*xsq;
    fuv = xsqsq * ( 25.0 - 48.0 * fuv + xsq * (25.0 -xsqsq));   
    uv = iuv + fuv*fuv*(3.0-2.0*fuv);         
    uv = (uv - 0.5)/textureResolution;
    vec3 colB = texture2D( iChannel0, uv ).xyz;
   
    vec3 colNoise = texture2D( iChannel0, uv ).xyz;//noise applied to whole screen, used for gloss warp
   
    float blendit = smoothstep(0.2, 0.8, fract(uv.x));//screenspace x axis blend from our ceramic to glass tiles
    colB = mix( colB, col_X, blendit );// blend between the two
    vec3 col = colB;
   
    //-----------------------------------------------------
    // fake glossyness along the middle area
    float refrWarp = 0.39;
    float glossFreq = mix( 0.15, 10.0, mo.y+0.13 );
    col.rgb *= 1.0+fract(smoothstep(0.0, 1.0, 2.0*sin((p.y*2.0)+
               2.0- (glossFreq * length(col.rgb+colNoise.rgb)*refrWarp)))
                        );
    //-----------------------------------------------------
   
    //-----------------------------------------------------
    // post
    vec2 q = p;
    col = pow( abs(clamp(col,0.0,1.0)), vec3(0.945) );// gamma warp
    // contrast, desat, tint and vignetting   
    col = col*1.2 + 0.2*col*col*col*col*(4.0-2.0*col);// hacky saturated contrast
    col = mix( col, vec3(col.x+col.y+col.z)*0.333, 0.25 );//desat
    col *= vec3(1.10,1.02,0.96);//tint
    col.rg *= 0.55 + 0.45*pow( abs(4.0*q.x*(2.0-q.x)*(0.0-q.y)), 0.95 );  //vignette fakelight across the screen
   
    col = clamp(col,0.0,1.0);//clamp to 0-1
    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/tex07.jpg")
tex1 = moon3d.image.load2d("./data/tex03.jpg")
tex2 = 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.madshaders.setShadertoyTexture(shadertoy_prog, tex1, 1)
moon3d.madshaders.setShadertoyTexture(shadertoy_prog, tex2, 2)

moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/CeramicGlassMosaic", 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: 4222
    • View Profile
Rotating Structure converted
« Reply #18 on: June 04, 2015, 02:46:03 PM »
Rotating Structure converted to GLSL Hacker format

I modified
float r = texture2D( iChannel1, fragCoord/iChannelResolution[1].xy ).x;

to
float r = texture2D( iChannel1, gl_FragCoord/iResolution.xy ).x;
else compiler fails.

Copy the code and save as Rotating_Structure_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/Rotating Structure"
          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/XtSGDK

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;



//

// Created by inigo quilez - iq/2015
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0

// set AA to 1 if you have a slow machine

#define AA 2


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

vec3 deform( in vec3 p, in float time, out float sca )
{
    float s = 0.34*sqrt(dot(p*p,p*p));
    //float s = 1.0;

    p = p/s;

    p.xyz += 4.0*sin(0.5*vec3(1.0,1.1,1.3)*time+vec3(0.0,2.0,4.0));
   
    sca = s;
   
    return p;
}

float shape( vec3 p )
{
    vec3 q = mod( p+1.0, 2.0 ) -1.0;

    float d1 = udRoundBox(q,vec3(0.10,0.02,1.00),0.02);
    float d2 = udRoundBox(q,vec3(0.02,1.00,0.10),0.02);
    float d3 = udRoundBox(q,vec3(1.00,0.10,0.02),0.02);
    float d4 = udRoundBox(q,vec3(0.30,0.30,0.30),0.02);

    return min( min(d1,d2), min(d3,d4) );
}

float map( vec3 p, float t )
{
    float s = 1.0;
    p = deform( p, t, s );
    return shape( p ) * s;
}

vec3 calcNormal( in vec3 pos, in float eps, in float t )
{
    vec2 e = vec2(1.0,-1.0)*0.5773*eps;
    return normalize( e.xyy*map( pos + e.xyy, t ) +
                      e.yyx*map( pos + e.yyx, t ) +
                      e.yxy*map( pos + e.yxy, t ) +
                      e.xxx*map( pos + e.xxx, t ) );
}

vec3 calcNormal2( in vec3 pos, in float eps )
{
    vec2 e = vec2(1.0,-1.0)*0.5773*eps;
    return normalize( e.xyy*shape( pos + e.xyy ) +
                      e.yyx*shape( pos + e.yyx ) +
                      e.yxy*shape( pos + e.yxy ) +
                      e.xxx*shape( pos + e.xxx ) );
}

float calcAO( in vec3 pos, in vec3 nor )
{
    float occ = 0.0;
    for( int i=0; i<8; i++ )
    {
        float h = 0.01 + 0.5*float(i)/7.0;
        occ += (h-shape( pos + h*nor ));
    }
    return clamp( 1.0 - 4.0*occ/8.0, 0.0, 1.0 );   
}

float softshadow( in vec3 ro, in vec3 rd, float k, in float time )
{
    float res = 1.0;
    float t = 0.01;
    for( int i=0; i<32; i++ )
    {
        float h = map(ro + rd*t, time);
        res = min( res, smoothstep(0.0,1.0,k*h/t) );
        t += clamp( h, 0.04, 0.1 );
        if( res<0.01 ) break;
    }
    return clamp(res,0.0,1.0);
}

vec4 texcube( sampler2D sam, in vec3 p, in vec3 n, in float k )
{
    vec3 m = pow( abs( n ), vec3(k) );
    vec4 x = texture2D( sam, p.yz );
    vec4 y = texture2D( sam, p.zx );
    vec4 z = texture2D( sam, p.xy );
    return (x*m.x + y*m.y + z*m.z) / (m.x + m.y + m.z);
}

vec3 shade( in vec3 ro, in vec3 rd, in float t, float time )
{
    float eps = 0.001;
   
    vec3 pos = ro + t*rd;
    vec3 nor = calcNormal( pos, eps, time );
    float kk;
    vec3 qos = deform( pos, time, kk );
    vec3 qor = calcNormal2( qos, eps );

    vec3 tex = texcube( iChannel0, qos*0.5, qor, 1.0 ).xyz;

    vec3 lig = normalize( vec3(2.0,1.0,0.2) );

    float fre = pow( clamp(1.0+dot(nor,rd), 0.0, 1.0 ), 2.0 );
    float occ = calcAO( qos, qor );

    float dif = clamp( dot(nor,lig), 0.0, 1.0 );
    float sha = softshadow( pos, lig, 64.0, time );
    dif *= sha;
       
    vec3 col = 2.0*vec3(1.1,0.8,0.6)*dif*(0.5+0.5*occ) + 0.6*vec3(0.1,0.27,0.4)*occ;
    col += 1.0*fre*(0.5+0.5*dif)*occ;
    float sh = 4.0 + tex.x*64.0;
    col += 0.1*sh*pow(clamp(-dot(rd,nor),0.0,1.0),sh)*occ*sha;
    col *= clamp(2.0*dot(pos,pos),0.0,1.0);

    col *= 6.0*tex;
   
    col *= exp( -1.5*t );

    return col;       
}

float intersect( in vec3 ro, in vec3 rd, const float maxdist, float time )
{
    float res = -1.0;
    vec3 resP = vec3(0.0);
    float t = 0.1;
    for( int i=0; i<100; i++ )
    {
        vec3 p = ro + t*rd;
        float h = map( p, time );
        res = t;

        if( h<(0.001*t) || t>maxdist ) break;
       
        t += h*0.5;
    }
    return res;
}

vec3 render( in vec3 ro, in vec3 rd, float time )
{
    vec3 col = vec3(0.0);
   
    const float maxdist = 32.0;
    float t = intersect( ro, rd, maxdist, time );
    if( t < maxdist )
    {
        col = shade( ro, rd, t, time );
    }

    return pow( col, vec3(0.55) );
}

mat3 setCamera( in vec3 ro, in vec3 rt, in float cr )
{
    vec3 cw = normalize(rt-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()
{   
#if AA>1
    vec3 col = vec3(0.0);
   
    float r = texture2D( iChannel1, gl_FragCoord/iResolution.xy ).x;
    for( int j=0; j<AA; j++ )
    for( int i=0; i<AA; i++ )
    {
        vec2 p = (-iResolution.xy+2.0*(gl_FragCoord.xy+vec2(i,j)/float(AA)))/iResolution.y;

        float time = iGlobalTime + (r+float(AA*j + i))/float(AA*AA) * (0.4/24.0);
       
        time = 41.73 + time;
       
        float an = 6.0 + 0.1*time;

        vec3 ro = vec3(0.0,1.0,0.5) + 2.0*vec3(cos(an),0.0,sin(an));
        vec3 ta = vec3(0.0,0.0,0.0);
        mat3 ca = setCamera( ro, ta, 0.3 );
        vec3 rd = normalize( ca * vec3(p,-1.5) );
       
        col += render( ro, rd, time );
    }
    col /= float(AA*AA);
#else
    vec2 p = (-iResolution.xy+2.0*(gl_FragCoord.xy))/iResolution.y;
   
    float time = iGlobalTime;
    time = 41.73 + time;
    float an = 6.0 + 0.1*time;

    vec3 ro = vec3(0.0,1.0,0.5) + 2.0*vec3(cos(an),0.0,sin(an));
    vec3 ta = vec3(0.0,0.0,0.0);
    mat3 ca = setCamera( ro, ta, 0.3 );
    vec3 rd = normalize( ca * vec3(p,-1.5) );

    vec3 col = render( ro, rd, time );
#endif   
    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/tex06.jpg")
tex1 = moon3d.image.load2d("./data/noise_texture_0001.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.madshaders.setShadertoyTexture(shadertoy_prog, tex1, 1)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/Rotating Structure", 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: 4222
    • View Profile
Slisesix converted
« Reply #19 on: June 05, 2015, 02:14:51 PM »
Slisesix converted to GLSL Hacker format

Copy the code and save as slisesix_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 - GLSLSandbox/Slisesix"
          width="800" height="400"
          gl_version_major="2" gl_version_minor="1" />
         
         
<gpu_program name="glslsandbox_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[     


//

// http://glslsandbox.com/e#4456.6

/*
Code for the 4k procedural graphics Slisesix from the demoscene
(http://www.pouet.net/prod.php?which=51074).
It made 1st position in the Euskal Party 2008 in Bilbao (Spain).
It's raymarching in a procedural distance field
*/
// port by @rotwang *** help needed ***
// removing unsupported stuff like
// if( (((submat>>10)&7)>6) )

#ifdef GL_ES
precision highp float;
#endif

#extension GL_EXT_gpu_shader4: enable

uniform vec2 resolution;
uniform float time;
uniform sampler2D tex0;

float random(vec2 co){
    return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}

int icoolfFunc3d2( in int n )
{
   // n=(n<<13)^n;
   // return (n*(n*n*15731+789221)+1376312589) & 0x7fffffff;
    float st = random( vec2(n));
    return int(st);
}

float coolfFunc3d2( in int n )
{
    return float(icoolfFunc3d2(n));
}

float noise3f( in vec3 p )
{
    ivec3 ip = ivec3(floor(p));
    vec3 u = fract(p);
    u = u*u*(3.0-2.0*u);

    int n = ip.x + ip.y*57 + ip.z*113;

    float res = mix(mix(mix(coolfFunc3d2(n+(0+57*0+113*0)),
                            coolfFunc3d2(n+(1+57*0+113*0)),u.x),
                        mix(coolfFunc3d2(n+(0+57*1+113*0)),
                            coolfFunc3d2(n+(1+57*1+113*0)),u.x),u.y),
                    mix(mix(coolfFunc3d2(n+(0+57*0+113*1)),
                            coolfFunc3d2(n+(1+57*0+113*1)),u.x),
                        mix(coolfFunc3d2(n+(0+57*1+113*1)),
                            coolfFunc3d2(n+(1+57*1+113*1)),u.x),u.y),u.z);

    return 1.0 - res*(1.0/1073741824.0);
}

float fbm( in vec3 p )
{
    return 0.5000*noise3f(p*1.0) +
           0.2500*noise3f(p*2.0) +
           0.1250*noise3f(p*4.0) +
           0.0625*noise3f(p*8.0);
}


float techo( in float x, in float y )
{
    y = 1.0 - y;
    if( x<0.1 || x>0.9 ) return y;
    x = x - 0.5;
    return -(sqrt(x*x+y*y)-0.4);
}


float distToBox( in vec3 p, in vec3 abc )
{
    vec3 di=max(abs(p)-abc,0.0);
    return dot(di,di);
}


float columna( in float x, in float y, in float z, in float mindist, in float offx )
{
    vec3 p=vec3(x,y,z);
    float di0=distToBox( p, vec3(0.14,1.0,0.14) );
    if( di0 > (mindist*mindist) ) return mindist + 1.0;

    float y2=y-0.40;
    float y3=y-0.35;
    float y4=y-1.00;

    float di1=distToBox( p, vec3(0.10,1.00,0.10) );
    float di2=distToBox( p, vec3(0.12,0.40,0.12) );
    float di3=distToBox( p, vec3(0.05,0.35,0.14) );
    float di4=distToBox( p, vec3(0.14,0.35,0.05) );
    float di9=distToBox( vec3(x,y4,z), vec3(0.14,0.02,0.14) );

    float di5=distToBox( vec3((x-y2)*0.7071, (y2+x)*0.7071, z), vec3(0.10*0.7071,  0.10*0.7071, 0.12) );
    float di6=distToBox( vec3(x, (y2+z)*0.7071, (z-y2)*0.7071), vec3(0.12,  0.10*0.7071, 0.10*0.7071) );
    float di7=distToBox( vec3((x-y3)*0.7071, (y3+x)*0.7071, z), vec3(0.10*0.7071,  0.10*0.7071, 0.14) );
    float di8=distToBox( vec3(x, (y3+z)*0.7071, (z-y3)*0.7071), vec3(0.14,  0.10*0.7071, 0.10*0.7071) );

    float di=min(min(min(di1,di2),min(di3,di4)),min(min(di5,di6),min(di7,di8)));
    di=min(di,di9);

  //  di += 0.00000003*max( fbm(10.1*p), 0.0);

    return di;
}



float bicho( vec3 x, in float mindist )
{
//    float ramo = noise3f( vec3(2.0*time, 2.3*time, 0.0) );

    x -= vec3(0.64,0.5,1.5);

    float r2 = dot(x,x);

    float sa = smoothstep(0.0,0.5,r2);
    float fax = 0.75 + 0.25*sa;
    float fay = 0.80 + 0.20*sa;

    x.x *= fax;
    x.y *= fay;
    x.z *= fax;

    r2 = dot(x,x);

    float r = sqrt(r2);

    float a1 = 1.0-smoothstep( 0.0, 0.75, r );
    a1 *= 0.40;
    float si1 = sin(a1);
    float co1 = cos(a1);
    x.xy = mat2(co1,si1,-si1,co1)*x.xy;


    float mindist2 = 100000.0;

    float rr = 0.05+sqrt(dot(x.xz,x.xz));
    float ca = (0.5-0.045*0.75) -6.0*rr*exp2(-10.0*rr);
    for( int j=1; j<7; j++ )
    {
        float an = (6.2831/7.0) * float(j);
        float aa = an + 0.40*rr*noise3f( vec3(4.0*rr, 2.5, an) ) + 0.29;
        float rc = cos(aa);
        float rs = sin(aa);
        vec3 q = vec3( x.x*rc-x.z*rs, x.y+ca, x.x*rs+x.z*rc );
        float dd = dot(q.yz,q.yz);
        if( q.x>0.0 && q.x<1.5 && dd<mindist2 ) mindist2=dd;
    }

    float c = sqrt(mindist2) - 0.045;
    float d = r-0.30;
    float a = clamp( r*3.0, 0.0, 1.0 );
    return c*a + d*(1.0-a);
}


float map( in vec3 pos, out int sid, out int submat )
{
    submat = 0;
    float dis, mindist;

    //-----------------------
    // suelo
    //-----------------------
    dis = pos.y;
    vec2 axz = vec2(128.0) + 6.0*vec2(pos.x+pos.z,pos.x-pos.z);
    ivec2 ixz = ivec2( floor(axz) );
    submat = icoolfFunc3d2(ixz.x+53*ixz.y);
    vec2 peldxz = fract( axz );
    float peld = smoothstep( 0.975, 1.0, max( peldxz.x, peldxz.y ) );
   
//    if( (((submat>>10)&7)>6) )
    if( submat>3 )
    {
        peld = 1.0;
    }
    dis += 0.005*peld;
    mindist = dis;
    sid = 0;
    if( peld>0.0000001 ) sid = 2;

    //-----------------------
    // techo
    //-----------------------
    float fx = fract( pos.x+128.0 );
    float fz = fract( pos.z+128.0 );
    if( pos.y>1.0 )
    {
        dis = max(techo(fx,pos.y),techo(fz,pos.y));
        if( dis<mindist )
        {
            mindist = dis;
            sid = 5;
        }
    }
    fx = fract( pos.x+128.0+.5 );
    fz = fract( pos.z+128.0+.5 );

    //-----------------------
    // columnas
    dis = columna( fx-.5, pos.y, fz-.5, mindist, 13.1*floor(pos.x)+17.7*floor(pos.z) );
    if( dis<(mindist*mindist) )
    {
        mindist = sqrt(dis);
        sid = 1;
    }

    //-----------------------
    // bicho
    //-----------------------

    dis = bicho( pos, mindist );
    if( dis<mindist )
    {
        mindist = dis;
        sid = 4;
    }
    //-----------------------

    return mindist;
}

vec3 calcNormal( in vec3 pos )
{
    float eps = 0.0002;
    vec3 nor;
    int kk, kk2;
    nor.x = map( vec3(pos.x+eps, pos.y, pos.z), kk, kk2 ) - map( vec3(pos.x-eps, pos.y, pos.z), kk, kk2 );
    nor.y = map( vec3(pos.x, pos.y+eps, pos.z), kk, kk2 ) - map( vec3(pos.x, pos.y-eps, pos.z), kk, kk2 );
    nor.z = map( vec3(pos.x, pos.y, pos.z+eps), kk, kk2 ) - map( vec3(pos.x, pos.y, pos.z-eps), kk, kk2 );
    return normalize( nor );
}



void main(void)
{
    vec2 pixel = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
    float an = time*0.15;
    vec2 sc = vec2(cos(an),sin(an));

    float r2 = pixel.x*pixel.x*0.32 + pixel.y*pixel.y;
    float tt = (7.0-sqrt(37.5-11.5*r2))/(r2+1.0);
    pixel *= tt;

    float asp = resolution.x/resolution.y;
    vec3 rd = normalize(vec3(asp*pixel.x*sc.x-sc.y,pixel.y,sc.x+asp*pixel.x*sc.y));
    vec3 ro = vec3(0.5+1.4*sc.y,0.5,1.5-1.4*sc.x);

    float t=0.5;
    int matID=666;
    int subMatID;
    vec3 pos;
    vec3 rgb = vec3(0.0);

    // cast ray
   int nt=0;
   // for( t=0.5; t<12.0; )
    for( int nt=0; nt<60; nt++)
    {
        pos = ro + t*rd;
        float h = map( pos, matID, subMatID );
        if( h<0.01 )
        break;
        t += h;
        if( t>12.0 )
        break;
    }
   

    // shade
    if( matID!=666 )
    {
        // calc normal
        vec3 nor = calcNormal(pos);


        // bump mapping
        float kke=0.0001;
        float bumpa=0.0075;
        if( matID!=5 ) bumpa*=0.75;
        if( matID==4 ) bumpa*=0.50;
        bumpa /= kke;
        float kk = fbm(32.0*pos);
        nor.x += bumpa*(fbm(32.0*vec3(pos.x+kke, pos.y, pos.z))-kk);
        nor.y += bumpa*(fbm(32.0*vec3(pos.x, pos.y+kke, pos.z))-kk);
        nor.z += bumpa*(fbm(32.0*vec3(pos.x, pos.y, pos.z+kke))-kk);
        nor = normalize(nor);

        // light
        float spe = 0.0;
        vec3 lig = vec3( 0.5-pos.x, 0.8-pos.y, 1.5-pos.z );
        float llig = dot(lig,lig);
        float im = inversesqrt(llig);
        lig = lig * im;
        float dif = dot(nor,lig);
        if( matID==4 )
            {dif=0.5+0.5*dif;}
        else
            {dif=0.1+0.9*dif;}
        //if( dif<0.0 ) dif=0.0;
        //dif=max(dif,0.0);
        dif = clamp(dif,0.0,1.0);
        dif *= 2.5*exp2(-1.75*llig);
        float dif2=(nor[0]+nor[1])*0.075;


        // materials
        if( matID==0 )
        {
        //float xoff = 13.1*float(subMatID&255);
            float xoff = 13.1*float(subMatID);
            float fb = fbm(16.0*vec3(pos.x+xoff,pos.y,pos.z));
            rgb = vec3(0.7) + fb*vec3(0.20,0.22,0.25);

           // float baldscale = float((subMatID>>9)&15)/14.0;
        float baldscale = float(subMatID)/14.0;
            baldscale = 0.51 + 0.34*baldscale;
            rgb *= baldscale;
            float fx = 1.0;
       
        //if( (subMatID&256)!=0 )
            if( (subMatID)!=0 )
            fx = -1.0;
       
            float m = sin( 64.0*pos.z*fx +  64.0*pos.x + 4.0*fb );
            m = smoothstep( 0.25, 0.5, m ) - smoothstep( 0.5, 0.75, m );
            rgb += m*vec3(0.15);
        }
        else if( matID==2 )
        {
            rgb = vec3(0.0);
        }
        else if( matID==1 )
        {
            float fb = fbm( 16.0*pos );
            float m = sin( 64.0*pos.z +  64.0*pos.x + 4.0*fb );
            m = smoothstep( 0.30, 0.5, m ) - smoothstep( 0.5, 0.70, m );
            rgb = vec3(0.59) + fb*vec3(0.17,0.18,0.21) + m*vec3(0.15) + vec3(dif2);
        }
        else if( matID==4 )
        {
            float ft = fbm( 16.0*pos );
            rgb = vec3(0.82,0.73,0.65) + ft*vec3(0.1);

            float fs = 0.9+0.1*fbm( 32.0*pos );
            rgb *= fs;

            float fre = max( -dot( nor, rd ), 0.0);
            rgb -= vec3(fre*fre*0.45);
            spe = clamp((nor.y-nor.z)*0.707,0.0,1.0);
            spe = 0.20*pow(spe,32.0);
        }
        // techo
        else //if( matID==5 )
        {
            float fb = fbm( 16.0*pos );
            rgb = vec3(0.64,0.61,0.59) + fb*vec3(0.21,0.19,0.19) + dif2;
        }
   
        // AO
        float ao;
        float totao = 0.0;
        float sca = 10.0;
        for( int aoi=0; aoi<5; aoi++ )
        {
            float hr = 0.01 + 0.015*float(aoi*aoi);
            vec3 aopos =  nor * hr + pos;
            int kk, kk2;
            float dd = map( aopos, kk, kk2 );
            ao = -(dd-hr);
            totao += ao*sca;
            sca *= 0.5;
        }
        ao = 1.0 - clamp( totao, 0.0, 1.0 );
   
        // shadow
        float so = 0.0;
        for( int i=0; i<6; i++ )
        {
            float h = float(i)/6.0;
            float hr = 0.01 + h;
            vec3 aopos = lig * hr + pos;
            int kk, kk2;
            float dd = map( aopos, kk, kk2 );
            so += (1.0-h)*dd*2.0 * (10.0/6.0);
        }
        dif *= clamp( (so-0.40)*1.5, 0.0, 1.0 );
   
        // lighting
        rgb = vec3(spe) + rgb * (ao*vec3(0.25,0.30,0.35) + dif*vec3(1.95,1.65,1.05));
        // fog
        rgb = rgb * exp2( -0.4*t );
    }

    // color correct
    rgb =(sqrt(rgb)*0.7+0.3*rgb)*vec3(0.83,1.0,0.83)*1.2;

    // vigneting
    rgb *= 0.25+0.75*clamp(0.60*abs(pixel.x-1.0)*abs(pixel.x+1.0),0.0,1.0);

    gl_FragColor=vec4(rgb,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)
glslsandbox_prog = moon3d.graphics.getGpuProgram("glslsandbox_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.bindPersp()

moon3d.graphics.bindGpuProgram(glslsandbox_prog)
moon3d.madshaders.updateGLSLSandboxCommonParams(glslsandbox_prog, global_time)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("GLSLSandbox/Slisesix", 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 05, 2015, 02:28:20 PM by Stefan »