Files
2026-03-04 09:37:33 +08:00

150 lines
7.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace GISTech.GISTerrainLoader
{
public class ProceduralTerrainSplatmap : MonoBehaviour
{
private string TerrainFilePath = Application.streamingAssetsPath + "/GIS Terrains/PNG Demo/ASTER30m.png";
private RuntimeTerrainGenerator RuntimeGenerator;
private GISTerrainLoaderPrefs Prefs;
private GISTerrainLoaderRuntimePrefs RuntimePrefs;
private void Start()
{
RuntimePrefs = GISTerrainLoaderMonoSingleton<GISTerrainLoaderRuntimePrefs>.Get;
Prefs = RuntimePrefs.Prefs;
RuntimeGenerator = GISTerrainLoaderMonoSingleton<RuntimeTerrainGenerator>.Get;
StartCoroutine(GenerateTerrain(TerrainFilePath));
}
private IEnumerator GenerateTerrain(string TerrainPath)
{
yield return new WaitForSeconds(2f);
if ((Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) && (string.IsNullOrEmpty(TerrainPath) || !File.Exists(TerrainPath)))
{
Debug.LogError("Terrain file null or not supported.. Try againe");
yield break;
}
InitializingRuntimePrefs(TerrainPath);
StartCoroutine(RuntimeGenerator.StartGenerating(Prefs));
}
private void InitializingRuntimePrefs(string TerrainPath)
{
RuntimeGenerator.enabled = true;
Prefs.RemovePrvTerrain = OptionEnabDisab.Enable;
Prefs.TerrainFilePath = TerrainFilePath;
Prefs.TerrainElevation = TerrainElevation.RealWorldElevation;
Prefs.terrainDimensionMode = TerrainDimensionsMode.Manual;
Prefs.TerrainDimensions = new DVector2(2.0, 2.0);
Prefs.heightmapResolution = 513;
Prefs.textureMode = TextureMode.Splatmapping;
Prefs.terrainCount = new Vector2Int(1, 1);
Prefs.Slope = 0.1f;
Prefs.MergeRadius = 20;
Prefs.MergingFactor = 2;
if (Application.platform == RuntimePlatform.WebGLPlayer || Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
StartCoroutine(AddSplatmapsToTerrainWebData(TerrainPath));
}
else
{
AddSplatmapsToTerrain(TerrainPath);
}
}
private void AddSplatmapsToTerrain(string TerrainPath)
{
string text = Path.GetFileName(TerrainPath).Split('.')[0];
string text2 = Path.GetDirectoryName(TerrainPath) + "/" + text + "_Splatmap";
Texture2D diffuse = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/Sand.jpg", TextureWrapMode.Repeat);
Texture2D normalMap = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/SandNormal.png", TextureWrapMode.Repeat);
Vector2Int vector2Int = new Vector2Int(100, 100);
GISTerrainLoaderTerrainLayer baseTerrainLayers = new GISTerrainLoaderTerrainLayer(diffuse, normalMap, vector2Int);
Texture2D diffuse2 = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/Grass.png", TextureWrapMode.Repeat);
Texture2D normalMap2 = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/GrassNormal.png", TextureWrapMode.Repeat);
Vector2Int vector2Int2 = new Vector2Int(100, 100);
GISTerrainLoaderTerrainLayer item = new GISTerrainLoaderTerrainLayer(diffuse2, normalMap2, vector2Int2);
Texture2D diffuse3 = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/CliffA.jpg", TextureWrapMode.Repeat);
Vector2Int vector2Int3 = new Vector2Int(300, 300);
GISTerrainLoaderTerrainLayer item2 = new GISTerrainLoaderTerrainLayer(diffuse3, null, vector2Int3);
Texture2D diffuse4 = GISTerrainLoaderTextureGenerator.LoadedTextureTile(text2 + "/CliffB.jpg", TextureWrapMode.Repeat);
Vector2Int vector2Int4 = new Vector2Int(300, 300);
GISTerrainLoaderTerrainLayer item3 = new GISTerrainLoaderTerrainLayer(diffuse4, null, vector2Int4);
Prefs.BaseTerrainLayers = baseTerrainLayers;
Prefs.TerrainLayers = new List<GISTerrainLoaderTerrainLayer>();
Prefs.TerrainLayers.Add(item);
Prefs.TerrainLayers.Add(item2);
Prefs.TerrainLayers.Add(item3);
GISTerrainLoaderSplatMapping.DistributingHeights(Prefs.TerrainLayers);
Prefs.TerrainLayers[0].X_Height = 0.1f;
}
private IEnumerator AddSplatmapsToTerrainWebData(string TerrainPath)
{
string terrainName = Path.GetFileName(TerrainPath).Split('.')[0];
_ = Path.GetDirectoryName(TerrainPath) + "/" + terrainName + "_Splatmap";
string gISFilePath = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/Sand.jpg");
Texture2D BaseTerrainTexture_Diffuse = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
BaseTerrainTexture_Diffuse = m_texture;
}));
string gISFilePath2 = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/SandNormal.jpg");
Texture2D BaseTerrainTexture_NormalMap = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath2, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
BaseTerrainTexture_NormalMap = m_texture;
}));
GISTerrainLoaderTerrainLayer baseLayer = new GISTerrainLoaderTerrainLayer(m_TextureSize: new Vector2Int(100, 100), m_Diffuse: BaseTerrainTexture_Diffuse, m_NormalMap: BaseTerrainTexture_NormalMap);
string gISFilePath3 = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/Grass.png");
Texture2D GrassTexture_Diffuse = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath3, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
GrassTexture_Diffuse = m_texture;
}));
string gISFilePath4 = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/GrassNormal.png");
Texture2D GrassTexture_NormalMap = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath4, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
GrassTexture_NormalMap = m_texture;
}));
GISTerrainLoaderTerrainLayer GrassLayer = new GISTerrainLoaderTerrainLayer(m_TextureSize: new Vector2Int(100, 100), m_Diffuse: GrassTexture_Diffuse, m_NormalMap: GrassTexture_NormalMap);
string gISFilePath5 = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/CliffA.jpg");
Texture2D CliffATexture_Diffuse = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath5, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
CliffATexture_Diffuse = m_texture;
}));
GISTerrainLoaderTerrainLayer CliffALayer = new GISTerrainLoaderTerrainLayer(m_TextureSize: new Vector2Int(300, 300), m_Diffuse: CliffATexture_Diffuse, m_NormalMap: null);
string gISFilePath6 = GISTerrainLoaderPlatformHelper.GetGISFilePath(Application.platform, Path.GetDirectoryName(TerrainPath), terrainName + "_Splatmap", "/CliffB.jpg");
Texture2D CliffBTexture_Diffuse = null;
yield return StartCoroutine(GISTerrainLoaderPlatformHelper.LoadTexture(gISFilePath6, delegate(Texture2D m_texture)
{
m_texture.wrapMode = TextureWrapMode.Repeat;
CliffBTexture_Diffuse = m_texture;
}));
GISTerrainLoaderTerrainLayer item = new GISTerrainLoaderTerrainLayer(m_TextureSize: new Vector2Int(300, 300), m_Diffuse: CliffBTexture_Diffuse, m_NormalMap: null);
Prefs.BaseTerrainLayers = baseLayer;
Prefs.TerrainLayers = new List<GISTerrainLoaderTerrainLayer>();
Prefs.TerrainLayers.Add(GrassLayer);
Prefs.TerrainLayers.Add(CliffALayer);
Prefs.TerrainLayers.Add(item);
GISTerrainLoaderSplatMapping.DistributingHeights(Prefs.TerrainLayers);
Prefs.TerrainLayers[0].X_Height = 0.1f;
yield return null;
}
}
}