Drawing a tri/quad with C++ in-game?


I was wondering if it was possible to create a mesh from code only at runtime ? I’m currently working a sort tool that would allow the user to specify 3 locations in space that would be used for creating a triangle from scratch.

I’m not worried about UVs since I plan on applying a material that will display something in screenspace.

Hi Fabrice,

Do you want to render your mesh in the world as part of the scene? Or do you wish to draw it over the world as a UI element?

In the scene, like a new 3D Mesh.

The difference between drawing a 2D (aka UI) and 3D mesh is very huge ?

It is a bit more complicated. Depending on the material applied to your mesh we have to render it in specific ways to make sure it receives lighting, casts shadows, is sorted properly with translucency if it is not opaque, etc.

That is different from Canvas which allows you to easily draw 2D or 3D shapes over top of the main scene being rendered.

We have a sample of how to do this from a plugin internally, hopefully we can get that out to Rocket users sooner rather than later.

Can I hope of any hints mainwhile ?
Looking forward for this plugin sample anyway. :slight_smile:

The quick run down is that you have to add your own subclass of UPrimitiveComponent that then add to the scene (e.g. by adding it to a blueprint).

In your primitive component you will have to implement FPrimitiveSceneProxy for your component. That is the class that actually handles interfacing with the renderer and will ultimately provide the triangles to the renderer for shading.

The tricky bit is that the component is updated on the main thread while the scene proxy is used on the rendering thread so you cannot safely read most UObjects from the scene proxy. That requires you to marshal data from the component to the proxy either by recreating the proxy or by using a rendering command to buffer the update to the rendering thread. We don’t have any great examples of this workflow in public code right now which is why this plugin will be so useful. It provides you with the patterns you need to get it all working without introducing race conditions in your code that would manifest as bugs or crashes that only happen occasionally.

You can try using FDynamicMeshBuilder, it will give you a good starting point.

Thanks for your answer. I started to dig a bit inside the FDynamicMeshBuilder. However I’m a bit stuck as I don’t see how to reference a FPrimiteDrawInterface. I guess I have to make a new one for drawing my mesh, if this is the case : how ?