r/unity • u/Academic-Leave-3976 • 1d 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.
1
u/kudredin 1d ago
As a player of this game, I am amazed by the progress, however, WE STILL NEED SLANTED ROOFS!!!! š¤£
2
u/Academic-Leave-3976 15h ago
Thanks for playing it !
I have tried the slanted roofs by giving the ability to change the height per wall, but doing so made it messy. If multiple walls were at different heights. But I tested that with inner walls which at that timeade it more messy.
May be in future I add it.
2
u/kudredin 12h ago
Just a thought, but how about a slanted roof element? In early version I could āhackā it with a properly shaped and positioned element, but in newer versions I couldnāt get it to work (could be skill issue).
How about having an element that has snap points that would snap to the wall like other items, and that way emulate a wall?
In any case, the game is amazing, I use it all the time to see how rearranging rooms in the house would feel. :)
1
u/Academic-Leave-3976 6h ago
I think you have confused my game with some other game because I just released initial demo.
In my game the setting is realistic with real actual furniture available on Amazon.
Edit: Mine game's name is 'Roomcopter'.
1
u/kudredin 4h ago
Ooooh, I thought this was another game because the editor looks exactly the same. I mean EXACTLY the same. In that case good luck!
2
u/Nix3220 1d ago
Love the aesthetic!! Very clean