/// <summary> /// Save the stamp /// </summary> /// <returns>Path of saved stamp</returns> public string SaveScan() { if (m_scanMap == null || !m_scanMap.HasData()) { Debug.LogWarning("Cant save scan as none has been loaded"); return(null); } //Save preview string path = Gaia.Utils.GetGaiaAssetPath(m_featureType, m_featureName); Gaia.Utils.CompressToSingleChannelFileImage(m_scanMap.Heights(), path, Gaia.GaiaConstants.fmtHmTextureFormat, false, true); path = Gaia.Utils.GetGaiaStampAssetPath(m_featureType, m_featureName); //Save stamp path = path + ".bytes"; float [] metaData = new float[5]; metaData[0] = m_scanWidth; metaData[1] = m_scanDepth; metaData[2] = m_scanHeight; metaData[3] = m_scanResolution; metaData[4] = m_baseLevel; byte[] byteData = new byte[metaData.Length * 4]; Buffer.BlockCopy(metaData, 0, byteData, 0, byteData.Length); m_scanMap.SetMetaData(byteData); m_scanMap.SaveToBinaryFile(path); return(path); }
/// <summary> /// Save the stamp /// </summary> /// <returns>Path of saved stamp</returns> public string SaveScan() { if (m_scanMap == null || !m_scanMap.HasData()) { Debug.LogWarning("Cant save scan as none has been loaded"); return(null); } #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Generating Texture", "Generating texture", 0.25f); #endif //work with a copy for the export - don't want to normalize etc. source data HeightMap heightmapCopy = new HeightMap(m_scanMap); heightmapCopy.AddClamped(0f, m_baseLevel, 1f); if (m_normalize) { heightmapCopy.Normalise(); } //Save preview string fullpath = m_exportFolder + "/" + m_exportFileName; GaiaUtils.CompressToMultiChannelFileImage(fullpath, heightmapCopy, heightmapCopy, heightmapCopy, null, TextureFormat.RGBAFloat, GaiaConstants.ImageFileType.Exr, m_baseLevel); GaiaUtils.SetDefaultStampImportSettings(fullpath + ".exr"); #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Generating Texture", "Compressing texture", 0.5f); #endif GaiaUtils.CompressToSingleChannelFileImage(heightmapCopy.Heights(), m_exportFolder, GaiaConstants.fmtHmTextureFormat, m_exportTextureAlso, false); //Save stamp if (m_exportBytesData) { #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Generating Texture", "Generating bytes data", 0.75f); #endif m_exportFolder += ".bytes"; float [] metaData = new float[5]; metaData[0] = m_scanWidth; metaData[1] = m_scanDepth; metaData[2] = m_scanHeight; metaData[3] = m_scanResolution; metaData[4] = m_baseLevel; byte[] byteData = new byte[metaData.Length * 4]; Buffer.BlockCopy(metaData, 0, byteData, 0, byteData.Length); heightmapCopy.SetMetaData(byteData); heightmapCopy.SaveToBinaryFile(m_exportFolder); } #if UNITY_EDITOR EditorUtility.ClearProgressBar(); #endif return(fullpath); }
/// <summary> /// Load the terrain provided /// </summary> /// <param name="texture">Terrain to load</param> public void LoadTerain(Terrain terrain) { //Check not null if (terrain == null) { Debug.LogError("Must supply a valid terrain! Terrain load aborted."); return; } //Clear out the old Reset(); //Load up the new m_featureName = terrain.name; m_scanMap = new UnityHeightMap(terrain); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load terrain file. Terrain load aborted."); return; } m_scanMap.Flip(); //Undo unity terrain shenannigans m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = (int)terrain.terrainData.size.y; m_scanResolution = 0.1f; m_scanBounds = new Bounds(transform.position, new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution * 0.4f, m_scanDepth * m_scanResolution)); m_baseLevel = m_scanMap.GetBaseLevel(); MeshFilter mf = GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); mf.hideFlags = HideFlags.HideInInspector; } MeshRenderer mr = GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); mr.hideFlags = HideFlags.HideInInspector; } mf.mesh = Gaia.Utils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; mr.sharedMaterial = m_previewMaterial; } }
/// <summary> /// Load the raw file at the path given /// </summary> /// <param name="path">Full path of the raw file</param> public void LoadRawFile(string path, GaiaConstants.RawByteOrder byteOrder, ref GaiaConstants.RawBitDepth bitDepth, ref int resolution) { if (string.IsNullOrEmpty(path)) { Debug.LogError("Must supply a valid path. Raw load Aborted!"); } //Clear out the old Reset(); //Load up the new m_featureName = Path.GetFileNameWithoutExtension(path); m_scanMap = new HeightMap(); m_scanMap.LoadFromRawFile(path, byteOrder, ref bitDepth, ref resolution); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load raw file. Raw load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; m_scanResolution = 0.1f; m_scanBounds = new Bounds(transform.position, new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution * 0.4f, m_scanDepth * m_scanResolution)); m_baseLevel = m_scanMap.GetBaseLevel(); MeshFilter mf = GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); mf.hideFlags = HideFlags.HideInInspector; } MeshRenderer mr = GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); mr.hideFlags = HideFlags.HideInInspector; } mf.mesh = Gaia.Utils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; mr.sharedMaterial = m_previewMaterial; } }
/// <summary> /// Load the terrain provided /// </summary> /// <param name="texture">Terrain to load</param> public void LoadTerain(Terrain terrain) { //Check not null if (terrain == null) { Debug.LogError("Must supply a valid terrain! Terrain load aborted."); return; } //Clear out the old ResetData(); m_scanMap = new UnityHeightMap(terrain); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load terrain file. Terrain load aborted."); return; } m_scanMap.Flip(); //Undo unity terrain shenannigans m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = (int)terrain.terrainData.size.y; m_scanResolution = 0.1f; //m_scanBounds = new Bounds(GetPosition(gameObject), new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution, m_scanDepth * m_scanResolution)); m_scanBounds = new Bounds(GetPosition(gameObject, terrain, true), new Vector3(terrain.terrainData.size.x, terrain.terrainData.size.y, terrain.terrainData.size.z)); //m_baseLevel = m_scanMap.GetBaseLevel(); SetOrCreateMeshComponents(); m_meshFilter.sharedMesh = GaiaUtils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; m_meshRenderer.sharedMaterial = m_previewMaterial; } gameObject.transform.position = m_scanBounds.center; m_exportFileName = terrain.name; m_boundsSet = true; }
/// <summary> /// Load the texture file provided /// </summary> /// <param name="texture">Texture file to load</param> public void LoadTextureFile(Texture2D texture) { //Check not null if (texture == null) { Debug.LogError("Must supply a valid texture! Texture load aborted."); return; } //Clear out the old ResetData(); m_scanMap = new UnityHeightMap(texture); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load Texture file. Texture load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; m_scanResolution = 0.1f; m_scanBounds = new Bounds(GetPosition(gameObject, null), new Vector3(texture.width / 2, m_scanWidth * m_scanResolution, texture.height / 2)); //m_baseLevel = m_scanMap.GetBaseLevel(); SetOrCreateMeshComponents(); m_meshFilter.sharedMesh = GaiaUtils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; m_meshRenderer.sharedMaterial = m_previewMaterial; } gameObject.transform.position = m_scanBounds.center; m_exportFileName = texture.name; m_boundsSet = true; }
/// <summary> /// Load the raw file at the path given /// </summary> /// <param name="path">Full path of the raw file</param> public void LoadRawFile(string path, GaiaConstants.RawByteOrder byteOrder, ref GaiaConstants.RawBitDepth bitDepth, ref int resolution) { if (string.IsNullOrEmpty(path)) { Debug.LogError("Must supply a valid path. Raw load Aborted!"); } //Clear out the old ResetData(); //Load up the new m_scanMap = new HeightMap(); m_scanMap.LoadFromRawFile(path, byteOrder, ref bitDepth, ref resolution); if (m_scanMap.HasData() == false) { Debug.LogError("Unable to load raw file. Raw load aborted."); return; } m_scanWidth = m_scanMap.Width(); m_scanDepth = m_scanMap.Depth(); m_scanHeight = m_scanWidth / 2; m_scanResolution = 0.1f; m_scanBounds = new Bounds(GetPosition(gameObject, null), new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution, m_scanDepth * m_scanResolution)); //m_baseLevel = m_scanMap.GetBaseLevel(); SetOrCreateMeshComponents(); m_meshFilter.sharedMesh = GaiaUtils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; m_meshRenderer.sharedMaterial = m_previewMaterial; } gameObject.transform.position = m_scanBounds.center; m_exportFileName = path.Substring(path.LastIndexOf('/')); m_boundsSet = true; }
/// <summary> /// Load the object provided /// </summary> /// <param name="go">Terrain to load</param> public void LoadGameObject(GameObject go) { //Check not null if (go == null) { Debug.LogError("Must supply a valid game object! GameObject load aborted."); return; } //Clear out the old Reset(); //Load up the new m_featureName = go.name; //Duplicate the object GameObject workingGo = GameObject.Instantiate(go); workingGo.transform.position = transform.position; workingGo.transform.localRotation = Quaternion.identity; workingGo.transform.localScale = Vector3.one; //Delete any old colliders Collider[] colliders = workingGo.GetComponentsInChildren <Collider>(); foreach (Collider c in colliders) { DestroyImmediate(c); } //Now add mesh colliders to all active game objects for the most accurate possible scanning Transform[] transforms = workingGo.GetComponentsInChildren <Transform>(); foreach (Transform child in transforms) { if (child.gameObject.activeSelf) { child.gameObject.AddComponent <MeshCollider>(); } } //Calculate bounds m_scanBounds.center = workingGo.transform.position; m_scanBounds.size = Vector3.zero; foreach (MeshCollider c in workingGo.GetComponentsInChildren <MeshCollider>()) { m_scanBounds.Encapsulate(c.bounds); } //Update scan array details - dont need to allocate mem until we scan m_scanWidth = (int)(Mathf.Ceil(m_scanBounds.size.x * (1f / m_scanResolution))); m_scanHeight = (int)(Mathf.Ceil(m_scanBounds.size.y * (1f / m_scanResolution))); m_scanDepth = (int)(Mathf.Ceil(m_scanBounds.size.z * (1f / m_scanResolution))); //Now scan the object m_scanMap = new HeightMap(m_scanWidth, m_scanDepth); Vector3 scanMin = m_scanBounds.min; Vector3 scanPos = scanMin; scanPos.y = m_scanBounds.max.y; RaycastHit scanHit; //Perform the scan - only need to store hits as float arrays inherently zero for (int x = 0; x < m_scanWidth; x++) { scanPos.x = scanMin.x + (m_scanResolution * (float)x); for (int z = 0; z < m_scanDepth; z++) { scanPos.z = scanMin.z + (m_scanResolution * (float)z); if (Physics.Raycast(scanPos, Vector3.down, out scanHit, m_scanBounds.size.y)) { m_scanMap[x, z] = 1f - (scanHit.distance / m_scanBounds.size.y); } } } //Now delete the scanned clone DestroyImmediate(workingGo); //Nad make sure we had some data if (m_scanMap.HasData() == false) { Debug.LogError("Unable to scan GameObject. GameObject load aborted."); return; } m_scanBounds = new Bounds(transform.position, new Vector3(m_scanWidth * m_scanResolution, m_scanWidth * m_scanResolution * 0.4f, m_scanDepth * m_scanResolution)); m_baseLevel = m_scanMap.GetBaseLevel(); MeshFilter mf = GetComponent <MeshFilter>(); if (mf == null) { mf = gameObject.AddComponent <MeshFilter>(); mf.hideFlags = HideFlags.HideInInspector; } MeshRenderer mr = GetComponent <MeshRenderer>(); if (mr == null) { mr = gameObject.AddComponent <MeshRenderer>(); mr.hideFlags = HideFlags.HideInInspector; } mf.mesh = Gaia.Utils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; mr.sharedMaterial = m_previewMaterial; } }
/// <summary> /// Load the object provided /// </summary> /// <param name="go">Terrain to load</param> public void LoadGameObject(GameObject go) { //Check not null if (go == null) { Debug.LogError("Must supply a valid game object! GameObject load aborted."); return; } //Clear out the old ResetData(); //Duplicate the object GameObject workingGo = GameObject.Instantiate(go); workingGo.transform.position = transform.position; workingGo.transform.localRotation = Quaternion.identity; workingGo.transform.localScale = Vector3.one; //Delete any old colliders Collider[] colliders = workingGo.GetComponentsInChildren <Collider>(); foreach (Collider c in colliders) { DestroyImmediate(c); } //Now add mesh colliders to all active game objects for the most accurate possible scanning Transform[] transforms = workingGo.GetComponentsInChildren <Transform>(); foreach (Transform child in transforms) { if (child.gameObject.activeSelf) { child.gameObject.AddComponent <MeshCollider>(); } } //Calculate bounds m_scanBounds.center = workingGo.transform.position; m_scanBounds.size = Vector3.zero; foreach (MeshCollider c in workingGo.GetComponentsInChildren <MeshCollider>()) { m_scanBounds.Encapsulate(c.bounds); } //Update scan array details - dont need to allocate mem until we scan m_scanWidth = (int)(Mathf.Ceil(m_scanBounds.size.x * (1f / m_scanResolution))); m_scanHeight = (int)(Mathf.Ceil(m_scanBounds.size.y * (1f / m_scanResolution))); m_scanDepth = (int)(Mathf.Ceil(m_scanBounds.size.z * (1f / m_scanResolution))); //Now scan the object m_scanMap = new HeightMap(m_scanWidth, m_scanDepth); Vector3 scanMin = m_scanBounds.min; Vector3 scanPos = scanMin; scanPos.y = m_scanBounds.max.y + 1; RaycastHit scanHit; #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Processing Mesh", "Processing mesh data", 0f); #endif int count = 0; int totalNumberOfRays = m_scanWidth * m_scanDepth; //Perform the scan - only need to store hits as float arrays inherently zero for (int x = 0; x < m_scanWidth; x++) { scanPos.x = scanMin.x + (m_scanResolution * (float)x); for (int z = 0; z < m_scanDepth; z++) { scanPos.z = scanMin.z + (m_scanResolution * (float)z); if (Physics.Raycast(scanPos, Vector3.down, out scanHit, m_scanBounds.size.y + 1)) { m_scanMap[x, z] = 1f - ((scanHit.distance - 1f) / m_scanBounds.size.y); } count++; } #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Processing Mesh", "Processing mesh data", (float)count / (float)totalNumberOfRays); #endif } //Now delete the scanned clone DestroyImmediate(workingGo); //Nad make sure we had some data if (m_scanMap.HasData() == false) { Debug.LogError("Unable to scan GameObject. GameObject load aborted."); return; } m_scanBounds = new Bounds(GetPosition(gameObject, null), new Vector3(m_scanWidth * m_scanResolution, m_scanBounds.size.y, m_scanDepth * m_scanResolution));//m_scanWidth * m_scanResolution * 0.4f, m_scanDepth * m_scanResolution)); //m_baseLevel = m_scanMap.GetBaseLevel(); SetOrCreateMeshComponents(); #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Processing Mesh", "Creating mesh from data", 0.95f); #endif m_meshFilter.sharedMesh = GaiaUtils.CreateMesh(m_scanMap.Heights(), m_scanBounds.size); if (m_previewMaterial != null) { m_previewMaterial.hideFlags = HideFlags.HideInInspector; m_meshRenderer.sharedMaterial = m_previewMaterial; } gameObject.transform.position = m_scanBounds.center; m_boundsSet = true; m_lastScanResolution = m_scanResolution; m_exportFileName = go.name; #if UNITY_EDITOR EditorUtility.ClearProgressBar(); #endif }