# "Synchronize" Backface Culling

**URL:** https://discussions.unity.com/t/synchronize-backface-culling/652627
**Category:** Graphics
**Created:** [January 19, 2017, 10:02pm UTC](https://discussions.unity.com/t/synchronize-backface-culling/652627 "2017-01-19T22:02:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mbue](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@mbue](https://discussions.unity.com/u/mbue)
#### Post date: [January 19, 2017, 10:02pm UTC](https://discussions.unity.com/t/synchronize-backface-culling/652627/1 "2017-01-19T22:02:10Z")

</div>

I got a quad primitive which some objects in front of it. The quad represents some kind of wall. I want the wall to be visible from one side only, back face culling comes in handy here. 😉  
However, as soon as the quad is backface culled, the objects infront of the quad should disappear.  
So I’d need something like (Pseudocode):

```csharp
objects.visible = !wall.backfaceCulled

```

I got it working for orthographic projection by going through a manual culling process:

- I calculate the normal of the triangle of the quad
- I use the dot product of that normal with the camera direction and check whether its \> 0.
- If \< 0, the renderer ob the objects in front of the wall are disabled.

It works fine with orthographic projection, however I’m missing something regarding perspective:  
The normal of the quad would need to be “distorted” by the projection view as in the following picture:

![](https://europe1.discourse-cdn.com/unity/original/4X/f/e/a/fea4d8b972db4f2fa41d3e559595ac3d2e27e372.png)

Yellow shows the calculated normal. If it would follow the blue line, the culling should work fine.

```csharp
            // filter is the MeshFilter of my quad     
            var v0 = filter.mesh.vertices[0];
            var v1 = filter.mesh.vertices[1];
            var v2 = filter.mesh.vertices[3];

            // Local space to world space
            var v0World = visibilityParent.transform.TransformPoint(v0);
            var v1World = visibilityParent.transform.TransformPoint(v1);
            var v2World = visibilityParent.transform.TransformPoint(v2);

            // Calc normal
            var sideVec1 = v1World - v0World;
            var sideVec2 = v2World - v0World;
            var normal = Vector3.Cross(sideVec1, sideVec2);
            normal.Normalize();

            var visible = Vector3.Dot(normal, Camera.main.transform.forward) > 0;

```

Any ideas?  
Thank you in advance.

---

<div class="post-metadata">

### Author: ![bgolus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/bgolus/32/391570_2.png) [@bgolus](https://discussions.unity.com/u/bgolus)
#### Post date: [January 21, 2017, 12:34am UTC](https://discussions.unity.com/t/synchronize-backface-culling/652627/2 "2017-01-21T00:34:23Z")

</div>

There’s not really a need to calculate the normal. Assuming you’re using the default quad you can just use [transform.forward](https://docs.unity3d.com/ScriptReference/Transform-forward.html) to get the normal direction (actually going to be -transform.forward). Then for the dot product you want use the world space relative vector from the camera to the quad’s transform rather than camera forward, assuming you’re using a perspective camera and not orthographic.

`var visible = Vector3.Dot(-transform.forward, transform.position - Camera.main.transform.position) > 0.0f;`

That might be backwards.

---

<div class="post-metadata">

### Author: ![mbue](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@mbue](https://discussions.unity.com/u/mbue)
#### Post date: [January 23, 2017, 11:11am UTC](https://discussions.unity.com/t/synchronize-backface-culling/652627/3 "2017-01-23T11:11:52Z")

</div>

Thank you really much, it works.  
I really overcomplicated this. 🙂
