private Texture2D GetTexture() { //handling input errors if (sourceTexture == null) { EditorUtility.DisplayDialog("Error", "Please set the source texture before convert.", "Ok"); return(null); } //Reimport the texture if it is not readable Texture2D curTexture = this.sourceTexture; try { curTexture.GetPixel(0, 0); } catch { Debug.Log("Reimporting texture, because it is not readable."); curTexture = VoxelContainer.ReImportTexture(this.sourceTexture); } return(curTexture); }
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); }