public void NewVoxelStructure() { GameObject newGameObject = new GameObject(); VoxelContainer newVoxelContainer = newGameObject.AddComponent<VoxelContainer>(); for (int i = 0; i < this.sizeVector.x; i++) { for (int j = 0; j < this.sizeVector.y; j++) { for (int z = 0; z < this.sizeVector.z; z++) { Voxel newVoxel = new Voxel(); newVoxel.position.x = i; newVoxel.position.y = j; newVoxel.position.z = z; newVoxelContainer.AddVoxel(newVoxel); } } } newVoxelContainer.BuildMesh(false, true, true, true); if (SceneView.lastActiveSceneView != null) { UnityEditor.Selection.activeGameObject = newGameObject; SceneView.lastActiveSceneView.pivot = newGameObject.transform.position; SceneView.lastActiveSceneView.Repaint(); } }
private void LoadVoxFile(string aFileName) { //I dissabled compiler warning, because we want to load some values from the file even if we would not use it later. //I think it is cleaner this way #pragma warning disable //Prepare Object GameObject newGameObject = new GameObject(); newGameObject.name = "ImportedFromMagicaVoxel"; VoxelContainer newVoxelContainer = newGameObject.AddComponent <VoxelContainer>(); //Open File BinaryReader br = new BinaryReader(File.OpenRead(aFileName)); string magic = new string(br.ReadChars(4)); int version = br.ReadInt32(); if (magic == "VOX ") { int sizex = 0, sizey = 0, sizez = 0; Color[] colors = null; MagicaVoxelData[] voxelData = null; while (br.BaseStream.Position < br.BaseStream.Length) { char[] chunkId = br.ReadChars(4); int chunkSize = br.ReadInt32(); int childChunks = br.ReadInt32(); string chunkName = new string(chunkId); if (chunkName == "SIZE") { sizex = br.ReadInt32(); sizey = br.ReadInt32(); sizez = br.ReadInt32(); br.ReadBytes(chunkSize - 4 * 3); } else if (chunkName == "XYZI") { int numVoxels = br.ReadInt32(); voxelData = new MagicaVoxelData[numVoxels]; for (int i = 0; i < voxelData.Length; i++) { voxelData[i] = new MagicaVoxelData(br); } } else if (chunkName == "RGBA") { colors = new Color[256]; for (int i = 0; i < 256; i++) { byte r = br.ReadByte(); byte g = br.ReadByte(); byte b = br.ReadByte(); byte a = br.ReadByte(); colors[i].r = r / 255f; colors[i].g = g / 255f; colors[i].b = b / 255f; colors[i].a = a / 255f; } } else { br.ReadBytes(chunkSize); } } if ((voxelData == null) || (voxelData.Length == 0)) { return; } for (int i = 0; i < voxelData.Length; i++) { Vector3 position = new Vector3(voxelData[i].x, voxelData[i].z, voxelData[i].y); if (!newVoxelContainer.voxels.ContainsKey(position)) { Voxel newVoxel = new Voxel(); newVoxel.position = position; newVoxel.color = (colors == null ? UShortToColor(voxColors[voxelData[i].color - 1]) : colors[voxelData[i].color - 1]); newVoxelContainer.AddVoxel(newVoxel, false); } } newVoxelContainer.UpdateStructure(); newVoxelContainer.BuildMesh(true, true, true, true); } else { Debug.LogError("Error durring vox import. Probably this is not a .vox file."); return; } #pragma warning restore }
private void ImportAssetOptionsQEObject(string aFilename) { if (Path.GetExtension(aFilename).ToUpper() != ".qb".ToUpper()) { Debug.LogError("Extension must be .qb"); return; } //Consts uint CODEFLAG = 2; uint NEXTSLICEFLAG = 6; //Parent Gameobject GameObject parentGameObject = new GameObject(); parentGameObject.name = Path.GetFileNameWithoutExtension(aFilename); BinaryReader br = new BinaryReader(File.Open(aFilename, FileMode.Open)); try { if (br == null) { Debug.Log("VoxelMax: Could not open file. Filename: " + aFilename); return; } uint fileVersion = br.ReadUInt32(); uint colorFormat = br.ReadUInt32(); uint zAxisOrientation = br.ReadUInt32(); uint compressed = br.ReadUInt32(); uint visibilityMaskEncoded = br.ReadUInt32(); uint numMatrices = br.ReadUInt32(); Debug.Log("Qubicle File Import Info\r\n" + "File Version: " + fileVersion + "\r\n" + "Color Format: " + colorFormat + "\r\n" + "zAxisOrientation: " + zAxisOrientation + "\r\n" + "visibilityMaskEncoded: " + visibilityMaskEncoded + "\r\n" + "Object Count: " + numMatrices); for (uint i = 0; i < numMatrices; i++) { // read matrix name byte nameLength = br.ReadByte(); string matrixName = new string(br.ReadChars(nameLength)); GameObject newGameObject = new GameObject(); newGameObject.transform.parent = parentGameObject.transform; newGameObject.name = matrixName; VoxelContainer newVoxelContainer = newGameObject.AddComponent <VoxelContainer>(); // read matrix size uint sizeX = br.ReadUInt32(); uint sizeY = br.ReadUInt32(); uint sizeZ = br.ReadUInt32(); // read matrix position (in this example the position is irrelevant) int posX = br.ReadInt32(); int posY = br.ReadInt32(); int posZ = br.ReadInt32(); newGameObject.transform.position = new Vector3(posX, posY, posZ); if (compressed == 0) // if uncompressd { for (uint z = 0; z < sizeZ; z++) { for (uint y = 0; y < sizeY; y++) { for (uint x = 0; x < sizeX; x++) { uint curColor = br.ReadUInt32(); if (curColor != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(curColor); newVoxelContainer.AddVoxel(newVoxel, false); } } } } } else // if compressed { uint z = 0; while (z < sizeZ) { z++; uint index = 0; while (true) { uint data = br.ReadUInt32(); if (data == NEXTSLICEFLAG) { break; } else if (data == CODEFLAG) { uint count = br.ReadUInt32(); data = br.ReadUInt32(); for (uint j = 0; j < count; j++) { uint x = index % sizeX + 1; // mod = modulo e.g. 12 mod 8 = 4 uint y = index / sizeX + 1; // div = integer division e.g. 12 div 8 = 1 index++; if (data != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(data); newVoxelContainer.AddVoxel(newVoxel, false); } } } else { uint x = index % sizeX + 1; uint y = index / sizeX + 1; index++; if (data != 0) { Voxel newVoxel = new Voxel(); newVoxel.position = new Vector3(x, y, z); newVoxel.color = this.UIntToColor(data); newVoxelContainer.AddVoxel(newVoxel, false); } } } } } newVoxelContainer.UpdateStructure(); newVoxelContainer.BuildMesh(true, true, true, true); } } finally { br.Close(); } }
private GameObject ConvertObject(GameObject aObject) { if (aObject == null) { return(null); } GameObject newVoxelObject = new GameObject(); newVoxelObject.name = aObject.name + "_Converted"; if (this.isRecursive) { //I do this because the child list always change because //I have to removed the child while I convert it since //the mesh collider does not look to be workin as a child for me. //Reach out for me if you have better idea :) List <Transform> childList = new List <Transform>(); for (int i = 0; i < aObject.transform.childCount; i++) { childList.Add(aObject.transform.GetChild(i)); } for (int i = 0; i < childList.Count; i++) { GameObject newChild = this.ConvertObject(childList[i].gameObject); if (newChild != null) { newChild.transform.parent = newVoxelObject.transform; newChild.transform.localPosition = childList[i].localPosition; } } } MeshCollider meshcollider = aObject.GetComponent <MeshCollider>(); if (meshcollider == null) { UnityEngine.Debug.LogError("VoxelMax: Source Object need meshcollider to be converted!"); return(newVoxelObject); } EditorUtility.ClearProgressBar(); EditorUtility.DisplayCancelableProgressBar("Building Voxel Matrix", "Building", 0f); //Clear colorpalette this.colorPalette = null; this.colorPalette = new List <Color>(); VoxelContainer voxelContainer = newVoxelObject.AddComponent <VoxelContainer>(); try { //Get Texture Texture2D objectTexture = null; Renderer objRenderer = aObject.GetComponent <Renderer>(); if (objRenderer != null) { if (objRenderer.sharedMaterial != null) { objectTexture = (Texture2D)objRenderer.sharedMaterial.mainTexture; if (objectTexture != null) { try { objectTexture.GetPixel(0, 0); } catch { UnityEngine.Debug.Log("Reimporting texture, because it is not readable."); objectTexture = VoxelContainer.ReImportTexture(objectTexture); } } } } Transform originalParent = aObject.transform.parent; aObject.transform.parent = null; float maxStepCount = (meshcollider.bounds.max.x - meshcollider.bounds.min.x + 2) * (meshcollider.bounds.max.y - meshcollider.bounds.min.y + 2) * (meshcollider.bounds.max.z - meshcollider.bounds.min.z + 2); float eachStep = (meshcollider.bounds.max.y - meshcollider.bounds.min.y + 2) * (meshcollider.bounds.max.z - meshcollider.bounds.min.z + 2); for (float x = (int)(meshcollider.bounds.min.x - 1f); x <= meshcollider.bounds.max.x + 1f; x += 1f) { for (float y = (int)(meshcollider.bounds.min.y - 1f); y <= meshcollider.bounds.max.y + 1f; y += 1f) { for (float z = (int)(meshcollider.bounds.min.z - 1f); z <= meshcollider.bounds.max.z + 1f; z += 1f) { Vector3 shootOrigin = new Vector3(x, y, z); if (this.selectedAlgorithm == ConvertAlgorithm.RayBased) { for (int k = 0; k < 6; k++) { this.ShootArray(shootOrigin, StaticValues.sixDirectionArray[k], voxelContainer, meshcollider, objectTexture); } } else { this.CheckVoxel(shootOrigin, voxelContainer, meshcollider, objectTexture); } } } if (EditorUtility.DisplayCancelableProgressBar("Building Voxel Matrix", "Building", (eachStep * x) / maxStepCount)) { EditorUtility.ClearProgressBar(); return(null); } } aObject.transform.parent = originalParent; } finally { EditorUtility.ClearProgressBar(); } // if (this.selectedAlgorithm == ConvertAlgorithm.RayBased) // voxelContainer.FillUpObject(); voxelContainer.BuildMesh(true, true, true, true); if (SceneView.lastActiveSceneView != null) { UnityEditor.Selection.activeGameObject = newVoxelObject; SceneView.lastActiveSceneView.pivot = newVoxelObject.transform.position; SceneView.lastActiveSceneView.Repaint(); } return(newVoxelObject); }