I have created a custom Mesh and a ShaderMaterial in Babylon.js. When I pass a custom vertex data named red to the mesh, the red attribute does not work at all.
I have carefully read the official documentation about customMesh, but I couldn’t find any content related to custom vertex attributes. I also searched on Google and found a related thread where someone achieved this by using mesh.setVertexData(), but the same code doesn’t work in my project.
Could anyone tell me what I should do? Thanks so much for your help!
const material = new BABYLON.ShaderMaterial(
"heatmap-material",
scene,
{
vertexSource: `
in vec2 position;
in float red;
varying float vred;
void main()
{
vred = red;
gl_Position = vec4(position.xy, 0.0, 1.0);
}
`,
fragmentSource: `
varying float vred;
void main(){
gl_FragColor = vec4(vred,0.0,0.0,1.0);
}
`,
attributes: ["position", "red"],
},
);
const mesh = new BABYLON.Mesh("heatmap", scene);
mesh.material = material;
const vertexData = new BABYLON.VertexData();
let position = new Float32Array([
-1, 1, 0,
-1, -1, 0,
1, -1, 0,
1, 1, 0,
]);
let indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
vertexData.positions = position;
vertexData.indices = indices;
vertexData.applyToMesh(mesh);
let red = new Float32Array([1, 1, 1, 1])
mesh.setVerticesData("red", red, false, 1);
Resources
