public void Voxelate() { if (m_pixels3DContainer == null) { m_spriteRenderer = GetComponent<SpriteRenderer> (); m_spriteBuilder = GetComponent<SpriteBuilder> (); m_spriteBuilder.Build (); var pixels = m_spriteBuilder.ToPixels (); m_spriteRenderer.enabled = false; var localScale = transform.localScale; var tempScale = new Vector3 (Scale.x * localScale.x, Scale.y * localScale.y, Scale.z * localScale.z); var rect = m_spriteBuilder.Sprite.rect; var halfWidth = (rect.width / 2) * tempScale.x; var halfHeight = (rect.height / 2) * tempScale.y; m_pixels3DContainer = new GameObject ("Pixels3DContainer"); if (KeepContainerParent) { m_pixels3DContainer.transform.parent = transform; } foreach (var p in pixels) { if (p.Color.a >= MinAlphaToVoxel) { var pixel3D = Instantiate (Pixel3DPrefab, new Vector3 ( transform.position.x + p.X * tempScale.x - halfWidth, transform.position.y + p.Y * tempScale.y - halfHeight, 0) , Quaternion.identity) as GameObject; var luminance = 0.2126f * p.Color.r + 0.7152f * p.Color.g + 0.0722f * p.Color.b; pixel3D.transform.localScale = new Vector3(tempScale.x, tempScale.y, tempScale.z + luminance * ZScaleByColorLuminance); var cubeRenderer = pixel3D.GetComponent<MeshRenderer> (); cubeRenderer.material.color = p.Color; pixel3D.transform.parent = m_pixels3DContainer.transform; } } } }