Пример #1
0
        public void CreateTerrains()
        {
            TerrainData     terrainData;
            GameObject      terrainObject;
            Terrain         terrain;
            TerrainCollider terrainCollider;

            string tileName;

            ClearToOne();

            terrains[0].size = terrainSize;
            int countTerrain = 0;

            index = terrains.Count - 1;

            Int2 tile = IndexToTile(terrainSelect);

            for (int y = 0; y < selectTiles.y; ++y)
            {
                for (int x = 0; x < selectTiles.x; ++x)
                {
                    // {
                    // }
                    // else if (terrains[countTerrain].terrain != null) { ++countTerrain; continue; }

                    if (countTerrain != 0)
                    {
                        terrains.Add(new TCUnityTerrain());
                        terrains[countTerrain] = CopyTerrain(terrains[0]);
                    }

                    terrains[countTerrain].tileX = x;
                    terrains[countTerrain].tileZ = y;

                    terrainObject = new GameObject();
                    terrainObject.transform.parent = transform;
                    terrain         = (Terrain)terrainObject.AddComponent(typeof(Terrain));
                    terrainCollider = (TerrainCollider)terrainObject.AddComponent(typeof(TerrainCollider));

                    tileName     = "_x" + x.ToString() + "_y" + y.ToString();
                    terrain.name = terrainName + tileName;

                    terrainData                 = new TerrainData();
                    terrainData.size            = terrains[0].size;
                    terrain.terrainData         = terrainData;
                    terrainCollider.terrainData = terrainData;

                    terrains[countTerrain].terrain = terrain;
                    terrains[countTerrain].ApplyAllSettings(terrains[0], settingsEditor);
                    terrains[countTerrain].terrainSettingsScript = terrain.gameObject.AddComponent <TC_TerrainSettings>();

                    // script.set_terrain_settings(terrainArea.terrains[index], "(siz)(res)");
                    //if (script.settings.copy_terrain_material)
                    //{
                    //    terrain.materialTemplate = terrainArea.terrains[0].terrain.materialTemplate;
                    //}

                    #if UNITY_EDITOR
                    string path = "Assets" + terrainDataPath.Replace(Application.dataPath, String.Empty);
                    path += "/" + terrain.name + ".asset";
                    UnityEditor.AssetDatabase.DeleteAsset(path);
                    UnityEditor.AssetDatabase.CreateAsset(terrainData, path);
                    #endif

                    // Debug.Log(terrains[countTerrain].splatPrototypes.Count);

                    terrains[countTerrain].ApplySplatTextures();
                    terrains[countTerrain].ApplyTrees();
                    terrains[countTerrain].ApplyGrass();

                    terrains[countTerrain].GetSettings(settingsEditor);

                    // script.set_terrain_parameters(terrainArea.terrains[index], terrainArea.terrains[0]);
                    // if (terrainArea.terrains[0].rtp_script) { script.assign_rtp_single(terrainArea.terrains[index]); }
                    ++countTerrain;
                }
            }

            if (terrains[0].terrain != null)
            {
                TC_Settings.instance.masterTerrain = terrains[0].terrain;
            }

            // script.set_all_terrain_area(script.terrains[0]);
            // script.set_terrainArea_splat_textures(terrainArea, terrainArea.terrains[0]);
            // script.assign_all_terrain_splat_alpha();
            // script.set_terrainArea_trees(terrainArea, terrainArea.terrains[0]);
            // script.set_terrainArea_details(terrainArea, terrainArea.terrains[0]);

            tiles = selectTiles;
            FitTerrainsPosition();
            CalcTotalResolutions();

            terrainSelect = TileToIndex(tile);
            if (terrainSelect > tiles.x * tiles.y)
            {
                terrainSelect = 0;
            }
        }
