public void CreateGridObjects()
        {
            TerrainData terrainData = terrain.terrainData;

            float[,] heights = terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight);
            Material matBase = new Material(Shader.Find("Diffuse"));

            matBase.mainTexture = TerrainToMeshTool.BakeBaseTexture(terrainData);
            List <Material> matAdd   = new List <Material>();
            List <Material> matFirst = new List <Material>();

            for (int l = 0; l < terrainData.alphamapLayers; l++)
            {
                matAdd.Add(TerrainToMeshTool.GetMaterial(terrainData, l, false));
                matFirst.Add(TerrainToMeshTool.GetMaterial(terrainData, l, true));
            }

            int w        = terrainData.heightmapWidth - 1;
            int gridNumX = w / gridSize;

            tiles = new TerrainToMeshTile[gridNumX * gridNumX];

            for (int x = 0; x < gridNumX; x++)
            {
                for (int y = 0; y < gridNumX; y++)
                {
                    GameObject objGrid = new GameObject("mesh_" + x + "_" + y);
                    objGrid.transform.SetParent(this.transform, false);
                    TerrainToMeshTile tile = objGrid.AddComponent <TerrainToMeshTile>();
                    tiles[y * gridNumX + x] = tile;
                    tile.matBase            = matBase;
                    tile.matAdd             = matAdd;
                    tile.matFirst           = matFirst;
                    tile.roots       = roots;
                    tile.trees       = new Node[roots.Length];
                    tile.lodLevel    = -1;
                    tile.terrainData = terrainData;
                    tile.heights     = heights;
                    for (int i = 0; i < roots.Length; i++)
                    {
                        tile.trees[i] = roots[i].FindSizeNode(x * gridSize, y * gridSize, gridSize);
                    }
                }
            }
            for (int x = 0; x < gridNumX; x++)
            {
                for (int y = 0; y < gridNumX; y++)
                {
                    //  2
                    //1   3
                    //  0
                    tiles[y * gridNumX + x].adjacencies[0] = y > 0 ? tiles[(y - 1) * gridNumX + x] : null;
                    tiles[y * gridNumX + x].adjacencies[2] = y < gridNumX - 1 ? tiles[(y + 1) * gridNumX + x] : null;
                    tiles[y * gridNumX + x].adjacencies[1] = x > 0 ? tiles[y * gridNumX + x - 1] : null;
                    tiles[y * gridNumX + x].adjacencies[3] = x < gridNumX - 1 ? tiles[y * gridNumX + x + 1] : null;
                }
            }
            Update();
        }
Esempio n. 2
0
        public void CollectInfos()
        {
            TerrainData     terrainData = terrain.terrainData;
            Texture2D       baseTexture = TerrainToMeshTool.BakeBaseTexture(terrain.terrainData);
            List <Material> matAdd      = new List <Material>();
            List <Material> matFirst    = new List <Material>();
            Material        matBase     = new Material(shaderBase);

            matBase.SetTexture("_MainTex", baseTexture);
            float[,] heights = terrain.terrainData.GetHeights(0, 0, terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight);
            Texture2DArray texArray        = null;
            Material       terrainMaterial = new Material(Shader.Find("Nature/Terrain/Diffuse"));

            if (layerType == LayerType.SingleMesh_LayerIndexTexture)
            {
                bakedControlTexture = TerrainToMeshTool.BakeTextureIndex(terrain.terrainData);
                {
                    Texture firstTexture = terrainData.splatPrototypes[0].texture;
                    texArray = new Texture2DArray(firstTexture.width, firstTexture.height, terrainData.splatPrototypes.Length, TextureFormat.ARGB32, true, true);
                    for (int i = 0; i < terrainData.splatPrototypes.Length; i++)
                    {
                        texArray.SetPixels32(terrainData.splatPrototypes[i].texture.GetPixels32(), i);
                    }
                    texArray.Apply();
                }
                if (terrainTextureIndexMaterial == null)
                {
                    terrainTextureIndexMaterial = new Material(Shader.Find("Mobile/TerrainTextureIndex"));
                }
                else
                {
                    terrainTextureIndexMaterial.shader = Shader.Find("Mobile/TerrainTextureIndex");
                }
                terrainTextureIndexMaterial.SetTexture("_IndexControl", bakedControlTexture);
                terrainTextureIndexMaterial.SetTexture("_TexArray", texArray);
                terrainTextureIndexMaterial.SetFloat("_TexArrayNum", terrainData.splatPrototypes.Length);

                terrainTextureIndexMaterial.SetVectorArray("_ScaleOffset", GetScaleOffsets(terrainData));
            }
            else if (layerType == LayerType.SingleMesh_Max4Layer)
            {
                bakedControlTexture = TerrainToMeshTool.BakeControlTexture(terrain.terrainData, roots[0], gridSize, 4);
            }
            else
            {
                for (int l = 0; l < terrainData.alphamapLayers; l++)
                {
                    LayerProperty lp = l < layerProperties.Length ? layerProperties[l] : null;
                    matAdd.Add(TerrainToMeshTool.GetMaterial(terrain, l, shaderAdd, lp));
                    matFirst.Add(TerrainToMeshTool.GetMaterial(terrain, l, shaderFirst, lp));
                }
            }

            int w        = terrainData.heightmapWidth - 1;
            int gridNumX = w / gridSize;

            tiles = new TerrainToMeshTile[gridNumX * gridNumX];

            for (int x = 0; x < gridNumX; x++)
            {
                for (int y = 0; y < gridNumX; y++)
                {
                    GameObject objGrid = new GameObject("mesh_" + x + "_" + y);
                    objGrid.transform.SetParent(GetRootTransform(), false);
                    TerrainToMeshTile tile = objGrid.AddComponent <TerrainToMeshTile>();
                    tiles[y * gridNumX + x] = tile;
                    tile.matBase            = matBase;
                    tile.matAdd             = matAdd;
                    tile.matFirst           = matFirst;
                    tile.lodLevel           = -1;
                    tile.terrainData        = terrainData;
                    tile.heights            = heights;
                    tile.roots                       = roots;
                    tile.trees                       = new Node[roots.Length];
                    tile.layerType                   = layerType;
                    tile.lodPower                    = lodPower;
                    tile.terrainMaterial             = terrainMaterial;
                    tile.bakedControlTexture         = bakedControlTexture;
                    tile.terrainTextureIndexMaterial = terrainTextureIndexMaterial;
                    tile.texArray                    = texArray;
                    for (int lod = 0; lod < roots.Length; lod++)
                    {
                        tile.trees[lod] = roots[lod].FindSizeNode(x * gridSize, y * gridSize, gridSize);
                        TerrainToMeshTool.SetNodeSkirts(tile.trees[lod], tile.trees[lod]);
                    }
                }
            }

            for (int x = 0; x < gridNumX; x++)
            {
                for (int y = 0; y < gridNumX; y++)
                {
                    tiles[y * gridNumX + x].CollectMeshInfo(maxLodLevel);
                }
            }
        }