Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ashkan_gc · Nov 19, 2009 at 09:19 PM · meshproceduralgeneration

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;
    }
}
Comment
Add comment · 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 tomazsaraiva · Jun 19, 2012 at 12:55 PM 0
Share

Hey,

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

Thanks

avatar image Deozaan · Jun 19, 2012 at 07:09 PM 0
Share

Here's a mirror of the PDF file from Fla$$anonymous$$gHairball's (Lincoln Green's) tutorial: http://games.deozaan.com/unity/$$anonymous$$eshTutorial.pdf

avatar image Bunny83 · Jun 19, 2012 at 08:12 PM 0
Share

@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

avatar image insominx · Feb 19, 2013 at 11:07 PM 1
Share

Unity Tips and Tricks has a good tutorial on it.

4 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Ehren · Nov 19, 2009 at 09:38 PM

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).

Comment
Add comment · Show 4 · 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
avatar image davebuchhofer · Apr 20, 2011 at 03:32 PM 0
Share

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

avatar image sandworm · Apr 26, 2011 at 12:44 AM 0
Share

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

avatar image kablammyman · Apr 29, 2011 at 12:39 AM 0
Share

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

avatar image Deozaan · Jan 22, 2012 at 01:41 AM 1
Share

The author, Fla$$anonymous$$gHairball, has posted the tutorials here: http://dl.dropbox.com/u/680550/$$anonymous$$eshTutorial.zip

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

avatar image
8

Answer by flaminghairball · May 12, 2011 at 07:05 PM

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

Comment
Add comment · Show 2 · 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
avatar image carlos3dx · Apr 26, 2012 at 08:41 AM 0
Share

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

avatar image tylerlindell · Mar 06, 2017 at 03:24 AM 0
Share

this link is dead

avatar image
4

Answer by $$anonymous$$ · Nov 20, 2009 at 08:04 AM

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

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
avatar image
0

Answer by Kiyaku · Nov 19, 2009 at 11:56 PM

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?

Comment
Add comment · Show 1 · 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
avatar image cupsster · Apr 26, 2012 at 09:10 AM 1
Share

.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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Flat shading procedural generated mesh? 1 Answer

Help in understanding Mesh Generation 1 Answer

generate procedural floating island 1 Answer

Why Unity freezes while creating Meshes procedurally? 1 Answer

Generated mesh loses lighting at certain angles 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