Exemple #1
0
        /// <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;
        }
Exemple #2
0
        /// <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;
        }
Exemple #3
0
        /// <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;
        }
Exemple #4
0
        /// <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
        }