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
1
Question by Spartan075 · Mar 12, 2014 at 09:24 PM · procedural generationmesh-deformation

Having trouble with triangles in mesh gen

So I'm currently using a script to generate a mesh for a 2d grid-based game I'm planning to make, but for some reason, when I apply a random color to the material during generation,it shows a peculiar occurrence...

Here's the code for it: using UnityEngine; using System.Collections;

 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshCollider))]
 [RequireComponent(typeof(MeshRenderer))]
 
 public class GridScript : MonoBehaviour {
 
     // Grid and tile size variables.
     public int size_x;
     public int size_y;
     public int tileSize;
 
     // Tile types and colliders
     string[,] tileType;
 
 
 
 
 
     // Use this for initialization
     void Start() {
 
         // Initialize the Grid Data variables.
         tileType = new string[size_x, size_y];
 
         // Run init functions.
         InitGrid();
         }
 
     void Update() {
         if(Input.GetMouseButtonDown(0)) {
             processGridClick();
             }
         }
 
 
 
     void genGridData() {
         for (int x = 0; x < size_x; x++) {
         for (int y = 0; y < size_y; y++) {
 
             tileType[x, y] = "empty";
         }
         }
         tileType[5, 5] = "wall";
 
     }
 
 
 
     public void processGridClick() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitInfo;
 
         if (collider.Raycast(ray, out hitInfo, Mathf.Infinity)) {
 
             int x = Mathf.FloorToInt(hitInfo.point.x / tileSize);
             int y = Mathf.FloorToInt(hitInfo.point.y / tileSize);
             Debug.Log(tileType[x,y]);
         }
     }
 
 
 
 
     public void buildTexture() {
         int texWidth = 10;
         int texHeight = 10;
         Texture2D texture = new Texture2D(texWidth, texHeight);
 
         for (int y = 0; y < texHeight; y++) {
             for (int x = 0; x < texWidth; x++) {
                 Color c = new Color(Random.Range  (0f, 1f), Random.Range (0f, 1f) , Random.Range (0f, 1f) );
 
                 texture.SetPixel(x, y, c);
             
             }
         }
 
         texture.filterMode = FilterMode.Point;
         texture.Apply();
         MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
         mesh_renderer.sharedMaterials[0].mainTexture = texture;
 
         }
 
         public void InitGrid() {
 
             // Initiate the required mesh variables.
             int numTiles = size_x * size_y;
             int vsize_x = size_x + 1;
             int vsize_y = size_y + 1;
             int numVerts = vsize_x * vsize_y;
             int numTris = numTiles * 2;
 
             // Initiate the mesh data.
             Vector3[] vertices = new Vector3[numVerts];
             Vector3[] normals = new Vector3[numVerts];
             Vector2[] uv = new Vector2[numVerts];
             int[] triangles = new int[numTris * 3];
 
 
             // For loops to full the vertices, normals, uvs, and triangles of the mesh.
             int x, y;
             for (y = 0; y < vsize_y; y++) {
                 for (x = 0; x < vsize_x; x++) {
                     vertices[y * vsize_x + x] = new Vector3(x * tileSize, y * tileSize, 0);
                     normals[y * vsize_x + x] = Vector3.forward;
                     uv[y * size_x + x] = new Vector2((float)x / size_x, (float)y / size_y);
                     }
                 }
 
             for (y = 0; y < size_y; y++) {
                 for (x = 0; x < size_x; x++) {
                     int squareIndex = y * size_x + x;
                     int triOffset = squareIndex * 6;
                     triangles[triOffset + 0] = y * vsize_x + x +           0;
                     triangles[triOffset + 1] = y * vsize_x + x + vsize_x + 0;
                     triangles[triOffset + 2] = y * vsize_x + x + vsize_x + 1;
 
                     triangles[triOffset + 3] = y * vsize_x + x +           0;
                     triangles[triOffset + 4] = y * vsize_x + x + vsize_x + 1;
                     triangles[triOffset + 5] = y * vsize_x + x +           1;
                     }
                 }
 
             // Create the mesh itself, and assign the variables used to manipulate it.
             Mesh mesh = new Mesh();
             mesh.vertices = vertices;
             mesh.triangles = triangles;
             mesh.normals = normals;
             mesh.uv = uv;
             MeshFilter mesh_filter = GetComponent<MeshFilter>();
             MeshCollider mesh_collider = GetComponent<MeshCollider>();
 
             mesh_filter.mesh = mesh;
             mesh_collider.sharedMesh = mesh;
 
             buildTexture();
 
             } // End of initGrid
 } // End of script

it's quite a bit, but something tells me the trouble is in initGrid... When it generates, the top triangles and bottom ones don't lie within the same quad, but I just can't seem to find what's causing it...

Comment
Add comment · Show 5
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 Bunnybomb7670 · Mar 12, 2014 at 09:34 PM 0
Share

Do you have an image of what exactly happens? would help towards helping you what to do to fix it.

avatar image Spartan075 · Mar 12, 2014 at 11:56 PM 0
Share

alt text

Sure, here ya go. If you need anything else, just shoot

avatar image Bunnybomb7670 · Mar 13, 2014 at 12:11 AM 0
Share

I think that its this : uv[y * size_x + x] = new Vector2((float)x / size_x, (float)y / size_y); which is causing the issue, You need to give the UVs in a specific order. Try making you're code loop per face rather than per vertex, then add all the data for the face to the arrays at once.

Here is an example of how I add my mesh data for my voxel game : http://puu.sh/7tc23.png ( full code : http://pastebin.com/zxw1zB7e )and the UV calculation : http://puu.sh/7tc5h.png . The code in the first image is called in a for loop so it does that for how many faces it needs to draw.

avatar image Spartan075 · Mar 13, 2014 at 12:42 AM 0
Share

Hmm... That code definitely looks a lot cleaner, though being a newbie to c#(only a little experience in Java to help) most of it is a tad confusing, and I've personally never seen a file that large, so a rundown might help.(Sorry for being a complete newbie to all of this, as well)

avatar image Bunnybomb7670 · Mar 13, 2014 at 07:15 AM 0
Share

np :) basically, to build my voxel meshes, The meshbuilder class is used, this recieves the chunk data and loops through every block in the chunk and checks every side of each block, it then compares whether each block is solid or air, if its air, it continues, if its solid, it checks if the blocks next to it are solid and if they are not solid, render a face at that position and direction. You can see how I used switches dependent on the side it checks, you will not need to do a switch based system, ins$$anonymous$$d you will just loop through every grid section and add a square at the position, this way you can easily group all you're meshing code into the same loop, if you get what I mean?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Mar 13, 2014 at 07:43 AM

That looks like a perfectly reasonable approach. Filling out the vertices first, then hooking up the triangles is the standard way of doin this.

I think the issue is the uv line. When indexing into the array you currently use size_x instead of vsize_x like the lines above it. That causing your row accesses to be off by 1 and thus giving you that zig zag effect.

Main lesson here - name variables nice and differently - typos like that are always tricky to spot if your key variable names are only different by 1 letter!

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

22 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

Related Questions

Splitting vertices in a mesh specifically a plane 0 Answers

Adding vertices to procedural mesh generator 2 Answers

Selectively deform a mesh by dragging? 0 Answers

Help with positioning of procedurally generating branches and then leaves of a tree. 1 Answer

How can I generate procedural 3D geometry faster? 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