Recent Posts

Pages: 1 ... 8 9 [10]
91
English forum / Audrey converted
« Last post by Stefan on June 06, 2015, 01:09:09 PM »
Audrey converted to GLSL Hacker format

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

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

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

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

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



//

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

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

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

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

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

    float col = 0.0;

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

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


//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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

92
English forum / Schrödinger's cat converted
« Last post by Stefan on June 06, 2015, 01:02:58 PM »
Schrödinger's cat converted to GLSL Hacker format.

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

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

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

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

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

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



//

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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

93
English forum / Raymarching primitives converted
« Last post by Stefan on June 06, 2015, 12:53:05 PM »
Raymarching primitives converted to GLSL Hacker format

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

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

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

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

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



//

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return res;
}

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

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


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

}

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

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




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

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

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

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

    }

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

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

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

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

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

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

    gl_FragColor=vec4( col, 1.0 );
}

//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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

94
English forum / Pyroclastic explosion converted
« Last post by Stefan on June 06, 2015, 12:49:09 PM »
Pyroclastic explosion converted to GLSL Hacker format

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

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

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

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

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



//

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

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

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

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

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

// iq's nice integer-less noise function

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

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


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

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

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

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

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


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

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

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

    return normalize(n);
}

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

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

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

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


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

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

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

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

    gl_FragColor = col;
}


//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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

95
English forum / The Cave converted
« Last post by Stefan on June 06, 2015, 12:46:54 PM »
The Cave converted to GLSL Hacker format

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

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

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

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

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



//

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

moon3d.madshaders.resetBenchmarkData()

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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

96
3D-Tech News Around The Web / gShaderReplacer 1.2
« Last post by Stefan on June 05, 2015, 05:51:02 PM »
gShaderReplacer - OpenGL proxy dll for shader replacement and debugging.

Copyright 2009-2015 Henrik Münther - psycho@loonies.dk

WHY
===
-Use the included config to run stuff that does not work otherwise.
-Fix non-working stuff without help from the authors (and maybe tell him
 what to fix afterwards).
-Easy debugging of your own stuff on different hardware, without the need
 for dev-tools.
-Analyzing/optimizing of released stuff.
-Peek in other ppl's shader sources ;) (sorry, could not find a reasonable way
 to avoid that while keeping main functionality).

HOW
===
Place the opengl32.dll in the same directory as your executable and do the
neccesary changes to gshaderreplacer.xml.
Depending on configuration the opengl will dump and/or replace all shaders
sent to OpenGL from executables in the directory.

BUT HOW?
========
The included gshaderreplacer.xml should be more or less self explaining.
For the general configuration you can tell it to dump shaders to disk or not
(no need to dump for "user" mode) and make global replacers. Replacers can also
be restricted to a specific .exe file, by putting it into an Application block.
There is no pattern matching for executables, but several names can be written,
like: <Application Name="intro.exe" Name0="intro_safe.exe" Name1="intro_low.exe">
etc.
Replacers can be either string (inlined in .xml file) or file (seperate file)
based - FileReplacers are useful for debugging but final fixes should be a couple
of StringReplacers.
Replacers can be restricted to a certain graphics card vendor, by using the
Vendor=ATI or Vendor=NVIDIA attribute. So if you are making a workaround for a
specific vendor, you should use this, but if you are just making the code
glsl-conformant (typically fixing something the nvidia compiler happily
compiles) there is no reason for a vendor attribute.

UPDATES
=======
I hope we can extend the default gshaderreplacer.xml to fix (or optimize) more
and more intros, so please send in additions to the file and I will update the
archive. Luckily the file is not too long at the moment, as most people fix the
errors when told how :)

PROBLEMS?
=========
For speed reasons it's not doing init code for every opengl function, so if
someone (incorrectly) hits a normal gl function before the context is set up,
it will crash. However, that's pretty easy to fix from the crash message,
where the  illegal jump adress tells which function that needs the init-code.
It intercepts shader source calls for the ARB extension and GL2.0.

HISTORY
=======
1.0 First public release.
1.0.1 Few fixes, more shaders
1.1 Support for glCreateShaderProgramv
1.2 Fixed a buffer overrun, more shaders
97
3D-Tech News Around The Web / Steam Machines
« Last post by JeGX on June 05, 2015, 03:59:41 PM »
Quote
Syber’s Steam Machine series gives gamers more power and more customization than standard video game consoles. Customers can start with the Steam Machine I, which features an Intel Core i3-4160, NVIDIA GeForce GTX 750 1GB, 4GB RAM and 500GB HDD with a starting price of $499.  The advanced Steam Machine X features an Intel Core i7-4790K CPU, NVIDIA GeForce GTX 980 graphics, 16GBs RAM and 1TB HDD at $1,419.

Links:

- http://anandtech.com/show/9341/steam-machines-steam-link-steam-controller-launching-november-10th
- http://www.sybergaming.com/products/steam-machine.aspx
98
English forum / Hypertexture trabeculum converted
« Last post by Stefan on June 05, 2015, 01:30:53 PM »
Hypertexture trabeculum converted to GLSL Hacker format

edit: code removed

99
English forum / Slisesix converted
« Last post by Stefan on June 05, 2015, 01: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>

100
3D-Tech News Around The Web / AMD BIOS and Kernel Developer’s Guide June 2015
« Last post by Stefan on June 05, 2015, 09:47:40 AM »
BIOS and Kernel Developer’s Guide (BKDG) for AMD Family 15h Models 60h-6Fh Processors

CZ BKDG Revision 3.00 Changes, June 1, 2015, Initial Public release
Pages: 1 ... 8 9 [10]