private List <VoxInfo> BuildVoxInfo(Voxel_t[] voxels, float unit) { List <VoxInfo> vi = new List <VoxInfo>(); if (!selectedMesh || !selectedMaterial) { return(vi); } Vector3 min = selectedMesh.bounds.min; Texture2D tex = GetReadableTexture(selectedMaterial.mainTexture); float n = 1 / unit; for (int i = 0; i < voxels.Length; i++) { Voxel_t v = voxels[i]; if (v.fill == 0) { continue; } Vector3 p = v.position - min; p *= n; int x = Mathf.RoundToInt(p.x); int y = Mathf.RoundToInt(p.y); int z = Mathf.RoundToInt(p.z); Vector3Int vp = new Vector3Int(x, y, z); Color col = Color.white; if (v.uv.x != -1)//Fill color? { if (tex != null) { col = tex.GetPixel((int)(v.uv.x * tex.width), (int)(v.uv.y * tex.height)); } else { col = selectedMaterial.color; } } VoxInfo nvi = new VoxInfo { pos = vp, col = col, idx = 1 }; vi.Add(nvi); } return(vi); }
private void CreatePreviewPalette() { IColorQuantizer quantizer = new PaletteQuantizer(); //List<Color32> colors; if (texturePalette != null) { Texture2D tp = GetReadableTexture(texturePalette); Color32[] cs32 = tp.GetPixels32(); if (tp.width == 256 && tp.height == 1) { //Don't use quantizer selectedPalette = new List <Color32>(); foreach (var c32 in cs32) { selectedPalette.Add(c32); } } else { foreach (var c32 in cs32) { quantizer.AddColor(c32); } selectedPalette = quantizer.GetPalette(255); } } else { for (int i = 0; i < selectedVoxelInfo.Count; i++) { VoxInfo v = selectedVoxelInfo[i]; quantizer.AddColor(v.col); } selectedPalette = quantizer.GetPalette(colorTotal); } previewPalette = new Texture2D(8, 32, TextureFormat.ARGB32, false); previewPalette.filterMode = FilterMode.Point; int xp = 0; int yp = 0; foreach (var c in selectedPalette) { previewPalette.SetPixel(xp, yp, c); xp++; if (xp >= 8) { xp = 0; yp++; } } previewPalette.Apply(); foreach (VoxInfo v in selectedVoxelInfo) { v.idx = (Byte)(ClosestColor(selectedPalette, v.col) + 1); } }