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 KitoCode · Apr 09, 2015 at 10:17 AM · mesharraytilemap

2d Array on mesh presented reversed or backwards? FIXED

Good afternoon all,

I seem to have my thoughts in a knot. I wish to present my mesh in the same fashion as if you were to read a 2D array. I have the script built so as if it can read in my 2d Array and present the data via JSON scripts, but the mesh doesn't properly present the data given.

alt text alt text

As your can see, on the left is the required output, while on the right is the result due to my scripting.

If someone could help me out, that would be great. My mind seems to be out of gas on this problem.


 private void BuildMesh()
 {
     int numTiles = m_RootObject.width * m_RootObject.height;
     int numTris = numTiles * 2;
         
     int vsize_x = m_RootObject.width + 1;
     int vsize_z = m_RootObject.height + 1;
     int numVerts = vsize_x * vsize_z;
         
     // Generate the mesh data
    Vector3[] vertices = new Vector3[ numVerts ];
    Vector3[] normals = new Vector3[numVerts];
     Vector2[] uv = new Vector2[numVerts];
         
     int[] triangles = new int[ numTris * 3 ];
         
     int x, z;
     for(z=0; z < vsize_z; z++) 
     {
         for(x=0; x < vsize_x; x++) 
         {
             vertices[ z * vsize_x + x ] = new Vector3( x, 0, z );
             normals[ z * vsize_x + x ] = Vector3.up;
             uv[ z * vsize_x + x ] = new Vector2( (float)x / (m_RootObject.width), (float)z / (m_RootObject.height) );
         }
     }
     
     for(z=0; z < m_RootObject.height; z++) 
     {
         for(x=0; x < m_RootObject.width; x++)
         {
             int squareIndex = z * m_RootObject.width + x;
             int triOffset = squareIndex * 6;
             triangles[triOffset + 0] = z * vsize_x + x + 0;
             triangles[triOffset + 1] = z * vsize_x + x + vsize_x + 0;
             triangles[triOffset + 2] = z * vsize_x + x + vsize_x + 1;
             
             triangles[triOffset + 3] = z * vsize_x + x + 0;
             triangles[triOffset + 4] = z * vsize_x + x + vsize_x + 1;
             triangles[triOffset + 5] = z * vsize_x + x + 1;
         }
     }
     
     // Create a new Mesh and populate with the data
     Mesh mesh = new Mesh();
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.normals = normals;
     mesh.uv = uv;
         
     // Assign our mesh to our filter/renderer/collider
     MeshFilter mesh_filter = GetComponent<MeshFilter>();
     MeshCollider mesh_collider = GetComponent<MeshCollider>();
     
     mesh_filter.mesh = mesh;
     mesh_collider.sharedMesh = mesh;
 }



Here is some more scripts, hopefully it can help find the issue.


 private void BuildTexture()
 {        
     int texWidth = m_RootObject.width * 32;
     int texHeight = m_RootObject.height * 32;
     Texture2D texture = new Texture2D(texWidth, texHeight);
 
     Color[][] tiles = ChopUpTiles();
 
     //m_RootObject.layers[0].data.Reverse();
     Debug.Log(m_RootObject.width);
     int g = 0;
     for(int y=0; y < m_RootObject.height; y++) 
     {
         for(int x=0; x < m_RootObject.width; x++)
         {
             Color[] p = tiles[ (m_RootObject.layers[0].data[g]-1 == -1) ? 0 : m_RootObject.layers[0].data[g]-1];
             texture.SetPixels(x*32, y*32, 32, 32, p);
             g++;
         }
     }
         
     texture.filterMode = FilterMode.Point;
     texture.wrapMode = TextureWrapMode.Clamp;
     texture.Apply();
         
     MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
     mesh_renderer.sharedMaterials[0].mainTexture = texture;
 }
 
 Color[][] ChopUpTiles() 
 {
     int numTilesPerRow = terrainTiles.width / 32;
     int numRows = terrainTiles.height / 32;
 
     Color[][] tiles = new Color[numTilesPerRow*numRows][];
         
     for(int y=0; y<numRows; y++) 
     {
         for(int x=0; x<numTilesPerRow; x++) 
         {
             tiles[y*numTilesPerRow + x] = terrainTiles.GetPixels( x*32, y*32, 32, 32 );
         }
     }
         
     return tiles;
 }



required.png (2.5 kB)
error.png (2.4 kB)
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

Answer by KitoCode · Apr 10, 2015 at 01:52 AM

Fixed my issue, only took me all day.

If anyone is interested, here is the pastebin code.

http://pastebin.com/QMdJmXGB

Comment
Add comment · 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

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

Assigning new Mesh Vertices 2 Answers

Seemingly random results winding triangles? 2 Answers

Mesh quad rendering order 1 Answer

Most Optimal way of computing Delaunay Triangulation given an Array of Vector3 points 0 Answers

What's the best way to store a 2D array in a .dat file? (C#) 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