What are the benefits of a wall being as a plane or as a box? Should I use a plane with a box collider instead of mesh collider?
4 Answers
There's going to be a couple different things that you'll need to consider.
Note: that the benefits from the optimizations described below only really matter with a ton of walls. At that point, it might be wise to consider a different solution like binary space partitioning (BSP) for making your walls in your game. This would make your levels highly optimized for rendering (if designed correctly) and for physics (as you have a very good definition of what collides and what doesn't.
Rendering
A box is going to take more to render than a plane. It has more vertices and triangles meaning your vertex shader and the rasterizer are going to have to be run over more data. Therefore, only use a box when you need to in terms of visibility. Remove faces as needed for what the player will need to see.
Physics
You should always use a BoxCollider
as opposed to a MeshCollider
when you can. MeshCollider
s have to apply collisions per face (or as a convex hull) of the mesh and make raycasting and physics slower (but it's a negligible amount for just one box). Another thing with BoxColliders
is that it's much harder for the player to phase through them when they are going really fast. Even with continuous collisions, if you find yourself still having things phase through walls, you can always just expand the BoxColliders
Productivity
If you're much faster working with planes with MeshColliders
(because you don't need to adjust all the BoxColliders
to match their dimensions) it may be fine to just use those if you get the performance you desire.
Expanding a bit on "Coburn"'s points:
Rendering: If you render the wall as only a plane, then the wall will only be visible from one side, as the plane (or more exactly its faces) only has one rendered side. So if the player is able to move around the wall, you would need multiple planes to cover all the sides, which means you end up with a box anyway.
Physics: Simple geometry colliders (spheres, boxes, etc.) are always faster to compute compared to full mesh colliders since not all faces of the mesh have to be checked individually. Instead "simple" raycasts can be used or distance calculations (e. g. distance to center of sphere minus sphere size).
For other physics engines that have a separate "plane collider" (which Unity doesn't), there is no "better" one. A plane might be slightly faster performance-wise, but you might then run into the issue of fast objects passing through it without actually colliding. Then you are left with either having thicker colliders (so box instead of plane) or move to Continous Collission Detection (CCD), as explained here.
-
\$\begingroup\$ You can almost always turn backface culling off. \$\endgroup\$– BálintSep 22, 2016 at 8:54
-
2\$\begingroup\$ @Bálint True. The user might then see the wall also from the other side although with wrong lighting in that case (light sent by the sources from the other side/room, instead the one he/she is standing in). \$\endgroup\$ Sep 22, 2016 at 9:42
-
\$\begingroup\$ In your second point you didn't actually specify whether it is a pro or a con and for what. A plane seems simpler geometrically than a box, so is that your point? \$\endgroup\$– BakuriuSep 22, 2016 at 14:15
-
\$\begingroup\$ @Bakuriu As far as I know, Unity does not have a "plane collider", so "box" is the nearest thing. I added a note for other physics engines. \$\endgroup\$ Sep 22, 2016 at 17:43
One more thing about Rendering: When you have your own shadow-mapping implementation, walls made out of boxes can be superior to walls made out of planes due to peter-panning that can appear with planes.
Here's an explanation of the symptom:
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/#peter-panning
-
-
\$\begingroup\$ did you follow the link that i provided? should be self-explanatory. \$\endgroup\$ Sep 22, 2016 at 7:55
I was always told that if u are going to have shadows that its best to make it solid to help with light bleeding because the difference in depth is to small and it will bleed.
I could be wrong, but I make all of my walls and stuff like that two sided, a wall will only be a few verts so it shouldn't be that much of a saving by having it a plane.
There are some things that I leave sides of but its mostly the bottom or top of stuff that has lots of stuff around it to help with light bleeding.
I do the same for physics, didn't every really think about it but to me it seemed right to use a wall as a wall and a plane as a plane but that is just me
-
\$\begingroup\$ This is in an important point that the other answers are missing. I have tried to build a room with planes but they only let light pass one way so the shadows are completely messed up. \$\endgroup\$– Ambo100Jun 29, 2021 at 14:43