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 BearShark1993 · Jan 12, 2015 at 11:13 AM · meshprocedural meshprocedural generationmeshfiltermesh-deformation

Splitting vertices in a mesh specifically a plane

So this is my first time using the Mesh class. I goal was to make a tilemap so that I can take a crack at procedural dungeon creation. I have data structures set up to create a blue print the challenge I am having is drawing it.

The idea I was going for was to create the dungeon out of a single plane made up of several vertices as shown in the code below to form tiles. I was hoping to raise and lower these vertices to create walls and chasms.

In the code I wrote a little test to see how raising vertices would affect the created mesh and what I noticed is that instead of a cube like structure rising out of the plane it was creating more of hill as shown in the picture. I think the issue is that vertices are shared between tiles.

I know I would need more vertices I was wondering if anyone could point me in the right direction of how to make it form a wall instead of a hill.

 ![using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof (MeshFilter))]
 [RequireComponent(typeof (MeshRenderer))]
 [RequireComponent(typeof (MeshCollider))]
 public class TileMap : MonoBehaviour {
     //number of tiles in the x direction
     public int size_x = 7*35;
     //number of tiles in the z direction
     public int size_z = 7*35;
 
     //size of each tile
     public float tileSize = 10f;
     // Use this for initialization
     void Start () {
         buildMesh();
     }
 
     public void buildMesh(){
         //number of tiles total on the map
         int numTiles = size_x*size_z;
         //number of triangles is double the number of tiles needed as 2 triangles make one square on the map.
         int numTris = numTiles*2;
 
         //number of vertices in the x dirextion
         int vsize_x = size_x + 1;
         //number of vertices in the z direction
         int vsize_z = size_z + 1;
         // number of vertices total in the map needed to make the plane.
         int numVerts = vsize_x * vsize_z;
 
 
         //Create mesh data.
         //array of vector 3's to store vertex data
         Vector3[] vertices = new Vector3[numVerts];
         //array of vector 3's to store normal data
         Vector3[] normals = new Vector3[numVerts];
         //array of vector 2's to store uv data.
         Vector2[] uv = new Vector2[numVerts];
 
         int[] triangles = new int[numTris*3];
         //One nested for loop to set up the vertices normals, and uv
         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*tileSize,0,z*tileSize);
                 normals [z * vsize_x + x] = Vector3.up;
                 uv[z * vsize_x + x] = new Vector2((float)x/size_x,(float)z/size_z);
 
             }
         }
         // a little test I wrote to see how elevating certain vetices affect the mesh.
         vertices[0] += new Vector3(0,10,0);
         vertices[1] += new Vector3(0,10,0);
         vertices[vsize_x] += new Vector3(0,10,0);
         vertices[vsize_x+1] += new Vector3(0,10,0);
         //end of test
 
         //one nested for loop to set up the triangle data for the mesh.
         for(z=0; z < size_z; z++){
             for(x=0; x < size_x; x++){
                 int squareIndex = z * size_x + x;
                 int triOffset = squareIndex*6;
 
                 triangles[triOffset +0] = z * vsize_x + x + 0;
                 triangles[triOffset +2] = z * vsize_x + x + vsize_x + 1;
                 triangles[triOffset +1] = z * vsize_x + x + vsize_x + 0;
 
                 triangles[triOffset +3] = z * vsize_x + x + 0; 
                 triangles[triOffset +5] = z * vsize_x + x + 1;
                 triangles[triOffset +4] = z * vsize_x + x + vsize_x + 1;
 
             }
         }
 
 
         //Create a new mesh
         Mesh mesh = new Mesh();
         //Populate mesh with data
         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>();
         MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
         MeshCollider mesh_collider = GetComponent<MeshCollider>();
 
         mesh_filter.mesh = mesh;
     }
 
 }][1]
 

 


[1]: /storage/temp/38625-question.png

question.png (77.6 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

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to manipulate(flatten) the Mesh of an object 0 Answers

Procedural Mesh problem from Editor Window 1 Answer

Do i have to use shared vertices if i want my proceduraly generated mesh to look smooth? 1 Answer

Cannot using two script to modify the same mesh at the same time 1 Answer

Procedural mesh with texture atlas, how to get rid of material artifacts? 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