Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Stefan

Pages: 1 2 3 [4] 5 6 ... 157
61
English forum / Trampoline converted
« on: June 07, 2015, 07:23:52 AM »
Trampoline converted to GLSL Hacker format

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

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

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

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

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

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

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

//

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

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

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

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

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

  return d;
}

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

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

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

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

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

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

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

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

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

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

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

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

  return color;
}

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

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

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

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

}

//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

moon3d.graphics.draw(quad)

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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


62
English forum / Clover tree converted
« on: June 07, 2015, 06:43:33 AM »
Clover tree converted to GLSL Hacker format.

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

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

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

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

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

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

//

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

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

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

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

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

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

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

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

    return vec4(d1, ma, uv );
   
}


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

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

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

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


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

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

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

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

//

]]></raw_data_ps>

</gpu_program>

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

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

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

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

winW, winH = moon3d.window.getSize()

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

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

        ]]></raw_data>
    </script>

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

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

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

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

moon3d.graphics.draw(quad)

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

moon3d.endFrame()

        ]]></raw_data>
    </script>
   

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

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

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


63
English forum / Audrey converted
« 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>


64
English forum / Schrödinger's cat converted
« 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>


65
English forum / Raymarching primitives converted
« 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>


66
English forum / Pyroclastic explosion converted
« 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>


67
English forum / The Cave converted
« 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>


68
3D-Tech News Around The Web / gShaderReplacer 1.2
« 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

69
English forum / Hypertexture trabeculum converted
« on: June 05, 2015, 01:30:53 PM »
Hypertexture trabeculum converted to GLSL Hacker format

edit: code removed


70
English forum / Slisesix converted
« 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>


71
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

72
3D-Tech News Around The Web / Intel Graphics driver v4229 for SNB/IVB
« on: June 05, 2015, 09:36:53 AM »
Fresh drivers for your "old" iGPUs

Download from MS update:

64bit

32bit

Quote
DriverVer=05/26/2015,9.17.10.4229
 %iSNBGM1%  = iSNBM0, PCI\VEN_8086&DEV_0106
 %iSNBGD1%  = iSNBD0, PCI\VEN_8086&DEV_0102
 %iSNBGM2%  = iSNBM0, PCI\VEN_8086&DEV_0116
 %iSNBGD2%  = iSNBD0, PCI\VEN_8086&DEV_0112
 %iSNBGM2P%  = iSNBM0, PCI\VEN_8086&DEV_0126
 %iSNBGD2P% = iSNBD0, PCI\VEN_8086&DEV_0122
 %iSNBGD3%  = iSNBD0, PCI\VEN_8086&DEV_010A
 %iIVBGD0%       = iIVBD0, PCI\VEN_8086&DEV_0162
 %iIVBGM0%       = iIVBM0, PCI\VEN_8086&DEV_0166
 %iIVBGD0SRV%    = iIVBD0, PCI\VEN_8086&DEV_016A
 %iIVBGD0GT1%    = iIVBD0, PCI\VEN_8086&DEV_0152
 %iIVBGM0GT1%    = iIVBM0, PCI\VEN_8086&DEV_0156
 %iIVBGD0SRVGT1% = iIVBD0, PCI\VEN_8086&DEV_015A

73
  Are You Running Out of Video Memory? Detecting Video-Memory Overcommitment using GPUView          

By Louis Bavoil,  Tags : GameWorks, GameWorks Expert Developer

While developing and playing PC games on Windows/WDDM, it is common for stuttering (uneven frame times) to start happening when enabling graphics features or increasing the screen resolution. There are a lot of possible root causes for stuttering; one of the most common is video-memory overcommitment which happens when an application is using more video memory than is physically available on the GPU. In this article, I will describe a method we have been using at NVIDIA to determine whether video-memory overcommittement is happening and is causing any stuttering on Windows Vista, 7, 8 or 8.1. (The method described in this article may not apply to Windows 10/WDDMv2, which has a different memory residency model.)

74
English forum / Rotating Structure converted
« on: June 04, 2015, 01: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>




75
English forum / CeramicGlassMosaic converted
« on: June 04, 2015, 09: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>