Пример #2
0
        public bool SetCurrentArea(TCUnityTerrain tcTerrain, int outputId)
        {
            // Debug.Log(tcTerrain.terrain.name);
            SetCurrentArea(tcTerrain);
            Terrain terrain = currentTerrain;

            if (!currentTCUnityTerrain.active)
            {
                return(false);
            }

            intResolution = new Int2();
            Int2 resolution2 = new Int2();

            if (terrain.terrainData.heightmapResolution > 2049)
            {
                resExpandBorder     = 0;
                resExpandBorderSize = 0;
            }
            else
            {
                resExpandBorder     = Mathf.RoundToInt((terrain.terrainData.heightmapResolution - 1) * TC_Settings.instance.resExpandBorderPercentage);
                resExpandBorderSize = terrain.terrainData.size.x * TC_Settings.instance.resExpandBorderPercentage;
            }

            // Debug.Log(resExpandBorder);
            // Debug.Log(resExpandBorderSize);

            if (outputId == TC.heightOutput)
            {
                intResolution.x = intResolution.y = (terrain.terrainData.heightmapResolution) + (resExpandBorder * 2);
                resolution2     = new Int2(terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution);
            }
            else if (outputId == TC.splatOutput)
            {
                intResolution.x = intResolution.y = terrain.terrainData.alphamapResolution;
                resolution2     = intResolution;
                splatLength     = TC.GetTerrainSplatTextureLength(currentTerrain);
                splatmapLength  = currentTerrain.terrainData.alphamapTextures.Length;
            }
            else if (outputId == TC.treeOutput)
            {
                intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.treeResolutionPM); resolution2 = intResolution;
            }
            else if (outputId == TC.grassOutput)
            {
                intResolution.x = intResolution.y = terrain.terrainData.detailResolution; resolution2 = intResolution;
            }
            else if (outputId == TC.objectOutput)
            {
                intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.objectResolutionPM); resolution2 = intResolution;
                //if (false)
                //{
                //    area.center = new Vector2((int)terrainLayer.objectTransform.position.x, (int)terrainLayer.objectTransform.position.z);
                //    area.size = new Vector2(terrainLayer.objectResolutionPM, terrainLayer.objectResolutionPM);
                //    totalArea.position = area.position;
                //    totalArea.size = terrainLayer.objectAreaSize;
                //    resolutionPM = new Vector2(terrainLayer.objectAreaSize.x / (resolution2.x), terrainLayer.objectAreaSize.y / (resolution2.y));
                //}
            }
            else if (outputId == TC.colorOutput)
            {
                intResolution.x = intResolution.y = terrainLayer.colormapResolution; resolution2 = intResolution;
            }

            outputOffsetV2 = new Vector2(terrainLayer.layerGroups[outputId].t.position.x, terrainLayer.layerGroups[outputId].t.position.z);
            outputOffsetV3 = new Vector3(outputOffsetV2.x, 0, outputOffsetV2.y);

            resolution = intResolution.ToVector2();

            if (intResolution.x < TC_Settings.instance.previewResolution)
            {
                previewResolution = intResolution.x; TC_Reporter.Log("From " + TC_Settings.instance.previewResolution + " To " + previewResolution);
            }
            else
            {
                previewResolution = TC_Settings.instance.previewResolution;
            }

            resToPreview = new Vector2((previewResolution - 0) / (totalArea.width + 0), (previewResolution - 0) / (totalArea.height + 0));

            if (outputId == TC.heightOutput || outputId == TC.splatOutput)
            {
                resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1));
            }
            else
            {
                resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 0), terrain.terrainData.size.z / (resolution2.y - 0));
            }

            // if (outputId == TC.objectOutput) Debug.Log(resolutionPM);

            // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1));

            if (outputId == TC.heightOutput)
            {
                // area = new Rect(terrain.transform.position.x - resolutionPM.x, terrain.transform.position.z - resolutionPM.y, intResolution.x - 0, intResolution.y - 0);
                area = new Rect(terrain.transform.position.x - (resolutionPM.x * resExpandBorder), terrain.transform.position.z - (resolutionPM.y * resExpandBorder), intResolution.x - 0, intResolution.y - 0);
            }
            else
            {
                // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x), terrain.terrainData.size.z / (resolution2.y));
                Vector2 posSnap;
                posSnap.x = Mathw.Snap(terrain.transform.position.x, resolutionPM.x);
                posSnap.y = Mathw.Snap(terrain.transform.position.z, resolutionPM.y);

                if (outputId == TC.treeOutput || outputId == TC.objectOutput)
                {
                    // posSnap += resolutionPM / 2;
                }
                area = new Rect(posSnap.x, posSnap.y, intResolution.x, intResolution.y);

                snapOffsetUV    = (new Vector2(terrain.transform.position.x, terrain.transform.position.z) - posSnap);
                snapOffsetUV.x /= terrain.terrainData.size.x;
                snapOffsetUV.y /= terrain.terrainData.size.z;

                // Debug.Log(area);
            }

            bounds      = new Bounds(terrain.transform.position + terrain.terrainData.size / 2, terrain.terrainData.size);
            startPos    = new Vector3(area.xMin, terrain.transform.position.y, area.yMin);
            terrainSize = terrain.terrainData.size;

            return(true);
        }
Пример #3
0
 public int TileToIndex(Int2 tile)
 {
     return((tile.y * tiles.x) + tile.x);
 }