- Home /
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
Answer by Eric5h5 · Jan 28, 2011 at 09:57 PM
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.
Answer by Jessy · Jan 28, 2011 at 09:57 PM
Normals are for lighting. You need to wind the triangles in the opposite order.
Answer by prinds · Jan 28, 2011 at 10:27 PM
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[i];
triangles[i] = triangles[i+2];
triangles[i+2] = tmp;
}
.. which simply changes the 'direction' of the triangles
Answer by UDN_1fb6f943-d639-43c7-816c-76367e5e91b7 · Nov 11, 2019 at 03:01 PM
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[i] = -normals[i];
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<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[i] = -normals[i];
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);
}
}
}
}
Answer by viesc123 · Nov 11, 2019 at 10:06 PM
You could also use a double sided shader (one that does not use backface culling): https://docs.unity3d.com/Manual/SL-CullAndDepth.html
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Conform mesh to arbitrary surfaces? 3 Answers
Procedural mesh, half the faces have wrong normals 1 Answer
What are normals? 1 Answer
Mesh with two materials made by script 2 Answers