• 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 maroltl · Jan 06, 2015 at 05:33 PM · arrayvoxel

Voxel terrain generation

I am trying to follow this tutorial, written in c# and I am converting it to javascript. I have a problem translating byte array, either I am getting bunch of errors, or my code just doesnt do anything. Here is code from tutorial

if you want to check full c# code it is available HERE

Here is my code. I assume there is something wrong with byte array in GenTerrain and BuildMesh functions. I find almost none documentation on web about byte arrays in javascript in unity.

 #pragma strict
 
 public var newVertices = new Array (); 
 public var newTriangles = new Array ();
 public var newUV = new Array ();
 
 public var blocks : byte[,];
 private var squareCount : int;
 
 var mesh : Mesh;
 
 private var texUnit : float = 0.25;
 private var texStone : Vector2 = new Vector2 (0, 0);
 private var texGrass : Vector2 = new Vector2 (0, 1);
 
 
 
 
 
 function Start () {
     
     mesh = gameObject.GetComponent(MeshFilter).mesh;
     
     var x : float = transform.position.x;
     var y : float = transform.position.y;
     var z : float = transform.position.z;
     
     var newVerticesBuildIn : Vector3[] = newVertices.ToBuiltin(Vector3) as Vector3[];    
     
     GenTerrain();
     BuildMesh();
     UpdateMesh();    
 }
 
 function UpdateMesh () {
 
     mesh.Clear();
     mesh.vertices = newVertices;
     mesh.triangles = newTriangles;
     mesh.uv = newUV;
     mesh.Optimize ();
     mesh.RecalculateNormals ();
     
     squareCount = 0;
     newVertices.Clear();
     newTriangles.Clear();
     newUV.Clear();
 }
 
 function GenSquare (x : int,  y : int, texture : Vector2) {
 
     newVertices.Add (new Vector3 (x, y, 0));
     newVertices.Add (new Vector3 (x + 1, y, 0));
     newVertices.Add (new Vector3 (x + 1, y - 1 , 0));
     newVertices.Add (new Vector3 (x , y - 1, 0));
     
     newTriangles.Add(0);
      newTriangles.Add(1);
      newTriangles.Add(3);
      newTriangles.Add(1);
      newTriangles.Add(2);
       newTriangles.Add(3);
       
       newUV.Add(new Vector2(texUnit * texStone.x, texUnit * texStone.y + texUnit));
       newUV.Add(new Vector2(texUnit * texStone.x + texUnit, texUnit * texStone.y + texUnit));
       newUV.Add(new Vector2(texUnit * texStone.x + texUnit, texUnit * texStone.y));
       newUV.Add(new Vector2(texUnit * texStone.x, texUnit * texStone.y));
       
       squareCount++;
       
       
 }
 
 function GenTerrain(){
 
     blocks = new byte[10,10];
     for(var px = 0; px < blocks.GetLength(0); px++){
         for(var py = 0; py < blocks.GetLength(1); py++){
             if(py == 5){
                 blocks[px,py] = 2;
             } else if(blocks.length < 5){
                 blocks[px, py] = 1; 
             }
         }
     }
             
 
 }
 
 function BuildMesh(){
 
     for(var px : int = 0; px < blocks.GetLength(0); px++){
         for(var py : int = 0; py < blocks.GetLength(1); py++){
             if(blocks[px, py] == 1){
                 GenSquare(px, py, texStone);
             } else if (blocks[px, py] == 2){
                 GenSquare(px, py, texGrass);
             }
         }
     }
     
 }

I am strugling with this for a few days and I would really appreciate any help.

Comment
Add comment · Show 6
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 maroltl · Jan 06, 2015 at 05:34 PM 0
Share

In this javascript code Build mesh and/or GenTerrain functions are not working properly, as I get no result from them.

avatar image Eric5h5 · Jan 06, 2015 at 06:30 PM 0
Share

You didn't define any newVertices etc. lists anywhere. Byte arrays in Unityscript are the same as in C#.

avatar image maroltl · Jan 06, 2015 at 07:00 PM 0
Share

