概要
「unity-voxelizer」を Unity プロジェクトに導入することで
3Dオブジェクトをボクセル化できるようになります
使用例
使い方
using UnityEngine; public class Example : MonoBehaviour { public GameObject m_voxelModel; private void Awake() { var bounds = new Bounds( Vector3.zero, Vector3.one ); var xDensity = 8; var yDensity = 8; var zDensity = 8; var voxeliser = new Voxeliser( bounds, xDensity, yDensity, zDensity ); voxeliser.Voxelize( transform ); var gridCubeSize = new Vector3 ( bounds.size.x / xDensity, bounds.size.y / yDensity, bounds.size.z / zDensity ); var worldCentre = bounds.min + gridCubeSize / 2; var voxelRoot = new GameObject( "Voxel Root" ); var rootTransform = voxelRoot.transform; for ( int x = 0; x < xDensity; x++ ) { for ( int y = 0; y < yDensity; y++ ) { for ( int z = 0; z < zDensity; z++ ) { if ( voxeliser.VoxelMap[ x ][ y ][ z ] ) { var pos = worldCentre + new Vector3 ( x * gridCubeSize.x, y * gridCubeSize.y, z * gridCubeSize.z ); var go = Instantiate( m_voxelModel, pos, Quaternion.identity ); go.transform.localScale = gridCubeSize; go.transform.SetParent( rootTransform, true ); } } } } } }
上記のようなスクリプトを作成することで使用できます