• 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
Question by Guennor · Aug 04, 2015 at 07:32 AM · meshruntimeproceduralperlin noise

Mesh creation during runtime

I was messing around with some of unity's example projects, more specifically, the heightmap one and the fractal texture one. I managed to combine them to make some really nice stuff. It was kinda hard because I had to convert them to c# (the language I know) from javascript. Although I figured out the majority of the script, I can't seem to figure out the mesh creation part. Can someone help me out?

 // Build triangle indices: 3 indices into vertex array for each triangle
         int[] triangles = new int[(height - 1) * (width - 1) * 6];
         int index = 0;
         
         for (y=0;y<height-1;y++)
         {
             for (x=0;x<width-1;x++)
             {
                 // For each grid cell output two triangles
                 triangles[index++] = (y     * width) + x;
                 triangles[index++] = ((y+1) * width) + x;
                 triangles[index++] = (y     * width) + x + 1;
 
                 triangles[index++] = ((y+1) * width) + x;
                 triangles[index++] = ((y+1) * width) + x + 1;
                 triangles[index++] = (y     * width) + x + 1;
             }
             
         }
         // And assign them to the mesh
         mesh.triangles = triangles;
         
         // Auto-calculate vertex normals from the mesh
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         
         // Assign tangents after recalculating normals
         mesh.tangents = tangents;

I think this is the part that creates the mesh. Basically, what the script does, is it creates a mesh from a texture generated with perlin noise. I want to do 2 things:

1- be able to create the meshes separated from each other (people say that's how you make the mesh hard-edged, and that's what I want) and 2 - I want to be able to change the position of the vertices.

But I don't see what part of the code that's setting up the positions. And I can't debug this for loop, if I put a print() or a debug.log() there, the editor crashes!

I'm very motivated to learn about procedural meshes, because they're so fascinating. So I appreciate if someone would be able to help me out with this :)

Comment

People who like this

0 Show 4
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 Cherno · Aug 04, 2015 at 08:34 AM 1
Share

In this code, the vertices are not positionated at all. There are a lot of mesh generation tutorials for Unity, check out this one:

Procedural generated mesh in Unity

as well as the Scripting API for Unity's Mesh class.

avatar image Guennor · Aug 04, 2015 at 06:32 PM 0
Share

Well, it isn't? Now i'm really confused. This is the original script, in javascript, found in one of unity's procedural examples:

 var heightMap : Texture2D;
 var material : Material;
 var size = Vector3(200, 30, 200);
 
 function Start ()
 {
     GenerateHeightmap();
 }
 
 function GenerateHeightmap ()
 {
     // Create the game object containing the renderer
     gameObject.AddComponent(MeshFilter);
     gameObject.AddComponent.<MeshRenderer>();
     if (material)
         GetComponent.<Renderer>().material = material;
     else
         GetComponent.<Renderer>().material.color = Color.white;
 
     // Retrieve a mesh instance
     var mesh : Mesh = GetComponent(MeshFilter).mesh;
 
     var width : int = Mathf.Min(heightMap.width, 255);
     var height : int = Mathf.Min(heightMap.height, 255);
     var y = 0;
     var x = 0;
 
     // Build vertices and UVs
     var vertices = new Vector3[height * width];
     var uv = new Vector2[height * width];
     var tangents = new Vector4[height * width];
     
     var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
     var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));
     
     for (y=0;y<height;y++)
     {
         for (x=0;x<width;x++)
         {
             var pixelHeight = heightMap.GetPixel(x, y).grayscale;
             var vertex = Vector3 (x, pixelHeight, y);
             vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
             uv[y*width + x] = Vector2.Scale(Vector2 (x, y), uvScale);
 
             // Calculate tangent vector: a vector that goes from previous vertex
             // to next along X direction. We need tangents if we intend to
             // use bumpmap shaders on the mesh.
             var vertexL = Vector3( x-1, heightMap.GetPixel(x-1, y).grayscale, y );
             var vertexR = Vector3( x+1, heightMap.GetPixel(x+1, y).grayscale, y );
             var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
             tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
         }
     }
     
     // Assign them to the mesh
     mesh.vertices = vertices;
     mesh.uv = uv;
 
     // Build triangle indices: 3 indices into vertex array for each triangle
     var triangles = new int[(height - 1) * (width - 1) * 6];
     var index = 0;
     for (y=0;y<height-1;y++)
     {
         for (x=0;x<width-1;x++)
         {
             // For each grid cell output two triangles
             triangles[index++] = (y     * width) + x;
             triangles[index++] = ((y+1) * width) + x;
             triangles[index++] = (y     * width) + x + 1;
 
             triangles[index++] = ((y+1) * width) + x;
             triangles[index++] = ((y+1) * width) + x + 1;
             triangles[index++] = (y     * width) + x + 1;
         }
     }
     // And assign them to the mesh
     mesh.triangles = triangles;
         
     // Auto-calculate vertex normals from the mesh
     mesh.RecalculateNormals();
     
     // Assign tangents after recalculating normals
     mesh.tangents = tangents;
 }



avatar image maccabbe · Aug 04, 2015 at 07:13 PM 0
Share

Lines 28-54 set the position of the vertices (the points). It also sets up the uvs (used for textures). Then lines 55-75 build the triangles from the vertices.

Every int in int[]triangles is the index of a vertex in Vector3[]vertices. Every 3 ints in int[]triangles defines 1 triangles.

avatar image Guennor · Aug 04, 2015 at 07:38 PM 0
Share

Could you give me an example of how could I change the position of a single vertex? Also, how can I make both triangles separated from each other? This code is really complex for me. I'm really trying to understand it. I can't visualize how does it get each vertex and sets up a position for it.

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing shape of Poly Mesh in ProBuilder at runtime. 0 Answers

Deleting one triangle out of mesh? Cut hole in mesh in runtime? 4 Answers

Check if a mesh has UV map at runtime 1 Answer

Conforming a mesh path to arbitrary surface - runtime 1 Answer

Runtime import of Collada files 3 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