ohh sorry, I just didnt paste it. I edited the code now, so it is complete script. All Vertices, triangles and UVs are defined. One square is created and texture is applied to it, but it should create an array of 10*10 squares with different textures I suppose

avatar image Eric5h5 · Jan 06, 2015 at 07:02 PM 1
Share

Don't use the JS Array class. Use generic Lists, like the C# version.

avatar image maroltl · Jan 06, 2015 at 09:25 PM 0
Share

I used generic lists ins$$anonymous$$d of arrays, except for the byte array, as it is in the c# code. I think that this byte array is causing problems, since one square is generated and this means that for loops in last two functions (GenTerrain() and Build$$anonymous$$esh()) is looped only once. But I just cant figure out what to do.

 #pragma strict
 import System.Collections.Generic;
 
 public var newVertices = List.<Vector3>();
 public var newTriangles = List.<int>();
 public var newUV = List.<Vector2>();
 
 
 public var blocks : byte[,];
 
 private var squareCount : int;
 
 var mesh : $$anonymous$$esh;
 
 private var texUnit : float = 0.25;
 private var texStone : Vector2 = new Vector2 (0, 0);
 private var texGrass : Vector2 = new Vector2 (0, 1);
 
 
 
 
 
 function Start () {
     
     mesh = gameObject.GetComponent($$anonymous$$eshFilter).mesh;
     
     var x : float = transform.position.x;
     var y : float = transform.position.y;
     var z : float = transform.position.z;
     
     GenTerrain();
     Build$$anonymous$$esh();
     Update$$anonymous$$esh();    
 }
 
 function Update$$anonymous$$esh () {
 
     mesh.Clear();
     mesh.vertices = newVertices.ToArray();
     mesh.triangles = newTriangles.ToArray();
     mesh.uv = newUV.ToArray();
     mesh.Optimize ();
     mesh.RecalculateNormals ();
     
     squareCount = 0;
     newVertices.Clear();
     newTriangles.Clear();
     newUV.Clear();
 }
 
 function GenSquare (x : int,  y : int, texture : Vector2) {
 
     newVertices.Add (new Vector3 (x, y, 0));
     newVertices.Add (new Vector3 (x + 1, y, 0));
     newVertices.Add (new Vector3 (x + 1, y - 1 , 0));
     newVertices.Add (new Vector3 (x , y - 1, 0));
     
     newTriangles.Add(0);
      newTriangles.Add(1);
      newTriangles.Add(3);
      newTriangles.Add(1);
      newTriangles.Add(2);
       newTriangles.Add(3);
       
       newUV.Add(new Vector2(texUnit * texStone.x, texUnit * texStone.y + texUnit));
       newUV.Add(new Vector2(texUnit * texStone.x + texUnit, texUnit * texStone.y + texUnit));
       newUV.Add(new Vector2(texUnit * texStone.x + texUnit, texUnit * texStone.y));
       newUV.Add(new Vector2(texUnit * texStone.x, texUnit * texStone.y));
       
       squareCount++;
       
       
 }
 
 function GenTerrain(){
 
     blocks = new byte[10,10];
     
     for(var px : int = 0; px < blocks.GetLength(0); px++){
         for(var py : int = 0; py < blocks.GetLength(1); py++){
             if(py == 5){
                 blocks[px,py] = 2;
             } else if(blocks.Length > 5){
                 blocks[px, py] = 1; 
             }
         }
     }
             
 
 }
 
 function Build$$anonymous$$esh(){
 
     for(var px : int = 0; px < blocks.GetLength(0); px++){
         for(var py : int = 0; py < blocks.GetLength(1); py++){
             if(blocks[px, py] == 1){
                 GenSquare(px, py, texStone);
             } else if (blocks[px, py] == 2){
                 GenSquare(px, py, texGrass);
             }
         }
     }
     
 }

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Access a multidimensional array inside an array 1 Answer

Both way expandable array for voxels 1 Answer

Implementing multi-texture shading with the marching cube algorithm (voxels) 1 Answer

How to split 3D array in to two - Breaking voxelized objects. 0 Answers

Set each sprite of image in an array to the same value from another array 2 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