x


creating a mesh procedurally

hi guys i want to generate a simple mesh at runtime and then use it for my object's mesh renderer but i can not see anything on the screen. i think my problem is with setting triangles. is there anyone that he knows how should i set triangles and what are the different approaches for setting triangles vertecies?

this is my code

using UnityEngine;
using System.Collections;
public class album : MonoBehaviour
{
    public Texture2D tex;
    Mesh m;
    IEnumerator Start()
    {
        yield return new WaitForSeconds(1f);
        m = new Mesh();
        m.vertices = new Vector3[5];
        m.uv = new Vector2[5];
        m.normals = new Vector3[5];
        m.normals[0] = new Vector3(0,0,-1); ;
        m.normals[1] = new Vector3(0, 0, -1);
        m.vertices[0].x = 0;
        m.vertices[0].y = 0;
        m.vertices[0].z = 0;
        m.uv[0] = new Vector2 (0,0);
        m.vertices[1].x = 1;
        m.vertices[1].y = 0;
        m.vertices[1].z = 0;
        m.uv[1] = new Vector2(1, 0);
        m.vertices[2].x = 0;
        m.vertices[2].y = -1;
        m.vertices[2].z = 0;
        m.uv[2] = new Vector2(0, 1);
        m.vertices[3].x = 1;
        m.vertices[3].y = -1;
        m.vertices[3].z = 0;
        m.uv[3] = new Vector2(1, 1);
        m.vertices[4].x = 0;
        m.vertices[4].y = 0;
        m.vertices[4].z = 0.3f;
        m.uv[4] = new Vector2(1, 1);
        m.triangles = new int[9];
        m.triangles[0] = 0;
        m.triangles[1] = 1;
        m.triangles[2] = 2;
        m.triangles[3] = 2;
        m.triangles[4] = 3;
        m.triangles[5] = 1;
        m.triangles[6] = 1;
        m.triangles[7] = 0;
        m.triangles[8] = 4;
        m.RecalculateBounds();
        MeshFilter mf = (MeshFilter)transform.GetComponent(typeof(MeshFilter));
        mf.mesh = m;
        renderer.material = new Material(Shader.Find(" Diffuse"));
        mf.renderer.material.mainTexture = tex;
    }
}
more ▼

asked Nov 19 '09 at 09:19 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

Hey,

I'm looking for the correct method to generate the triangles from an array of vertices. The link above is dead. May someone make it available again?

Thanks

Jun 19 '12 at 12:55 PM maveryck21

Here's a mirror of the PDF file from FlamingHairball's (Lincoln Green's) tutorial: http://games.deozaan.com/unity/MeshTutorial.pdf

Jun 19 '12 at 07:09 PM Deozaan

@maveryck21: Don't post an answer when you don't want to answer the queestion. I've converted your answer into a comment.

There is no correct way to form triangles out of vertices, because it depends on what you actually want to create. You can create a triangle between any vertices in the vertices array.

Just look at the example in the docs. Phil has posted the link already in his answer below...

Also there are many questions like this already on UA. Just use the internal search function or even better google

Jun 19 '12 at 08:12 PM Bunny83

Unity Tips and Tricks has a good tutorial on it.

Feb 19 at 11:07 PM insominx
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

This tutorial from the UniKnowledge contest covers creating meshes via scripting, including how to correctly set the triangles (see page 9 of the pdf in Tutorial.zip).

more ▼

answered Nov 19 '09 at 09:38 PM

Ehren gravatar image

Ehren
4.2k 34 43 77

Looks like his link is dead, is it available anywhere else?

Apr 20 '11 at 03:32 PM davebuchhofer

I would also like to know if this tut on creating meshes is available anywhere.

Apr 26 '11 at 12:44 AM sandworm

3rd...there needs to be more info about creating meshes by hand!

Apr 29 '11 at 12:39 AM kablammyman

The author, FlamingHairball, has posted the tutorials here: http://dl.dropbox.com/u/680550/MeshTutorial.zip

EDIT: Since the above link is dead, I've mirrored the PDF file here: http://games.deozaan.com/unity/MeshTutorial.pdf

Jan 22 '12 at 01:41 AM Deozaan
(comments are locked)
10|3000 characters needed characters left

Hey guys,

Sorry for the late response. I've been swamped and am in the process of transitioning hosting, so my server is down. You can temporarily access the tutorial here: http://dl.dropbox.com/u/680550/MeshTutorial.zip

It's an archive that contains the tutorial PDF and the required files. Sorry for the inconvenience!

Best wishes,

-Lincoln Green

more ▼

answered May 12 '11 at 07:05 PM

flaminghairball gravatar image

flaminghairball
928 4 9 21

The file only contains PDFs, no the required files for part 2.

Apr 26 '12 at 08:41 AM carlos3dx
(comments are locked)
10|3000 characters needed characters left

I believe you need to fill in your array values first and then finish by assigning mesh.uvs, mesh.uvs, mesh.vertices, mesh.triangles, all at once as in the Mesh documentation examples - http://unity3d.com/support/documentation/ScriptReference/Mesh.html

more ▼

answered Nov 20 '09 at 08:04 AM

technicat gravatar image

technicat
1.5k 10 11 34

(comments are locked)
10|3000 characters needed characters left

But can you see the new mesh gets applied in the inspector to your transform?

I never really created meshes at runtime but did you try using "m.RecalculateNormals();" instead?

more ▼

answered Nov 19 '09 at 11:56 PM

Kiyaku gravatar image

Kiyaku
261 2 2 9

.Recalculatenormals(); is required only when you need your mesh to cooperate with lighting engine. It is not required but I would advise of use it for convience.

Apr 26 '12 at 09:10 AM cupsster
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1360
x300
x92

asked: Nov 19 '09 at 09:19 PM

Seen: 18058 times

Last Updated: Feb 19 at 11:07 PM