Added textures to maps
This commit is contained in:
12
Game/Assets/Scripts/Unity/InitializeScript.cs.meta
Normal file
12
Game/Assets/Scripts/Unity/InitializeScript.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e6476d0467e3fbd47a0b69d427d68e48
|
||||
timeCreated: 1431068945
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: -100
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,10 +1,10 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using TransportGame.Utils;
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using TransportGame.Generator;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Business;
|
||||
using TransportGame.Utils;
|
||||
using UnityEngine;
|
||||
|
||||
public class TerrainGeneratorScript : MonoBehaviour
|
||||
{
|
||||
@ -13,6 +13,7 @@ public class TerrainGeneratorScript : MonoBehaviour
|
||||
public int TerrainWidth = 1024;
|
||||
public int TerrainHeight = 1024;
|
||||
public GameObject WaterObject;
|
||||
public Texture2D[] Textures;
|
||||
|
||||
// Use this for initialization
|
||||
void Start()
|
||||
@ -61,7 +62,27 @@ public class TerrainGeneratorScript : MonoBehaviour
|
||||
terrainData.SetDetailResolution(1024, 8);
|
||||
terrainData.SetHeights(0, 0, map.Heights);
|
||||
terrainData.name = "Generated Terrain Data";
|
||||
yield return null;
|
||||
yield return null;
|
||||
|
||||
if (map.Biome.Textures != null)
|
||||
{
|
||||
SplatPrototype[] prototypes = new SplatPrototype[map.Biome.Textures.Length];
|
||||
|
||||
for (int i = 0; i < map.Biome.Textures.Length; i++)
|
||||
{
|
||||
Texture2D texture = Textures.FirstOrDefault(tex => tex != null && tex.name == map.Biome.Textures[i].Source);
|
||||
|
||||
if (texture == null)
|
||||
throw new Exception("Texture " + map.Biome.Textures[i].Source + " not found!");
|
||||
|
||||
prototypes[i] = new SplatPrototype();
|
||||
prototypes[i].texture = texture;
|
||||
}
|
||||
|
||||
terrainData.splatPrototypes = prototypes;
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Create terrain object
|
||||
GameObject terrain = Terrain.CreateTerrainGameObject(terrainData);
|
||||
@ -78,11 +99,71 @@ public class TerrainGeneratorScript : MonoBehaviour
|
||||
MeshFilter waterMesh = WaterObject.GetComponent<MeshFilter>();
|
||||
waterMesh.mesh = GenerateWater();
|
||||
}
|
||||
yield return null;
|
||||
|
||||
// Set up textures
|
||||
|
||||
SetupSplatmaps(terrainData);
|
||||
}
|
||||
|
||||
private void SetupSplatmaps(TerrainData terrainData)
|
||||
{
|
||||
float[, ,] splatData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];
|
||||
Expression[] expressions = new Expression[terrainData.alphamapLayers];
|
||||
|
||||
for (int y = 0; y < terrainData.alphamapHeight; y++)
|
||||
for (int x = 0; x < terrainData.alphamapWidth; x++)
|
||||
{
|
||||
float y_01 = (float)y / (float)terrainData.alphamapHeight;
|
||||
float x_01 = (float)x / (float)terrainData.alphamapWidth;
|
||||
|
||||
int ix = Mathf.RoundToInt(x_01 * TerrainWidth);
|
||||
int iy = Mathf.RoundToInt(y_01 * TerrainHeight);
|
||||
|
||||
// Get height
|
||||
float height = map.Heights[ix, iy] * terrainData.size.y;
|
||||
|
||||
// Get steepness
|
||||
int safex = (ix == 0) ? 1 : ix;
|
||||
int safey = (iy == 0) ? 1 : iy;
|
||||
|
||||
float dx = map.Heights[safex - 1, safey] * map.Biome.Height - height;
|
||||
float dy = map.Heights[safex, safey - 1] * map.Biome.Height - height;
|
||||
float steepness = dx * dx + dy * dy;
|
||||
|
||||
|
||||
float[] weights = new float[terrainData.alphamapLayers];
|
||||
float sum = 0;
|
||||
|
||||
// Go through each texture layer
|
||||
for (int t = 0; t < terrainData.alphamapLayers; t++)
|
||||
{
|
||||
// Set up expression
|
||||
if (expressions[t] == null)
|
||||
{
|
||||
expressions[t] = new Expression(map.Biome.Textures[t].Expression);
|
||||
expressions[t].ParseExpression();
|
||||
}
|
||||
|
||||
expressions[t].Variables["height"] = height;
|
||||
expressions[t].Variables["steepness"] = steepness;
|
||||
expressions[t].Variables["maxHeight"] = terrainData.size.y;
|
||||
expressions[t].Variables["waterLevel"] = map.WaterLevel - 1f;
|
||||
|
||||
// Obtain weight
|
||||
weights[t] = expressions[t].Evaluate();
|
||||
sum += weights[t];
|
||||
}
|
||||
|
||||
// Normalize and copy weights
|
||||
for (int t = 0; t < terrainData.alphamapLayers; t++)
|
||||
{
|
||||
splatData[x, y, t] = weights[t] / sum;
|
||||
}
|
||||
}
|
||||
|
||||
terrainData.SetAlphamaps(0, 0, splatData);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
12
Game/Assets/Scripts/Unity/TerrainGeneratorScript.cs.meta
Normal file
12
Game/Assets/Scripts/Unity/TerrainGeneratorScript.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: face2544c49ac4a4b8871b8952f2427a
|
||||
timeCreated: 1431068977
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user