THREE.js custom attributes with indexed BufferGeometry - three.js

I'm using indexed BufferGeometry for raytracing. Which means I have a flat list of unique vertices and index buffer for faces. How can I add custom attribute per vertex of face? As far as I understand the size of each attribute buffer should match the size of position buffer.

Related

Using separate vertex buffer and texture coord buffer and multiple indices to draw tris DirectX11

So I saw these posts DX10+ multiple vertex buffers, single index buffer and Using Multiple Vertex Buffers In DX10/DX11 and vague understood Why Directx11 doesn't support multiple index buffers in IASetIndexBuffer
When rendering indexed tris, I was wondering if it is possible to have a vertex buffer and a separate texture coordinate buffer. But then specify the triangles when drawn by like a set of indices into the vertex buffer and a set of indices into the texture coordinate buffer (separate), so a total of 6 indices per triangle. So that I can reuse texture coords and vertices. Because otherwise i have to duplicate vertices for every texture coordinate which I am not too keen on.
Is this possible in DirectX 11?
Duplicating vertices is in fact the standard solution if you need different vertex elements with shared positions.

Is it possible to specify one color per quad?

I'm working on 2D quad batching. The vertex array is very structured: 4 vertices per quad with position and texture vectors, but I only need one color per quad.
Putting duplicate color information in every vertex seems like a waste.
Is there a way I can optimize this?
Color is attribute of vertex and not of primitive. While using vertex attribute arrays openg mandates to have number of vertices should be equal to no tex coords, no of normals, no of colors and so on.
Some references:
How can I specify per-face colors when using indexed vertex arrays in OpenGL 3.x?
How to specify color per primitive for glDrawElements()

Can Geometry vertex data be shared across three.js Ribbons?

I have a large set of vertices generated by interpolation of heart contours extracted from cardiac MRI slices which form a generally cylindrical shape. In the current (C++) visualization, the vertex data is held in a single array and each lateral strip in the model is rendered as a triangle strip.
I am wondering if there is a way to share Geometry vertex data across multiple Ribbons and avoid having to create separate Geometry objects for each strip (thereby duplicating vertex data).

What is the most efficient way to send my geometry to glsl?

In my scene I have a tree of meshes. Each mesh contains a transformation matrix, a list of vertices, normals, uvs and a list of child meshes.
I would like to send my geometry in an 'array of structures' to glsl using a VBO, but I don't know how to handle updating each mesh's matrix in the event of a parent mesh's matrix transformation. Should I traverse the mesh tree and add a matrix for every vertex every frame? Should I premultiply the vertices each frame? Should I hold a list of matrices in a uniform and reference the current vertex's matrix by index? Should I use one glDrawArrays or glDrawElements for every mesh?
The 'standard' approach would be to use on glDraw call per mesh.
For each mesh, you feed its transform matrix in a single uniform that is applied for the whole mesh. This matrix is then used in the vertex shader to transform the mesh from 'object space' to 'clip space'.
Having multiple matrices for one mesh (or draw call) is generally used for non-rigid transformations on meshes (deformations induced by bones for example).
Another approach, depending on the number of meshes you have to draw, would be to use 'hardware instancing'. Where you can draw a similar mesh multiple times, providing an array of matrices, and let the hardware pick the according matrix given the instanceID being drawn.

How many normals?

if you are calculating the normals of a polygon for rendering it on WebGL, do you use a normal for every index in the index array or for every vertex on the vertex array?
In the notes here, the user is calculating them for each vertex.
Every vertex. A vertex, in the WebGL sense (which is the same as OpenGL ES and other predecessors), isn't really a point in space, but rather a combination of attributes. One of these is almost always the location (though in unusual cases you might not have that), and others are generally things like the normal vector, the colour, the texture coordinates, and so on.
The index array, by contrast, is an offset into the vertex attribute arrays. So when you specify index (say) 1 in an index array, it's shorthand for "the vertex made of combining the first location in the location buffer, the first normal in the normal buffer, the first colour in the colour buffer, and the first texture coordinate in the texture coordinate buffer".
The most counter-intuitive thing for me when learning this was separating vertices from the locations they happen to occupy. There's no reason why two vertices can't have the same location.

Resources