Created
January 6, 2023 18:55
-
-
Save NepNet/5240175c84521e330a74cd25aa5d6d3a to your computer and use it in GitHub Desktop.
A weird cube mesh generator for unity with option to generate only certain faces
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public class CubeGenerator : MonoBehaviour | |
{ | |
public struct Face | |
{ | |
public Vector3[] vertices; | |
public Vector3[] normals; | |
public Vector2[] uvs; | |
public int[] indices; | |
} | |
private static readonly Vector3[] _corners = | |
{ | |
new Vector3(-0.5f, -0.5f, -0.5f), | |
new Vector3(+0.5f, -0.5f, -0.5f), | |
new Vector3(+0.5f, -0.5f, +0.5f), | |
new Vector3(-0.5f, -0.5f, +0.5f), | |
new Vector3(-0.5f, +0.5f, -0.5f), | |
new Vector3(+0.5f, +0.5f, -0.5f), | |
new Vector3(+0.5f, +0.5f, +0.5f), | |
new Vector3(-0.5f, +0.5f, +0.5f), | |
}; | |
private static readonly Vector2[] _uvs = | |
{ | |
new Vector2(0, 1), | |
new Vector2(1, 1), | |
new Vector2(1, 0), | |
new Vector2(0, 0) | |
}; | |
private static readonly int[][] _faces = | |
{ | |
new[] { 0, 1, 2, 3 }, | |
new[] { 7, 6, 5, 4 }, | |
new[] { 4, 5, 1, 0 }, | |
new[] { 5, 6, 2, 1 }, | |
new[] { 6, 7, 3, 2 }, | |
new[] { 7, 4, 0, 3 }, | |
}; | |
private static readonly Vector3[] _normals = | |
{ | |
Vector3.down, | |
Vector3.up, | |
Vector3.back, | |
Vector3.right, | |
Vector3.forward, | |
Vector3.left | |
}; | |
private static readonly int[] _indices = { 0, 1, 2, 2, 3, 0 }; | |
void Start() | |
{ | |
var filter = GetComponent<MeshFilter>(); | |
var mesh = new Mesh(); | |
var facesToBuild = new int[] { 0, 1, 2, 3, 4, 5 }; | |
var vertexCount = 0; | |
var vertices = new List<Vector3>(); | |
var normals = new List<Vector3>(); | |
var uvs = new List<Vector2>(); | |
var indices = new List<int>(); | |
foreach (var face in facesToBuild.Select(f => GetFace(Vector3.zero, f, vertexCount))) | |
{ | |
vertices.AddRange(face.vertices); | |
normals.AddRange(face.normals); | |
uvs.AddRange(face.uvs); | |
indices.AddRange(face.indices); | |
vertexCount += face.vertices.Length; | |
} | |
mesh.vertices = vertices.ToArray(); | |
mesh.normals = normals.ToArray(); | |
mesh.uv = uvs.ToArray(); | |
mesh.triangles = indices.ToArray(); | |
filter.mesh = mesh; | |
} | |
public Face GetFace(Vector3 position, int direction, int vertexOffset) | |
{ | |
if (direction < 0 || direction > 5) | |
{ | |
throw new ArgumentException("face id is not valid"); | |
} | |
var face = new Face(); | |
face.vertices = _faces[direction].Select(i => _corners[i] + position).ToArray(); | |
face.normals = Enumerable.Repeat(_normals[direction], 4).ToArray(); | |
face.uvs = _uvs; | |
face.indices = _indices.Select(i => i + vertexOffset).ToArray(); | |
return face; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment