Custom Vertex Attributes problem

Hey, I have recently found out that it is possible to use setAttribute method for a standard material to set some custom attributes. So I did that as described in the docs, and it it does not work for me. I am not sure what do I do wrong here. The value is read as 0 not matter what I am passing. No errors or warnings, it just does not seem to be working.

                const bodyMesh = body.render.meshInstances[0].mesh;
                const positions = [];
                bodyMesh.getPositions(positions);
                const distanceFromSpine = new Float32Array(positions.length / 3);
                const spinePosition = spine.getPosition();
                const bodyPosition = body.getPosition();
                
                for (let i = 0; i < positions.length / 3; i++) {
                    const x = bodyPosition.x + positions[i * 3];
                    const y = bodyPosition.y + positions[i * 3 + 1];
                    const z = bodyPosition.z + positions[i * 3 + 2];
                    distanceFromSpine[i] = Math.sqrt(
                        Math.pow(x - spinePosition.x, 2) +
                        Math.pow(y - spinePosition.y, 2) +
                        Math.pow(z - spinePosition.z, 2)
                    );
                }
                bodyMesh.setVertexStream(pc.SEMANTIC_ATTR15, distanceFromSpine, 1);
                m.setAttribute("distanceFromSpine", pc.SEMANTIC_ATTR15);
                m.chunks.APIVersion = pc.CHUNKAPI_1_65;


                m.chunks.startVS = `
                    attribute float distanceFromSpine;
                    varying float vDistanceFromSpine;

                    void main(void) {
                        gl_Position = getPosition();
                        vDistanceFromSpine = distanceFromSpine; // always 0 at this point
                `;

to add a stream to the mesh, you first need to copy out all existing vertex streams you want to use. Then call mesh.clear() which resets its internal vertex format. Then assign all streams you want (including the new one), and call mesh.update(), which creates a vertex buffer with the format to match assigned streams.

1 Like

got it, thank you