Example #1
0
        /// <summary>
        /// Setup for the automatically probe spawning
        /// </summary>
        /// <param name="profile"></param>
        public static void CreateAutomaticProbes(ReflectionProbeData profile)
        {
            int numberTerrains = Terrain.activeTerrains.Length;

            if (numberTerrains == 0)
            {
                Debug.LogError("Unable to initiate probe spawning systen. No terrain found");
            }
            else
            {
                if (profile.lightProbesPerRow < 2)
                {
                    Debug.LogError("Please set Light Probes Per Row to a value of 2 or higher");
                }
                else
                {
                    LoadProbesFromScene();

                    m_probeLocations = null;
                    ClearCreatedLightProbes();

                    float seaLevel = 0f;

                    PWS_WaterSystem gaiawater = GameObject.FindObjectOfType <PWS_WaterSystem>();
                    if (gaiawater != null)
                    {
                        seaLevel = gaiawater.SeaLevel;
                    }

                    if (GaiaUtils.HasDynamicLoadedTerrains())
                    {
                        Action <Terrain> terrAction = (t) => GenerateProbesOnTerrain(t, profile, seaLevel);
                        GaiaUtils.CallFunctionOnDynamicLoadedTerrains(terrAction, true, null, "Generating Light Probes");
                    }
                    else
                    {
                        foreach (var activeTerrain in Terrain.activeTerrains)
                        {
                            GenerateProbesOnTerrain(activeTerrain, profile, seaLevel);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Load the probes in from the scene
        /// </summary>
        private static void LoadProbesFromScene()
        {
            //Start time
            //DateTime startTime = DateTime.Now;

            //Destroy previous contents
            m_probeLocations = null;

            //Work out the bounds of the environment
            float   minY          = float.NaN;
            float   minX          = float.NaN;
            float   maxX          = float.NaN;
            float   minZ          = float.NaN;
            float   maxZ          = float.NaN;
            Terrain sampleTerrain = null;

            foreach (Terrain terrain in Terrain.activeTerrains)
            {
                if (float.IsNaN(minY))
                {
                    sampleTerrain = terrain;
                    minY          = terrain.transform.position.y;
                    minX          = terrain.transform.position.x;
                    minZ          = terrain.transform.position.z;
                    maxX          = minX + terrain.terrainData.size.x;
                    maxZ          = minZ + terrain.terrainData.size.z;
                }
                else
                {
                    if (terrain.transform.position.x < minX)
                    {
                        minX = terrain.transform.position.x;
                    }
                    if (terrain.transform.position.z < minZ)
                    {
                        minZ = terrain.transform.position.z;
                    }
                    if ((terrain.transform.position.x + terrain.terrainData.size.x) > maxX)
                    {
                        maxX = terrain.transform.position.x + terrain.terrainData.size.x;
                    }
                    if ((terrain.transform.position.z + terrain.terrainData.size.z) > maxZ)
                    {
                        maxZ = terrain.transform.position.z + terrain.terrainData.size.z;
                    }
                }
            }

            if (sampleTerrain != null)
            {
                Rect terrainBounds = new Rect(minX, minZ, maxX - minX, maxZ - minZ);
                m_probeLocations = new Quadtree <LightProbeGroup>(terrainBounds);
            }
            else
            {
                Rect bigSpace = new Rect(-10000f, -10000f, 20000f, 20000f);
                m_probeLocations = new Quadtree <LightProbeGroup>(bigSpace);
            }

            //Now grab all the light probes in the scene
            LightProbeGroup probeGroup;

            LightProbeGroup[] probeGroups = UnityEngine.Object.FindObjectsOfType <LightProbeGroup>();

            for (int probeGroupIdx = 0; probeGroupIdx < probeGroups.Length; probeGroupIdx++)
            {
                probeGroup = probeGroups[probeGroupIdx];
                for (int probePosition = 0; probePosition < probeGroup.probePositions.Length; probePosition++)
                {
                    m_probeLocations.Insert(probeGroup.transform.position.x + probeGroup.probePositions[probePosition].x, probeGroup.transform.position.z + probeGroup.probePositions[probePosition].z, probeGroup);
                }
            }
        }
        #pragma warning restore 414

        /// <summary>
        /// Load the trees in from the terrain
        /// </summary>
        public void LoadTreesFromTerrain()
        {
            //Destroy previous contents
            m_terrainTrees         = null;
            m_terrainTreeLocations = null;

            //Work out the bounds of the environment
            float   minY          = float.NaN;
            float   minX          = float.NaN;
            float   maxX          = float.NaN;
            float   minZ          = float.NaN;
            float   maxZ          = float.NaN;
            Terrain sampleTerrain = null;

            foreach (Terrain terrain in Terrain.activeTerrains)
            {
                if (float.IsNaN(minY))
                {
                    sampleTerrain = terrain;
                    minY          = terrain.transform.position.y;
                    minX          = terrain.transform.position.x;
                    minZ          = terrain.transform.position.z;
                    maxX          = minX + terrain.terrainData.size.x;
                    maxZ          = minZ + terrain.terrainData.size.z;
                }
                else
                {
                    if (terrain.transform.position.x < minX)
                    {
                        minX = terrain.transform.position.x;
                    }
                    if (terrain.transform.position.z < minZ)
                    {
                        minZ = terrain.transform.position.z;
                    }
                    if ((terrain.transform.position.x + terrain.terrainData.size.x) > maxX)
                    {
                        maxX = terrain.transform.position.x + terrain.terrainData.size.x;
                    }
                    if ((terrain.transform.position.z + terrain.terrainData.size.z) > maxZ)
                    {
                        maxZ = terrain.transform.position.z + terrain.terrainData.size.z;
                    }
                }
            }

            if (sampleTerrain != null)
            {
                Rect terrainBounds = new Rect(minX, minZ, maxX - minX, maxZ - minZ);

                m_terrainTreeLocations = new Quadtree <int>(terrainBounds, 32);
                m_terrainTrees         = new List <TreePrototype>(sampleTerrain.terrainData.treePrototypes);

                foreach (Terrain terrain in Terrain.activeTerrains)
                {
                    float          terrainOffsetX       = terrain.transform.position.x;
                    float          terrainOffsetZ       = terrain.transform.position.z;
                    float          terrainWidth         = terrain.terrainData.size.x;
                    float          terrainDepth         = terrain.terrainData.size.z;
                    TreeInstance[] terrainTreeInstances = terrain.terrainData.treeInstances;
                    for (int treeIdx = 0; treeIdx < terrainTreeInstances.Length; treeIdx++)
                    {
                        TreeInstance treeInstance = terrainTreeInstances[treeIdx];
                        m_terrainTreeLocations.Insert(terrainOffsetX + (treeInstance.position.x * terrainWidth), terrainOffsetZ + (treeInstance.position.z * terrainDepth), terrainTreeInstances[treeIdx].prototypeIndex);
                    }
                }
            }
        }