public void AddVoxel(Voxel voxel) { _voxelAnimation.AddVoxelAnimation(_voxelData.HasVoxelAtPos(voxel.Pos), voxel, () => { _voxelData.AddVoxel(voxel); _voxelRenderer.RenderMesh(_voxelData); }); }
// TODO: Clean all of this code up... /// <summary> /// Update the model to represent the given voxel data /// </summary> public void RenderMesh(VoxelData voxelData) { List <Vector3> verts = new List <Vector3>(); List <int> trigs = new List <int>(); List <Color> colors = new List <Color>(); Mesh mesh = _meshFilter.mesh; foreach (Voxel voxel in voxelData.Voxels) { if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Up)) { GenTopFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Up)) { GenBottomFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Forward)) { GenNorthFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Forward)) { GenSouthFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Right)) { GenEastFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Right)) { GenWestFace(voxel, verts, trigs, colors); } } // Update the mesh filter to the new model mesh.Clear(); mesh.vertices = verts.ToArray(); mesh.triangles = trigs.ToArray(); mesh.colors = colors.ToArray(); mesh.uv = new List <Vector2>(verts.Count).ToArray(); mesh.Optimize(); mesh.RecalculateNormals(); // Update the mesh collider to the new model _meshCollider.sharedMesh = null; _meshCollider.sharedMesh = mesh; }
// TODO: Clean all of this code up... /// <summary> /// Update the model to represent the given voxel data /// </summary> public void RenderMesh(VoxelData voxelData) { List<Vector3> verts = new List<Vector3>(); List<int> trigs = new List<int>(); List<Color> colors = new List<Color>(); Mesh mesh = _meshFilter.mesh; foreach (Voxel voxel in voxelData.Voxels) { if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Up)) { GenTopFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Up)) { GenBottomFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Forward)) { GenNorthFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Forward)) { GenSouthFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos + Vector3Int.Right)) { GenEastFace(voxel, verts, trigs, colors); } if (!voxelData.HasVoxelAtPos(voxel.Pos - Vector3Int.Right)) { GenWestFace(voxel, verts, trigs, colors); } } // Update the mesh filter to the new model mesh.Clear(); mesh.vertices = verts.ToArray(); mesh.triangles = trigs.ToArray(); mesh.colors = colors.ToArray(); mesh.uv = new List<Vector2>(verts.Count).ToArray(); mesh.Optimize(); mesh.RecalculateNormals(); // Update the mesh collider to the new model _meshCollider.sharedMesh = null; _meshCollider.sharedMesh = mesh; }