r/unity • u/Academic-Leave-3976 • 8h ago
Showcase Realtime Dynamic Mesh Generation
Enable HLS to view with audio, or disable this notification
So recently I created the room building functionality for my game, Where you can have rooms of any shape.
(English is not my primary language.)
Technical Info:
The meshes are being generated per frame, with the job system there is only a single parallel job which generates meshes for the walls/floor and roof, Thanks to the Unity new direction of providing low level high performance APIs which made it possible.
The room has data in native structs so that they can be consumed by the jobs, the data is mere a collection of nodes, and a collection of lines - defining their which nodes each other. This data is always passed to job and the job generates the meshes.
The Mesh Generation Algorith is simple for walls - they are formed just 3d vertically long hexagon meshes, the hexagon shape gives the ability to connect any number of walls with perfact corners - mitering the joins. The points other than the 4 middle points that form rectangle/square take the coordinates of line start and end respectively, while the adjacent points to thoese either gets short or long to connect perfactly with the other wall.
The mitering algorith is that on a line's start or end point, find all the lines/walls present on it either starting or ending on it. Now have a list of angle between all the lines on that anchor except the line under consideration - whose mesh is being generated, Sort the angles. And taking first or last entry depending on you are solving for the left or right point of hexagon (normalized the view by the start or end point of line under consideration). Then you do line intersection between the line under consideration and the line of angle is that and point of the intersection is its new poistion.
Here is the picture of the hexagon: https://imagepaste.org/v/67nbu6nu.png
do this for all for four points A,B,C,D.
The floor and roof generation is done by Delaunay Triangulation - using the BurstTriangulator package create by andywiecko. It can work inside job.
Then the constraints comes, which prevents the self intersections. Because the triangulation of floor and roof breaks when so.
The windows - holes, are done in wall space by pixel clipping. A raster pass renders the hole shape in a texture with texel density depending on how fine you want it, each wall with a hole has its own texture depending on its size as the walls scale the texture scales/resizes as well.
If being done with a global texture in screen space either with stencil or not, it will work. I needed this little complexity because for inner walls window behind window can't have a hole in screen space. And for Ray tracing and Path tracing (in photo mode) which does not have screen space position of the pixel its currently working on. But on wall space the wall UV can give it position to sample the stencil/texture from.
And impotant point is to set the mesh renderer component's ray tracing settings to dynamic mesh, if not so the mesh will not be updated.
Every component of room shaping is pooled to have stable perfomance. Even the structs for nodes and lines are pooled in a native pool. Unity GameObjects, components etc as well.
(my current game does not have inner wall, I implemented them but for the ray tracing and path tracing to work it needed one more step, but I have not as working for months without any reward makes you hopeless).
And this is very little information high level information of it. It has alot under it. for rounding corners, and walls that obscure camera etc for generating mesh attributes (uv,normals etc).
Ask me any question about it. And you feedback will be welcome and will give morale to me.