void GenerateColorMapAsset() { ColorBasedModelDefinition baseModel = QubicleBinaryToColorBasedModelDefinition(); if (baseModel.colors == null) { return; } ColorToVoxelMap colorMap = VoxelPlayConverter.GetColorToVoxelMapDefinition(baseModel); if (!string.IsNullOrEmpty(baseModel.name)) { colorMap.name = baseModel.name; } colorMap.name += " ColorMap"; // Create a suitable file path string path = GetPathForNewAsset(); AssetDatabase.CreateAsset(colorMap, path + "/" + GetFilenameForNewModel(colorMap.name) + ".asset"); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = colorMap; EditorGUIUtility.PingObject(colorMap); }
public static ColorToVoxelMap GetColorToVoxelMapDefinition(ColorBasedModelDefinition model) { ColorToVoxelMap mapping = ScriptableObject.CreateInstance <ColorToVoxelMap> (); List <Color32> uniqueColors = new List <Color32> (); Color32 prevColor = Misc.color32Transparent; for (int k = 0; k < model.colors.Length; k++) { Color32 color = model.colors [k]; if (color.a == 0 || (color.r == prevColor.r && color.g == prevColor.g && color.b == prevColor.b && color.a == prevColor.a)) { continue; } if (!uniqueColors.Contains(color)) { uniqueColors.Add(color); } } int colorCount = uniqueColors.Count; mapping.colorMap = new ColorToVoxelMapEntry[colorCount]; for (int k = 0; k < colorCount; k++) { mapping.colorMap [k].color = uniqueColors [k]; } return(mapping); }
ColorBasedModelDefinition TextureToColorBasedModelDefinition(Texture2D texture) { Color32[] colors = texture.GetPixels32(); ColorBasedModelDefinition model = new ColorBasedModelDefinition(); model.name = texture.name; model.sizeX = texture.width; model.sizeY = texture.height; model.sizeZ = 1; model.colors = colors; return(model); }
void GeneratePrefab() { ColorBasedModelDefinition baseModel = QubicleBinaryToColorBasedModelDefinition(); if (baseModel.colors == null) { return; } // Generate a cuboid per visible voxel int sizeX = baseModel.sizeX; int sizeY = baseModel.sizeY; int sizeZ = baseModel.sizeZ; float offsetX = 0, offsetY = 0, offsetZ = 0; if (!importIgnoreOffset) { offsetX += baseModel.offsetX; offsetY += baseModel.offsetY; offsetZ += baseModel.offsetZ; } Color32[] colors = baseModel.colors; GameObject obj = VoxelPlayConverter.GenerateVoxelObject(colors, sizeX, sizeY, sizeZ, new Vector3(offsetX, offsetY, offsetZ), scale); string path = GetPathForNewAsset(); path += "/" + GetFilenameForNewModel(baseModel.name) + ".prefab"; #if UNITY_2018_3_OR_NEWER GameObject prefab = PrefabUtility.SaveAsPrefabAsset(obj, path); #else GameObject prefab = PrefabUtility.CreatePrefab(path, obj); #endif // Store the mesh inside the prefab Mesh mesh = obj.GetComponent <MeshFilter>().sharedMesh; AssetDatabase.AddObjectToAsset(mesh, prefab); prefab.GetComponent <MeshFilter> ().sharedMesh = mesh; Material mat = obj.GetComponent <MeshRenderer> ().sharedMaterial; AssetDatabase.AddObjectToAsset(mat, prefab); prefab.GetComponent <MeshRenderer> ().sharedMaterial = mat; MeshCollider mc = prefab.AddComponent <MeshCollider> (); mc.sharedMesh = mesh; mc.convex = true; Rigidbody rb = prefab.AddComponent <Rigidbody> (); rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ; AssetDatabase.SaveAssets(); DestroyImmediate(obj); EditorUtility.FocusProjectWindow(); Selection.activeObject = prefab; EditorGUIUtility.PingObject(prefab); }
ColorBasedModelDefinition QubicleBinaryToColorBasedModelDefinition() { ColorBasedModelDefinition baseModel = ColorBasedModelDefinition.Null; Stream file = System.IO.File.Open(importFilename, FileMode.Open); try { baseModel = QubicleImporter.ImportBinary(file, System.Text.Encoding.UTF8); } catch { } finally { file.Close(); } return(baseModel); }
GameObject GeneratePrefab() { ColorBasedModelDefinition baseModel = TextureToColorBasedModelDefinition(texture); if (baseModel.colors == null) { return(null); } // Generate a cuboid per visible voxel int sizeX = baseModel.sizeX; int sizeY = baseModel.sizeY; int sizeZ = baseModel.sizeZ; Color32[] colors = baseModel.colors; Vector3 scale = new Vector3(size.x / texture.width, size.y / texture.height, size.z); Vector3 offset = new Vector3(0, -0.5f, 0); GameObject obj = VoxelPlayConverter.GenerateVoxelObject(colors, sizeX, sizeY, sizeZ, offset, scale); string path = GetPathForNewAsset(); path += "/" + GetFilenameForNewModel(baseModel.name) + ".prefab"; #if UNITY_2018_3_OR_NEWER GameObject prefab = PrefabUtility.SaveAsPrefabAsset(obj, path); #else GameObject prefab = PrefabUtility.CreatePrefab(path, obj); #endif // Store the mesh inside the prefab Mesh mesh = obj.GetComponent <MeshFilter> ().sharedMesh; AssetDatabase.AddObjectToAsset(mesh, prefab); prefab.GetComponent <MeshFilter> ().sharedMesh = mesh; Material mat = obj.GetComponent <MeshRenderer> ().sharedMaterial; AssetDatabase.AddObjectToAsset(mat, prefab); prefab.GetComponent <MeshRenderer> ().sharedMaterial = mat; MeshCollider mc = prefab.AddComponent <MeshCollider> (); mc.sharedMesh = mesh; mc.convex = true; Rigidbody rb = prefab.AddComponent <Rigidbody> (); rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ; AssetDatabase.SaveAssets(); DestroyImmediate(obj); EditorUtility.FocusProjectWindow(); Selection.activeObject = prefab; EditorGUIUtility.PingObject(prefab); return(prefab); }
void GenerateModelAsset() { ColorBasedModelDefinition baseModel = QubicleBinaryToColorBasedModelDefinition(); if (baseModel.colors == null) { return; } ModelDefinition newModel = VoxelPlayConverter.GetModelDefinition(null, baseModel, importIgnoreOffset, mapping); if (!string.IsNullOrEmpty(baseModel.name)) { newModel.name = baseModel.name; } // Create a suitable file path string path = GetPathForNewAsset(); AssetDatabase.CreateAsset(newModel, path + "/" + GetFilenameForNewModel(newModel.name) + ".asset"); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = newModel; EditorGUIUtility.PingObject(newModel); }
public static ModelDefinition GetModelDefinition(VoxelDefinition voxelTemplate, ColorBasedModelDefinition model, bool ignoreOffset, ColorToVoxelMap colorMap = null) { ModelDefinition md = ScriptableObject.CreateInstance <ModelDefinition> (); md.sizeX = model.sizeX; md.sizeY = model.sizeY; md.sizeZ = model.sizeZ; if (!ignoreOffset) { md.offsetX = model.offsetX; md.offsetY = model.offsetY; md.offsetZ = model.offsetZ; } if (colorMap != null && voxelTemplate == null) { voxelTemplate = colorMap.defaultVoxelDefinition; } List <ModelBit> bits = new List <ModelBit> (); for (int y = 0; y < model.sizeY; y++) { int posy = y * model.sizeX * model.sizeZ; for (int z = 0; z < model.sizeZ; z++) { int posz = z * model.sizeX; for (int x = 0; x < model.sizeX; x++) { int index = posy + posz + x; if (model.colors [index].a > 0) { ModelBit bit = new ModelBit(); bit.voxelIndex = index; if (colorMap != null) { bit.voxelDefinition = colorMap.GetVoxelDefinition(model.colors [index], voxelTemplate); bit.color = Misc.color32White; } else { bit.voxelDefinition = voxelTemplate; bit.color = model.colors [index]; } bits.Add(bit); } } } } md.bits = bits.ToArray(); return(md); }
public static ColorBasedModelDefinition ImportBinary(Stream stream, Encoding encoding) { uint version; uint colorFormat; uint compressed; uint index, count; Color32 [] colors = null; const int CODEFLAG = 2; const int NEXTSLICEFLAG = 6; using (BinaryReader br = new BinaryReader(stream, encoding)) { version = br.ReadUInt32(); if (version != 257) { Debug.LogError("Voxel Play: Unrecognized Qubicly binary version in model!"); return(ColorBasedModelDefinition.Null); } colorFormat = br.ReadUInt32(); br.ReadUInt32(); // zAxis Orientation compressed = br.ReadUInt32(); br.ReadUInt32(); // Visibility encoded, not used uint numMatrices = br.ReadUInt32(); if (numMatrices == 0) { Debug.LogError("Voxel Play: Model is empty."); return(ColorBasedModelDefinition.Null); } // List<Color[]> matrixList = new List<Color[]> (); // for (int i = 0; i < numMatrices; i++) { // for each matrix stored in // read matrix name int nameLength = br.ReadByte(); string name = new string (br.ReadChars(nameLength)); // read matrix size uint sizeX = br.ReadUInt32(); uint sizeY = br.ReadUInt32(); uint sizeZ = br.ReadUInt32(); if (sizeX < 0 || sizeX > 128 || sizeY < 0 || sizeY > 128 || sizeZ < 0 || sizeZ > 128) { Debug.LogError("Voxel Play: Invalid model size: " + sizeX + "/" + sizeY + "/" + sizeZ); // return ColorBasedModelDefinition.Null; } // read matrix position (in this example the position is irrelevant) int offsetX = br.ReadInt32(); int offsetY = br.ReadInt32(); int offsetZ = br.ReadInt32(); // create matrix and add to matrix list colors = new Color32 [sizeX * sizeY * sizeZ]; // matrixList.Add (matrix); if (compressed == 0) // if uncompressd { for (int z = 0; z < sizeZ; z++) { for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { long colorIndex = x + z * sizeX + y * sizeX * sizeZ; if (colorIndex >= colors.Length) { Debug.LogError("Voxel Play: Model size or contents do not match."); return(ColorBasedModelDefinition.Null); } colors [colorIndex] = GetColor(br.ReadUInt32(), colorFormat); } } } } else // if compressed { int z = -1; while (++z < sizeZ) { index = 0; while (true) { uint data = br.ReadUInt32(); if (data == NEXTSLICEFLAG) { break; } if (data == CODEFLAG) { count = br.ReadUInt32(); data = br.ReadUInt32(); for (int j = 0; j < count; j++) { if (data != 0) { uint x = index % sizeX; // mod = modulo e.g. 12 mod 8 = 4 uint y = index / sizeX; // div = integer division e.g. 12 div 8 = 1 long colorIndex = x + z * sizeX + y * sizeX * sizeZ; if (colorIndex >= colors.Length) { Debug.LogError("Voxel Play: Model size or contents do not match."); return(ColorBasedModelDefinition.Null); } colors [colorIndex] = GetColor(data, colorFormat); } index++; } } else { if (data != 0) { uint x = index % sizeX; uint y = index / sizeX; long colorIndex = x + z * sizeX + y * sizeX * sizeZ; if (colorIndex >= colors.Length) { Debug.LogError("Voxel Play: Model size or contents do not match."); return(ColorBasedModelDefinition.Null); } colors [colorIndex] = GetColor(data, colorFormat); } index++; } } } } // } ColorBasedModelDefinition model = new ColorBasedModelDefinition(); model.name = name; model.offsetX = offsetX; model.offsetY = offsetY; model.offsetZ = offsetZ; model.sizeX = (int)sizeX; model.sizeY = (int)sizeY; model.sizeZ = (int)sizeZ; model.colors = colors; return(model); } }