Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- //Add the mesh filter and Renderer
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- public class BaseModel : MonoBehaviour {
- //Create variables for the meshfilter and mesh
- private Mesh m_mesh;
- private MeshFilter m_meshFilter;
- //Create list for the vertices and traingles
- [SerializeField]List<Vector3> m_vertices = new List<Vector3>();
- [SerializeField]List<int> m_triangles = new List<int>();
- //How many times should it subdivide (Get more rounder)
- [SerializeField]private int m_subdivisions = 3;
- [SerializeField]private float m_radius = 1f;
- private void Awake() {
- //Creating the mesh and give it a name
- m_mesh = new Mesh();
- m_mesh.name = "Quadrangle";
- }
- // Use this for initialization
- void Start() {
- //Get the meshFilter and put it in the variable
- m_meshFilter = GetComponent<MeshFilter>();
- m_meshFilter.mesh = m_mesh;
- //Run the function CalculateVertices
- CalculateVertices(m_subdivisions, m_radius);
- }
- public void CreateFirstObject() {
- //Hard coding the octahedrons mesh
- m_triangles.Add(0);
- m_triangles.Add(3);
- m_triangles.Add(2);
- m_triangles.Add(4);
- m_triangles.Add(3);
- m_triangles.Add(0);
- m_triangles.Add(4);
- m_triangles.Add(0);
- m_triangles.Add(5);
- m_triangles.Add(0);
- m_triangles.Add(2);
- m_triangles.Add(5);
- m_triangles.Add(1);
- m_triangles.Add(2);
- m_triangles.Add(3);
- m_triangles.Add(3);
- m_triangles.Add(4);
- m_triangles.Add(1);
- m_triangles.Add(5);
- m_triangles.Add(1);
- m_triangles.Add(4);
- m_triangles.Add(1);
- m_triangles.Add(5);
- m_triangles.Add(2);
- //Give the world space position to the vertices
- //top and bottom
- m_vertices.Add(new Vector3(0, 1, 0));
- m_vertices.Add(new Vector3(0, -1, 0));
- //left and right
- m_vertices.Add(new Vector3(1, 0, 0));
- m_vertices.Add(new Vector3(0, 0, 1));
- //back and forward
- m_vertices.Add(new Vector3(-1, 0, 0));
- m_vertices.Add(new Vector3(0, 0, -1));
- //Put them in the array and recalculate normals
- m_mesh.vertices = m_vertices.ToArray();
- m_mesh.triangles = m_triangles.ToArray();
- m_mesh.RecalculateNormals();
- }
- public void CalculateVertices(int subdivisions, float radius) {
- //Run the function CreateFirstObject
- CreateFirstObject();
- //Creat local lists for the vertices and for the traingles
- List<Vector3> vertices = new List<Vector3>();
- List<int> triangles = new List<int>();
- //The first for loop checks how many times you want to subdivide it
- //Checks how many vertices that are in the list
- for (int i = 0; i < subdivisions; i++) {
- for (int j = 0; j < m_vertices.Count; j++) {
- vertices.Add(Normalize(m_vertices[j], m_radius));
- }
- //Create an int to store the lenght of the local variable vertices to use later on
- int oldVerticeCount = vertices.Count;
- //Use a for loop to go trough the traingles and get there reference point and store it in the variables
- for (int j = 0; j < m_triangles.Count; j += 3) {
- int one = m_triangles[j];
- int two = m_triangles[j + 1];
- int three = m_triangles[j + 2];
- //Calculate a new point for the vertices for further use later on
- Vector3 vertices01 = Normalize((m_vertices[one] + m_vertices[two]) / 2, m_radius);
- Vector3 vertices02 = Normalize((m_vertices[one] + m_vertices[three]) / 2, m_radius);
- Vector3 vertices03 = Normalize((m_vertices[two] + m_vertices[three]) / 2, m_radius);
- //Vector3 nvertices04 = ((vertices02 + vertices01) / 2);
- //Vector3 nvertices05 = ((vertices03 + vertices02) / 2);
- //Vector3 nvertices06 = ((vertices01 + vertices03) / 2);
- //Add the new vertices in the local vertices variable
- vertices.Add(vertices01);
- vertices.Add(vertices02);
- vertices.Add(vertices03);
- //Creating the new a new triangle
- triangles.Add(one);
- triangles.Add(oldVerticeCount + j);
- triangles.Add(oldVerticeCount + j + 1);
- triangles.Add(oldVerticeCount + j);
- triangles.Add(oldVerticeCount + j + 2);
- triangles.Add(oldVerticeCount + j + 1);
- triangles.Add(oldVerticeCount + j);
- triangles.Add(two);
- triangles.Add(oldVerticeCount + j + 2);
- triangles.Add(oldVerticeCount + j + 1);
- triangles.Add(oldVerticeCount + j + 2);
- triangles.Add(three);
- }
- //Run the copy list function
- CopyList(m_vertices, vertices);
- CopyList(m_triangles, triangles);
- }
- //Add the new vertices and traingles to the array and recalculate the normals again
- m_mesh.vertices = m_vertices.ToArray();
- m_mesh.triangles = m_triangles.ToArray();
- m_mesh.RecalculateNormals();
- }
- private void CopyList(List<Vector3> oldVertices, List<Vector3> newVertices) {
- //Clear the old vertices
- oldVertices.Clear();
- //Add the new vertices to the old vertices list
- for (int i = 0; i < newVertices.Count; i++) {
- oldVertices.Add(newVertices[i]);
- }
- //Clear the new vertices
- newVertices.Clear();
- }
- private void CopyList(List<int> oldTriangles, List<int> newTriangles) {
- //Clear the old traingles
- oldTriangles.Clear();
- //Add the new triangles to the old triangle list
- for (int i = 0; i < newTriangles.Count; i++) {
- oldTriangles.Add(newTriangles[i]);
- }
- //Clear the new triangle list
- newTriangles.Clear();
- }
- //Self made normalize function that we use to normalize the vertices
- private Vector3 Normalize(Vector3 vector, float radius) {
- float lenght = Mathf.Sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
- float x = vector.x / lenght * radius;
- float y = vector.y / lenght * radius;
- float z = vector.z / lenght * radius;
- return new Vector3(x, y, z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement