Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 R3D24 · Jul 18, 2017 at 11:22 PM · meshesvoxelscombineinstance

How can I make this code add to the mesh instead of over writing it?

I am trying to make a system like Minecraft where an array of values is used to generate a 32^3 cubic chunk, and I'm working on making it so that all block faces in a 'chunk' are combined as one mesh... except for one problem:

In the code below used to render a face of a block in a given position and rotation, instead of adding a face to the mesh of a 'chunk' it is overwriting that chunk's mesh... So I've tried to make a new mesh on a child and combine them, but that is giving me this error "Combine mesh instance 0 is null" on the first attempt to combine meshes, the following attempts to combine seem to do nothing.

Is there a better way to read the cubic array and make a mesh for the visible faces of the blocks?

 void AddFace (int x, int y, int z, int side){
     MeshFilter mf = transform.GetChild (0).gameObject.GetComponent<MeshFilter> ();
     Mesh mesh = mf.mesh;
     //Vertices!
     Vector3[] vert = new Vector3[4];
     if (side == 0) {
         vert [0] = new Vector3 (x + 1, y, z + 1);
         vert [1] = new Vector3 (x, y, z + 1);
         vert [2] = new Vector3 (x + 1, y + 1, z + 1);
         vert [3] = new Vector3 (x, y + 1, z + 1);
     } else if (side == 1) {
         vert [0] = new Vector3 (x + 1, y, z);
         vert [1] = new Vector3 (x + 1, y, z + 1);
         vert [2] = new Vector3 (x + 1, y + 1, z);
         vert [3] = new Vector3 (x + 1, y + 1, z + 1);

            **Ect...**

     } else if (side == 5) {
         vert [0] = new Vector3 (x + 1, y, z);
         vert [1] = new Vector3 (x, y, z);
         vert [2] = new Vector3 (x + 1, y, z + 1);
         vert [3] = new Vector3 (x, y, z + 1);
     }
     //Triangles!
     int[] tri = new int[6];

     tri [0] = 0;
     tri [1] = 2;
     tri [2] = 1;
 
     tri [3] = 2;
     tri [4] = 3;
     tri [5] = 1;

     //Normals!
     Vector3[] norm = new Vector3[4];
     if (side == 0) {
         norm [0] = Vector3.forward;
         norm [1] = Vector3.forward;
         norm [2] = Vector3.forward;
         norm [3] = Vector3.forward;
     }
             **Ect...**

     else if (side == 5) {
         norm [0] = -Vector3.up;
         norm [1] = -Vector3.up;
         norm [2] = -Vector3.up;
         norm [3] = -Vector3.up;
     }

     //UVs
     Vector2[] uv = new Vector2[4];

     uv [0] = new Vector2 (0, 0);
     uv [1] = new Vector2 (0, 1);
     uv [2] = new Vector2 (1, 0);
     uv [3] = new Vector2 (1, 1);

     //Assign!
     mesh.vertices = vert;
     mesh.triangles = tri;
     mesh.normals = norm;
     mesh.uv = uv;

     //combine!
     MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
     CombineInstance[] combine = new CombineInstance[meshFilters.Length];
     Destroy (this.gameObject.GetComponent<MeshCollider> ());
     int i = 0;
     while (i < meshFilters.Length) {
         combine [i].mesh = meshFilters [i].sharedMesh;
         combine [i].transform = meshFilters [i].transform.localToWorldMatrix;
         meshFilters [i].gameObject.SetActive (false);
         i++;
     }
     transform.GetComponent<MeshFilter> ().mesh = new Mesh ();
     transform.GetComponent<MeshFilter> ().mesh.CombineMeshes (combine, true);
     transform.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
     transform.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
     transform.GetComponent<MeshFilter> ().mesh.Optimize ();

     this.gameObject.AddComponent<MeshCollider> ();
     transform.gameObject.SetActive (true);
 }
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

68 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

Related Questions

Cubic Voxel World 0 Answers

Why can we add vertex alpha if there are no proper shaders? 1 Answer

Color mismatch between meshes 0 Answers

Ideas on Cloning A Game 0 Answers

my invertion of mesh.triangles is not working correctly, any idea why? 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