• 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 rabirland · Jan 05, 2017 at 06:56 AM · meshproceduralprocedural meshvoxelprocedural-generation

What is wrong with this mesh editing code?

I'm building a procedurally generated mesh, and need to fix normals at the chunk edges. I wrote this method to solve this for me, however, even tho in my Debugger screen the 2 vertex in world space are in the very same position, this code does not find them somehow. I mean the Vector3.Distance will be some random number, and sometimes I get crazy values which is not even the reality. For example when 2 vertices has the same position (-4, 0, 0) in world space, this code fids something (12, 0, 0) and I have no idea how.

 void MixNormals(VoxelChunk chunk1, VoxelChunk chunk2)
     {
         Mesh mesh1 = null;
         Mesh mesh2 = null;
         if (chunk1.chunkObject.GetComponent<MeshFilter>() != null) mesh1 = chunk1.chunkObject.GetComponent<MeshFilter>().mesh;
         if (chunk2.chunkObject.GetComponent<MeshFilter>() != null) mesh2 = chunk2.chunkObject.GetComponent<MeshFilter>().mesh;
 
         if (mesh1 != null && mesh2 != null)
         {
             Vector3[] vertices1 = mesh1.vertices;
             Vector3[] vertices2 = mesh2.vertices;
             List<Vector3> normals1 = new List<Vector3>(mesh1.normals);
             List<Vector3> normals2 = new List<Vector3>(mesh2.normals);
 
             for (int i = 0; i < mesh1.vertexCount; i++)
             {
                 for (int j = 0; j < mesh2.vertexCount; j++)
                 {
                     float dist = Vector3.Distance(chunk1.chunkObject.transform.position + vertices1[i], chunk2.chunkObject.transform.position + vertices2[j]);
                     if (dist <= vertexMergeMaxDistance)
                     {
                         normals1[i] = normals2[j] = (normals1[i] + normals2[j]) / 2;
                     }
                 }
             }
         }
     }

Comment
Add comment · Show 2
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 villevli · Jan 05, 2017 at 07:49 AM 0
Share

On line 19. If your chunkObject.transform has any rotation or scaling then adding the position is not enough to find the world space position of the vertex. Use chunkObject.transform.TransformPoint(vertex)

avatar image rabirland villevli · Jan 05, 2017 at 09:11 AM 0
Share

They don't have any rotation, only translation. Also the problem was that I used a copy of the vertices array, and I had to use SetNormals at the end...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Proceducal Mesh terrain 2 Answers

Creating caves in voxel procedural mesh 0 Answers

Editor lags when coming out of play mode...? 0 Answers

How can I implement "3D voxel digging"? 2 Answers

How do I calculate the UV's for a procedurally generated shape? 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