Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

HD shaders

edited March 2014 in Shaders Posts: 437

I was searching for a fire shader, ...Ended up with these HD shaders but with 1FPS , so...
Take care!
http://http.developer.nvidia.com/GPUGems/gpugems_ch38.html
...

Main program to test shaders. (some includes touches)

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)

function setup()
    parameter.watch("1/DeltaTime")
    m = mesh()
    m.texture = image(1,1) --allTextures[Texture]
    m.shader = shader("Documents:FireG")
   -- m.shader = shader(getshader())
    rIdx = m:addRect(0, 0, 0, 0)
    m:setRect(rIdx, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    m.shader.iGlobalTime = ElapsedTime
    m.shader.iMouse = vec4(0,0,0,0)
    m.shader.iResolution = vec3(WIDTH,HEIGHT,0)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Here we set up the rect texture and size
    --m.texture = allTextures[Texture]
    --local cw,ch = spriteSize(allTextures[Texture])
    --m:setRect(rIdx, WIDTH/2, HEIGHT/2, cw, ch)

    -- Configure out custom uniforms for the ripple shader
    m.shader.iGlobalTime = ElapsedTime
    m.shader.iMouse = vec4(CurrentTouch.x, CurrentTouch.y,0,0)
    
    -- Draw the mesh
    m:draw()
end

function getshader()
    return [[
    uniform mat4 modelViewProjection;

//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;

//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;

void main()
{
    //Pass the mesh color to the fragment shader
    vColor = color;
    vTexCoord = texCoord;

    //Multiply the vertex position by our combined transform
    gl_Position = modelViewProjection * position;
}
    ]],
    [[
    uniform lowp vec3      iResolution;
    uniform lowp float iGlobalTime;
    uniform lowp vec4 iMouse;

    PASTE SHADER CODE HERE

    ]]
end

Now the shaders, first realistic fire shader, the slowest:

//
// A basic fragment shader
//

//Default precision qualifier
uniform highp vec3      iResolution;
uniform highp float iGlobalTime;
uniform highp vec4 iMouse;

precision highp float;


//This represents the current texture on the mesh
mat3 rotm;

mat3 rot(vec3 v, float angle)
{
    float c = cos(angle);
    float s = sin(angle);
    
    return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
        (1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
        (1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
        );
}


vec2 dragonkifs(vec3 p) {
    float otrap=1000.;
    float l=0.;
    for (int i=0; i<20; i++) {
        p.y*=-sign(p.x);
        p.x=abs(p.x);
        p=p*rotm*1.18-vec3(1.2);
        l=length(p);
        if (l>100.) otrap=0.;
        otrap=min(otrap,l);        
    }
    return vec2(l*pow(1.18,float(-20))*.15,clamp(otrap*.3,0.,1.));
}


void main(void)
{
    float time = iGlobalTime*.5;
    vec2 coord = gl_FragCoord.xy / iResolution.xy *2. - vec2(1.);
    coord.y *= iResolution.y / iResolution.x;
    vec3 from = 16.*normalize(vec3(sin(time),cos(time),.5+sin(time)));
    vec3 up=vec3(1,sin(time*2.)*.5,1);
    vec3 edir = normalize(-from);
    vec3 udir = normalize(up-dot(edir,up)*edir);
    vec3 rdir = normalize(cross(edir,udir));
    vec3 dir=normalize((coord.x*rdir+coord.y*udir)*.9+edir);
    vec3 col=vec3(0.);
    float dist=0.;
    rotm=rot(normalize(vec3(1.)),time*3.);
    for (int r=0; r<120; r++) {
        vec3 p=from+dist*dir;
        vec2 d=dragonkifs(p);
        dist+=max(0.02,abs(d.x));
        if (dist>20.) break;
        col+=vec3(1.+d.y*1.8,.5+d.y,d.y*.5)*.125*sign(d.y);
    }
    col=col*length(col)*0.001;
    gl_FragColor = vec4(col,1.0);    
    
}

Another fire shader, faster but not enought, it also can be used to create an ice fx changing the iResolution field in "y"

uniform highp vec3      iResolution;
uniform highp float iGlobalTime;
uniform highp vec4 iMouse;

precision highp float;
float rand(vec2 n) { 
    return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}

float noise(vec2 n) {
    const vec2 d = vec2(0.0, 1.0);
    vec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));
    return mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);
}

