Friday, May 19, 2017

unity - Room generation with external walls and doors


I am looking to implement a means to generate rooms with outer walls with windows and doors from prefab game objects in Unity.


The idea is that a group of tiles is defined as a room which I then iterate through to find the outer tiles and then use their external edges to generate the walls of the entire room. Gathering the outer tiles is easy enough and working out the external edges is already implemented.


What I need to understand is the best approach for using prefab game objects as the set pieces for walls and floors.



Currently for a single wall piece, I have created a quad and made that into a prefab which I then clone, position and rotate into place along the outer edges. The below screenshot shows a room with walls made using the prefab wall GameObject. In this screenshot, the room consists of 12 wall GameObjects. I want to avoid having lots of GameObjects for single wall segments and instead perhaps merge them all into a single mesh on a single GameObject.


generated room


What I would like to achieve is to be able to create a consistent set of pieces for each outer wall, with windows and doors (most likely cloned from other prefabs) to build a realistic looking room. For example, any single length of a wall would be made up from


Wall -> Wall -> Window -> Wall -> Wall


Perhaps there is a much easier way to achieve this that I have overlooked but searching for room generation always leads back to examples for dungeon crawlers and roguelikes. I am not creating a dungeon crawler, nor am I generating the environment proceedurally, instead the player will be able to create rooms in a similar fashion to Evil Genius and The Sims.


In the current approach, creating a room which is 5x5 would mean that there are a total of 20 wall GameObjects and 25 floor GameObjects. I am not keen on having this many objects in the scene for a single room. I would prefer to be able to create a mesh for the entire room, adding in the vertices and triangles from the prefab mesh to cut down on the number of GameObjects in the scene.


Does anyone have any experience with building rooms in this fashion, or are there any clear examples of how to achieve this? I am not sure how to translate and rotate a prefab and then merge the translated meshes into a single mesh. Perhaps I am barking up the wrong tree and there is a better approach but for now, this is the only way I can see which is possible to achieve what I want.



Answer



You can merge the meshes of multiple game objects into one using Mesh.CombineMeshes.


Note that this does not care about which materials you assigned to the input meshes. The GameObject into which you merge the meshes will use whatever materials you assign to its own MeshRenderer. That means when you are merging meshes with different materials, you should use false for the second argument, so you end up with a sub-mesh for each original mesh and then add all the materials to the MeshRenderer of the resulting GameObject.



There is a performance penalty for using the same material multiple times, so you might want to first merge all meshes with the same material with mergeSubMeshes = true and then the resulting meshes with mergeSubMeshes = false.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...