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 ob103ninja · Nov 19, 2014 at 04:54 PM · c#mesharraysvertices

Setting mesh.vertices to a 3-dimensional array

Ello everyone! this is my first time posting in this forum.

Anyways, I have a code that I wrote, of which I plan to create a voxel terrain from.

I have some 3d arrays set up, so that it can be simpler to generate a "sponge" of points. In other words, I will not only have points on the outside of the mesh, but filling the inside as well, so as to later be able to just switch them on and off. My plan is that this will be used to decide which vertices are triangulated with other adjacent vertices.

Alright, to my point.

I have a code here, and I have assigned the vertices, uvs and tris to the mesh like so -

 using UnityEngine;
 using System.Collections;
 
 public class Voxel : MonoBehaviour {
 
     //initializes the size changing values
     public int WidthX;
     public int WidthY;
     public int WidthZ;
 
     //initializes the arrays
     public Vector3[,,] Verts;
     public Vector2[,,] UVs;
     public int[,,] Tris;
 
     void Start () {
 
         //sets the voxel size
         WidthX = 5;
         WidthY = 10;
         WidthZ = 5;
 
         //applies the voxelmesh to the game object
         MeshFilter mf = GetComponent<MeshFilter>();
         Mesh vox = new Mesh();
         mf.mesh = vox;
         vox = GetComponent<MeshFilter>().mesh;
         vox.Clear();
 
         //set vertices, uvs, and tris here
         Verts = new Vector3[WidthX,WidthY,WidthZ];
 
         UVs = new Vector2[WidthX,WidthY,WidthZ];
 
         Tris = new int[WidthX*3,WidthY*3,WidthZ*3];
 
         //apply values to mesh here
         vox.vertices = Verts;
         vox.uv = UVs;
         vox.triangles = Tris;
 
         vox.RecalculateNormals();
         vox.RecalculateBounds();
 
     }
 }

So, when I hit F8 to build it, it says "Cannot convert Vector3[] to Vector3[,,]". No errors before I assign them, so I know that the 3d arrays exist and work. My problem is the compatibility.

Any suggestions?

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

1 Reply

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

Answer by robertbu · Nov 19, 2014 at 05:42 PM

Mesh.vertices, Mesh.uv, and Mesh.triangles are all one dimensional arrays, so you are going going to have to unwind your 3D arrays somehow. Iterating over them and building the 1D array is probably the most efficient, but you can have simpler code using LinQ:

http://stackoverflow.com/questions/13822750/how-to-use-linq-on-a-multidimensional-array-to-unwind-the-array

In addition, if I understand what you are doing correctly, you've sized your triangles array incorrectly. You will have two triangles (three entries each) for every four vertices. So the number of entries will be:

   no. vertices / 4 * 2 * 3
Comment
Add comment · Show 3 · 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 ob103ninja · Nov 19, 2014 at 06:35 PM 0
Share

I see that I have sized the triangles wrong, but just to reiterate, this is a voxel terrain, so the triangles to vertices ratio may change.

And please note, there are Vector3's and Vector2's, with xyz values, so that unwinder may give an unwanted result.

avatar image robertbu · Nov 19, 2014 at 08:55 PM 0
Share

The way you describe your problem you are using cube voxels. Due to uv mapping and/or vertex colors, you will not share vertices. The result is that for every quad you want to show, you will have two triangles.

I cannot see how unwinding will give unwanted results. If it were me,for performance reasons, I'd use a 1D array and map to it mathematically.

avatar image ob103ninja · Nov 19, 2014 at 09:46 PM 0
Share

I simply have a 3d array for the purpose of readability, considering that the chunk will usually be massive. However, if I find the program too slow afterwards, I'll change it.

And I think I see your point with not getting unwanted results :P

Thanks for your time.

oh, and you messed up a bit on sizing the array xD

 ((no. vertices) / 4)*2*3;

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

how to make a planet 1 Answer

Get all vertices in gameObject to array. 1 Answer

Generating a 3D Mesh from Audio data 0 Answers

Procedurally generated mesh acting weird. 0 Answers

Multiple Cars not working 1 Answer


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