float fbm(vec2 n) {
    float total = 0.0, amplitude = 1.0;
    for (int i = 0; i < 7; i++) {
        total += noise(n) * amplitude;
        n += n;
        amplitude *= 0.5;
    }
    return total;
}

void main() {
    const vec3 c1 = vec3(0.1, 0.0, 0.0);
    const vec3 c2 = vec3(0.7, 0.0, 0.0);
    const vec3 c3 = vec3(0.2, 0.0, 0.0);
    const vec3 c4 = vec3(1.0, 0.9, 0.0);
    const vec3 c5 = vec3(0.1);
    const vec3 c6 = vec3(0.9);
    vec2 p = gl_FragCoord.xy * 8.0 / iResolution.xx;
    float q = fbm(p - iGlobalTime * 0.1);
    vec2 r = vec2(fbm(p + q + iGlobalTime * 0.7 - p.x - p.y), fbm(p + q - iGlobalTime * 0.4));
    vec3 c = mix(c1, c2, fbm(p + r)) + mix(c3, c4, r.x) - mix(c5, c6, r.y);
    gl_FragColor = vec4(c * cos(1.57 * gl_FragCoord.y / iResolution.y), 1.0);
}


Screenshots and videos
http://www.dropbox.com/sh/j9nlh9zschj2ku6/-h-WPRpkCF

