- Home /
modular structure (like planetbase) (way of approach)
Hey, I'm trying to 'sort of' clone this game called Planetbase, as a learning project, In this game you build a Colony on a foreign planet. Each building has a connection to another one, so that all theese buildings form one structure.
PlanetBase Website: http://planetbase.madrugaworks.com/
Random Gameplay Video: https://www.youtube.com/watch?v=6QfrdQI-9nI
The position of theese connections are not totally fixed. I was wondering how I would realize this myself in unity. I started off simple and created a dome, copied out a part and created a mask. This mask would just need to be moved and I could move the entrance/exit. I also created a simple archway to obstruct that I'm just cutting out a part of the dome.
http://gfycat.com/EnlightenedSmoothJabiru
The Dome is 2 Objects and also the Mask is 2 Objects, with seperate models for inside and outside. The inside of the Mask obstructs the inside of the Dome and the outside of the Mask obstructs the outside of the Dome.
Now, to me it sorta looks like I got things right and I just need to follow down this road. But doing this all with mask objects feels like an enormous fiddly task. I'm allready having trouble to keep control of the renderlayers. I can't view my Dome from every direction, if there is another one next to it, without getting display bugs.
Do any of you guys have some tips for me? How would you approach this?
Answer by Bunny83 · Apr 17, 2016 at 01:28 AM
Using a depth mask isn't a good approach in this case. That usually works if you have one object that should be masked. For a modular system the result will be unpredictable.
I would recommend one of those approaches:
Actually cut out the doorway by performing some simple CSG operations on the geometry procedurally. You most likely only need a sphere as cut-geometry. Cutting / trimming a mesh isn't as difficult as it sounds, just a bit of fiddling. Also keep an eye open for ready to use solutions. This one states that runtime support for CSG is coming soon.
Use a shader that does the masking in one go. You could add parameters to a shader that define 2 or more spheres which are used to discard fragments based on their placement. @vexe once wrote such a shader. I would suggest a similar approach.
Keep in mind that realtime masking (the second approach) can add quite some overhead if you have a lot of such shaders at the same time. The first solution just requires some processing when the connection is placed. This has the advantage that the geometry can still be batched by the rendering system and renders in a single draw call.
Answer by bloeys · Apr 16, 2016 at 12:11 PM
You could try modeling different types of domes, for example one with only an entrance, one with two doors, and another with three etc...
Now since they are models you won't need to fiddle with masks and such, they will just be simple models. Then when you place a dome, you can just rotate the whole thing around until the exits are where you want them to be.
That would mean fixed connection positions. And that is the opposite of my goal here. You somehow just said, 'you could just give up on what you want to achieve' what? why?
Oh I didn't realize you wanted them to be movable, its not very clear in your question. How about manipulating the vertices? If you can know which vertices are masked, you might be able to manipulate/remove them, though that can be a bit tricky.
Your current solution seems simple enough, but your problem seems to be in the rendering part, can you explain a bit more about what you are doing and what the problem with rendering is, it will make it clearer for us to help you.
I'm changing the Renderlayer for the materials in a script an I use a shader to apply the mask. Both are found on the internet and not written by myself. As I'm just starting to uncover alot of theese topics. Allready learned some things about shaders tho.
mask inside: 3001
dome inside: 3002
mask outside: 3003
dome outside: 3004
Script: /* SetRenderQueue.cs
Sets the RenderQueue of an object's materials on Awake. This will instance
the materials, so the script won't interfere with other renderers that
reference the same materials.
*/
using System.Collections.Generic;
using UnityEngine;
[AddComponent$$anonymous$$enu("Rendering/SetRenderQueue")]
public class SetRenderQueue : $$anonymous$$onoBehaviour
{
[SerializeField]
protected int[] m_queues = new int[] { 3000 };
protected void Awake() {
Renderer[] rr = GetComponents<Renderer>();
List<$$anonymous$$aterial> materials = new List<$$anonymous$$aterial>();
foreach (Renderer r in rr) {
materials.AddRange(r.materials);
}
Terrain t = GetComponent<Terrain>();
if (t) materials.Add(t.materialTemplate);
for (int i = 0; i < materials.Count && i < m_queues.Length; ++i) {
materials[i].renderQueue = m_queues[i];
}
enabled = false;
}
}
Shader: Shader "$$anonymous$$asked/$$anonymous$$ask" {
SubShader{
// Render the mask after regular geometry, but before masked geometry and
// transparent things.
//Tags{ "Queue" = "Geometry+10" }
// Don't draw in the RGBA channels; just the depth buffer
Color$$anonymous$$ask 0
ZWrite On
// Do nothing specific in the pass:
Pass{}
}
}
Hmmm... I'm not really experienced in shaders, but I can see why playing with rendering in the shader might cause issues with multiple of those, I think they might be overlapping each other or something. Note: If this is making new materials, be careful with your draw call count, because if each dome gets a different material, then each dome will use a draw call.
Aside from the rendering stuff, have you figured collisions yet? If you are only getting this affect by using shaders, this means that the physics engine still considers it to be a solid, and there is really no way around that if you only rely on shaders.
What you will probably have to do is do vertex manipulation. I haven't done much of that so I can't tell exactly, but you might need to create a new mesh with the doors as 'holes' in them, or modify the existing mesh to create new holes in it, then you can use a mesh collider to make the physics engine acknowledge the doors.
As far as I can tell, I won't need physics colliders for that region.