void Awake()
    {
        splat[0] = Resources.Load("Textures/" + "rock_2048") as Texture2D;
        splat[1] = Resources.Load("Textures/" + "forst_1024") as Texture2D;
        splat[2] = Resources.Load("Textures/" + "snow_512") as Texture2D;
        splat[3] = Resources.Load("Textures/" + "GoodDirt") as Texture2D;

        detailTexture[0] = Resources.Load("Details/SimpleGrass/" + "Grass") as Texture2D;
        detailTexture[1] = Resources.Load("Details/SimpleGrass/" + "WhiteFlowers") as Texture2D;

        trees[0] = Resources.Load("Trees/" + "Tree4Master") as GameObject;
        trees[1] = Resources.Load("Trees/" + "Tree5") as GameObject;
        trees[2] = Resources.Load("Trees/" + "Tree4Master") as GameObject;
        trees[3] = Resources.Load("Trees/" + "Bush1") as GameObject;

        Vector2[] splatTileSize = new Vector2[4] {
            new Vector2(Tile0, Tile0), new Vector2(Tile1, Tile1), new Vector2(Tile2, Tile2), new Vector2(Tile3, Tile3)
        };
        for (int i = 0; i < numOfSplatPrototypes; i++)
        {
            m_splatPrototypes[i] = new SplatPrototype();
        }

        for (int i = 0; i < numOfSplatPrototypes; i++)
        {
            m_splatPrototypes[i].texture    = splat[i];
            m_splatPrototypes[i].tileOffset = Vector2.zero;
            m_splatPrototypes[i].tileSize   = splatTileSize[i];
            m_splatPrototypes[i].texture.Apply(true);
        }

        for (int i = 0; i < numOfDetailPrototypes; i++)
        {
            m_detailProtoTypes[i] = new DetailPrototype();
            m_detailProtoTypes[i].prototypeTexture = detailTexture[i];
            m_detailProtoTypes[i].renderMode       = detailMode;
            m_detailProtoTypes[i].healthyColor     = m_grassHealthyColor;
            m_detailProtoTypes[i].dryColor         = m_grassDryColor;
            m_detailProtoTypes[i].maxHeight        = 0.5f;
            m_detailProtoTypes[i].maxWidth         = 0.2f;
        }
        for (int i = 0; i < numOfTreePrototypes; i++)
        {
            m_treeProtoTypes[i]        = new TreePrototype();
            m_treeProtoTypes[i].prefab = trees[i];
        }
        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                TerrainData terrainData = new TerrainData();

                terrainData.wavingGrassStrength = m_wavingGrassStrength;
                terrainData.wavingGrassAmount   = m_wavingGrassAmount;
                terrainData.wavingGrassSpeed    = m_wavingGrassSpeed;
                terrainData.wavingGrassTint     = m_wavingGrassTint;
                terrainData.heightmapResolution = m_heightMapSize;
                terrainData.size = new Vector3(m_landScapeSize, m_terrainHeight, m_landScapeSize);
                terrainData.alphamapResolution = m_alphaMapSize;
                terrainData.splatPrototypes    = m_splatPrototypes;
                terrainData.treePrototypes     = m_treeProtoTypes;
                terrainData.SetDetailResolution(m_detailMapSize, m_detailResolutionPerPatch);
                terrainData.detailPrototypes = m_detailProtoTypes;

                m_terrainGrid[i, j] = Terrain.CreateTerrainGameObject(terrainData).GetComponent <Terrain>();
            }
        }

        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                m_terrainGrid[i, j].gameObject.AddComponent <TerrainScript>();
                m_terrainGrid[i, j].transform.parent = gameObject.transform;

                m_terrainGrid[i, j].transform.position = new Vector3(
                    m_terrainGrid[1, 1].transform.position.x + (i - 1) * m_landScapeSize, m_terrainGrid[1, 1].transform.position.y,
                    m_terrainGrid[1, 1].transform.position.z + (j - 1) * m_landScapeSize);

                m_terrainGrid[i, j].treeDistance            = m_treeDistance;
                m_terrainGrid[i, j].treeBillboardDistance   = m_treeBillboardDistance;
                m_terrainGrid[i, j].treeCrossFadeLength     = m_treeCrossFadeLength;
                m_terrainGrid[i, j].treeMaximumFullLODCount = m_treeMaximumFullLODCount;

                m_terrainGrid[i, j].detailObjectDensity  = m_detailObjectDensity;
                m_terrainGrid[i, j].detailObjectDistance = m_detailObjectDistance;

                m_terrainGrid[i, j].GetComponent <Collider>().enabled = false;
                m_terrainGrid[i, j].basemapDistance = 4000;
                m_terrainGrid[i, j].castShadows     = false;

                PatchManager.AddTerrainInfo(curGlobalIndexX + i - 1, curGlobalIndexZ + j - 1, m_terrainGrid[i, j], m_terrainGrid[i, j].transform.position);
            }
        }
        PatchManager.MakePatches();

        int patchCount = PatchManager.patchQueue.Count;

        for (int i = 0; i < patchCount; i++)
        {
            PatchManager.patchQueue.Dequeue().ExecutePatch();
        }

        UpdateIndexes();
        UpdateTerrainNeighbors();

        StartCoroutine(FlushTerrain());
        terrainIsFlushed = true;

        m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].GetComponent <Collider>().enabled = false;
        m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].GetComponent <Collider>().enabled = true;
    }
