July
5th
2014

Flip Normals of a Mesh in Unity
by

 In Unity, there are a number of primitive object types that can be created directly such as sphere or cube. With these primitive objects, they are made to be visible only from outide the mesh, but what if, for example, you want to make a sphere or a cube visible from inside?

Here is a script to attach to your mesh in order to flip the normals and make it visible from inside:

using System.Linq;
using UnityEngine;
[ExecuteInEditMode]
public class FlipNormals : MonoBehaviour
{
	void Start()
	{
		var mesh = (transform.GetComponent("MeshFilter") as MeshFilter).mesh;
		mesh.uv = mesh.uv.Select(o => new Vector2(1 - o.x, o.y)).ToArray();
		mesh.triangles = mesh.triangles.Reverse().ToArray();
		mesh.normals = mesh.normals.Select(o -> -o).ToArray();
	}
}

In the screenshot below, the left sphere is the original primitive object: mesh is only visible from the outside. However, on the right sphere, the FlipNormals script is attached: the exterior part of the mesh became invisible. We can only see now the interior part of the sphere.

Left: Original Normals - Right: Flipped Normals

Left: Original Normals – Right: Flipped Normals