The shader doesn't have a uniform called "textur

I have this really simple fragment shader (this is just the starting point…):

out vec4 fragColor;

uniform sampler2D texture;

void main() {

	fragColor = texture2D(texture, vec2(0.5, 0.5));
}
PShader shader;
PImage img;
PImage img2;

public void settings() {
  size(600, 600, P3D);
}


public void setup() {
  img = loadImage("/Users/doekewartena/Desktop/1098_2.png");
  img2 = loadImage("/Users/doekewartena/Desktop/1098_3.png");
  shader = loadShader("frag.glsl"); // "vert.glsl"
  shader.set("texture", img2);
}

void draw() {
  background(0);
  shader(shader);
  image(img, 0, 0);
}

When compiling I get the following error:

The shader doesn’t have a uniform called “texture” OR the uniform was removed during compilation because it was unused.

Why is that? cause it clearly is used.

1 Like

Specifying the shader version worked for me.

#version 330

I wish I had time to make better documentation of how to use shaders in processing.

1 Like

You need to use the Shader implementation of Processing to use shader, if don’t do the code writing is more complexe, below an example:
glsl

// Processing implementation
#ifdef GL_ES
precision highp float;
#endif
#define PROCESSING_TEXTURE_SHADER
varying vec4 vertColor;
varying vec4 vertTexCoord;
uniform vec2 resolution; // WARNING VERY IMPORTANT // need this name for unknow reason :( here your pass your resolution texture
// vec2 texOffset   = vec2(1) / resolution; // only work with uniform resolution

// Rope implementation
uniform sampler2D texture;

void main() {
  vec2 uv = vertTexCoord.st;
  uv.y = 1.-uv.y;
  gl_FragColor = texture2D(texture,uv);
}

sketch

PShader shader;
PImage img;

public void setup() {
  size(100, 100, P3D);
  img = loadImage("small_puros_girl.jpg");
  surface.setSize(img.width,img.height);
  shader = loadShader("data/simple_shader.glsl"); // "vert.glsl"
  shader.set("texture", img);
}

void draw() {
  background(0);
  shader(shader);
  image(img, 0, 0);
}
1 Like

When I change the name textureto tex console return no error, but that’s don’t work too :slight_smile: maybe the work texture is reserved ?

and this one only almost work with texture()not with old texture2D() on my laptop.

This is almost certainly to do with the code at https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGL.java#L1904 that does automatic rewriting of shaders to match the underlying OpenGL version unless you specify #version

Incidentally, looking at that and then changing your code to shader.set("texMap", img); seems to “work”.

1 Like