Is there a way to create an interleaved VertexBuffer from a non-interleaved one by script? I’ve searched the forum and github but didn’t find anything related.
Generating a mesh, setting positions and normals then reading vertexBuffer produces a non-interleaved vertex buffer naturally.
This is relevant in the context of using VertexBuffer with TransformFeedback, which only accepts interleaved vertex buffers if more than 1 attribute is used (more than just positions).
It would be great if VertexBuffer had a method to interleave its data when called.
There is, but not using Mesh API directly, that one creates non-interleaved only.
You’d need to create a fill in vertex buffer manually, something like this:
Here’s a more complex example, setting up a mesh as well:
(search for ‘_vertexFormat’ and ‘SEMANTIC_POSITION perhaps’)
But my recommendation, as a more flexible solution (and compatible with WebGPU in the future), you might want to consider rendering to texture to process data on GPU - we use it in a few places in the engine such as morph target updates, or particle system.
The transform feedback allows you to transform data stored on GPU into another buffer again on the GPU, that you can use as an input to vertex shader. For example this is often used to do skinning one time and then render the mesh as non-skinned.
You can do the same thing using textures (especially float textures). You store some data in the texture, and render them to another buffer, often of the same resolution. Fragment shader does the processing.