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 chilunliu · Feb 07, 2014 at 03:12 PM · meshverticestriangles

mesh into smaller triangles

Hi;

I'm having a tough time with my problem at the moment and I'm hoping anyone on here can help. I have a mesh that is made up of two triangles (see below):

alt text

I want to be able to split these triangles down into smaller triangles so that I can begin to play around with them. I'm working with a c# script, any help would be greatly appreciated. Thank you

(code below)

 using UnityEngine;
 using System.Collections;
 
 public class CreatePlaneMesh : MonoBehaviour {
 
     public float width = 50f; //width of terrain
     public float height = 50f; //height of terrain
 
     public Material mat; // material variable
 
     // Use this for initialization
     void Start () {
         gameObject.AddComponent<MeshFilter>(); //add mesh filter to the gameobject (empty in this case)
         MeshFilter mf = GetComponent<MeshFilter>(); //assign meshfilter to the 'mf' meshfilter variable
         Mesh mesh = new Mesh(); //create a new mesh object
         mf.mesh = mesh; //assign mesh to the 'mf' meshfilter variable
 
 
 
         //vertices
         Vector3[] vertices = new Vector3[4]; //create 4 slots to store vectors to create plane - local space
 
         /**     MESH
         //
         //   v[2] v[3]
         //    |\   |
         //    | \  |
         //    |  \ |  
         //   v[0] v[1]
         **/
 
         vertices[0] = new Vector3 (width, 0, 0); //bottom left
         vertices[1] = new Vector3 (width, 0, height); //bottom right
         vertices[2] = new Vector3 (0, 0, height); //top left
         vertices[3] = new Vector3 (0, 0, 0); //top right
 
         mesh.vertices = vertices;
 
         //get all vertex positions of the mesh object
         mesh = GetComponent<MeshFilter>().mesh;
         Vector3[] v = mesh.vertices;
         int i = 0; while (i < v.Length){ //while loop to go through vertex positions and store then in the 'v' vector3 array
             v[i]+= Vector3.up * Time.deltaTime; //same as v(0,1,0) - go along edges of mesh and mine position co-ordinates;
             i++;
         }
 
         //convert local positions to in-game(world) positions
         Vector3[] worldPOS = new Vector3[vertices.Length]; for(int j=0; j < vertices.Length; j++) {worldPOS[j] = transform.TransformPoint(vertices[j]);}
 
 
         mesh.vertices = vertices; //assign vertices to mesh variable
         mesh.RecalculateBounds(); //verify all is okay to auto-calculate bounds of the mesh from the vertices
         //
 
         int[] indices = new int[6]; //create boxes to store indices values
 
         /**    TRIANGLES TO FORM MESH
         //
         //    0----1        * NOTE. (T1) indices[0]+indices[1]+indices[2] -> triangle on bottom left
         //    |\ T2|                (T2) indices[3]+indices[4]+indices[5] -> triangle on top right
         //    | \  |
         //    |T1\ |  
         //    3---\2
         **/
 
         indices[0] = 0; indices[1] = 3; indices[2] = 2; //one triangle
         indices[3] = 0; indices[4] = 2; indices[5] = 1; //other triangle
 
         mesh.SetIndices(indices, MeshTopology.Triangles, 0); //squish triangles together to form a plane like mesh
 
         gameObject.AddComponent<MeshRenderer>(); //add meshrenderer object to the empty game object
         MeshRenderer mr = GetComponent<MeshRenderer>(); //assign meshrenderer object to the 'mr' variable
         mr.material.color = Color.green; //set the colour of the 'mr' meshrenderer 
         mesh.RecalculateNormals(); //verify all is okay to output to the screen
 
 
     }
 
 } 
capture.png (21.8 kB)
Comment
Add comment · Show 2
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 robertbu · Feb 07, 2014 at 05:56 PM 0
Share

Not a direct answer, but you might take a look at the code in the Unity Wiki CreatePlane script.

http://wiki.unity3d.com/index.php?title=CreatePlane

It generates a mesh with whatever resolution you want. Basically it takes what you've already done and duplicates it across a 2D array of positions.

avatar image chilunliu · Feb 14, 2014 at 06:25 PM 0
Share

Sorry for the delay; I'll give this a try.

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

18 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

Related Questions

Null Reference Exception Assigning Vertices to Mesh 0 Answers

Best Way to Link Vertices into Mesh 0 Answers

Best way to optimize mesh updating through code? 1 Answer

Can you apply smoothing to split vertices on a generated mesh? 1 Answer

Get an specific vertices of a mesh 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