certain shadertoy shaders throwing casting errors in processing?

edited March 2016 in GLSL / Shaders

Although most of the shadertoy shader work fine in processing when following codeanticode's instructions (https://codeanticode.wordpress.com/2012/08/03/shaders-in-processing-2-0-part-2/) there are some that throw casting errors. For example, https://www.shadertoy.com/view/XtjSDK and https://www.shadertoy.com/view/4ljGW1, work fine in shadertoy but will throw 'implicit casting of vec3 to vec2' errors when ported to processing. The shader code is unchanged except for the usual replacing of the uniforms and renaming of ichannel/fragcoord/fragcolor. Does anyone have any idea why this would be?

sketch:

PShader myShader;
PImage  myImage;

void setup() {
  size(960, 512, P2D);
  noStroke();
  ((PGraphics2D)g).textureWrap(Texture.REPEAT);
  myImage = loadImage("tex03.jpg");
  myShader = loadShader("artificial.glsl");
  myShader.set("resolution", float(width), float(height));
  myShader.set("texture", myImage);
}

void draw() {
  background(0);
  myShader.set("time", millis() / 1000.0);
  shader(myShader);
  image( myImage, 0, 0, width, height ); 
}

Shader:

#ifdef GL_ES
precision highp float;
#endif

uniform float u_time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform sampler2D texture;


// Layer between Processing and Shadertoy uniforms
vec3 iResolution = vec3(resolution,0.0);
float iGlobalTime = u_time;
vec4 iMouse = vec4(mouse,0.0,0.0); // zw would normally be the click status

/*by musk License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.*/

float time = iGlobalTime+99.0; //i hate the name in the uniforms

void angularRepeat(const float a, inout vec2 v)
{
    float an = atan(v.y,v.x);
    float len = length(v);
    an = mod(an+a*.5,a)-a*.5;
    v = vec2(cos(an),sin(an))*len;
}

float mBox(vec3 p, vec3 b)
{
    return max(max(abs(p.x)-b.x,abs(p.y)-b.y),abs(p.z)-b.z);
}

float mSphere(vec3 p, float r)
{
    return length(p)-r;
}

float rtime1 = time*.012;
float rtime2 = time*.027;
float rtime3 = time*.013;
mat3 rot = mat3(cos(rtime1),0,sin(rtime1),0,1,0,-sin(rtime1),0,cos(rtime1))*
    mat3(cos(rtime2),sin(rtime2),.0,-sin(rtime2),cos(rtime2),.0,0,0,1)*
    mat3(1,0,0,0,cos(rtime3),sin(rtime3),0,-sin(rtime3),cos(rtime3));

float df(vec3 p)
{
    float e=.4;
    for (int i=0;i<8; i++)
    {
        p = abs(p*rot)-e;
        p.y-=p.x*.1;
        p.x-=p.z*.1;
        e = e*.8+e*e*.1;
    }
    p = abs(p*rot)-e;
    p = abs(p*rot)-e;
    return mBox(p,vec3(.05));
}

vec3 nf(vec3 p)
{
    vec2 e = vec2(0,0.005);
    return normalize(vec3(df(p+e.yxx),df(p+e.xyx),df(p+e.xxy)));
}

void rotate(const float a, inout vec2 v)
{
    float cs = cos(a), ss = sin(a);
    vec2 u = v;
    v.x = u.x*cs + u.y*ss;
    v.y = u.x*-ss+ u.y*cs;
}

void main(  )
{
    vec2 uv = (gl_FragCoord.xy-iResolution.xy*.5) / iResolution.yy;
    vec2 mouse = (iMouse.xy-iResolution.xy*.5) / iResolution.yy;

    vec3 pos = vec3(0,0,-5);
    vec3 dir = normalize(vec3(uv,1.0-length(uv)*.6));

    float rx = mouse.x*8.0 + time*.04 +.1;
    float ry = mouse.y*8.0 + time*.024+.4;

    rotate(rx,pos.xz);
    rotate(rx,dir.xz);    
    rotate(ry,pos.yx);
    rotate(ry,dir.yx);

    for (int i=0; i<40; i++)
    {
        float dist = df(pos);
        pos += dist*dir;
        if (dist<0.001||dist>10.0)break;
    }

    vec3 light = normalize(vec3(1,2,3));

    float value = 
        df(pos+light)+
        df(pos+light*.5)*2.0+
        df(pos+light*.25)*4.0+
        df(pos+light*.125)*8.0+
        df(pos+light*.6125)*16.0;

    value=value*.1+.04;

    vec3 ref = reflect(dir,nf(pos));
    float ro = min(max(min(min(df(pos+ref),df(pos+ref*0.25)*4.0), df(pos+ref*.5)*2.0)*.5,.0),1.0);

    vec3 tex = textureCube(texture,ref).xyz;
    float fres = (dot(dir,nf(pos))*.5+.5)*9.0;

    vec3 color = value*vec3(dot(nf(pos),light)*.5+.5)*.5 + fres*tex*ro;

    color -= pow(length(uv),2.0)*.1;
    color = mix(color,vec3(length(color)),length(color)*.5);

    gl_FragColor = vec4(pow(color,vec3(1.0/2.2)),1.0);
}

Answers

  • I don't get any casting errors, perhaps it has to do with the version of GLSL available on your system? I ported the shadertoy code and put it up on github. Note that I explicitly set the GLSL version to 140 with the #version directive.

  • Thanks very much for looking into this. Your modified version of (https://www.shadertoy.com/view/4ljGW1) does indeed run without errors. Besides including the glsl version directive, I saw you also changed line 104 from " vec3 tex = textureCube(iChannel0,ref).xyz;" to " vec3 tex = vec3(1, 1, 1); ". So I tried to apply the same changes to the other shader I mentioned (https://www.shadertoy.com/view/XtjSDK) but without success. It continues to throw casting errors. As for my system, it's less than 2 years old, windows 10 pro, i7 with a GTX780 so I'm guessing its not a problem with my glsl version.

    I understand that this is GLSL related and not Processing but if you have time any advice would be very much appreciated. Either way, thanks again for your help.

Sign In or Register to comment.