I am trying to make my simple plane visible from both sides. The usual (and probably best) answer is of course to duplicate and flip the vertices in my 3d program before importing to unity...
However i am doing some live mesh deformation on the plane and find it easy to simply make the deformation script also do the duplicate and flip.
But I am seeing some strange behavior after I duplicate and flip the vertices, triangles and normals. I've made a small test project that illustrates the problem. You can find it here
Generally the mesh get's duplicated alright, but even though i flip the normals, the duplicated part of the mesh is still only visible from the same side as the original, while the lighting on the duplicated part is affected by the flipped normals.
Please check out the example project if my description doesn't make sense.
Flipping normals only reverses the lighting info; you need to reverse the winding order of the triangles in order to affect which side is visible. "Flipping normals" in a 3D app refers to face normals, which don't actually exist in a mesh in Unity as such (that's actually the winding order), and aren't the same thing as vertex normals.
Make new gameobject like child and reverse only parent mesh
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class InvertNomals : MonoBehaviour
{
void Start()
{
MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
if (filter != null && transform.parent.GetComponent<InvertNomals>() == null)
{
GameObject duplicateMesh = Instantiate(gameObject);
duplicateMesh.transform.parent = transform;
duplicateMesh.transform.Translate(transform.position);
Mesh mesh = filter.mesh;
Vector3[] normals = mesh.normals;
for (int i = 0; i < normals.Length; i++)
normals _= -normals*;*_
mesh.normals = normals; for (int m = 0; m < mesh.subMeshCount; m++) { int[] triangles = mesh.GetTriangles(m); for (int i = 0; i < triangles.Length; i += 3) { int temp = triangles[i + 0]; triangles[i + 0] = triangles[i + 1]; triangles[i + 1] = temp; } mesh.SetTriangles(triangles, m); } } } },Make new game object like children and reverse only parent mesh. using UnityEngine; [RequireComponent(typeof(MeshFilter))] public class InvertNomals : MonoBehaviour { void Start() { MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
if (filter != null && transform.parent.GetComponent() == null) { GameObject duplicateMesh = Instantiate(gameObject); duplicateMesh.transform.parent = transform; duplicateMesh.transform.Translate(transform.position); Mesh mesh = filter.mesh; Vector3[] normals = mesh.normals; for (int i = 0; i < normals.Length; i++) normals = -normals*;* mesh.normals = normals; for (int m = 0; m < mesh.subMeshCount; m++) { int[] triangles = mesh.GetTriangles(m); for (int i = 0; i < triangles.Length; i += 3) { int temp = triangles[i + 0]; triangles[i + 0] = triangles[i + 1]; triangles[i + 1] = temp; } mesh.SetTriangles(triangles, m); } } } }