Skip to content

Instantly share code, notes, and snippets.

@Ethan-Bierlein
Created January 18, 2017 23:15
Show Gist options
  • Save Ethan-Bierlein/c0d0fe902b85833e6907a966105a34df to your computer and use it in GitHub Desktop.
Save Ethan-Bierlein/c0d0fe902b85833e6907a966105a34df to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SkyVoxels.Engine.Utilities
{
/// <summary>
/// This class serves as an advanced "wrapper" of sorts to the default Unity
/// Mesh class to generate visual or physics meshes dynamically. This class
/// is specially tailored for this project.
/// </summary>
public class DynamicMesh
{
public List<Vector3> RenderingVertices { get; set; }
public List<int> RenderingTriangles { get; set; }
public List<Vector2> RenderingUVs { get; set; }
public List<Color> RenderingColors { get; set; }
public List<Vector3> ColliderVertices { get; set; }
public List<int> ColliderTriangles { get; set; }
/// <summary>
/// Constructor for the DynamicMesh class.
/// </summary>
/// <param name="overallAllocationSize">The base allocation size for all collections in the DynamicMesh.</param>
public DynamicMesh(int overallAllocationSize)
{
this.RenderingVertices = new List<Vector3>(overallAllocationSize) { };
this.RenderingTriangles = new List<int>(overallAllocationSize) { };
this.RenderingUVs = new List<Vector2>(overallAllocationSize) { };
this.RenderingColors = new List<Color>(overallAllocationSize) { };
this.ColliderVertices = new List<Vector3>(overallAllocationSize) { };
this.ColliderTriangles = new List<int>(overallAllocationSize) { };
}
/// <summary>
/// Add a "quad" into the lists of triangle indices.
/// </summary>
/// <param name="isFlipped">Whether or not the rendering quad is flipped.</param>
/// <param name="generateColliderData"></param>
public void AddQuad(bool isFlipped, bool generateColliderData = true)
{
if(this.RenderingVertices.Count >= 4)
{
if(isFlipped)
{
this.RenderingTriangles.Add(this.RenderingVertices.Count - 4);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 3);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 2);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 4);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 2);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 1);
}
else
{
this.RenderingTriangles.Add(this.RenderingVertices.Count - 4);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 3);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 1);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 3);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 2);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 1);
}
}
else
{
throw new System.Exception("At least 4 vertices are required to add a rendering quad.");
}
if(generateColliderData && this.ColliderVertices.Count >= 4)
{
this.ColliderTriangles.Add(this.ColliderVertices.Count - 4);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 3);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 1);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 3);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 2);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 1);
}
else
{
throw new System.Exception("At least 4 vertices are required to add a collider. quad.");
}
}
/// <summary>
/// Add a "triangle" into the lists of triangle indices.
/// </summary>
/// <param name="generateColliderData">Whether or not collider triangles should be generated.</param>
public void AddTriangle(bool generateColliderData = true)
{
if(this.RenderingVertices.Count >= 3)
{
this.RenderingTriangles.Add(this.RenderingVertices.Count - 3);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 2);
this.RenderingTriangles.Add(this.RenderingVertices.Count - 1);
}
else
{
throw new System.Exception("At least 3 vertices are required to add a rendering triangle.");
}
if(generateColliderData && this.ColliderVertices.Count >= 3)
{
this.ColliderTriangles.Add(this.ColliderVertices.Count - 3);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 2);
this.ColliderTriangles.Add(this.ColliderVertices.Count - 1);
}
else
{
throw new System.Exception("At least 3 vertices are required to add a collider triangle.");
}
}
/// <summary>
/// Add an array of UVs to the list of UV coordinates.
/// </summary>
/// <param name="uvCoordinates">The list of UV coordinates to add.</param>
public void AddUVs(Vector2[] uvCoordinates)
{
this.RenderingUVs.AddRange(uvCoordinates);
}
/// <summary>
/// Add a UV to the list of UV coordinates.
/// </summary>
/// <param name="uvCoordinate">The UV coordinate to add.</param>
public void AddUV(Vector2 uvCoordinate)
{
this.RenderingUVs.Add(uvCoordinate);
}
/// <summary>
/// Add an array of vertices into the lists of vertices.
/// </summary>
/// <param name="vertexPositions">The array of vertex positions.</param>
/// <param name="colors">The array of vertex colors.</param>
/// <param name="generateColliderData">Whether or not collider data should be generated.</param>
public void AddVertices(Vector3[] vertexPositions, Color[] colors, bool generateColliderData = true)
{
this.RenderingVertices.AddRange(vertexPositions);
this.RenderingColors.AddRange(colors);
if(generateColliderData)
{
this.ColliderVertices.AddRange(vertexPositions);
}
}
/// <summary>
/// Add a singular vertex into the lists of vertices.
/// </summary>
/// <param name="vertexPosition">The position of the vertex.</param>
/// <param name="color">The color of the vertex.</param>
/// <param name="generateColliderData">Whether or not the vertex should be added to the collider data.</param>
public void AddVertex(Vector3 vertexPosition, Color32 color, bool generateColliderData = true)
{
this.RenderingVertices.Add(vertexPosition);
this.RenderingColors.Add(color);
if(generateColliderData)
{
this.ColliderVertices.Add(vertexPosition);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment