Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Wednesday 20 February 2013

Shader API changed in latest processing version at GitHub

Looks as there will a simpler (well at least abbreviated) shader language in processing. This is probably a good idea as it may de-couple processing from changes to native glsl whatever that is. Anyway more work to on my ruby-processing shader examples ho-hum. Anyway it got me thinking would it be nice to have a text editor for this glsl language, turns out there is community contributed mode over at JEdit, with a bit of hacking I guess I can create a mode tuned for processing, perfect...
   1 #define PROCESSING_COLOR_SHADER
   2 
   3 uniform float time;
   4 uniform vec2 resolution;
   5 
   6 // NEBULA - CoffeeBreakStudios.com (CBS)
   7 // Work in progress...
   8 //
   9 // 3148.26: Switched from classic to simplex noise
  10 // 3148.27: Reduced number of stars
  11 // 3249.0:  Switched to fast computed 3D noise. Less quality but ~ 2x faster
  12 // 3249.5:  Removed use of random number generator to gain performance
  13 // 3265.0:  Added rotation: glsl.heroku.com/e#3005.1
  14 
  15 //Utility functions
  16 
  17 vec3 fade(vec3 t) {
  18   return vec3(1.0,1.0,1.0);//t*t*t*(t*(t*6.0-15.0)+10.0);
  19 }
  20 
  21 vec2 rotate(vec2 point, float rads) {
  22   float cs = cos(rads);
  23   float sn = sin(rads);
  24   return point * mat2(cs, -sn, sn, cs);
  25 } 
  26 
  27 vec4 randomizer4(const vec4 x)
  28 {
  29     vec4 z = mod(x, vec4(5612.0));
  30     z = mod(z, vec4(3.1415927 * 2.0));
  31     return(fract(cos(z) * vec4(56812.5453)));
  32 }
  33 
  34 // Fast computed noise
  35 // http://www.gamedev.net/topic/502913-fast-computed-noise/
  36 
  37 const float A = 1.0;
  38 const float B = 57.0;
  39 const float C = 113.0;
  40 const vec3 ABC = vec3(A, B, C);
  41 const vec4 A3 = vec4(0, B, C, C+B);
  42 const vec4 A4 = vec4(A, A+B, C+A, C+A+B);
  43 
  44 float cnoise4(const in vec3 xx)
  45 {
  46     vec3 x = mod(xx + 32768.0, 65536.0);
  47     vec3 ix = floor(x);
  48     vec3 fx = fract(x);
  49     vec3 wx = fx*fx*(3.0-2.0*fx);
  50     float nn = dot(ix, ABC);
  51 
  52     vec4 N1 = nn + A3;
  53     vec4 N2 = nn + A4;
  54     vec4 R1 = randomizer4(N1);
  55     vec4 R2 = randomizer4(N2);
  56     vec4 R = mix(R1, R2, wx.x);
  57     float re = mix(mix(R.x, R.y, wx.y), mix(R.z, R.w, wx.y), wx.z);
  58 
  59     return 1.0 - 2.0 * re;
  60 }
  61 float surface3 ( vec3 coord, float frequency ) {
  62   
  63   float n = 0.0;  
  64     
  65   n += 1.0  * abs( cnoise4( coord * frequency ) );
  66   n += 0.5  * abs( cnoise4( coord * frequency * 2.0 ) );
  67   n += 0.25 * abs( cnoise4( coord * frequency * 4.0 ) );
  68   n += 0.125  * abs( cnoise4( coord * frequency * 8.0 ) );
  69   n += 0.0625 * abs( cnoise4( coord * frequency * 16.0 ) );
  70   
  71   return n;
  72 }
  73   
  74 void main( void ) {
  75   float rads = radians(time*3.15);
  76   vec2 position = gl_FragCoord.xy / resolution.xy;
  77   position += rotate(position, rads);
  78   float n = surface3(vec3(position*sin(time*0.1), time * 0.05)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.9);
  79   float n2 = surface3(vec3(position*cos(time*0.1), time * 0.04)*mat3(1,0,0,0,.8,.6,0,-.6,.8),0.8);
  80       float lum = length(n);
  81       float lum2 = length(n2);
  82 
  83   vec3 tc = pow(vec3(1.0-lum),vec3(sin(position.x)+cos(time)+4.0,8.0+sin(time)+4.0,8.0));
  84   vec3 tc2 = pow(vec3(1.1-lum2),vec3(5.0,position.y+cos(time)+7.0,sin(position.x)+sin(time)+2.0));
  85   vec3 curr_color = (tc*0.8) + (tc2*0.5);
  86   
  87   
  88   //Let's draw some stars
  89   
  90   float scale = sin(0.3 * time) + 5.0;
  91   vec2 position2 = (((gl_FragCoord.xy / resolution) - 0.5) * scale);
  92   float gradient = 0.0;
  93   vec3 color = vec3(0.0);
  94   float fade = 0.0;
  95   float z = 0.0;
  96   vec2 centered_coord = position2 - vec2(sin(time*0.1),sin(time*0.1));
  97   centered_coord = rotate(centered_coord, rads);
  98   
  99   for (float i=1.0; i<=60.0; i++)
 100   {
 101     vec2 star_pos = vec2(sin(i) * 250.0, sin(i*i*i) * 250.0);
 102     float z = mod(i*i - 10.0*time, 256.0);
 103     float fade = (256.0 - z) /256.0;
 104     vec2 blob_coord = star_pos / z;
 105     gradient += ((fade / 384.0) / pow(length(centered_coord - blob_coord), 1.5)) * ( fade);
 106   }
 107 
 108   curr_color += gradient;
 109   
 110   gl_FragColor = vec4(curr_color, 1.0);
 111 }

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2