Pass vertexColor buffer to shaderMaterial?

Hi :slight_smile:
I’m in need of rendering a unique color per triangle of a mesh, thinking of using a vertexColor buffer.
However, it must be separate from the mesh own vertexColors.

Is it possible to pass such a buffer to a shaderMaterial or custom shader?

I looked around, but was unable to find any information on it, few examples used “vColor” in shader code but turned out to just be a singlular color.

Any examples, documentation, pointers and/or other ideas to achieve it are more than welcome!
Thanks

I read that wrong sorry, I had to delete my response.

Here’s how to do it with a custom material:

https://playground.babylonjs.com/#MC11EY

With a ShaderMaterial, you need to pass the name of your custom attribute through the attributes property of the options parameter passed to the constructor.

1 Like

Interesting and so simple…
And it seems if the mesh has it’s own vertexColors these are simply ignored while the material used, awesome.
Thank you! :slight_smile:

awww, I should not have deleted my response then… I did read it right.

I thought you wanted each facet to have its own solid color, not blended between vertex colors… damn.||

If you are looking to do it with a customShader then in the constructor just add the “color” in the attributes section.

Then on the VertexFragment add the

attribute vec4 color;
varying vec4 vColor;

main(){
...
vColor = color;
...
}

it should then pass that attribute automatically for the shaderMaterial for you I belive.

was my original post, I added it back because it looks like popov covered doing it for the CustomMaterial only.

Be careful that you will collide with names (color and vcolor) already used by the engine when you enable vertex colors.

That’s why I used mycolor and vMyColor in my sample :wink:

1 Like

not on a customShader you wont, I dont think unless I am mistaken?

I might learn something here.

So even on a customShader we have those reserved by the engine and that creates conflicts?

default vertexColors buffer (BABYLON.VertexBuffer.ColorKind) is “color”, i think it will use those, no?

I would believe so, especially for a customShader. For the customMaterial I think Popov is 100% right. I might be mistaken though.

Yes, even for the ShaderMaterial material you have some reserved names, like position / normal / uv / color for the attributes, world / projection / … for the uniforms.

If your geometry has a “color” vertex buffer and the mesh is flagged to use vertex colors, you will get a “color” attribute in your shader.

1 Like

Don’t you have to declare “color” in the attribute list for the shader instance in the constructor though? Otherwise I was under the impression it did not.

Yes, you also have to say you want to use this attribute in the shader by passing its name through the constructor.

1 Like

Coming back to this looking to increase precision of the triangle display (no blending)
Can we set/use a custom UV buffer in a simular manner to @Evgeni_Popov solution ?

You can pass any buffer you want to the custom shader (in your GPU limit card).

For custom uv set, you would pass a vec2 buffer instead of a vec4 as in the sample.

1 Like

Thanks!
Sorry, complete shader-code noob :slight_smile:
managed to get it to work after a slight headache :smiley:

and now ofcourse,
i realise uv aproach won’t work when the mesh has shared positions…
not sure what i was thinking :thinking:

expected; https://playground.babylonjs.com/#MC11EY#5
actual: https://playground.babylonjs.com/#MC11EY#4