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 /
This question was closed Dec 30, 2014 at 11:56 AM by Pkninja16 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Pkninja16 · Dec 27, 2014 at 09:34 AM · c#mesh

Mesh Generation(C#): Mesh has jagged edges plus strange rendering

I want to make my program generate a 3D graph of a formula within certain boundaries and a certain resolution. The problem with the code is that when I run it, the mesh has strange jagged edges and the triangles aren't connecting properly making the mesh render very strangely(rendering on both sides). alt text

alt text

Here is the code(C#):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CustomGraph : MonoBehaviour {
 
     string formula;
     List<Vector3>verts;//Stores the list of vertices for the mesh
     List<int>tris;//Stores the list of triangles for the mesh
     List<Vector2> uvs;//Stores the list of uvs for the mesh
     MeshFilter graphMesh = null;
 
     void Start()
     {
         verts = new List<Vector3> ();
         tris = new List<int> ();
         uvs = new List<Vector2> ();
         graphMesh = gameObject.GetComponent<MeshFilter> ();
         if(graphMesh == null)
         {
             Debug.LogError("Graph does not have MeshFilter");
         }
         GenerateGraph(0,10,1f,"x");
     }
 
     void GenerateGraph(int start, int end, float res, string form)
     {
         ShuntingYard (form);
         CalculateVerts (start, end, res);
         CalculateTris (end-start);
         UpdateGraph ();
     }
 
     void ShuntingYard(string form)
     {
         //Does whatever it does and saves it to the variable 'formula'
         //Isn't needed for testing
     }
 
     void CalculateVerts(int start, int end, float res)
     {
         verts.Clear ();
         for(float z = start; z <= end; z += res)
         {
             for(float x = start; x <= end; x += res)
             {
                 Vector3 current_point = new Vector3();
                 current_point.x = x;
                 current_point.z = z;
                 current_point.y = CalculateY(x,z);
                 verts.Add (current_point);
                 uvs.Add (new Vector2(x,z));
             }
         }
     }
 
     float CalculateY(float x, float z)
     {
         //Calculates y using the ShuntingYard algorithm and returns the value
         //For testing, the formula will be manually input
         return x;
     }
 
     void CalculateTris(int width)
     {
         tris.Clear ();
         for(int index =0; index < verts.Count-width; index++)
         {
             if(index == 0)
             {
                 tris.Add(index);
                 tris.Add(index+1);
                 tris.Add (index+width);
             }
             else if(index % width == 0)
             {
                 tris.Add(index);
                 tris.Add(index+1);
                 tris.Add (index+width);
             }
             else if((index + 1) % width == 0)
             {
                 tris.Add(index);
                 tris.Add(index + width);
                 tris.Add (index + width - 1);
             }
             else
             {
                 tris.Add(index);
                 tris.Add(index + width);
                 tris.Add (index + width - 1);
                 tris.Add(index);
                 tris.Add(index+1);
                 tris.Add (index+width);
             }
         }
     }
 
     void UpdateGraph()
     {
         Mesh finalgraph = new Mesh ();
         finalgraph.vertices = verts.ToArray ();
         finalgraph.triangles = tris.ToArray ();
         finalgraph.uv = uvs.ToArray ();
         finalgraph.RecalculateBounds ();
         finalgraph.RecalculateNormals ();
         graphMesh.mesh.Clear ();
         graphMesh.mesh = finalgraph;
 
     }
 
 }
 

I've been messing around with the CalculateTris() method and that seems to be the problem, but I just can't figure out what it is. Please let me know if there are any clarifications that are needed. Thanks!

untitled.png (156.3 kB)
untitled2.png (154.4 kB)
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Pkninja16 · Dec 29, 2014 at 01:40 AM

Don't worry, I solved it already. I was connecting the triangles incorrectly and the I have to add one for the width in the CalculateTris() method.

 void CalculateTris(int width)
 {
     tris.Clear ();
     width++;
     for(int index =0; index < verts.Count-width; index++)
     {
         if(index == 0)
         {
             tris.Add(index);
             tris.Add (index+width);
             tris.Add(index+1);
         }
         else if(index % width == 0)
         {
             tris.Add(index);
             tris.Add (index+width);
             tris.Add(index+1);
         }
         else if((index + 1) % width == 0)
         {
             tris.Add(index);
             tris.Add (index + width - 1);
             tris.Add(index + width);
         }
         else
         {
             tris.Add(index);
             tris.Add (index + width - 1);
             tris.Add(index + width);
             tris.Add(index);
             tris.Add (index+width);
             tris.Add(index+1);
         }
     }
 }

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Generate mesh from faces, invisible 1 Answer

Set UVs depending on input 1 Answer

Multiple Cars not working 1 Answer

calculate normals 2 Answers

How to Change an Object's Mesh to another Object's Mesh via Code C# 1 Answer


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