Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by GameDeveloperAf · May 01, 2021 at 01:16 PM · meshverticesworldspacemeshfiltermatrix4x4

[SOLVED] How to convert Mesh from local to World Space? CODE FIXED!

Hi. I spawn cube on each click and when the cube spawned and the cube send its own world space vertices to Chunk gameobject for store all cube data into it and to draw. And this Chunk gameobject draws with given world space vertices. I can get my cubes' vertices with World Position using Matrix4X4. And then I can draw mesh with given world space vertices. It draws very well. But it draws well when the Chunk gameobject position in the 0, 0, 0. But when I change position of Chunk gameobject then it draws at another position because my Chunk gameobject do not convert mesh to world space. How to convert mesh to world space?

I send vertices with world space but when I change Chunk gameobject and it draws at another position. How to fix this? I spent 2 days to find this issue but I could not find


CODE to draw mesh: This script called "GlobalChunkScript"

 [System.Serializable]
   public class MeshData
   {
       public float[] Vertices;
       public int[] Triangles;
   }
   
   [System.Serializable]
   public class MeshDatabase
   {
       public List<MeshData> MeshDatas;
   }
    public class GlobalChunkScript : MonoBehaviour
    {
           public MeshFilter MeshFilter;
           public MeshDatabase MeshDatabase;
           public void DrawMesh()
           {
               Mesh mesh = new Mesh();
   
               List<Vector3> VertiexList = new List<Vector3>();
               List<int> TriangleList = new List<int>();
           
               int[] Triangles;
   
               for (int x = 0; x < MeshDatabase.MeshDatas.Count; x++)
               {
                 // Vertices
                 for (int i = 0; i < MeshDatabase.MeshDatas[x].MeshTop.vertices.Length / 3; i++)
                  {
                      VertiexList.Add(new Vector3(
                      MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3] - transform.position.x,
                      MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3 + 1] - transform.position.y,
                         MeshDatabase.MeshDatas[x].MeshTop.vertices[i * 3 + 2] - transform.position.z
                       ));
                  }
   
                 // Triangles
                 for (int i = 0; i < MeshDatabase.MeshDatas[x].Triangles.Length; i++)
                 {
                     TriangleList.Add(MeshDatabase.MeshDatas[x].Triangles[i]);
                 }
           }
   
           Triangles = TriangleList.ToArray();
   
           mesh.SetVertices(VertiexList);
           mesh.triangles = Triangles;
   
           mesh.Optimize();
           mesh.RecalculateNormals();
   
           MeshFilter.mesh = mesh;
       }
   }



And this CODE send data to GlobalChunkScript:

 public void SendMeshData
   {
   MeshFilter MeshFilter;
   MeshFilter = GetComponentInChildren<MeshFilter>();
   
   GlobalChunkScript GlobalChunkScript;
   MeshData MeshData;
   
   float[] vertices;
   int[] triangles;
   
   Matrix4x4 LocalToWorld = transform.localToWorldMatrix;
   
   vertices = new float[MeshFilter.mesh.vertexCount * 3];
   
   for (int i = 0; i < MeshFilter.mesh.vertexCount; i++)
   {
         vertices[i * 3] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).x;              
         vertices[i * 3 + 1] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).y;
         vertices[i * 3 + 2] = LocalToWorld.MultiplyPoint3x4(MeshFilter.mesh.vertices[i]).z;
    }
   
   triangles = new int[MeshFilter.mesh.triangles.Length];
   
   for (int i = 0; i < MeshFilter.mesh.triangles.Length; i++)
    {
          triangles[i] = MeshFilter.mesh.triangles[i] + 24 * GlobalChunkScript.MeshDatabase.MeshDatas.Count;
     }
   
   MeshData = new MeshData()
   {
         Vertices  = vertices,
         Triangles = triangles
    };
   
     GlobalChunkScript.MeshDatabase.MeshDatas.Add(MeshData);
   }

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

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

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert Mesh from local to World Space? 0 Answers

HELP URGENT How to convert Mesh from local to World Space? 0 Answers

URGENT How to get triangles of mesh in world space? 0 Answers

Point on a triangle. 1 Answer

Object's mesh does not move/rotate when object moves/rotates 0 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