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 raoz · Jan 05, 2013 at 11:13 AM · c#meshruntimevoxel

How to create a custom cuboids at runtime?

I am creating a voxel based game (think Minecraft adventure mode) and the performance of having many box colliders and unneeded vertices/draw calls of separate cubes was really low. My first approach was combining the cubes into single mesh, which I did(for each block type) but I still had separate colliders for each block and I still had performance issues when a bigger area was loaded and a huge spike when dynamically loading world around me due to the overhead of creating colliders.

So I decided to compress blocks into bigger cuboids but I can't work out how to create custom cuboids at runtime(specified by two Vector3s). My first approach was taking the basic cube and scaling it, but I'm not a huge fan of scaling and I had some texturing issues, so I decided to use the Mesh class and create it from scratch. I looked at the examples but it's a bit unclear, so I am asking for people with experience using the Mesh class to help me work out how to create a custom cuboid.

Thank you for taking your time!

Comment
Add comment · Show 4
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 whydoidoit · Jan 05, 2013 at 12:35 PM 0
Share

So your two Vector3s are "centre" and "size"?

avatar image raoz · Jan 05, 2013 at 12:41 PM 0
Share

No, they are origin and end (for image http://kjmaclean.com/Geometry/Cubeoctahedron_files/image004.jpg points A and G), should have been more clear in the question. Sorry.

avatar image whydoidoit · Jan 05, 2013 at 12:43 PM 0
Share

And how are you thinking of mapping a texture onto it? Texture covers each face completely?

avatar image raoz · Jan 05, 2013 at 12:45 PM 0
Share

Yes, I am gonna create a texture for the compound cuboid probably. I actually figured most out myself and already supplied an answer. Any suggestions are welcome.

1 Reply

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

Answer by raoz · Jan 05, 2013 at 12:43 PM

After looking at some examples, I managed to figure it out, it's only missing UV coords(I just put in default X-Z planar UV coords as a placeholder). The task seemed more intimidating than it actually was. Here's the code:

EDIT: Split up the vertices for texture mapping and did the texture mapping work. Also added adding a box collider.

 using UnityEngine;

using System.Collections;

public static class CreateArbitaryPrimitive {

 public static void CreateCuboid(Vector3 origin, Vector3 end)
 {
     Vector3 diff = end - origin;

     Mesh mesh = new Mesh();
     Vector3[] vertices = new Vector3[24];
     Vector2[] uv = new Vector2[vertices.Length];
     int[] triangles = new int[36];
     vertices[0] = new Vector3(0, 0, 0);//side1
     uv[0] = new Vector2(1 / 6f, 0);
     vertices[1] = new Vector3(diff.x, 0, 0);//side1
     uv[1] = new Vector2(0 / 6f, 0);
     vertices[2] = new Vector3(0, diff.y, 0);//side1
     uv[2] = new Vector2(1 / 6f, 1);
     vertices[4] = new Vector3(diff.x, diff.y, 0);//side1
     uv[4] = new Vector2(0 / 6f, 1);
     vertices[3] = new Vector3(0, 0, diff.z); //side2
     uv[3] = new Vector2(1 / 6f, 0);
     vertices[8] = new Vector3(0, diff.y, 0); //side2
     uv[8] = new Vector2(2 / 6f, 1);
     vertices[5] = new Vector3(0, diff.y, diff.z);//side2
     uv[5] = new Vector2(1 / 6f, 1);
     vertices[9] = new Vector3(0, 0, 0);//side2
     uv[9] = new Vector2(2 / 6f, 0);
     vertices[10] = new Vector3(0, diff.y, diff.z);//side3
     uv[10] = new Vector2(3 / 6f, 1);
     vertices[6] = new Vector3(diff.x, 0, diff.z);//side3
     uv[6] = new Vector2(2 / 6f, 0);
     vertices[7] = new Vector3(diff.x, diff.y, diff.z);//side3
     uv[7] = new Vector2(2 / 6f, 1);
     vertices[11] = new Vector3(0, 0, diff.z);//side3
     uv[11] = new Vector2(3 / 6f, 0);
     vertices[12] = new Vector3(diff.x, diff.y, diff.z);//side4
     uv[12] = new Vector2(4 / 6f, 1);
     vertices[13] = new Vector3(diff.x, 0, diff.z);//side4
     uv[13] = new Vector2(4 / 6f, 0);
     vertices[14] = new Vector3(diff.x, diff.y, 0);//side4
     uv[14] = new Vector2(3 / 6f, 1);
     vertices[15] = new Vector3(diff.x, 0, 0);//side4
     uv[15] = new Vector2(3 / 6f, 0);
     vertices[16] = new Vector3(0, 0, 0);//bottom
     uv[16] = new Vector2(5 / 6f, 0);
     vertices[17] = new Vector3(diff.x, 0, diff.z);//bottom
     uv[17] = new Vector2(4 / 6f, 1);
     vertices[18] = new Vector3(0, 0, diff.z);//bottom
     uv[18] = new Vector2(5 / 6f, 1);
     vertices[19] = new Vector3(diff.x, 0, 0);//bottom
     uv[19] = new Vector2(4 / 6f, 0);
     vertices[20] = new Vector3(diff.x, diff.y, diff.z);//top
     uv[20] = new Vector2(1, 1);
     vertices[21] = new Vector3(0, diff.y, 0); //top
     uv[21] = new Vector2(5 / 6f, 0);
     vertices[22] = new Vector3(0, diff.y, diff.z);//top
     uv[22] = new Vector2(5 / 6f, 1);
     vertices[23] = new Vector3(diff.x, diff.y, 0);//top
     uv[23] = new Vector2(1, 0);//top




     #region Triangles

     //sides
     //side1
     triangles[0] = 2;
     triangles[1] = 1;
     triangles[2] = 0;

     triangles[3] = 1;
     triangles[4] = 2;
     triangles[5] = 4;

     //side2
     triangles[6] = 3;
     triangles[7] = 8;
     triangles[8] = 9;

     triangles[9] = 5;
     triangles[10] = 8;
     triangles[11] = 3;

     //side3
     triangles[12] = 10;
     triangles[13] = 6;
     triangles[14] = 7;

     triangles[15] = 11;
     triangles[16] = 6;
     triangles[17] = 10;


     //side4
     triangles[18] = 12;
     triangles[19] = 13;
     triangles[20] = 14;

     triangles[21] = 14;
     triangles[22] = 13;
     triangles[23] = 15;

     //bottom

     triangles[24] = 16;
     triangles[25] = 17;
     triangles[26] = 18;

     triangles[27] = 19;
     triangles[28] = 17;
     triangles[29] = 16;

     //top

     triangles[30] = 22;
     triangles[31] = 20;
     triangles[32] = 21;

     triangles[33] = 21;
     triangles[34] = 20;
     triangles[35] = 23;
     #endregion

     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.uv = uv;

     mesh.RecalculateNormals();
     mesh.RecalculateBounds();

     GameObject go = new GameObject("Cuboid", typeof(MeshRenderer), typeof(MeshFilter), typeof(BoxCollider));
     go.transform.position = origin;
     go.GetComponent<MeshFilter>().mesh = mesh;
     BoxCollider collider = go.GetComponent<BoxCollider>();
     collider.size = mesh.bounds.size;
     collider.center = mesh.bounds.size / 2;
 }

}

Comment
Add comment · Show 8 · 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 whydoidoit · Jan 05, 2013 at 12:46 PM 1
Share

You might want to dupe up some of the vertices if you want to effectively have them with different UV coordinates (which I think you do...)

avatar image whydoidoit · Jan 05, 2013 at 12:47 PM 0
Share

So F in your diagram for instance probably needs a different UV for the face it shares with A to the coordinates with the face it shares with G.

avatar image raoz · Jan 05, 2013 at 12:49 PM 0
Share

I see what you mean, didn't think about that. Figuring the whole thing out in my head is a pain.

avatar image whydoidoit · Jan 05, 2013 at 12:54 PM 2
Share

Yeah - I know :) So your code produced a very efficient cube and would be correct if you want to create a single texture that wraps the cube (usually this is a cross shape - but that is a inefficient as there is wasted space so you probably want to "fill in the gaps" - even so the texture needs to be square)

     X     
   XXXX 
     X
avatar image raoz · Jan 05, 2013 at 12:58 PM 0
Share

I could probably move the cross to a line, but this is getting really complicated :) I better create a spec for the texture in my spec wiki :)

Show more comments

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

10 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

Related Questions

Mesh Creator (editor script) At Runtime 1 Answer

Cube Voxel not working. Help please 1 Answer

How can I add complex mesh data directly to a list through code? 1 Answer

How come do I keep getting this error on my voxel script 1 Answer

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