Last active
January 23, 2018 03:18
-
-
Save Domiii/ddfbdc70d36ad0ff0aac01f67be1a6fc to your computer and use it in GitHub Desktop.
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Voxelizer : MonoBehaviour { | |
public int xSlices = 5; | |
public int ySlices = 5; | |
public int zSlices = 5; | |
public GameObject Cube; | |
void Start () { | |
Voxelize (); | |
print ("Done"); | |
} | |
// fill a cube with many many small cubes | |
void Voxelize() { | |
var mesh = Cube.GetComponent<MeshRenderer> (); | |
var newCube = new GameObject (); | |
newCube.transform.position = Cube.transform.position; | |
newCube.transform.rotation = Cube.transform.rotation; | |
newCube.transform.localScale = Cube.transform.localScale; | |
for (var i = 0; i < xSlices; ++i) { | |
for (var j = 0; j < ySlices; ++j) { | |
for (var k = 0; k < zSlices; ++k) { | |
var smallCube = Instantiate(Cube, newCube.transform); | |
var x = Cube.transform.position.x; | |
var y = Cube.transform.position.y; | |
var z = Cube.transform.position.z; | |
var sliceSizeX = 2 * mesh.bounds.extents.x / xSlices + Mathf.Epsilon; | |
x = x - mesh.bounds.extents.x + sliceSizeX / 2 + i * sliceSizeX; | |
var sliceSizeY = 2 * mesh.bounds.extents.y / ySlices + Mathf.Epsilon; | |
y = y - mesh.bounds.extents.y + sliceSizeY / 2 + j * sliceSizeY; | |
var sliceSizeZ = 2 * mesh.bounds.extents.z / zSlices + Mathf.Epsilon; | |
z = z - mesh.bounds.extents.z + sliceSizeZ / 2 + k * sliceSizeZ; | |
smallCube.transform.position = new Vector3(x, y, z); | |
smallCube.transform.localScale = new Vector3(1.0f/xSlices, 1.0f/ySlices, 1.0f/zSlices); | |
smallCube.transform.localRotation = Quaternion.identity; | |
} | |
} | |
} | |
Cube.SetActive (false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment