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 The Pirate Duck · Sep 16, 2014 at 05:28 PM · c#meshgenerated

Need help with procedurally generated tunnel mesh

I have been trying to make a procedurally generated tunnel with a mesh. The tunnel is based on a regular polygon with 12 sides. I have successfully created a path the tunnel to follow with center points for each face, and also a single part - two connected faces - separately, but I really can't figure out why this isn't working. I know it's probably something very obvious, but I just can't spot it.

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class LevelController : MonoBehaviour {
 
     public float minimumLength;
     public float maximumLength;
     public float maximumAngle;
     
     public Level currentLevel;
     
     [HideInInspector] public GameObject gameObj;
     [HideInInspector] new public Transform transform;
     
     void Start () {
         
         gameObj = gameObject;
         
         transform = gameObj.GetComponent<Transform> ();
         
         currentLevel = new Level (50, 100, maximumAngle, minimumLength, maximumLength);
         
         transform.GetComponent<MeshFilter> ().mesh = currentLevel.mesh;
         
     }
     
     public struct Level {
         
         public Mesh mesh;
         
         public Face[] face;
         public Vector3[] faceCenterPoint;
         public Quaternion[] faceRotation;
         
         public Vector3[] vertices;
         public Vector2[] uv;
         public int[] triangles;
         
         public Level (int partCount, float firstPartLength, float maximumAngle, float minimumLength, float maximumLength) {
             
             mesh = new Mesh ();
             
             face = new Face[partCount + 1];
             faceCenterPoint = new Vector3[partCount + 1];
             faceRotation = new Quaternion[partCount + 1];
             vertices = new Vector3[(partCount + 1) * 12];
             uv = new Vector2[vertices.Length];
             triangles = new int[24 * partCount];
             
             faceCenterPoint[0] = new Vector3 (0, 0, 0);
             faceRotation[0] = Quaternion.identity;
             face[0] = new Face (faceRotation[0], faceCenterPoint[0]);
             
             faceCenterPoint[1] = new Vector3 (0, 0, firstPartLength);
             faceRotation[1] = Quaternion.Euler (faceRotation[0].eulerAngles.x + Random.Range (-maximumAngle, maximumAngle), faceRotation[0].eulerAngles.y + Random.Range (-maximumAngle, maximumAngle), 0);
             face[1] = new Face (faceRotation[1], faceCenterPoint[1]);
             
             Debug.DrawLine(faceCenterPoint[0], faceCenterPoint[1], Color.red, 600, true);
             
             for (int n = 2; n < partCount; n++) {
                 
                 float partLength = Random.Range (minimumLength, maximumLength);
                 Vector3 centerPoint = Vector3.zero;
                 
                 Debug.Log (faceCenterPoint[n - 1]);
                 
                 centerPoint.x = partLength * Mathf.Sin (faceRotation[n - 1].eulerAngles.y) * Mathf.Cos (faceRotation[n - 1].eulerAngles.x) + faceCenterPoint[n - 1].x;
                 centerPoint.y = partLength * Mathf.Cos (faceRotation[n - 1].eulerAngles.y) * Mathf.Sin (faceRotation[n - 1].eulerAngles.x) + faceCenterPoint[n - 1].y;
                 centerPoint.z = partLength * Mathf.Cos (faceRotation[n - 1].eulerAngles.x) * Mathf.Cos (faceRotation[n - 1].eulerAngles.y) + faceCenterPoint[n - 1].z;
                 
                 faceCenterPoint[n] = centerPoint;
                 faceRotation[n] = Quaternion.Euler (faceRotation[n - 1].eulerAngles.x + Random.Range (-maximumAngle, maximumAngle), faceRotation[n - 1].eulerAngles.y + Random.Range (-maximumAngle, maximumAngle), 0);
                 
                 Debug.DrawLine(faceCenterPoint[n - 1], faceCenterPoint[n], Color.red, 600, true);
                 
                 face[n] = new Face (faceRotation[n], faceCenterPoint[n]);
                 
             }
             
             for (int n = 0; n < face.Length - 1; n++) {
                 
                 vertices[n * 12] = face[n].vertices[0];
                 vertices[n * 12 + 1] = face[n].vertices[1];
                 vertices[n * 12 + 2] = face[n].vertices[2];
                 vertices[n * 12 + 3] = face[n].vertices[3];
                 vertices[n * 12 + 4] = face[n].vertices[4];
                 vertices[n * 12 + 5] = face[n].vertices[5];
                 vertices[n * 12 + 6] = face[n].vertices[6];
                 vertices[n * 12 + 7] = face[n].vertices[7];
                 vertices[n * 12 + 8] = face[n].vertices[8];
                 vertices[n * 12 + 9] = face[n].vertices[9];
                 vertices[n * 12 + 10] = face[n].vertices[10];
                 vertices[n * 12 + 11] = face[n].vertices[11];
                 
             }
             
             for (int n = 0; n < partCount - 1; n++) {
                 
                 for (int j = 0; j < 12; j++) {
                     
                     if (j == 11) {
                         
                         triangles[j * n] = j + (n * 12);
                         triangles[j * n + 1] = (n + 1) * 12;
                         triangles[j * n + 2] = j + (n + 1) * 12;
                         
                         triangles[j * n + 3] = j + (n * 12);
                         triangles[j * n + 4] = n * 12;
                         triangles[j * n + 5] = (n + 1) * 12;
                         
                     } else {
                         
                         triangles[j * n] = j + (n * 12);
                         triangles[j * n + 1] = j + 13 + (n * 12);
                         triangles[j * n + 2] = (n + 1) * 12;
                         
                         triangles[j * n + 3] = j + (n * 12);
                         triangles[j * n + 4] = j + 1 + (n * 12);
                         triangles[j * n + 5] = j + 13 + (n * 12);
                         
                     }
                     
                 }
                 
             }
             
             mesh.vertices = vertices;
             mesh.uv = uv;
             mesh.triangles = triangles;
             mesh.Optimize ();
             mesh.RecalculateNormals ();
             
         }
         
     }
     
     public struct Face {
         
         public Vector3 centerPoint;
         public Vector3[] vertices;
         public Vector2[] uv;
         
         public Face (Quaternion rotation, Vector3 centerP) {
             
             centerPoint = centerP;
             vertices = new Vector3[12];
             uv = new Vector2[12];
             
             vertices[0] = new Vector3 (-3, -11, centerP.z);
             vertices[1] = new Vector3 (-8, 8, centerP.z);
             vertices[2] = new Vector3 (-11, -3, centerP.z);
             vertices[3] = new Vector3 (-11, 3, centerP.z);
             vertices[4] = new Vector3 (-8, 8, centerP.z);
             vertices[5] = new Vector3 (-3, 11, centerP.z);
             vertices[6] = new Vector3 (3, 11, centerP.z);
             vertices[7] = new Vector3 (8, 8, centerP.z);
             vertices[8] = new Vector3 (11, 3, centerP.z);
             vertices[9] = new Vector3 (11, -3, centerP.z);
             vertices[10] = new Vector3 (8, -8, centerP.z);
             vertices[11] = new Vector3 (3, -11, centerP.z);
             
             for (int n = 0; n < vertices.Length; n++) {
                 
                 vertices[n] = rotation * (vertices[n] - centerPoint) + centerPoint;
                 
             }
             
         }
         
     }
     
 }

The weird part is that the triangles[] ends up being filled with seemingly random numbers in the range from 0 to ~700. If someone can explain to what I'm doing wrong or where the obvious mistake is, it'd be great.

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

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

Answer by The Pirate Duck · Sep 20, 2014 at 08:44 PM

I figured it out my self. If anyone is interested in almost working code, here's a link - https://gist.github.com/ThePirateDuck/7b95397733a09bf9e593

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

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

23 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

Related Questions

Procedural generated mesh problem 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to active mesh renderer with a script C# 2 Answers

How do I sync a mesh over network? (Photon) 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