Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Using GLSL shaders

$
0
0
Hi all!

thanks for your examples and explanations!

The source code above doesn't work anymore with Processing 2.0, as the PShader specifications have changed a bit (for instance, PShader.FLAT doesn't exist anymore).

I've corrected it in order to work in version 2.0, this should be the new code:

  1. PShader nebula;
  2. PGraphics pg;
  3. void setup() {
  4.  
  5.   size(1024, 768, P3D);
  6.   pg = createGraphics(256, 196, P3D);
  7.  
  8.   nebula = loadShader("nebula.glsl");
  9.   nebula.set("resolution", float(256), float(196));
  10.  
  11.   pg.shader(nebula);
  12.  
  13. }
  14. void draw() {
  15.  
  16.   nebula.set("time", millis() / 1000.0);
  17.   // The rect is needed to make the fragment shader go through every pixel of
  18.   // the screen, but is not used for anything else since the rendering is entirely
  19.   // generated by the shader.
  20.  
  21.   pg.beginDraw();
  22.   pg.noStroke();
  23.   pg.fill(0);
  24.   pg.rect(0, 0, 256, 196);
  25.   pg.endDraw();
  26.   image(pg, 0, 0, 1024, 768);
  27.   println (frameRate);
  28.  
  29. }

and also the nebula.glsl file should be updated by adding the corresponding #define command:
  1.     #ifdef GL_ES
  2.     precision mediump float;
  3.     #endif
  4.     #define PROCESSING_COLOR_SHADER

  5.     uniform float time;
  6.     uniform vec2 resolution;

  7.     // NEBULA - CoffeeBreakStudios.com (CBS)
  8.     // Work in progress...
  9.     //
  10.     // 3148.26: Switched from classic to simplex noise
  11.     // 3148.27: Reduced number of stars
  12.     // 3249.0:  Switched to fast computed 3D noise. Less quality but ~ 2x faster
  13.     // 3249.5:  Removed use of random number generator to gain performance
  14.     // 3265.0:  Added rotation: glsl.heroku.com/e#3005.1

  15.     //Utility functions

  16.     vec3 fade(vec3 t) {
  17.       return vec3(1.0,1.0,1.0);//t*t*t*(t*(t*6.0-15.0)+10.0);
  18.     }
  19.     vec2 rotate(vec2 point, float rads) {
  20.     float cs = cos(rads);
  21.     float sn = sin(rads);
  22.     return point * mat2(cs, -sn, sn, cs);
  23.     }
  24.     vec4 randomizer4(const vec4 x)
  25.     {
  26.         vec4 z = mod(x, vec4(5612.0));
  27.         z = mod(z, vec4(3.1415927 * 2.0));
  28.         return(fract(cos(z) * vec4(56812.5453)));
  29.     }
  30.     // Fast computed noise
  31.     // http://www.gamedev.net/topic/502913-fast-computed-noise/
  32.     const float A = 1.0;
  33.     const float B = 57.0;
  34.     const float C = 113.0;
  35.     const vec3 ABC = vec3(A, B, C);
  36.     const vec4 A3 = vec4(0, B, C, C+B);
  37.     const vec4 A4 = vec4(A, A+B, C+A, C+A+B);
  38.     float cnoise4(const in vec3 xx)
  39.     {
  40.         vec3 x = mod(xx + 32768.0, 65536.0);
  41.         vec3 ix = floor(x);
  42.         vec3 fx = fract(x);
  43.         vec3 wx = fx*fx*(3.0-2.0*fx);
  44.         float nn = dot(ix, ABC);
  45.         vec4 N1 = nn + A3;
  46.         vec4 N2 = nn + A4;
  47.         vec4 R1 = randomizer4(N1);
  48.         vec4 R2 = randomizer4(N2);
  49.         vec4 R = mix(R1, R2, wx.x);
  50.         float re = mix(mix(R.x, R.y, wx.y), mix(R.z, R.w, wx.y), wx.z);
  51.         return 1.0 - 2.0 * re;
  52.     }
  53.     float surface3 ( vec3 coord, float frequency ) {
  54.     float n = 0.0;
  55.     n += 1.0 * abs( cnoise4( coord * frequency ) );
  56.     n += 0.5 * abs( cnoise4( coord * frequency * 2.0 ) );
  57.     n += 0.25 * abs( cnoise4( coord * frequency * 4.0 ) );
  58.     n += 0.125 * abs( cnoise4( coord * frequency * 8.0 ) );
  59.     n += 0.0625 * abs( cnoise4( coord * frequency * 16.0 ) );
  60.     return n;
  61.     }
  62.     void main( void ) {
  63.     float rads = radians(time*3.15);
  64.     vec2 position = gl_FragCoord.xy / resolution.xy;
  65.     position += rotate(position, rads);
  66.     float n = surface3(vec3(position*sin(time*0.1), time * 0.05)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.9);
  67.     float n2 = surface3(vec3(position*cos(time*0.1), time * 0.04)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.8);
  68.         float lum = length(n);
  69.         float lum2 = length(n2);
  70.     vec3 tc = pow(vec3(1.0-lum),vec3(sin(position.x)+cos(time)+4.0,8.0+sin(time)+4.0,8.0));
  71.     vec3 tc2 = pow(vec3(1.1-lum2),vec3(5.0,position.y+cos(time)+7.0,sin(position.x)+sin(time)+2.0));
  72.     vec3 curr_color = (tc*0.8) + (tc2*0.5);
  73.     //Let's draw some stars
  74.     float scale = sin(0.3 * time) + 5.0;
  75.     vec2 position2 = (((gl_FragCoord.xy / resolution) - 0.5) * scale);
  76.     float gradient = 0.0;
  77.     vec3 color = vec3(0.0);
  78.     float fade = 0.0;
  79.     float z = 0.0;
  80.       vec2 centered_coord = position2 - vec2(sin(time*0.1),sin(time*0.1));
  81.     centered_coord = rotate(centered_coord, rads);
  82.     for (float i=1.0; i<=60.0; i++)
  83.     {
  84.     vec2 star_pos = vec2(sin(i) * 250.0, sin(i*i*i) * 250.0);
  85.     float z = mod(i*i - 10.0*time, 256.0);
  86.     float fade = (256.0 - z) /256.0;
  87.     vec2 blob_coord = star_pos / z;
  88.     gradient += ((fade / 384.0) / pow(length(centered_coord - blob_coord), 1.5)) * ( fade);
  89.     }
  90.     curr_color += gradient;
  91.     gl_FragColor = vec4(curr_color, 1.0);
  92.     }






Viewing all articles
Browse latest Browse all 1768

Trending Articles