76
English forum / Piggy converted
« on: June 04, 2015, 09: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.


77
English forum / Re: Nano Tubes converted
« on: June 03, 2015, 10:04:56 PM »
Don't use Notepad, but Notepad++

Otherwise download from here

78
AMD Catalyst™ 15.5 Proprietary Linux x86 Display Driver

Linux Distributions Supported

This version of the AMD Catalyst™ Linux Proprietary Graphics Driver is designed to support the following Linux distributions:
Distributions supported:
    • Red Hat Enterprise Linux Suite 7.0, 6.5, 6.4
    • Ubuntu 12.04.4 LTS, 14.04 LTS
    • SUSE® Linux Enterprise 11 SP3, 12
    • OpenSuSE 13.1
Distribution specific packages are available for:
    • Red Hat Enterprise Linux Suite 7.0, 6.5
    • Ubuntu 12.04.4 LTS, 14.04 LTS
New Features
    • Support for SUSE® Linux Enterprise Desktop 12
Resolved Issues
    • [417630]: Fixes the issue of discrete GPU not being powered off in Power-Saving mode on some PowerXpress AMD GPU + AMD APU platforms
    • [416499]: Fixes minor screen corruption when resuming from S3 caused by display hot plugging
Known Issues
    • [419960]: Vari-Bright on some configurations is not decreasing brightness as expected

79
English forum / Nano Tubes converted
« on: June 03, 2015, 04:36:01 PM »
Nano Tubes converted to GLSL Hacker format

Copy the code and save as nano_tubes_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/Nano Tubes"
          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/lslGRH

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;



//

// With tweaks from fernlightning

float rand(vec3 n) {
  n = floor(n);
  return fract(sin((n.x+n.y*1e2+n.z*1e4)*1e-4)*1e5);
}

// .x is distance, .y = colour
vec2 map( vec3 p ) {
    const float RADIUS = 0.25;

    // cylinder
    vec3 f = fract( p ) - 0.5;
    float d = length( f.xy );
        float cr = rand( p );
    float cd = d - cr*RADIUS;

    // end - calc (rand) radius at more stable pos
    p.z -= 0.5;
    float rr = rand( p );
    float rn = d - rr*RADIUS;
    float rm = abs( fract( p.z ) - 0.5 );  // offset so at end of cylinder
       
    float rd = sqrt( rn*rn + rm*rm ); // end with ring

    return (cd < rd) ?  vec2( cd, cr ) : vec2( rd, rr ); // min
}

void main()
{
    vec2 pos = (gl_FragCoord.xy*2.0 - iResolution.xy) / iResolution.y;
    vec3 camPos = vec3(cos(iGlobalTime*0.3), sin(iGlobalTime*0.3), 3.5);
    vec3 camTarget = vec3(0.0, 0.0, .0);

    vec3 camDir = normalize(camTarget-camPos);
    vec3 camUp  = normalize(vec3(0.0, 1.0, 0.0));
    vec3 camSide = cross(camDir, camUp);
    float focus = 1.8;

    vec3 rayDir = normalize(camSide*pos.x + camUp*pos.y + camDir*focus);
    vec3 ray = camPos;
    float m = 0.32;
    vec2 d;
    float total_d = 0.;
    const int MAX_MARCH = 100;
    const float MAX_DISTANCE = 100.0;
    for(int i=0; i<MAX_MARCH; ++i) {
        d = map(ray-vec3(0.,0.,iGlobalTime/2.));
        total_d += d.x;
        ray += rayDir * d.x;
        m += 1.0;
        if(abs(d.x)<0.01) { break; }
        if(total_d>MAX_DISTANCE) { total_d=MAX_DISTANCE; break; }
    }

    float c = (total_d)*0.0001;
    vec4 result = vec4( 1.0-vec3(c, c, c) - vec3(0.025, 0.025, 0.02)*m*0.8, 1.0 );
    gl_FragColor = result*d.y;
}


//

]]></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/Nano Tubes", 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>


80
English forum / Doom 2 shader converted
« on: June 03, 2015, 04: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>


Pages: 1 2 3 [4] 5 6 ... 157