duplicate and flip mesh in code

Hello unity community,

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.

Can someone help me out on this?

Many thanks

Prinds

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.

Normals are for lighting. You need to wind the triangles in the opposite order.

Okay,

Just some extra info for anyone else with this issue..

To wind the duplicated triangles in the opposite direction, I added this snippet..

for(int i=0; i<triangles.Length; i=i+3)
    {
        int tmp = triangles*;*
 _triangles *= triangles[i+2];*_
 _*triangles[i+2] = tmp;*_
 _*}*_
_*```*_
_*<p>.. which simply changes the 'direction' of the triangles</p>*_

Make new gameobject like child and reverse only parent mesh :slight_smile:

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 :wink: 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);
}
}
}
}

You could also use a double sided shader (one that does not use backface culling): Unity - Manual: ShaderLab: commands