The TerraForge 2 Runtime Scripting API provides developers with powerful tools to control terrain generation and modifications programmatically. This API allows for runtime customization and automation, making it easier to integrate TerraForge 2 into dynamic environments. Below are the primary methods and examples for using the API.
Terrain Generation
Basic Terrain Generation
To generate terrain using the TerraForgeTerrainGenerator component:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI_TerrainGenerate:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Initiates terrain generationterrainGenerator.TerrainGenerate(); }}
Saving Parameters with PlayerPrefs
To save the parameters of the TerraForgeTerrainGenerator component after generating terrain:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Generates the terrainterrainGenerator.TerrainGenerate(); // Saves the component data using PlayerPrefsterrainGenerator.SaveComponentData(); }}
Loading Saved Parameters with PlayerPrefs
To load previously saved parameters before generating terrain:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Loads the component data from PlayerPrefsterrainGenerator.LoadComponentData(); // Generates the terrainterrainGenerator.TerrainGenerate(); }}
Tracking Terrain Generation Completion
To track when the terrain generation process is complete:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Subscribes to the OnGenerationComplete eventterrainGenerator.OnGenerationComplete+= HandleGenerationComplete; // Starts the terrain generation processterrainGenerator.TerrainGenerate(); } // Method called when terrain generation is completevoidHandleGenerationComplete() {Debug.Log("Terrain generation is complete!"); }voidOnDestroy() { // Unsubscribes from the OnGenerationComplete event to prevent memory leaksif (terrainGenerator !=null) {terrainGenerator.OnGenerationComplete-= HandleGenerationComplete; } }}
Applying Biome Settings
Applying Biome Settings to a Terrain Generator
To apply specific biome settings to the TerraForgeTerrainGenerator:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the biome settingspublicBiomeSettings biome; // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Applies the biome settings to the terrain generatorbiome.ApplyingBiomeSettings(terrainGenerator,null,false,15); // Generates the terrainterrainGenerator.TerrainGenerate(); }}// What the used method looks like in the BiomeSettings componentpublicclassBiomeSettings:ScriptableObject{ /// <summary> /// Applies the biome settings to the specified terrain generator and terrain game object. /// </summary> /// <paramname="generator">The terrain generator to apply the settings to.</param> /// <paramname="terrainGameObject">The terrain game object to apply the settings to.</param> /// <paramname="isEmptyBiome">Indicates if the biome is empty.</param> /// <paramname="emptyBiomesHeight">The height for empty biomes.</param> public void ApplyingBiomeSettings(TerraForgeTerrainGenerator generator, GameObject terrainGameObject, bool isEmptyBiome, float emptyBiomesHeight)
{ // Implementation of biome application logic... }}
Modifying Generation Parameters
Changing Generation Parameters of the Terrain Generator
To change the generation parameters dynamically:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Change the terrain height parameterterrainGenerator.generalSettings.terrainHeight=12f; // Copy and modify terrain layersterrainGenerator.terrainLayers=newTerrainLayerSettings[terrainGenerator.terrainLayers.Length];for (int i =0; i <terrainLayers.Length; i++) {terrainGenerator.terrainLayers[i] =newTerrainLayerSettings(terrainGenerator.terrainLayers[i]);terrainGenerator.terrainLayers[i].seed=UnityEngine.Random.Range(0,1000); } // Modify hydraulic erosion settings terrainGenerator.hydraulicErosionLayerSettings = new HydraulicErosionLayerSettings(terrainGenerator.hydraulicErosionLayerSettings);
terrainGenerator.hydraulicErosionLayerSettings.inertia=3f; // Generates the terrain with new settingsterrainGenerator.TerrainGenerate(); }}
Advanced Terrain Generation
Creating a New TerrainData File
To create a new TerrainData file and generate terrain:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Creates and sets a new cloned TerrainData fileterrainGenerator.CreateAndSetCloneTerrainData(); // Generates the terrainterrainGenerator.TerrainGenerate(); }}
Changing Terrain Resolution
To change the resolution of the terrain:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrain generatorpublicTerraForgeTerrainGenerator terrainGenerator;voidStart() { // Change the terrain resolution settingterrainGenerator.generalSettings.terrainResolution=TerrainResolution.Resolution513; // Apply the new resolution and refresh generationterrainGenerator.ChangeTerrainResolution(true); }}// What the used method looks like in the TerraForgeTerrainGenerator componentpublicclassTerraForgeTerrainGenerator:MonoBehaviour,IGenerator{ /// <summary> /// Changes the terrain resolution based on the resolution specified in the general settings. /// </summary> /// <param name="refreshGeneration">Flag indicating whether to refresh terrain generation after resolution change.</param>
publicvoidChangeTerrainResolution(bool refreshGeneration) { // Implementation of resolution change logic... }}
Generating a Terrains Grid with Custom Parameters
To generate a grid of terrains with custom parameters:
usingUnityEngine;usingTerraForge2.Scripts.Generators;publicclassTestAPI:MonoBehaviour{ // Reference to the TerraForge terrains grid generatorpublicTerraForgeTerrainsGridGenerator terrainsGridGenerator;voidStart() { // Set the number of grid columns and linesterrainsGridGenerator.gridColumns=2;terrainsGridGenerator.gridLines=2; // Initiates the generation of the terrain gridterrainsGridGenerator.NewGenerateTerrainGrid(); }}
These are just examples of usage. You can create your own unique methods and ways of using the API. The demo scenes also partially utilize the above methods.