Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.70 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. //Add the mesh filter and Renderer
  5. [RequireComponent(typeof(MeshFilter))]
  6. [RequireComponent(typeof(MeshRenderer))]
  7. public class BaseModel : MonoBehaviour {
  8.  
  9.     //Create variables for the meshfilter and mesh
  10.     private Mesh m_mesh;
  11.     private MeshFilter m_meshFilter;
  12.  
  13.     //Create list for the vertices and traingles
  14.     [SerializeField]List<Vector3> m_vertices = new List<Vector3>();
  15.     [SerializeField]List<int> m_triangles = new List<int>();
  16.  
  17.     //How many times should it subdivide (Get more rounder)
  18.     [SerializeField]private int m_subdivisions = 3;
  19.     [SerializeField]private float m_radius = 1f;
  20.  
  21.     private void Awake() {
  22.         //Creating the mesh and give it a name
  23.         m_mesh = new Mesh();
  24.         m_mesh.name = "Quadrangle";
  25.     }
  26.  
  27.     // Use this for initialization
  28.     void Start() {
  29.         //Get the meshFilter and put it in the variable
  30.         m_meshFilter = GetComponent<MeshFilter>();
  31.         m_meshFilter.mesh = m_mesh;
  32.  
  33.         //Run the function CalculateVertices
  34.         CalculateVertices(m_subdivisions, m_radius);
  35.     }
  36.  
  37.     public void CreateFirstObject() {
  38.         //Hard coding the octahedrons mesh
  39.         m_triangles.Add(0);
  40.         m_triangles.Add(3);
  41.         m_triangles.Add(2);
  42.  
  43.         m_triangles.Add(4);
  44.         m_triangles.Add(3);
  45.         m_triangles.Add(0);
  46.  
  47.         m_triangles.Add(4);
  48.         m_triangles.Add(0);
  49.         m_triangles.Add(5);
  50.  
  51.         m_triangles.Add(0);
  52.         m_triangles.Add(2);
  53.         m_triangles.Add(5);
  54.  
  55.         m_triangles.Add(1);
  56.         m_triangles.Add(2);
  57.         m_triangles.Add(3);
  58.  
  59.         m_triangles.Add(3);
  60.         m_triangles.Add(4);
  61.         m_triangles.Add(1);
  62.  
  63.         m_triangles.Add(5);
  64.         m_triangles.Add(1);
  65.         m_triangles.Add(4);
  66.  
  67.         m_triangles.Add(1);
  68.         m_triangles.Add(5);
  69.         m_triangles.Add(2);
  70.  
  71.         //Give the world space position to the vertices
  72.         //top and bottom
  73.         m_vertices.Add(new Vector3(0, 1, 0));
  74.         m_vertices.Add(new Vector3(0, -1, 0));
  75.  
  76.         //left and right
  77.         m_vertices.Add(new Vector3(1, 0, 0));
  78.         m_vertices.Add(new Vector3(0, 0, 1));
  79.         //back and forward
  80.         m_vertices.Add(new Vector3(-1, 0, 0));
  81.         m_vertices.Add(new Vector3(0, 0, -1));
  82.  
  83.         //Put them in the array and recalculate normals
  84.         m_mesh.vertices = m_vertices.ToArray();
  85.         m_mesh.triangles = m_triangles.ToArray();
  86.         m_mesh.RecalculateNormals();
  87.     }
  88.  
  89.     public void CalculateVertices(int subdivisions, float radius) {
  90.         //Run the function CreateFirstObject
  91.         CreateFirstObject();
  92.  
  93.         //Creat local lists for the vertices and for the traingles
  94.         List<Vector3> vertices = new List<Vector3>();
  95.         List<int> triangles = new List<int>();
  96.        
  97.  
  98.         //The first for loop checks how many times you want to subdivide it
  99.         //Checks how many vertices that are in the list
  100.         for (int i = 0; i < subdivisions; i++) {
  101.             for (int j = 0; j < m_vertices.Count; j++) {
  102.                 vertices.Add(Normalize(m_vertices[j], m_radius));
  103.             }
  104.            
  105.             //Create an int to store the lenght of the local variable vertices to use later on
  106.             int oldVerticeCount = vertices.Count;
  107.  
  108.             //Use a for loop to go trough the traingles and get there reference point and store it in the variables
  109.             for (int j = 0; j < m_triangles.Count; j += 3) {
  110.                 int one = m_triangles[j];
  111.                 int two = m_triangles[j + 1];
  112.                 int three = m_triangles[j + 2];
  113.                
  114.                 //Calculate a new point for the vertices for further use later on
  115.                 Vector3 vertices01 = Normalize((m_vertices[one] + m_vertices[two]) / 2, m_radius);
  116.                 Vector3 vertices02 = Normalize((m_vertices[one] + m_vertices[three]) / 2, m_radius);
  117.                 Vector3 vertices03 = Normalize((m_vertices[two] + m_vertices[three]) / 2, m_radius);
  118.  
  119.                 //Vector3 nvertices04 = ((vertices02 + vertices01) / 2);
  120.                 //Vector3 nvertices05 = ((vertices03 + vertices02) / 2);
  121.                 //Vector3 nvertices06 = ((vertices01 + vertices03) / 2);
  122.  
  123.                 //Add the new vertices in the local vertices variable
  124.                 vertices.Add(vertices01);
  125.                 vertices.Add(vertices02);
  126.                 vertices.Add(vertices03);
  127.  
  128.                 //Creating the new a new triangle
  129.                 triangles.Add(one);
  130.                 triangles.Add(oldVerticeCount + j);
  131.                 triangles.Add(oldVerticeCount + j + 1);
  132.  
  133.                 triangles.Add(oldVerticeCount + j);
  134.                 triangles.Add(oldVerticeCount + j + 2);
  135.                 triangles.Add(oldVerticeCount + j + 1);
  136.  
  137.                 triangles.Add(oldVerticeCount + j);
  138.                 triangles.Add(two);
  139.                 triangles.Add(oldVerticeCount + j + 2);
  140.  
  141.                 triangles.Add(oldVerticeCount + j + 1);
  142.                 triangles.Add(oldVerticeCount + j + 2);
  143.                 triangles.Add(three);
  144.             }
  145.  
  146.             //Run the copy list function
  147.             CopyList(m_vertices, vertices);
  148.             CopyList(m_triangles, triangles);
  149.         }
  150.  
  151.         //Add the new vertices and traingles to the array and recalculate the normals again
  152.         m_mesh.vertices = m_vertices.ToArray();
  153.         m_mesh.triangles = m_triangles.ToArray();
  154.         m_mesh.RecalculateNormals();
  155.     }
  156.  
  157.    
  158.  
  159.     private void CopyList(List<Vector3> oldVertices, List<Vector3> newVertices) {
  160.         //Clear the old vertices
  161.         oldVertices.Clear();
  162.  
  163.         //Add the new vertices to the old vertices list
  164.         for (int i = 0; i < newVertices.Count; i++) {
  165.             oldVertices.Add(newVertices[i]);
  166.         }
  167.         //Clear the new vertices
  168.         newVertices.Clear();
  169.     }
  170.  
  171.     private void CopyList(List<int> oldTriangles, List<int> newTriangles) {
  172.         //Clear the old traingles
  173.         oldTriangles.Clear();
  174.  
  175.         //Add the new triangles to the old triangle list
  176.         for (int i = 0; i < newTriangles.Count; i++) {
  177.             oldTriangles.Add(newTriangles[i]);
  178.         }
  179.         //Clear the new triangle list
  180.         newTriangles.Clear();
  181.     }
  182.  
  183.  
  184.     //Self made normalize function that we use to normalize the vertices
  185.     private Vector3 Normalize(Vector3 vector, float radius) {
  186.         float lenght = Mathf.Sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
  187.         float x = vector.x / lenght * radius;
  188.         float y = vector.y / lenght * radius;
  189.         float z = vector.z / lenght * radius;
  190.  
  191.         return new Vector3(x, y, z);      
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement