Hey All!
TL;DR: I can’t pass neither integer nor float custom values to my shaders properly.
In depth:
I’m working on a shader and I need to pass in 4 integer values per vertex. I’ve read on this forum that I can’t create a custom buffer type but JMonkeyEngine is flexible so I can use other unused buffers so I choose TexCoord7.
I have the following mesh, which is a quad for demonstration purposes:
App.java
Mesh m = new Mesh();
// Some code here to set up a quad with 4 vertex, texture coordinates and normals.
// ...
// Custom 4 integers I want to add per vertex
float[] blendIndexes = {
0, 2, 2, 2, // for vertex 0
1, 2, 2, 2, // for vertex 1
0, 3, 3, 3, // for vertex 2
3, 3, 3, 3 // for vertex 3
};
// ...
m.setBuffer(VertexBuffer.Type.TexCoord7, 4, BufferUtils.createFloatBuffer(blendIndexes));
Vertex shader
attribute vec4 inTexCoord7;
flat varying vec4 blendIndex;
// ...
blendIndex = inTexCoord7;
Fragment shader
flat varying vec4 blendIndex;
//...
// Simple visual debug: set diffuseColor to black and turn it to red if blendIndex.x is 0. But it is never 0. But on the custom data, the first vertex should have 0 value in the custom integers' x component.
vec4 diffuseColor = vec4(0.0, 0.0, 0.0, 1.0);
if(blendIndex.x == 0.0) { // This should make my quad partially red because blendIndex.x should be 0.0 for the first vertex but it is not 0.0.
diffuseColor.r = 1.0;
}
I figured out the problem lies between passing the custom values to the vertex shader, because if I do this in the vertex shader then my quad turns red where it should be:
Modified Vertex Shader
// blendIndex = inTexCoord7;
blendIndex = vec4(0.0, 2.0, 2.0, 2.0); // This works. I just set values here manually. Of course this will be same for all vertices but we're debugging.
I also tried passing in the custom data in different ways:
// Tried different types..
float[] blendIndexes = { 0.0, 2.0, 2.0, 2.0.....}
int[] blendIndexes = {0, 2, 2, 2.....}
// Tried setting the format to Float..
m.setBuffer(VertexBuffer.Type.TexCoord7, 4, VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(blendIndexes));
// ...and to Int.
m.setBuffer(VertexBuffer.Type.TexCoord7, 4, VertexBuffer.Format.Int, BufferUtils.createIntBuffer(blendIndexes));
Question: vec4 blendIndex in vertex and fragment shaders never has the proper custom values. What am I doing wrong?
Edit: edited question at the end