Giter Club home page Giter Club logo

Comments (4)

mikemahoney218 avatar mikemahoney218 commented on August 23, 2024

Create terrain objects with no height data -- from https://forum.unity.com/threads/create-terrain-from-code.174306/

     
    using UnityEngine;
    using UnityEditor;
    using System.IO;
     
    public class CreateTiledTerrain : EditorWindow {
       
        private static EditorWindow window;
       
        private static Vector2 tileAmount = Vector2.one;
       
        private float width  = 1000;
        private float lenght = 1000;
        private float height = 600;
       
        private int heightmapResoltion          = 513;
        private int detailResolution            = 1024;
        private int detailResolutionPerPatch    = 8;
        private int controlTextureResolution    = 512;
        private int baseTextureReolution        = 1024;
       
        private string path = string.Empty;
       
        private enum Alphabet{
            None,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
        }
        private Alphabet alphabet = Alphabet.None;
     
       
     
        [MenuItem("Terrain/Create Tiled Terrain")]
        public static void CreateWindow(){
            window = EditorWindow.GetWindow(typeof(CreateTiledTerrain));
            window.title = "Tiled Terrain";
            window.minSize = new Vector2(500f, 700f);
        }
       
        private void OnGUI(){
            EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(false));
            tileAmount = EditorGUILayout.Vector2Field("Amount", tileAmount);
            EditorGUILayout.EndHorizontal();
           
            width = EditorGUILayout.FloatField("Terrain Width", width);
            lenght = EditorGUILayout.FloatField("Terrain Lenght", lenght);
            height = EditorGUILayout.FloatField("Terrain Height", height);
           
            EditorGUILayout.Space();
     
            heightmapResoltion = EditorGUILayout.IntField("Heightmap Resoltion", heightmapResoltion);
            heightmapResoltion = Mathf.ClosestPowerOfTwo(heightmapResoltion) + 1;
            heightmapResoltion = Mathf.Clamp(heightmapResoltion, 33, 4097);
           
            detailResolution = EditorGUILayout.IntField("Detail Resolution", detailResolution);
            detailResolution = Mathf.ClosestPowerOfTwo(detailResolution);
            detailResolution = Mathf.Clamp(detailResolution, 0, 4096);
           
            detailResolutionPerPatch = EditorGUILayout.IntField("Detail Resolution Per Patch", detailResolutionPerPatch);
            detailResolutionPerPatch = Mathf.ClosestPowerOfTwo(detailResolutionPerPatch);
            detailResolutionPerPatch = Mathf.Clamp(detailResolutionPerPatch, 8, 128);
           
            controlTextureResolution = EditorGUILayout.IntField("Control Texture Resolution", controlTextureResolution);
            controlTextureResolution = Mathf.ClosestPowerOfTwo(controlTextureResolution);
            controlTextureResolution = Mathf.Clamp(controlTextureResolution, 16, 1024);
           
            baseTextureReolution = EditorGUILayout.IntField("Base Texture Reolution", baseTextureReolution);
            baseTextureReolution = Mathf.ClosestPowerOfTwo(baseTextureReolution);
            baseTextureReolution = Mathf.Clamp(baseTextureReolution, 16, 2048);
           
            EditorGUILayout.Space();
            GUILayout.Label("Path were to save TerrainDate:");
            path = EditorGUILayout.TextField("Assets/", path);
           
            if(GUILayout.Button("Create")){
                ValidatePath();
                CreateTerrain();
               
                path = string.Empty;
            }
        }
       
        private void ValidatePath(){
            if(path == string.Empty) path = "TiledTerrain/TerrainData/";
           
            string pathToCheck = Application.dataPath + "/" + path;
            if(Directory.Exists(pathToCheck) == false){
                Directory.CreateDirectory(pathToCheck);
            }
        }
       
        private void CreateTerrain(){
            GameObject parent = (GameObject)Instantiate(new GameObject("Terrain"));
            parent.transform.position = new Vector3(0, 0, 0);
     
           
            for(int x = 1; x <= tileAmount.x; x++){
                for(int y = 1; y <= tileAmount.y; y++){
                   
                    TerrainData terrainData = new TerrainData();
                   
                    alphabet = (Alphabet)x;
                    string name = alphabet + "-" + y;
           
                    terrainData.size = new Vector3( width / 16f,
                                                    height,
                                                    lenght / 16f);
                   
                    terrainData.baseMapResolution = baseTextureReolution;
                    terrainData.heightmapResolution = heightmapResoltion;
                    terrainData.alphamapResolution = controlTextureResolution;
                    terrainData.SetDetailResolution(detailResolution, detailResolutionPerPatch);
     
                    terrainData.name = name;
                    GameObject terrain = (GameObject)Terrain.CreateTerrainGameObject(terrainData);
                   
                    terrain.name = name;
                    terrain.transform.parent = parent.transform;
                    terrain.transform.position = new Vector3(lenght * (x - 1), 0, width * (y - 1));
     
                    AssetDatabase.CreateAsset(terrainData, "Assets/" + path + name + ".asset");
     
                   
                }
            }
     
     
           
        }
       
    }

from unifir.

mikemahoney218 avatar mikemahoney218 commented on August 23, 2024

Relevant docs

https://docs.unity3d.com/ScriptReference/TerrainData.html
https://docs.unity3d.com/ScriptReference/Terrain.html

from unifir.

mikemahoney218 avatar mikemahoney218 commented on August 23, 2024

"Taking terrainr tiles into Unity" feels like an odd thing to live in this package, instead of terrainr

But I think if anything that's an argument to move raster_from_raw_tiles out of terrainr -- make that a tight API client package with a few spatial utilities, move the Unity stuff into its own package, pass single merged images between the two to interface

from unifir.

github-actions avatar github-actions commented on August 23, 2024

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

from unifir.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.