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 Ozy51 · May 01, 2015 at 04:32 PM · meshproceduralnormalsflipping

Meshes flipping on procedural generation

Hi,

Im trying to procedural recycle some game objects that i created in a pool of objects. When the camera position goes some distance ahead of the first object it will reconfigure their mesh/position so it can be rendered and positioned ahead of the last object.

I use this method to Set the mesh to its new values:

 public void SetMesh(GameObject go,float center,float width,float height,float distance, float floor){
             Mesh meshset = go.GetComponent<MeshFilter>().mesh;
             meshset.vertices = new Vector3[] {
                 new Vector3(center-width, floor, 0),
                 new Vector3(center+width, floor, 0),
                 new Vector3(center+width, floor+height, 0),
                 new Vector3(center-width, floor+height, 0)
             };
 
 
             meshset.RecalculateBounds ();
             meshset.RecalculateNormals ();
             meshset.Optimize ();
             go.GetComponent<MeshFilter> ().mesh = meshset;
         }

and the method to make the verification of the position of the camera vs position of object with this on update:

 void Update () {
         float posz = GameObject.Find ("Main Camera").transform.position.z;
         
         if (listab[count2].transform.position.z < posz - 3.0F) {
             float lastdistance = listab[count2].transform.position.z;
             float x = Random.Range(-5.0F, 5.0F) * 0.1F;
             float y = Random.Range(5.0F, 10.0F) * 0.1F;
 
             Barrier novo = new BlueBarrier();
             novo.SetMesh(listab[count2],0,x,y,lastdistance + 10.0F, floor);
             listab[count2].transform.position += new Vector3(0,0,10.0F);
             //listab[count2].transform.Rotate(0, 90, 0);
 
             if(count2==listab.Count-1){ count2 =0;
             }
             else{ count2++;}
         }


What happens and i dont understand why is that as seen in the following images some of them are set right and some are set flipped :

alt text

alt text

As you can see on the code i have a commented rotation and what it does is that it sets some meshs right, some meshs get flipped and some get rotated. Tried to change from Update to LateUpdate to FixedUpdate and the results are the same. Anyone know how to solve this?

Thank you

2.jpg (41.0 kB)
3.jpg (41.2 kB)
Comment
Add comment · Show 6
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 Sessional · May 01, 2015 at 04:41 PM 0
Share

Just for clarification: when you generate these meshes, do they always have exactly 4 vertices? When generating meshes procedurally you usually set the vertices, set the triangles and then set the uvs/colors if any.

avatar image Ozy51 · May 01, 2015 at 06:45 PM 0
Share

the code to generate the meshes at first is this:

 public $$anonymous$$esh Create$$anonymous$$esh(float center,float width, float height, float distance, float floor)
             {
                 $$anonymous$$esh m = new $$anonymous$$esh();
                 m.name = name + "$$anonymous$$esh";
                 m.vertices = new Vector3[] {
                     new Vector3(center+width, floor, distance),
                     new Vector3(center-width, floor, distance),
                     new Vector3(center-width, floor+height, distance),
                     new Vector3(center+width, floor+height, distance)
                 };
                 m.uv = new Vector2[] {
                     new Vector2 (0, 0),
                     new Vector2 (0, 1),
                     new Vector2(1, 1),
                     new Vector2 (1, 0)
                 };
                 m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
                 m.RecalculateNormals();
                 m.RecalculateBounds ();
                 
                 return m;
             }

and :

 public GameObject Gen(float center,float width, float height, float distance, float floor, int count){
             GameObject quad = new GameObject(name + count);
             $$anonymous$$eshFilter meshFilter = ($$anonymous$$eshFilter)quad.AddComponent(typeof($$anonymous$$eshFilter));
             meshFilter.mesh = Create$$anonymous$$esh(center,width, height, distance, floor);
             $$anonymous$$eshRenderer renderer = quad.AddComponent(typeof($$anonymous$$eshRenderer)) as $$anonymous$$eshRenderer;
             renderer.material.shader = Shader.Find ("Standard");
             Texture2D tex = new Texture2D(1, 1);
             tex.SetPixel(0, 0, color);
             tex.Apply();
             renderer.material.mainTexture = tex;
             renderer.material.color = color;
             //BoxCollider collider = quad.AddComponent (typeof(BoxCollider)) as BoxCollider;
             quad.transform.position = new Vector3 (center, floor, distance);
             //count++; // needs to be set outside
             return quad;
             
         }



avatar image Sessional · May 01, 2015 at 07:19 PM 0
Share

Do you need to regenerate them? Or can you just move the original object?

avatar image Ozy51 · May 02, 2015 at 11:54 AM 0
Share

What i do is use the Gen() method to generate them and when i have to reposition them i use the method Set$$anonymous$$esh() which changes the vertices and transforms its position. What happens is that as you see in the images, some faces are facing the camera and some are flipped or reversed, and they should all be facing the camera

avatar image siaran · May 02, 2015 at 01:42 PM 0
Share

I see that you set the normals with RecalculateNormals(), maybe you should set them manually?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Seam on Procedural Mesh 1 Answer

Creating a mesh out of a linerenderer - how do you convert revolved vertices set to triangles and uv's 1 Answer

Procedural Mesh, Strange Clipping 1 Answer

[SOLVED]Procedural generated side by side cubes without 'seams'? 2 Answers

Determine whether or not model is "inside out" 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