public static float[] CreateCubeNormals() { List <float> normals = new List <float>(); void AppendVertexes(TextureType side) { var normal = FaceSides.Parse(side); for (int i = 0; i < 4; i++) { normals.Add(normal.x); normals.Add(normal.y); normals.Add(normal.z); } } AppendVertexes(TextureType.up); AppendVertexes(TextureType.down); AppendVertexes(TextureType.north); AppendVertexes(TextureType.south); AppendVertexes(TextureType.east); AppendVertexes(TextureType.west); return(normals.ToArray()); }
public static void AppendFace(TextureType side, float[] from, float[] to, Vector3 rotation, Vector3 rotationOrigin, ref float[] vertexes, ref float[] normals, int startIndex) { FaceSides faceSide = FaceSides.Parse(side); //TextureType parsed to FaceSides, also a normal of this face Vector3 normal = faceSide.ToVec(); float[] unitFace = _cube[faceSide]; //one side of the cube in unit size float x = from[0] / 16f; float y = from[1] / 16f; float z = from[2] / 16f; float sx = to[0] / 16f - x; //the size of the cube part float sy = to[1] / 16f - y; float sz = to[2] / 16f - z; for (var i = 0; i < unitFace.Length; i += 3) { var vertex = RotateVertex(new Vector3( x + unitFace[i] * sx, y + unitFace[i + 1] * sy, z + unitFace[i + 2] * sz), rotation, rotationOrigin); vertexes[startIndex + i] = vertex.X; vertexes[startIndex + i + 1] = vertex.Y; vertexes[startIndex + i + 2] = vertex.Z; var nrm = RotateVertex(normal, rotation, rotationOrigin); normals[startIndex + i] = nrm.X; normals[startIndex + i + 1] = nrm.Y; normals[startIndex + i + 2] = nrm.Z; } }
//unused for now public void AppendVertexesForSide(FaceSides side, List <float> vertexes, BlockPos offset) { /* * top = 0 * bottom = 1 * north = 2 * south = 3 * west = 4 * east = 5 * --> these are used as indexes of the faces, since the model vertex data is added in the same exact order of the TextureType enum values */ if (!FaceSides.Parse(side, out var parsed)) //FaceSides is a struct containing Vector2 values (normals) { return; } int faceIndex = (int)parsed * 12; for (int i = 0; i < 12; i += 3) { vertexes.Add(_vertexes[faceIndex + i] + offset.X); vertexes.Add(_vertexes[faceIndex + i + 1] + offset.Y); vertexes.Add(_vertexes[faceIndex + i + 2] + offset.Z); } }
//TODO - only use these when the block model is one 1x1x1 big cube public void AppendVertexDataForSide(FaceSides side, List <float> vertexes, List <float> normals, List <float> uvs, BlockPos offset) { if (!FaceSides.Parse(side, out var parsed)) //FaceSides is a struct containing Vector2 values (normals) { return; } int faceIndex = (int)parsed * 12; for (int i = 0; i < 12; i += 3) { vertexes.Add(_vertexes[faceIndex + i] + offset.X); vertexes.Add(_vertexes[faceIndex + i + 1] + offset.Y); vertexes.Add(_vertexes[faceIndex + i + 2] + offset.Z); normals.Add(_normals[faceIndex + i]); normals.Add(_normals[faceIndex + i + 1]); normals.Add(_normals[faceIndex + i + 2]); } faceIndex = (int)parsed * 8; for (int i = 0; i < 8; i += 2) { uvs.Add(_uvs[faceIndex + i]); uvs.Add(_uvs[faceIndex + i + 1]); } }
public static void AppendFace(Facing side, float[] from, float[] to, Vector3 rotation, Vector3 rotationOrigin, ref List <float> vertexes, ref List <float> normals) { FaceSides faceSide = FaceSides.Parse(side); //TextureType parsed to FaceSides, also a normal of this face Vector3 normal = faceSide.ToVec(); float[] unitFace = _cubeTriangles[faceSide]; //one side of the cube in unit size float x = from[0] / 16f; float y = from[1] / 16f; float z = from[2] / 16f; float sx = to[0] / 16f - x; //the size of the cube part float sy = to[1] / 16f - y; float sz = to[2] / 16f - z; for (var i = 0; i < unitFace.Length; i += 3) { var vertex = RotateVertex(new Vector3( x + unitFace[i] * sx, y + unitFace[i + 1] * sy, z + unitFace[i + 2] * sz), rotation, rotationOrigin); vertexes.Add(vertex.X); vertexes.Add(vertex.Y); vertexes.Add(vertex.Z); var nrm = RotateVertex(normal, rotation, rotationOrigin); normals.Add(nrm.X); normals.Add(nrm.Y); normals.Add(nrm.Z); } }
public void AppendUvsForSide(FaceSides side, List <float> uvs) { if (!FaceSides.Parse(side, out var parsed)) { return; } int faceIndex = (int)parsed * 8; for (int i = 0; i < 8; i += 2) { uvs.Add(_uvs[faceIndex + i]); uvs.Add(_uvs[faceIndex + i + 1]); } }
//unused for now public void AppendNormalsForSide(FaceSides side, List <float> normals) { if (!FaceSides.Parse(side, out var parsed)) { return; } int faceIndex = (int)parsed * 12; for (int i = 0; i < 12; i += 3) { normals.Add(_normals[faceIndex + i]); normals.Add(_normals[faceIndex + i + 1]); normals.Add(_normals[faceIndex + i + 2]); } }
public static float[] CreateCubeVertexes(bool centered = false) { List <float> vertexes = new List <float>(); void AppendVertexes(TextureType side) { var face = _cube[FaceSides.Parse(side)]; for (var index = 0; index < face.Length; index += 3) { var x = face[index]; var y = face[index + 1]; var z = face[index + 2]; if (centered) { x -= 0.5f; y -= 0.5f; z -= 0.5f; } vertexes.Add(x); vertexes.Add(y); vertexes.Add(z); } } AppendVertexes(TextureType.up); AppendVertexes(TextureType.down); AppendVertexes(TextureType.north); AppendVertexes(TextureType.south); AppendVertexes(TextureType.east); AppendVertexes(TextureType.west); return(vertexes.ToArray()); }