Shader from Shadertoy running slow in Processing
in
Programming Questions
•
6 months ago
Hello,
I'm porting shaders from Shadertoy and some of them get very laggy when running in Processing.
For exemple,
this cloud shader.
This is my sketch code:
- PShader myShader;
- void setup() {
- size(640, 360, P2D);
- noStroke();
- myShader = loadShader("shader.glsl");
- myShader.set("resolution", float(width), float(height));
- }
- void draw() {
- background(0);
- myShader.set("time", millis() / 1000.0);
- shader(myShader);
- rect(0, 0, width, height);
- }
And the GLSL code:
- // CLOUD SHADER
- // Based on https://www.shadertoy.com/view/XslGRr by inigo quilez
- // Created by inigo quilez - iq/2013
- // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
- #ifdef GL_ES
- precision highp float;
- #endif
- // Type of shader expected by Processing
- #define PROCESSING_COLOR_SHADER
- // Processing specific input
- uniform float time;
- uniform vec2 resolution;
- uniform vec2 mouse;
- // Layer between Processing and Shadertoy uniforms
- vec3 iResolution = vec3(resolution,0.0);
- float iGlobalTime = time;
- vec4 iMouse = vec4(mouse,0.0,0.0); // zw would normally be the click status
- // ------- Below is the unmodified Shadertoy code ----------
- 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 );
- return f;
- }
- vec4 map( in vec3 p )
- {
- float d = 0.2 - p.y;
- d += 3.0 * fbm( p*1.0 - vec3(1.0,0.1,0.0)*iGlobalTime );
- 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 brdf = vec3(0.65,0.68,0.7)*1.35 + 0.45*vec3(0.7, 0.5, 0.3)*dif;
- col.xyz *= brdf;
- #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 );
- }
Am I doing something wrong?
Thanks for your help.
R.
1