Ejemplo n.º 2
0
    public void Awake()
    {
        if (manager == null)
        {
            manager = this;
        }
        else if (manager != this)
        {
            DestroyImmediate(this.gameObject);
        }

        chunks = new Chunk[manager.dim, manager.dim];

        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                TerrainData t_data = new TerrainData();
                t_data.heightmapResolution = MAP_SIZE;
                t_data.size  = new Vector3(dimension, maxHeight, dimension);
                chunks[i, j] = new Chunk(Terrain.CreateTerrainGameObject(t_data).GetComponent <Terrain>(), i, j, 1);
            }
        }

        Chunk centerChunk = chunks[1, 1];

        centerChunk.SetGlobalPos(centerChunk.globalPosX + centerPos.x - dimension / 2,
                                 centerChunk.globalPosZ + centerPos.y - dimension / 2);

        float ratio = baseDimension / dimension;

        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                Chunk thisChunk = chunks[i, j];

                float x_global = centerChunk.globalPosX + (i - 1) * dimension;
                float z_global = centerChunk.globalPosZ + (j - 1) * dimension;

                thisChunk.GetChunk().transform.parent = this.transform;

                thisChunk.SetGlobalPos(x_global, z_global);

                thisChunk.Deactivate();
                thisChunk.GetChunk().basemapDistance = 4000;
                thisChunk.GetChunk().castShadows     = false;

                PatchManager.AddTerrainInfo(x_global * ratio, z_global * ratio, thisChunk);
            }
        }

        PatchManager.QueuePatches();

        int patchCount = PatchManager.patchQueue.Count;

        for (int i = 0; i < patchCount; i++)
        {
            PatchManager.patchQueue.Dequeue().Execute();
        }

        UpdateIndexes();
        UpdateNeighbors();

        StartCoroutine(FlushChunks());

        chunks[curCyclicIndexX, curCyclicIndexZ].Activate();
    }
    private void UpdateTerrainPositions()
    {
        if (curGlobalIndexZ != prevGlobalIndexZ && curGlobalIndexX != prevGlobalIndexX)
        {
            int z; int z0;
            if (curGlobalIndexZ > prevGlobalIndexZ)
            {
                z0 = curGlobalIndexZ + 1;
                z  = PreviousCyclicIndex(prevCyclicIndexZ);
            }
            else
            {
                z0 = curGlobalIndexZ - 1;
                z  = NextCyclicIndex(prevCyclicIndexZ);
            }

            int[] listX = { PreviousCyclicIndex(prevCyclicIndexX), prevCyclicIndexX, NextCyclicIndex(prevCyclicIndexX) };
            for (int i = 1; i < dim; i++)
            {
                Vector3 newPos = new Vector3(
                    m_terrainGrid[prevCyclicIndexX, curCyclicIndexZ].transform.position.x + (i - 1) * m_landScapeSize,
                    m_terrainGrid[prevCyclicIndexX, curCyclicIndexZ].transform.position.y,
                    m_terrainGrid[prevCyclicIndexX, curCyclicIndexZ].transform.position.z + (curGlobalIndexZ - prevGlobalIndexZ) * m_landScapeSize);

                PatchManager.AddTerrainInfo(prevGlobalIndexX + i - 1, z0, m_terrainGrid[listX[i], z], newPos);
            }
            int x; int x0;
            if (curGlobalIndexX > prevGlobalIndexX)
            {
                x0 = curGlobalIndexX + 1;
                x  = PreviousCyclicIndex(prevCyclicIndexX);
            }
            else
            {
                x0 = curGlobalIndexX - 1;
                x  = NextCyclicIndex(prevCyclicIndexX);
            }

            int[] listZ = { PreviousCyclicIndex(curCyclicIndexZ), curCyclicIndexZ, NextCyclicIndex(curCyclicIndexZ) };
            for (int i = 0; i < dim; i++)
            {
                Vector3 newPos = new Vector3(
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.x + (curGlobalIndexX - prevGlobalIndexX) * m_landScapeSize,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.y,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.z + (i - 1) * m_landScapeSize);

                PatchManager.AddTerrainInfo(x0, curGlobalIndexZ + i - 1, m_terrainGrid[x, listZ[i]], newPos);
            }
        }
        else if (curGlobalIndexZ != prevGlobalIndexZ)
        {
            int z; int z0;
            if (curGlobalIndexZ > prevGlobalIndexZ)
            {
                z0 = curGlobalIndexZ + 1;
                z  = PreviousCyclicIndex(prevCyclicIndexZ);
            }
            else
            {
                z0 = curGlobalIndexZ - 1;
                z  = NextCyclicIndex(prevCyclicIndexZ);
            }
            int[] listX = { PreviousCyclicIndex(prevCyclicIndexX), prevCyclicIndexX, NextCyclicIndex(prevCyclicIndexX) };
            for (int i = 0; i < dim; i++)
            {
                Vector3 newPos = new Vector3(
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.x + (i - 1) * m_landScapeSize,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.y,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.z + (curGlobalIndexZ - prevGlobalIndexZ) * m_landScapeSize);

                PatchManager.AddTerrainInfo(curGlobalIndexX + i - 1, z0, m_terrainGrid[listX[i], z], newPos);
            }
        }
        else if (curGlobalIndexX != prevGlobalIndexX)
        {
            int x; int x0;
            if (curGlobalIndexX > prevGlobalIndexX)
            {
                x0 = curGlobalIndexX + 1;
                x  = PreviousCyclicIndex(prevCyclicIndexX);
            }
            else
            {
                x0 = curGlobalIndexX - 1;
                x  = NextCyclicIndex(prevCyclicIndexX);
            }

            int[] listZ = { PreviousCyclicIndex(prevCyclicIndexZ), prevCyclicIndexZ, NextCyclicIndex(prevCyclicIndexZ) };
            for (int i = 0; i < dim; i++)
            {
                Vector3 newPos = new Vector3(
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.x + (curGlobalIndexX - prevGlobalIndexX) * m_landScapeSize,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.y,
                    m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].transform.position.z + (i - 1) * m_landScapeSize);

                PatchManager.AddTerrainInfo(x0, curGlobalIndexZ + i - 1, m_terrainGrid[x, listZ[i]], newPos);
            }
        }
        PatchManager.MakePatches();
    }
    void Awake()
    {
        RenderDetailsStatic       = RenderDetails;
        RenderTreesStatic         = RenderTrees;
        UsePresetLandMassesStatic = UsePresetLandMasses;

        StaticTestCurve  = TestCurve;
        StaticTestCurve2 = TestCurve2;

        Texture2D falloff = new Texture2D(513, 513);

        fallOffTable = GenerateFalloffTable();

        UnityEngine.Random.InitState(InfiniteLandscape.RandomSeed);
        for (int i = 0; i < m_storedRandoms.Length; i++)
        {
            //m_storedRandoms[i] = Random.Range(0, 7); // lots of plains

            m_storedRandoms[i] = UnityEngine.Random.Range(1, 9);             // less plains, more water
        }

        if (UsePresetLandMassesStatic)
        {
            /*Set landmass values of starting area here.
             *
             * 1:hills
             * 2: mountains
             * 4: ridged mountains
             * 8: plains
             *
             * values ""stack", so 3 would be hills and mountains
             *
             */
            int iX = InfiniteLandscape.initialGlobalIndexX;
            int iZ = InfiniteLandscape.initialGlobalIndexZ;

            SetFixedLandmassValue(iX - 1, iZ - 1, 8);               // there is 3x3 terrains but we preset 5x5 so we can control also
            // nonvisible adjacent areas
            SetFixedLandmassValue(iX + 0, iZ - 1, 8);
            SetFixedLandmassValue(iX + 1, iZ - 1, 9);
            SetFixedLandmassValue(iX + 2, iZ - 1, 8);
            SetFixedLandmassValue(iX + 3, iZ - 1, 8);

            SetFixedLandmassValue(iX - 1, iZ + 0, 8);
            SetFixedLandmassValue(iX + 0, iZ + 0, 8);
            SetFixedLandmassValue(iX + 1, iZ + 0, 9);
            SetFixedLandmassValue(iX + 2, iZ + 0, 8);
            SetFixedLandmassValue(iX + 3, iZ + 0, 8);


            SetFixedLandmassValue(iX - 1, iZ + 1, 12);
            SetFixedLandmassValue(iX + 0, iZ + 1, 12);
            SetFixedLandmassValue(iX + 1, iZ + 1, 13);             //"Center of the map";
            SetFixedLandmassValue(iX + 2, iZ + 1, 12);
            SetFixedLandmassValue(iX + 3, iZ + 1, 12);


            SetFixedLandmassValue(iX - 1, iZ + 2, 8);
            SetFixedLandmassValue(iX + 0, iZ + 2, 8);
            SetFixedLandmassValue(iX + 1, iZ + 2, 9);
            SetFixedLandmassValue(iX + 2, iZ + 2, 8);
            SetFixedLandmassValue(iX + 3, iZ + 2, 8);


            SetFixedLandmassValue(iX - 1, iZ + 3, 8);
            SetFixedLandmassValue(iX + 0, iZ + 3, 8);
            SetFixedLandmassValue(iX + 1, iZ + 3, 9);
            SetFixedLandmassValue(iX + 2, iZ + 3, 8);
            SetFixedLandmassValue(iX + 3, iZ + 3, 8);
        }

        // changed tree types:
        m_treeProtoTypes = new TreePrototype[numOfTreePrototypes];

        m_treeProtoTypes[0]        = new TreePrototype();
        m_treeProtoTypes[0].prefab = m_tree0;

        m_treeProtoTypes[1]        = new TreePrototype();
        m_treeProtoTypes[1].prefab = m_tree1;

        m_treeProtoTypes[2]        = new TreePrototype();
        m_treeProtoTypes[2].prefab = m_tree2;

        m_treeProtoTypes[3]        = new TreePrototype();
        m_treeProtoTypes[3].prefab = m_tree3;

        m_treeProtoTypes[4]        = new TreePrototype();
        m_treeProtoTypes[4].prefab = m_tree4;

        m_treeProtoTypes[5]        = new TreePrototype();
        m_treeProtoTypes[5].prefab = m_tree5;


        trees[0] = m_treeProtoTypes[0].prefab as GameObject;
        trees[1] = m_treeProtoTypes[1].prefab as GameObject;
        trees[2] = m_treeProtoTypes[2].prefab as GameObject;
        trees[3] = m_treeProtoTypes[3].prefab as GameObject;
        trees[4] = m_treeProtoTypes[4].prefab as GameObject;



        // changed tree types:
        m_treeProtoTypes = new TreePrototype[numOfTreePrototypes];

        m_treeProtoTypes[0]        = new TreePrototype();
        m_treeProtoTypes[0].prefab = m_tree0;

        m_treeProtoTypes[1]        = new TreePrototype();
        m_treeProtoTypes[1].prefab = m_tree1;

        m_treeProtoTypes[2]        = new TreePrototype();
        m_treeProtoTypes[2].prefab = m_tree2;

        m_treeProtoTypes[3]        = new TreePrototype();
        m_treeProtoTypes[3].prefab = m_tree3;

        m_treeProtoTypes[4]        = new TreePrototype();
        m_treeProtoTypes[4].prefab = m_tree4;

        m_treeProtoTypes[5]        = new TreePrototype();
        m_treeProtoTypes[5].prefab = m_tree5;


        trees[0] = m_treeProtoTypes[0].prefab as GameObject;
        trees[1] = m_treeProtoTypes[1].prefab as GameObject;
        trees[2] = m_treeProtoTypes[2].prefab as GameObject;
        trees[3] = m_treeProtoTypes[3].prefab as GameObject;
        trees[4] = m_treeProtoTypes[4].prefab as GameObject;
        trees[5] = m_treeProtoTypes[5].prefab as GameObject;

        Vector2[] splatTileSize = new Vector2[5] {
            new Vector2(Tile0, Tile0), new Vector2(Tile1, Tile1), new Vector2(Tile2, Tile2), new Vector2(Tile3, Tile3), new Vector2(Tile4, Tile4)
        };
        for (int i = 0; i < numOfSplatPrototypes; i++)
        {
            m_splatPrototypes[i] = new SplatPrototype();
        }


        for (int i = 0; i < numOfSplatPrototypes; i++)
        {
            m_splatPrototypes[i].texture    = splat[i];
            m_splatPrototypes[i].normalMap  = splatNormals[i];
            m_splatPrototypes[i].specular   = splatSpecular[i];
            m_splatPrototypes[i].tileOffset = Vector2.zero;
            m_splatPrototypes[i].tileSize   = splatTileSize[i];
            m_splatPrototypes[i].texture.Apply(true);
        }

        for (int i = 0; i < numOfDetailPrototypes; i++)
        {
            if (i == 2) // overriding third detail with mesh
            {
                m_detailProtoTypes[i] = new DetailPrototype();
                m_detailProtoTypes[i].usePrototypeMesh = true;

                m_detailProtoTypes[i].prototype  = detailMesh[0];
                m_detailProtoTypes[i].renderMode = DetailRenderMode.VertexLit;

                m_detailProtoTypes[i].minHeight = 0.5f;
                m_detailProtoTypes[i].minWidth  = 0.5f;

                m_detailProtoTypes[i].maxHeight = 1;
                m_detailProtoTypes[i].maxWidth  = 1;

                //m_detailProtoTypes[i].noiseSpread = ???

                m_detailProtoTypes[i].healthyColor = Color.white;
                m_detailProtoTypes[i].dryColor     = Color.white;
            }
            else
            {
                m_detailProtoTypes[i] = new DetailPrototype();
                m_detailProtoTypes[i].prototypeTexture = detailTexture[i];
                m_detailProtoTypes[i].renderMode       = detailMode;
                m_detailProtoTypes[i].healthyColor     = m_grassHealthyColor;
                m_detailProtoTypes[i].dryColor         = m_grassDryColor;
                m_detailProtoTypes[i].maxHeight        = 1; //0.5f;
                m_detailProtoTypes[i].maxWidth         = 1; //0.2f;
                m_detailProtoTypes[i].noiseSpread      = 0.5f;
            }
        }
        for (int i = 0; i < numOfTreePrototypes; i++)
        {
            m_treeProtoTypes[i]        = new TreePrototype();
            m_treeProtoTypes[i].prefab = trees[i];
        }
        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                TerrainData terrainData = new TerrainData();
                terrainData.wavingGrassStrength = m_wavingGrassStrength;
                terrainData.wavingGrassAmount   = m_wavingGrassAmount;
                terrainData.wavingGrassSpeed    = m_wavingGrassSpeed;
                terrainData.wavingGrassTint     = m_wavingGrassTint;
                terrainData.heightmapResolution = m_heightMapSize;
                terrainData.size = new Vector3(m_landScapeSize, m_terrainHeight, m_landScapeSize);
                terrainData.alphamapResolution = m_alphaMapSize;

                // TODO update to new api:
                terrainData.splatPrototypes = m_splatPrototypes;

                terrainData.treePrototypes = m_treeProtoTypes;
                terrainData.SetDetailResolution(m_detailMapSize, m_detailResolutionPerPatch);
                terrainData.detailPrototypes = m_detailProtoTypes;


                GameObject terrainGameObject = Terrain.CreateTerrainGameObject(terrainData);
                m_terrainGrid[i, j] = terrainGameObject.GetComponent <Terrain>();


                if (useTestMaterial)
                {
                    m_terrainGrid[i, j].materialType     = Terrain.MaterialType.Custom;
                    m_terrainGrid[i, j].materialTemplate = testMaterial;
                }

                if (useDrawInstancing)
                {
                    m_terrainGrid[i, j].drawInstanced = false;
                }
            }
        }

        for (int i = 0; i < dim; i++)
        {
            for (int j = 0; j < dim; j++)
            {
                m_terrainGrid[i, j].gameObject.AddComponent <TerrainScript>();
                m_terrainGrid[i, j].transform.parent = gameObject.transform;

                m_terrainGrid[i, j].transform.position = new Vector3(
                    m_terrainGrid[1, 1].transform.position.x + (i - 1) * m_landScapeSize, m_terrainGrid[1, 1].transform.position.y,
                    m_terrainGrid[1, 1].transform.position.z + (j - 1) * m_landScapeSize);

                m_terrainGrid[i, j].treeDistance            = m_treeDistance;
                m_terrainGrid[i, j].treeBillboardDistance   = m_treeBillboardDistance;
                m_terrainGrid[i, j].treeCrossFadeLength     = m_treeCrossFadeLength;
                m_terrainGrid[i, j].treeMaximumFullLODCount = m_treeMaximumFullLODCount;

                m_terrainGrid[i, j].detailObjectDensity  = m_detailObjectDensity;
                m_terrainGrid[i, j].detailObjectDistance = m_detailObjectDistance;

                m_terrainGrid[i, j].GetComponent <Collider>().enabled = false;
                m_terrainGrid[i, j].basemapDistance = 4000;
                m_terrainGrid[i, j].castShadows     = false;

                // m_terrainGrid[i, j].terrainData.wavingGrassAmount = 1000;

                string xName = (curGlobalIndexX + i - 1).ToString(); // name will be used for identifying the correct entry in landmamassDictionary
                string zName = (curGlobalIndexZ + j - 1).ToString();
                m_terrainGrid[i, j].name = xName + "_new_" + zName;

                PatchManager.AddTerrainInfo(curGlobalIndexX + i - 1, curGlobalIndexZ + j - 1, m_terrainGrid[i, j], m_terrainGrid[i, j].transform.position);
            }
        }
        PatchManager.MakePatches();

        int patchCount = PatchManager.patchQueue.Count;

        for (int i = 0; i < patchCount; i++)
        {
            PatchManager.patchQueue.Dequeue().ExecutePatch();
        }

        UpdateIndexes();
        UpdateTerrainNeighbors();

        StartCoroutine(FlushTerrain());
        terrainIsFlushed = true;

        m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].GetComponent <Collider>().enabled = false;
        m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].GetComponent <Collider>().enabled = true;
    }