Last active
February 28, 2017 13:50
-
-
Save andrewzimmer906/8f3ea6b55aeaa2b79b36cf3d67b82001 to your computer and use it in GitHub Desktop.
Create a cube with additional vertices in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Andrew Zimmer. | |
* 3/27/2017 | |
* | |
* Free to use and distribute. | |
* | |
**/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
enum Direction {Up, Down, West, East, North, South}; | |
public class SubdividedCube : MonoBehaviour { | |
private MeshFilter meshFilter; | |
private Mesh mesh; | |
private List<Vector3> verticies = new List<Vector3>(); | |
private List<int> triangles = new List<int>(); | |
private int squareCount; | |
[Range(0, 4)] | |
public int divisions; | |
public void Awake() { | |
RenderCube (); | |
} | |
public void RenderCube() { | |
if (meshFilter == null) { | |
meshFilter = GetComponent<MeshFilter> (); | |
mesh = new Mesh (); | |
} | |
float sizeOfFace = 1f / (float)(divisions + 1); | |
for (int x = 0; x < divisions + 1; x++) { | |
for (int y = 0; y < divisions + 1; y++) { | |
for (int z = 0; z < divisions + 1; z++) { | |
float fx = x * sizeOfFace; | |
float fy = y * sizeOfFace; | |
float fz = z * sizeOfFace; | |
if (x == 0) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.West); | |
} | |
if (x == divisions) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.East); | |
} | |
if (y == 0) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.Down); | |
} | |
if (y == divisions) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.Up); | |
} | |
if (z == 0) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.South); | |
} | |
if (z == divisions) { | |
AddSquare (fx, fy, fz, sizeOfFace, Direction.North); | |
} | |
} | |
} | |
} | |
UpdateMesh (); | |
} | |
/* -- Geometry -- */ | |
void AddSquare (float x, float y, float z, float size, Direction direction) { | |
if (direction == Direction.Up || | |
direction == Direction.Down) { | |
AddFaceForTopBottom (x, y, z, size, direction); | |
} else if (direction == Direction.North || | |
direction == Direction.South) { | |
AddFaceForNorthSouth (x, y, z, size, direction); | |
} else if (direction == Direction.West || | |
direction == Direction.East) { | |
AddFaceForWestEast (x, y, z, size, direction); | |
} | |
squareCount++; | |
} | |
void AddFaceForTopBottom(float x, float y, float z, float size, Direction direction) { | |
if (direction == Direction.Up) { | |
y += size; | |
} | |
verticies.Add (new Vector3(x, y, z + size)); | |
verticies.Add (new Vector3(x + size, y , z + size)); | |
verticies.Add (new Vector3(x + size, y, z)); | |
verticies.Add (new Vector3(x, y, z)); | |
AddLatestTriangles (direction == Direction.Up); | |
} | |
void AddFaceForWestEast(float x, float y, float z, float size, Direction direction) { | |
if (direction == Direction.East) { | |
x += size; | |
} | |
verticies.Add ( new Vector3(x, y + size, z) ); | |
verticies.Add ( new Vector3(x, y + size, z + size)); | |
verticies.Add ( new Vector3(x, y, z + size)); | |
verticies.Add ( new Vector3(x, y, z)); | |
AddLatestTriangles (direction == Direction.East); | |
} | |
void AddFaceForNorthSouth(float x, float y, float z, float size, Direction direction) { | |
if (direction == Direction.North) { | |
z += size; | |
} | |
verticies.Add (new Vector3(x, y + size, z)); | |
verticies.Add (new Vector3(x + size, y + size, z)); | |
verticies.Add (new Vector3(x + size, y, z)); | |
verticies.Add (new Vector3(x, y, z)); | |
AddLatestTriangles (direction == Direction.South); | |
} | |
void AddLatestTriangles(bool clockwise) { | |
if (clockwise) { | |
triangles.Add(squareCount * 4 ); // 1 | |
triangles.Add(squareCount * 4 + 1 ); // 2 | |
triangles.Add(squareCount * 4 + 2 ); // 3 | |
triangles.Add(squareCount * 4 ); // 1 | |
triangles.Add(squareCount * 4 + 2 ); // 3 | |
triangles.Add(squareCount * 4 + 3 ); // 4 | |
} else { | |
triangles.Add(squareCount * 4 + 2 ); // 3 | |
triangles.Add(squareCount * 4 + 1 ); // 2 | |
triangles.Add(squareCount * 4 ); // 1 | |
triangles.Add(squareCount * 4 + 3 ); // 4 | |
triangles.Add(squareCount * 4 + 2 ); // 3 | |
triangles.Add(squareCount * 4 ); // 1 | |
} | |
} | |
/* -- Draw -- */ | |
void UpdateMesh () { | |
mesh.Clear (); | |
mesh.vertices = verticies.ToArray(); | |
mesh.triangles = triangles.ToArray(); | |
mesh.RecalculateNormals (); | |
// Reset back to factory | |
verticies.Clear(); | |
triangles.Clear(); | |
squareCount = 0; | |
meshFilter.sharedMesh = mesh; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Andrew Zimmer. | |
* 3/27/2017 | |
* | |
* Free to use and distribute. | |
* | |
**/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor (typeof (SubdividedCube))] | |
public class SubdividedCubeEditor : Editor { | |
public override void OnInspectorGUI () { | |
SubdividedCube cubeDivide = (SubdividedCube)target; | |
if (DrawDefaultInspector ()) { | |
} | |
if (GUILayout.Button ("Render")) { | |
cubeDivide.RenderCube (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment