Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by prinds · Jan 28, 2011 at 09:49 PM · meshnormalsflip

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prinds · Jan 28, 2011 at 10:20 PM 0
Share

Thanks.. your explanation makes sense..

avatar image
1
Best Answer

Answer by Jessy · Jan 28, 2011 at 09:57 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prinds · Jan 28, 2011 at 10:18 PM 0
Share

Thanks for the quick and clear answer

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges