ドット絵からボクセルを生成「DotToBox」
#Unity でドット絵を読み込んで,立体にするコードです. https://gyazo.com/259ca2b2c2b6adc7f66f86ae2338e83a
https://gyazo.com/cc39ba987f1945e948d563a1079fe025
【仕組み】
まず,左上から右下に向かって画像のピクセルを読み込んでいきます.
ピクセルの色と同じ色のキューブをその位置に生成し,次の位置へ移動し,新たなピクセルを読み込みます.
それを繰り返すと,ドット絵がそのままボコッと盛り上がったような立体物ができます.
また,アルファも読み込んでいるため,透明度が一定以下であれば描画しないということも可能です.
詳しくは下記.
code:cs
//ignore transparent dot
if (mainTexture.GetPixel(currentWidth, currentHeight).a > alphaThreshold) {
//make cube
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//move it
cube.transform.Translate(currentPosition);
//get color from pixel and paint
cube.GetComponent<Renderer>().material.color = mainTexture.GetPixel(currentWidth,currentHeight);
}