Setting custom shader attributes

I’ve written a custom shader (see below) based on the default but with an additional texture co-ordinate attribute, aCorners, but can’t work out how to set it.

  1. How do I set it?
    (I’ve tried using this.material.setParameter("aCorners", new Float32Array(screenPositions)); but no joy.)
  2. Have I defined it correctly (line 5: shader definition)?
  3. Do I need aCorners as an additional attribute (pc.SEMANTIC_TEXCOORD1) or should I over-ride aUv0?
  4. If so, how?

Thanks in advance

Vertex shader

attribute vec3 aPosition;
attribute vec2 aUv0;
attribute vec2 aCorners;

uniform mat4 matrix_model;
uniform mat4 matrix_viewProjection;

varying vec2 vUv0;
varying vec2 vCorners;

void main(void)
{
    vCorners = aCorners;
    vUv0 = aUv0;
    gl_Position = matrix_viewProjection * matrix_model * vec4(aPosition, 1.0);
}

Fragment shader

precision highp float;

uniform sampler2D uColorBuffer;

varying vec2 vCorners;
varying vec2 vUv0;

void main() {
   vec4 color = texture2D(uColorBuffer, vCorners);

   gl_FragColor = color;
}

Shader Defintion

let shaderDefinition = {
    attributes: {
        aPosition: pc.SEMANTIC_POSITION,
        aUv0: pc.SEMANTIC_TEXCOORD0,
        aCorners: pc.SEMANTIC_TEXCOORD1
    },
    vshader: vertexShader,
    fshader: fragmentShader
};

Hi @CarlBateman,

You should declare aCorners as a uniform and then setParameter will work as expected. Unless you have a reason do so, declaring it as an attribute isn’t required.

1 Like

Well, since I want to use the interpolated values as UVs to read from a texture, they should be passed as an attribute, yes? Uniforms can’t be interpolated, can they?
I tried changing aCorners to a uniform and just got a solid colour.
Thanks

Both

mesh.setVertexStream(pc.SEMANTIC_TEXCOORD1, screenPositions);

and

mesh.setUvs(1, screenPositions);

seem to work.

Now, I just have a bunch of other problems to sort out. :roll_eyes: :thinking: :scream:

2 Likes