# How can I cut a doorway through my wall at runtime?

I am working on a Unity project where I have 3 models: a stacked, rounded wall (think logs), a flat wall (think bricks), and a door.

The door has an animation to open / close itself, and when it's attached to a wall, there's one major problem -- when the door swings in, it reveals not the empty space one would expect, but the segment of wall it's layered on top of!

Since all 3 models are part of dynamic mesh structures, I can't "pre-calculate" what the model should look like to account for the empty space of the door -- what if the player puts the door on the next segment of wall instead?

I considered expanding the "door" model to a "door + wall segment" model (replacing that wall segment entirely), but because I already have two separate shapes of wall, it quickly becomes a combinatorics problem -- 2 models for each door, per door shape (not to mention windows, or a 3rd [or fourth!] wall shape!). In short, this doesn't seem like a tenable solution.

What would work most cleanly is if I could remove a "Door-shaped" segment of my Wall segment mesh, leaving a clear hole that the Door model could snugly fit.

In an equation: Final Mesh A = Mesh A Volume - (Mesh A Volume ∪ Mesh B Volume)

Is an operation like that feasible? My gut tells me that polyhedron intersection is hard, and there might be a better way to do this. I'm also equally interested if someone has a better suggestion on how to solve the initial problem of doors intersecting with wall segments.

• Do you really want to know if mesh subtraction is possible? If so, the answer is obviously "yes" — any decent 3D modelling software implements tools for this, and it's been seen in various forms in games dating back decades. Does that answer your question, or do you really want to ask "How can I cut a doorway through my wall at runtime?" — mesh modification might not even be required for that. Shader clipping or stencil tricks may suffice. Nov 11 '19 at 19:27
• Do you have any limits on how many holes you need to cut in a single wall segment? Nov 11 '19 at 21:24
• Interested in solving for a single cut. (I'd think most solutions could be adapted to support disjointed holes if things were to get fancy.) Nov 11 '19 at 21:38
• Shaders can definitely do this. It shouldn't even be hard. Sebastian Lague has made a video about this (among other things) that you can find here: youtu.be/Cp5WWtMoeKg?t=159. However, I don't know how this places with colliders. I suspect you're going to have to modify that separately; but it shouldn't prove too hard. Nov 11 '19 at 22:31
• Luckily, I'm not using mesh colliders at all, so no worries on that front! I'll check out the video. Nov 11 '19 at 22:45