Comments

  • edited August 2013 Posts: 437

    This shader is for a procedural grass, with more than 4 BLADES it turns so slow, <5 fps, you can even try xD

    uniform highp vec3      iResolution;
    uniform highp float iGlobalTime;
    uniform highp vec4 iMouse;
    #define BLADES 5
    precision highp float;
    vec3 rotateX(float a, lowp vec3 v)
    {
        return vec3(v.x, cos(a) * v.y + sin(a) * v.z, cos(a) * v.z - sin(a) * v.y);
    }
    
    vec3 rotateY(float a, vec3 v)
    {
        return vec3(cos(a) * v.x + sin(a) * v.z, v.y, cos(a) * v.z - sin(a) * v.x);
    }
    
    vec3 rotateZ(float a, vec3 v)
    {
        return vec3(cos(a) * v.x + sin(a) * v.y, cos(a) * v.y - sin(a) * v.x, v.z);
    }
    
    vec4 grass(vec2 p, float x)
    {
        float s = mix(0.7, 2.0, 0.5 + sin(x * 12.0) * 0.5);
        p.x += pow(1.0 + p.y, 2.0) * 0.1 * cos(x * 0.5 + iGlobalTime);
        p.x *= s;
        p.y = (1.0 + p.y) * s - 1.0;
        float m = 1.0 - smoothstep(0.0, clamp(1.0 - p.y * 1.5, 0.01, 0.6) * 0.2 * s, pow(abs(p.x) * 19.0, 1.5) + p.y - 0.6);
        return vec4(mix(vec3(0.05, 0.1, 0.0) * 0.8, vec3(0.0, 0.3, 0.0), (p.y + 1.0) * 0.5 + abs(p.x)), m * smoothstep(-1.0, -0.9, p.y));
    }
    
    vec3 backg(vec3 ro, vec3 rd)
    {
        float t = (-1.0 - ro.y) / rd.y;
        vec2 tc = ro.xz + rd.xz * t;
        vec3 horiz = vec3(0.0, 0.2, 0.2) * 0.7;
        vec3 sky = mix(horiz, vec3(0.1, 0.13, 0.15) * 0.8, dot(rd, vec3(0.0, 1.0, 0.0)));
        vec3 ground = mix(horiz, vec3(0.04, 0.07, 0.0) * 0.6, pow(max(0.0, dot(rd, vec3(0.0, -1.0, 0.0))), 0.2));
        return mix(sky, ground, step(0.0, t));
    }
    
    // some simple noise just to break up the hideous banding
    float dither()
    {
        return fract(gl_FragCoord.x * 0.482635532 + gl_FragCoord.y * 0.1353412 + iGlobalTime * 100.0) * 0.008;
    }
    
    void main()
    {
        vec3 ct = vec3(0.0, 1.0, 5.0);
        vec3 cp = rotateY(cos(iGlobalTime * 0.2) * 0.4, vec3(0.0, 0.6, 0.0));
        vec3 cw = normalize(cp - ct);
        vec3 cu = normalize(cross(cw, vec3(0.0, 1.0, 0.0)));
        vec3 cv = normalize(cross(cu, cw));
        
        mat3 rm = mat3(cu, cv, cw);
        
        vec2 uv = (gl_FragCoord.xy / iResolution.xy) * 2.0 - vec2(1.0);
        vec2 t = uv;
        t.x *= iResolution.x / iResolution.y;
        
        vec3 ro = cp, rd = rotateY(sin(iGlobalTime * 0.7) * 0.1,
                                   rm * rotateZ(sin(iGlobalTime * 0.15) * 0.1, vec3(t, -1.3)));
        
        vec3 fcol = backg(ro, rd);
        
        for(int i = 0; i < BLADES; i += 1)
        {
            float z = -(float(BLADES - i) * 0.1 + 1.0);
            vec4 pln = vec4(0.0, 0.0, -1.0, z);
            float t = (pln.w - dot(pln.xyz, ro)) / dot(pln.xyz, rd);
            vec2 tc = ro.xy + rd.xy * t;
            
            tc.x += cos(float(i) * 3.0) * 4.0;
            
            float cell = floor(tc.x);
            
            tc.x = (tc.x - cell) - 0.5;
            
            vec4 c = grass(tc, float(i) + cell * .0);
            
            fcol = mix(fcol, c.rgb, step(0.1, t) * c.w);
        }
        
        fcol = pow(fcol * 1.1, vec3(0.8));
        
        
        // iñigo quilez's great vigneting effect!
        //vec2 q = (uv + vec2(1.0)) * 0.5;
        //fcol *= 0.2 + 0.8*pow( 16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y), 0.1 );
        
        
        gl_FragColor.rgb = fcol + vec3(dither());
        gl_FragColor.a = 1.0;
    }
    
    
  • Posts: 437

    This is a Cloud generator, only iPad3+

        uniform lowp float     iGlobalTime; 
        uniform lowp vec4      iMouse;  
        uniform lowp vec3      iResolution; 
        precision highp float;
        // Created by inigo quilez - iq/2013
    // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
    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;
        return mix(mix(mix( hash(n+  0.0), hash(n+  1.0),f.x),
                       mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),
                   mix(mix( hash(n+113.0), hash(n+114.0),f.x),
                       mix( hash(n+170.0), hash(n+171.0),f.x),f.y),f.z);
    }
    
    
    vec4 map( in vec3 p )
    {
        float d = 0.2 - p.y;
    
        vec3 q = p - vec3(1.0,0.1,0.0)*iGlobalTime;
        float f;
        f  = 0.5000*noise( q ); q = q*2.02;
        f += 0.2500*noise( q ); q = q*2.03;
        f += 0.1250*noise( q ); q = q*2.01;
        f += 0.0625*noise( q );
    
        d += 3.0 * f;
    
        d = clamp( d, 0.0, 1.0 );
        
        vec4 res = vec4( d );
    
        res.xyz = mix( 1.15*vec3(1.0,0.95,0.8), vec3(0.7,0.7,0.7), res.x );
        
        return res;
    }
    
    
    vec3 sundir = vec3(-1.0,0.0,0.0);
    
    
    vec4 raymarch( in vec3 ro, in vec3 rd )
    {
        vec4 sum = vec4(0, 0, 0, 0);
    
        float t = 0.0;
        for(int i=0; i<44; i++)
        {
            vec3 pos = ro + t*rd;
            vec4 col = map( pos );
            
            #if 1
            float dif =  clamp((col.w - map(pos+0.3*sundir).w)/0.6, 0.0, 1.0 );
    
            vec3 lin = vec3(0.65,0.68,0.7)*1.35 + 0.45*vec3(0.7, 0.5, 0.3)*dif;
            col.xyz *= lin;
            #endif
            
            col.a *= 0.35;
            col.rgb *= col.a;
    
            sum = sum + col*(1.0 - sum.a);    
    
            //if (sum.a > 0.99) break;
            #if 0
            t += 0.1;
            #else
            t += max(0.1,0.05*t);
            #endif
        }
    
        sum.xyz /= (0.001+sum.w);
    
        return clamp( sum, 0.0, 1.0 );
    }
    
    void main(void)
    {
        vec2 q = gl_FragCoord.xy / iResolution.xy;
        vec2 p = -1.0 + 2.0*q;
        p.x *= iResolution.x/ iResolution.y;
        vec2 mo = -1.0 + 2.0*iMouse.xy / iResolution.xy;
        
        // camera
        vec3 ro = 4.0*normalize(vec3(cos(2.75-3.0*mo.x), 0.7+(mo.y+1.0), sin(2.75-3.0*mo.x)));
        vec3 ta = vec3(0.0, 1.0, 0.0);
        vec3 ww = normalize( ta - 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 );
    
        
        vec4 res = raymarch( ro, rd );
    
        float sun = clamp( dot(sundir,rd), 0.0, 1.0 );
        vec3 col = vec3(0.6,0.71,0.75) - rd.y*0.2*vec3(1.0,0.5,1.0) + 0.15*0.5;
        col += 0.2*vec3(1.0,.6,0.1)*pow( sun, 8.0 );
        col *= 0.95;
        col = mix( col, res.xyz, res.w );
        col += 0.1*vec3(1.0,0.4,0.2)*pow( sun, 3.0 );
            
       gl_FragColor = vec4( col, 1.0 );
    }
    
  • edited August 2013 Posts: 437

    And this is an space sky generator, also for iPad3+

    uniform lowp float     iGlobalTime;           // shader playback time (in seconds)
    //uniform samplerXX iChannel0..3;          // input channel. XX = 2D/Cube
    uniform lowp vec4  iDate;                 // (year, month, day, time in seconds)
    //Default precision qualifier
    precision highp float;
    /* Created by Nikita Miropolskiy, nikat/2013
     * Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
     * http://creativecommons.org/licenses/by-nc-nd/3.0/
     */
    
    #define PROCEDURAL_NOISE
    
    vec2 rotate2(vec2 p, float theta) {
        return vec2(
            p.x*cos(theta)-p.y*sin(theta),
            p.x*sin(theta)+p.y*cos(theta)
        );
    }
    
    
    /* discontinuous random vec2 */
    vec2 random2(vec2 c) {
        float j = 4906.0*sin(dot(c,vec2(169.7, 5.8)));
        vec2 r;
        r.x = fract(512.0*j);
        j *= .125;
        r.y = fract(512.0*j);
        return r-0.5;
    }
    
    //#ifdef PROCEDURAL_NOISE
    
    /* skew constants for 2d simplex functions */
    const float F2 =  0.3660254;
    const float G2 = -0.2113249;
    
    /* 2d simplex (tetrahedron) derivative noise*/
    float simplex2d(vec2 p) {
         vec2 s = floor(p + (p.x+p.y)*F2);
         vec2 x = p - s - (s.x+s.y)*G2;
         
         float e = step(0.0, x.x-x.y);
         vec2 i1 = vec2(e, 1.0-e);
              
         vec2 x1 = x - i1 - G2;
         vec2 x2 = x - 1.0 - 2.0*G2;
         
         vec3 w, d;
              
         w.x = dot(x, x);
         w.y = dot(x1, x1);
         w.z = dot(x2, x2);
              
         w = max(0.5 - w, 0.0);
         
         d.x = dot(random2(s + 0.0), x);
         d.y = dot(random2(s +  i1), x1);
         d.z = dot(random2(s + 1.0), x2);
         
         w *= w;
         w *= w;
         d *= w;
              
         return 0.5+dot(d, vec3(70.0));
    }
    
    
    
    /* random star parameters */
    vec4 randomStar(vec2 c) {
        vec4 r;
        r.xy = random2(c);
        r.zw = random2(c+0.5)+0.5;
        return r;
    }
    
    /* star density */
    float star_density(vec2 p) {
        p *= vec2(0.40, 0.20);
        p += simplex2d(p);
        return simplex2d(p)-0.2;
        
    }
    
    /* draw star in tile s fragment x */
    float stars2d_tile(vec2 s, vec2 x, float scale, float theta) {
        float density = star_density(s*3.5/scale);    
        vec4 star = randomStar(s);
        
        if (star.w*1.2 > density) {
            return 0.0;
        }
    
        float starMagnitude = 0.7 + star.z*2.0;
        float starBrightness = 4.0 - star.z*4.0;
        vec2 v = starMagnitude*rotate2(x - star.xy, -theta);
    
        /* bright star with beams */
        if (scale <= 8.0) {
            v*=2.0;
            return 4.0*max(0.0, 0.5-smoothstep(0.0, 1.6, pow(dot(v,v), 0.125)))                   
                     + max(0.0, 0.5-smoothstep(0.0, 1.0, pow(dot(v,v), 0.25)))
                     + max(0.0, 0.6-dot(abs(v), vec2(16.0, 1.0)))  // beam
                     + max(0.0, 0.6-dot(abs(v), vec2(1.0, 16.0))); // beam
        }
        
        /* cheap trick against aliasing */
        float pixels = min(1.0, 24.0/(scale*starMagnitude));
        v *= max(0.6, pixels);
        starBrightness *= pixels*pixels;
        
        float d = pow(dot(v,v), 0.25);
        return starBrightness*max(0.0, 0.5-smoothstep(0.0, 1.0, d));
    }
    
    
    /* 2d rectangle stars function with density */
    float stars2d(vec2 p, float scale, float theta) {
        p*=scale;
        vec2 s = floor(p);
        vec2 x = p - s;
        
        return 0.0
            +stars2d_tile(s + vec2(0.0, 0.0), x - vec2(0.0, 0.0), scale, theta)
            +stars2d_tile(s + vec2(1.0, 0.0), x - vec2(1.0, 0.0), scale, theta)
            +stars2d_tile(s + vec2(0.0, 1.0), x - vec2(0.0, 1.0), scale, theta)
            +stars2d_tile(s + vec2(1.0, 1.0), x - vec2(1.0, 1.0), scale, theta)
        ;
    }
    
    float sky(vec2 p, float theta) {
        p = rotate2(p, theta);
        return 0.0
            + stars2d(p, 2.0, theta) 
            //+ stars2d(p, 4.0, theta) 
            //+ stars2d(p, 8.0, theta) 
            //+ stars2d(p, 16.0, theta)
            + stars2d(p, 32.0, theta)
            + stars2d(p, 64.0, theta)
            + stars2d(p, 128.0, theta)
            + 0.2*star_density(p*3.5)
            + 0.15*simplex2d(p*128.0)
            ;
    }
    
    vec3 palette(float v) {
        vec3 c;
    
        c.b = v;
        c.g = smoothstep(0.0, 2.0, 2.0*c.b);
        c.r = smoothstep(0.0, 4.0, 4.0*c.g);
        
        return c;
    }
    
    void main(void)
    {
        vec2 p = (gl_FragCoord.xy + 300.0)/(400.0); //fixed pixels
        
        float theta = iGlobalTime*0.01;
        
        gl_FragColor = vec4(palette(sky(p, theta)), 1.0);
    }
    
    //This represents the current texture on the mesh
    //uniform lowp sampler2D texture;
    
    //The interpolated vertex color for this fragment
    //varying lowp vec4 vColor;
    
    //The interpolated texture coordinate for this fragment
    //varying highp vec2 vTexCoord;
    /*
    void main()
    {
        //Sample the texture at the interpolated coordinate
        lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;
    
        //Set the output color to the texture color
        if (max(col.r, max(col.g, col.b)) > 0.75)
        {
            gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
        }
        else
        {
            gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
        }
    }*/
    
  • edited August 2013 Posts: 437

    My favorite, a procedural sunset over the seas
    https://www.dropbox.com/sh/j9nlh9zschj2ku6/-h-WPRpkCF#lh:null-Vídeo 04-08-13 02 12 02.mp4

        uniform lowp float     iGlobalTime; 
        uniform lowp vec4      iMouse;  
        uniform lowp vec3      iResolution; 
       precision highp float;
    const bool USE_MOUSE = true; // Set this to true for God Mode :)
    
    const float PI = 3.14159265;
    const float MAX_RAYMARCH_DIST = 150.0;
    const float MIN_RAYMARCH_DELTA = 0.00015; 
    const float GRADIENT_DELTA = 0.015;
    float waveHeight1 = 0.005;
    float waveHeight2 = 0.004;
    float waveHeight3 = 0.001;
    vec2 mouse = vec2(iMouse.x / iResolution.x, iMouse.y / iResolution.y);
    
    // --------------------- START of SIMPLEX NOISE
    //
    // Description : Array and textureless GLSL 2D simplex noise function.
    //      Author : Ian McEwan, Ashima Arts.
    //  Maintainer : ijm
    //     Lastmod : 20110822 (ijm)
    //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
    //               Distributed under the MIT License. See LICENSE file.
    //               https://github.com/ashima/webgl-noise
    // 
    
    vec3 mod289(vec3 x) {
      return x - floor(x * (1.0 / 289.0)) * 289.0;
    }
    
    vec2 mod289(vec2 x) {
      return x - floor(x * (1.0 / 289.0)) * 289.0;
    }
    
    vec3 permute(vec3 x) {
      return mod289(((x*34.0)+1.0)*x);
    }
    
    float snoise(vec2 v)
      {
      const vec4 C = vec4(0.211324865405187,  // (3.0-sqrt(3.0))/6.0
                          0.366025403784439,  // 0.5*(sqrt(3.0)-1.0)
                         -0.577350269189626,  // -1.0 + 2.0 * C.x
                          0.024390243902439); // 1.0 / 41.0
    // First corner
      vec2 i  = floor(v + dot(v, C.yy) );
      vec2 x0 = v -   i + dot(i, C.xx);
    
    // Other corners
      vec2 i1;
      //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
      //i1.y = 1.0 - i1.x;
      i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
      // x0 = x0 - 0.0 + 0.0 * C.xx ;
      // x1 = x0 - i1 + 1.0 * C.xx ;
      // x2 = x0 - 1.0 + 2.0 * C.xx ;
      vec4 x12 = x0.xyxy + C.xxzz;
      x12.xy -= i1;
    
    // Permutations
      i = mod289(i); // Avoid truncation effects in permutation
      vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
            + i.x + vec3(0.0, i1.x, 1.0 ));
    
      vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
      m = m*m ;
      m = m*m ;
    
    // Gradients: 41 points uniformly over a line, mapped onto a diamond.
    // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
    
      vec3 x = 2.0 * fract(p * C.www) - 1.0;
      vec3 h = abs(x) - 0.5;
      vec3 ox = floor(x + 0.5);
      vec3 a0 = x - ox;
    
    // Normalise gradients implicitly by scaling m
    // Approximation of: m *= inversesqrt( a0*a0 + h*h );
      m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
    
    // Compute final noise value at P
      vec3 g;
      g.x  = a0.x  * x0.x  + h.x  * x0.y;
      g.yz = a0.yz * x12.xz + h.yz * x12.yw;
      return 130.0 * dot(m, g);
    }
    
    // --------------------- END of SIMPLEX NOISE
    
    
    float map(vec3 p) {
        return p.y + (0.5 + waveHeight1 + waveHeight2 + waveHeight3) 
            + snoise(vec2(p.x + iGlobalTime * 0.4, p.z + iGlobalTime * 0.6)) * waveHeight1
            + snoise(vec2(p.x * 1.6 - iGlobalTime * 0.4, p.z * 1.7 - iGlobalTime * 0.6)) * waveHeight2
              + snoise(vec2(p.x * 6.6 - iGlobalTime * 1.0, p.z * 2.7 + iGlobalTime * 1.176)) * waveHeight3;
    }
    
    vec3 gradientNormalFast(vec3 p, float map_p) {
        return normalize(vec3(
            map_p - map(p - vec3(GRADIENT_DELTA, 0, 0)),
            map_p - map(p - vec3(0, GRADIENT_DELTA, 0)),
            map_p - map(p - vec3(0, 0, GRADIENT_DELTA))));
    }
    
    float intersect(vec3 p, vec3 ray_dir, out float map_p, out int iterations) {
        iterations = 0;
        if (ray_dir.y >= 0.0) { return -1.0; } // to see the sea you have to look down
        
        float distMin = (- 0.5 - p.y) / ray_dir.y;
        float distMid = distMin;
        for (int i = 0; i < 50; i++) {
            //iterations++;
            distMid += max(0.05 + float(i) * 0.002, map_p);
            map_p = map(p + ray_dir * distMid);
            if (map_p > 0.0) { 
                distMin = distMid + map_p;
            } else { 
                float distMax = distMid + map_p;
                // interval found, now bisect inside it
                for (int i = 0; i < 10; i++) {
                    //iterations++;
                    distMid = distMin + (distMax - distMin) / 2.0;
                    map_p = map(p + ray_dir * distMid);
                    if (abs(map_p) < MIN_RAYMARCH_DELTA) return distMid;
                    if (map_p > 0.0) {
                        distMin = distMid + map_p;
                    } else {
                        distMax = distMid + map_p;
                    }
                }
                return distMid;
            }
        }
        return distMin;
    }
    
    void main( void ) {
        float waveHeight = USE_MOUSE ? mouse.x * 5.0 : cos(iGlobalTime * 0.03) * 1.2 + 1.6;
        waveHeight1 *= waveHeight;
        waveHeight2 *= waveHeight;
        waveHeight3 *= waveHeight;
        
        vec2 position = vec2((gl_FragCoord.x - iResolution.x / 2.0) / iResolution.y, (gl_FragCoord.y - iResolution.y / 2.0) / iResolution.y);
        vec3 ray_start = vec3(0, 0.2, -2);
        vec3 ray_dir = normalize(vec3(position,0) - ray_start);
        ray_start.y = cos(iGlobalTime * 0.5) * 0.2 - 0.25 + sin(iGlobalTime * 2.0) * 0.05;
        
        const float dayspeed = 0.04;
        float subtime = max(-0.16, sin(iGlobalTime * dayspeed) * 0.2);
        float middayperc = USE_MOUSE ? mouse.y * 0.3 - 0.15 : max(0.0, sin(subtime));
        vec3 light1_pos = vec3(0.0, middayperc * 200.0, USE_MOUSE ? 200.0 : cos(subtime * dayspeed) * 200.0);
        float sunperc = pow(max(0.0, min(dot(ray_dir, normalize(light1_pos)), 1.0)), 190.0 + max(0.0,light1_pos.y * 4.3));
        vec3 suncolor = (1.0 - max(0.0, middayperc)) * vec3(1.5, 1.2, middayperc + 0.5) + max(0.0, middayperc) * vec3(1.0, 1.0, 1.0) * 4.0;
        vec3 skycolor = vec3(middayperc + 0.8, middayperc + 0.7, middayperc + 0.5);
        vec3 skycolor_now = suncolor * sunperc + (skycolor * (middayperc * 1.6 + 0.5)) * (1.0 - sunperc);
        vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
        float map_p;
        int iterations;
        float dist = intersect(ray_start, ray_dir, map_p, iterations);
        if (dist > 0.0) {
            vec3 p = ray_start + ray_dir * dist;
            vec3 light1_dir = normalize(light1_pos - p);
                vec3 n = gradientNormalFast(p, map_p);
            vec3 ambient = skycolor_now * 0.1;
                vec3 diffuse1 = vec3(1.1, 1.1, 0.6) * max(0.0, dot(light1_dir, n)  * 2.8);
            vec3 r = reflect(light1_dir, n);
            vec3 specular1 = vec3(1.5, 1.2, 0.6) * (0.8 * pow(max(0.0, dot(r, ray_dir)), 200.0));        
            float fog = min(max(p.z * 0.07, 0.0), 1.0);
                color.rgb = (vec3(0.6,0.6,1.0) * diffuse1 + specular1 + ambient)  * (1.0 - fog) + skycolor_now * fog;
            } else {
                color.rgb = skycolor_now.rgb;
            }
        gl_FragColor = color;
    }
    
    

    For iPad2 +

  • Jmv38Jmv38 Mod
    Posts: 3,295

    Why is it ipad2+ or 3+ only?
    How can translate this ti ipad1?
    Thanks

  • Posts: 437

    Ah, efficience issues, @jmv38 , you can try in an ipad1 but probably it will run out of mem and cpu overload

  • How can I use these? Like, where do I paste the code?

  • IgnatzIgnatz Mod
    edited March 2014 Posts: 5,396

    @Monkeyman32123 - start with the code at the top of the first post above, then paste one of the shader code chunks into the spot where it says PASTE SHADER CODE HERE

  • I literally didn't even see that there, I read through code looking for something like that, and just didn't see. I feel horrendously dumb right now >_<

  • IgnatzIgnatz Mod
    Posts: 5,396

    @Monkeyman32123 - actually, I just edited that post to include "PASTE SHADER CODE HERE" a few minutes ago, to make it easier for you

  • Oh...well, thanks :p
    Now I definitely feel a lot less dumb.

  • edited December 2014 Posts: 28

    @Ignatz
    well i just tried to copy and passte this and it did not do anything

    displayMode(FULLSCREEN)
    
    function setup()
        parameter.watch("1/DeltaTime")
        m = mesh()
        m.texture = image(1,1) --allTextures[Texture]
        m.shader = shader("Documents:FireG")
       -- m.shader = shader(getshader())
        rIdx = m:addRect(0, 0, 0, 0)
        m:setRect(rIdx, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
        m.shader.iGlobalTime = ElapsedTime
        m.shader.iMouse = vec4(0,0,0,0)
        m.shader.iResolution = vec3(WIDTH,HEIGHT,0)
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color
        background(40, 40, 50)
    
        -- This sets the line thickness
        strokeWidth(5)
    
        -- Here we set up the rect texture and size
        --m.texture = allTextures[Texture]
        --local cw,ch = spriteSize(allTextures[Texture])
        --m:setRect(rIdx, WIDTH/2, HEIGHT/2, cw, ch)
    
        -- Configure out custom uniforms for the ripple shader
        m.shader.iGlobalTime = ElapsedTime
        m.shader.iMouse = vec4(CurrentTouch.x, CurrentTouch.y,0,0)
    
        -- Draw the mesh
        m:draw()
    end
    
    function getshader()
        return [[
        uniform mat4 modelViewProjection;
    
    //This is the current mesh vertex position, color and tex coord
    // Set automatically
    attribute vec4 position;
    attribute vec4 color;
    attribute vec2 texCoord;
    
    //This is an output variable that will be passed to the fragment shader
    varying lowp vec4 vColor;
    varying highp vec2 vTexCoord;
    
    void main()
    {
        //Pass the mesh color to the fragment shader
        vColor = color;
        vTexCoord = texCoord;
    
        //Multiply the vertex position by our combined transform
        gl_Position = modelViewProjection * position;
    }
        ]],
        [[
        uniform lowp vec3      iResolution;
        uniform lowp float iGlobalTime;
        uniform lowp vec4 iMouse;
              PASTE CODE HERE 
    
    
    
    
    
    
    
            //
    // A basic fragment shader
    //
    
    //Default precision qualifier
    uniform highp vec3      iResolution;
    uniform highp float iGlobalTime;
    uniform highp vec4 iMouse;
    
    precision highp float;
    
    
    //This represents the current texture on the mesh
    mat3 rotm;
    
    mat3 rot(vec3 v, float angle)
    {
        float c = cos(angle);
        float s = sin(angle);
    
        return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y,
            (1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x,
            (1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z
            );
    }
    
    
    vec2 dragonkifs(vec3 p) {
        float otrap=1000.;
        float l=0.;
        for (int i=0; i<20; i++) {
            p.y*=-sign(p.x);
            p.x=abs(p.x);
            p=p*rotm*1.18-vec3(1.2);
            l=length(p);
            if (l>100.) otrap=0.;
            otrap=min(otrap,l);        
        }
        return vec2(l*pow(1.18,float(-20))*.15,clamp(otrap*.3,0.,1.));
    }
    
    
    void main(void)
    {
        float time = iGlobalTime*.5;
        vec2 coord = gl_FragCoord.xy / iResolution.xy *2. - vec2(1.);
        coord.y *= iResolution.y / iResolution.x;
        vec3 from = 16.*normalize(vec3(sin(time),cos(time),.5+sin(time)));
        vec3 up=vec3(1,sin(time*2.)*.5,1);
        vec3 edir = normalize(-from);
        vec3 udir = normalize(up-dot(edir,up)*edir);
        vec3 rdir = normalize(cross(edir,udir));
        vec3 dir=normalize((coord.x*rdir+coord.y*udir)*.9+edir);
        vec3 col=vec3(0.);
        float dist=0.;
        rotm=rot(normalize(vec3(1.)),time*3.);
        for (int r=0; r<120; r++) {
            vec3 p=from+dist*dir;
            vec2 d=dragonkifs(p);
            dist+=max(0.02,abs(d.x));
            if (dist>20.) break;
            col+=vec3(1.+d.y*1.8,.5+d.y,d.y*.5)*.125*sign(d.y);
        }
        col=col*length(col)*0.001;
        gl_FragColor = vec4(col,1.0);    
    
    }
    
    
    
    
    
    
    
        ]]
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    @Ray_Spahn - why are you asking me? That's not my code, is it?

    PS no offence meant, but do you understand what shaders do? If not, I suggest you learn about them before trying something like this.

Sign In or Register to comment.