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 1MachoKualquiera · Feb 24, 2015 at 03:34 AM · javascriptinstantiatemeshobject

When generating a procedural voxel mesh, i get something weird.

Okay, so this is my code

 #pragma strict
 var ChunkPrefab : GameObject;
 var World : int[,,];
 var WorldSize : Vector3;
 var WorldRandomizer : int; 
 var HeightMultiplier : int;
 var LastChunk : GameObject;
 var LastMesh : Mesh;
 var LastFaces : boolean[];
 var LastVerts : Array;
 var LastTriangles : Array;
 var VertsAmount : int;
 function Create () {
     WorldRandomizer = Random.Range(0, 999999);
     World = new int[WorldSize.x, WorldSize.y, WorldSize.z];
     for(var x : float = 0; x < WorldSize.x; x++) {
         for(var z : float = 0; z < WorldSize.z; z++) {
             World[x, PerlinCalculations(x, z), z] = 1;
         }
     }
     LoadChunk(0, 0);
 }
 function PerlinCalculations (x : float, z: float) {
     return(Mathf.RoundToInt(Mathf.PerlinNoise(WorldRandomizer+x/HeightMultiplier, WorldRandomizer+z/HeightMultiplier)*HeightMultiplier));
 }
 function LoadChunk(x : double, z : double) {
     // 0 top
     // 1 front
     // 2 bot
     // 3 back
     // 4 left
     // 5 right
     LastChunk = GameObject.Instantiate(ChunkPrefab);
     LastChunk.transform.position = new Vector3(x*8, z*8);
     LastMesh = LastChunk.GetComponent.<MeshFilter>().mesh;
     LastVerts = new Array ();
     LastTriangles = new Array ();
     VertsAmount = 0;
     for(var cx : int = 0; cx < 8; cx++) {
         for(var cz : int = 0; cz < 8; cz++) {
             for(var cy : int = 0; cy < WorldSize.y; cy++) {
                 if(World[cx + x, cy, cz + z] == 1) {
                     LastFaces = new boolean[6];
                     //World edge faces
                     if(cy == WorldSize.y - 1) {
                         LastFaces[0] = true;
                     }
                     else {
                         if(World[cx + x, cy + 1, cz + z] == 1) {} else {
                             LastFaces[0] = true;
                         }
                     }
                     if(cx == 0) {
                         Debug.Log("Test");
                         LastFaces[1] = true;
                     }
                     else {
                         if(World[cx + x - 1, cy, cz + z] == 1) {} else {
                             LastFaces[1] = true;
                         }
                     }
                     if(cy == 0) {
                         LastFaces[2] = true;
                     }
                     else {
                         if(World[cx + x, cy - 1, cz + z] == 1) {} else {
                             LastFaces[2] = true;
                         }
                     }
                     if(cx == WorldSize.x - 1) {
                         LastFaces[3] = true;
                     }
                     else {
                         if(World[cx + x + 1, cy, cz + z] == 1) {} else {
                             LastFaces[3] = true;
                         }
                     }
                     if(cz == 0) {
                         LastFaces[4] = true;
                     }
                     else {
                         if(World[cx + x, cy, cz + z - 1] == 1) {} else {
                             LastFaces[4] = true;
                         }
                     }
                     if(cz == WorldSize.z - 1) {
                         LastFaces[5] = true;
                     }
                     else {
                         if(World[cx + x, cy, cz + z + 1] == 1) {} else {
                             LastFaces[5] = true;
                         }
                     }
                     CreateVox(new Vector3(cx + x, cy, cz + z));
                 }
             }
         }
     }
     LastMesh.Clear ();
     LastMesh.vertices = LastVerts;
     LastMesh.triangles = LastTriangles;
     LastMesh.Optimize ();
     LastMesh.RecalculateNormals();
 }
 function CreateVox (Place : Vector3) {
     LastVerts.Add(new Vector3(Place.x + 0, Place.y + 0, Place.z + 0));
     LastVerts.Add(new Vector3(Place.x + 0, Place.y + 1, Place.z + 0));
     LastVerts.Add(new Vector3(Place.x + 1, Place.y + 1, Place.z + 0));
     LastVerts.Add(new Vector3(Place.x + 1, Place.y + 0, Place.z + 0));
     LastVerts.Add(new Vector3(Place.x + 0, Place.y + 0, Place.z + 1));
     LastVerts.Add(new Vector3(Place.x + 0, Place.y + 1, Place.z + 1));
     LastVerts.Add(new Vector3(Place.x + 1, Place.y + 1, Place.z + 1));
     LastVerts.Add(new Vector3(Place.x + 1, Place.y + 0, Place.z + 1));
     if(LastFaces[0]) {
         LastTriangles.Add(2+VertsAmount);
         LastTriangles.Add(1+VertsAmount);
         LastTriangles.Add(5+VertsAmount);
         LastTriangles.Add(6+VertsAmount);
         LastTriangles.Add(2+VertsAmount);
         LastTriangles.Add(5+VertsAmount);
     }
     if(LastFaces[1]) {
         LastTriangles.Add(0+VertsAmount);
         LastTriangles.Add(1+VertsAmount);
         LastTriangles.Add(2+VertsAmount);
         LastTriangles.Add(2+VertsAmount);
         LastTriangles.Add(3+VertsAmount);
         LastTriangles.Add(0+VertsAmount);
     }
     if(LastFaces[2]) {
         LastTriangles.Add(0+VertsAmount);
         LastTriangles.Add(3+VertsAmount);
         LastTriangles.Add(4+VertsAmount);
         LastTriangles.Add(3+VertsAmount);
         LastTriangles.Add(7+VertsAmount);
         LastTriangles.Add(4+VertsAmount);
     }
     if(LastFaces[3]) {
         LastTriangles.Add(6+VertsAmount);
         LastTriangles.Add(5+VertsAmount);
         LastTriangles.Add(7+VertsAmount);
         LastTriangles.Add(7+VertsAmount);
         LastTriangles.Add(5+VertsAmount);
         LastTriangles.Add(4+VertsAmount);
     }
     if(LastFaces[4]) {
         LastTriangles.Add(5+VertsAmount);
         LastTriangles.Add(1+VertsAmount);
         LastTriangles.Add(4+VertsAmount);
         LastTriangles.Add(4+VertsAmount);
         LastTriangles.Add(1+VertsAmount);
         LastTriangles.Add(0+VertsAmount);
     }
     if(LastFaces[5]) {
         LastTriangles.Add(2+VertsAmount);
         LastTriangles.Add(6+VertsAmount);
         LastTriangles.Add(3+VertsAmount);
         LastTriangles.Add(6+VertsAmount);
         LastTriangles.Add(7+VertsAmount);
         LastTriangles.Add(3+VertsAmount);
     }
     VertsAmount+= 8;
 }

ChunkPrefab is just a simple GameObject with a MeshFilter and a MeshRenderer. And I'm getting something like this: alt text

Please, help!

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
1
Best Answer

Answer by maccabbe · Feb 24, 2015 at 05:01 AM

The main problem with the code is that you have flipped x and z axis. For instance if 1 is front face then you should be checking World[cx, cy, cz+1] instead of World[cx + x - 1, cy, cz + z]. Allr 4 x/z checks need to be checked.

The strange shading is because in unity normals are per vertex so a vertex that is on 3 faces will have a normal that is the average of the normal of the faces. To fix the shading you should make 4 vertices per face so 4*6 vertices per cube.

Finally you are making alot of vertices that aren't being used. Since you probably need 4 vertices per face anyways just generate then with the triangles.

Comment
Add comment · Show 1 · 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 1MachoKualquiera · Feb 24, 2015 at 08:12 PM 0
Share

Oh, thanks. I don't why i did that stupid mistake. Another question, to unload the chunks should i just destroy the GameObject?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Object instantiation Logic Javascript. 1 Answer

Combining Meshes? 1 Answer

Cannot assign prefab in Inspector? 2 Answers

Setting a variable to an instantiated object. 1 Answer

Storing and using weapons and objects 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