Added textures to maps
This commit is contained in:
@ -39,6 +39,7 @@ namespace TransportGame.Business
|
||||
// Try to deserialize biome
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(Biome));
|
||||
var biome = (Biome)serializer.Deserialize(stream);
|
||||
biome.FileName = file;
|
||||
|
||||
// Add it to biome list
|
||||
biomes.Add(biome);
|
||||
|
12
Game/Assets/Scripts/Generator/RoadGenerator.cs.meta
Normal file
12
Game/Assets/Scripts/Generator/RoadGenerator.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca14a99c3a7bbee468354076f2d6f486
|
||||
timeCreated: 1431067754
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -45,6 +45,13 @@ namespace TransportGame.Model
|
||||
/// Gets or sets an array of textures to use
|
||||
/// </summary>
|
||||
[XmlArray("textures")]
|
||||
[XmlArrayItem("texture")]
|
||||
public Texture[] Textures { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the biome file name
|
||||
/// </summary>
|
||||
[XmlIgnore]
|
||||
public string FileName { get; set; }
|
||||
}
|
||||
}
|
||||
|
12
Game/Assets/Scripts/Model/Texture.cs.meta
Normal file
12
Game/Assets/Scripts/Model/Texture.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c67fd1150208b74a835ab23e76d4024
|
||||
timeCreated: 1431072739
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Game/Assets/Scripts/Storage.meta
Normal file
9
Game/Assets/Scripts/Storage.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aac1e643530b91a4387c8da7db6cc4c5
|
||||
folderAsset: yes
|
||||
timeCreated: 1426153506
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
9
Game/Assets/Scripts/Unity.meta
Normal file
9
Game/Assets/Scripts/Unity.meta
Normal file
@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc0bbd2288d13c34ca0d8a7df18c4ff4
|
||||
folderAsset: yes
|
||||
timeCreated: 1431068859
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
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:
|
@ -4,13 +4,16 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GraphingCalculator
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public class Expression
|
||||
{
|
||||
#region Types
|
||||
|
||||
public enum TokenType
|
||||
/// <summary>
|
||||
/// Token types
|
||||
/// </summary>
|
||||
protected enum TokenType
|
||||
{
|
||||
None, Literal, Identifier, Operator, ArgSeparator,
|
||||
LParanthesis, RParanthesis, Function
|
||||
@ -21,9 +24,21 @@ namespace GraphingCalculator
|
||||
/// </summary>
|
||||
protected class Token
|
||||
{
|
||||
/// <summary>
|
||||
/// Token type
|
||||
/// </summary>
|
||||
public TokenType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Token text
|
||||
/// </summary>
|
||||
public string Text { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a token
|
||||
/// </summary>
|
||||
/// <param name="type">Token type</param>
|
||||
/// <param name="text">Token text</param>
|
||||
public Token(TokenType type, string text)
|
||||
{
|
||||
Type = type;
|
||||
@ -33,15 +48,25 @@ namespace GraphingCalculator
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables, properties
|
||||
|
||||
private string expressionString;
|
||||
private bool parsed = false;
|
||||
|
||||
#region Public properties
|
||||
/// <summary>
|
||||
/// Gets the tokens in order
|
||||
/// </summary>
|
||||
protected List<Token> Tokens { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the postfix expression
|
||||
/// </summary>
|
||||
protected List<Token> Postfix { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of variables
|
||||
/// </summary>
|
||||
public Dictionary<string, float> Variables { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the expression string
|
||||
/// </summary>
|
||||
public string ExpressionString
|
||||
{
|
||||
get { return expressionString; }
|
||||
@ -51,18 +76,21 @@ namespace GraphingCalculator
|
||||
parsed = false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other stuff
|
||||
#region Private fields
|
||||
|
||||
public void AddVariable(string name, float value)
|
||||
{
|
||||
Variables.Add(name, value);
|
||||
}
|
||||
private string expressionString;
|
||||
private bool parsed = false;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes this instance of expression
|
||||
/// </summary>
|
||||
public Expression()
|
||||
{
|
||||
Tokens = new List<Token>();
|
||||
@ -72,13 +100,33 @@ namespace GraphingCalculator
|
||||
Variables.Add("pi", Mathf.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes this instance of expression
|
||||
/// </summary>
|
||||
/// <param name="expr">Expression text</param>
|
||||
public Expression(string expr)
|
||||
: this()
|
||||
{
|
||||
expressionString = expr;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Other stuff
|
||||
|
||||
/// <summary>
|
||||
/// Adds a variable
|
||||
/// </summary>
|
||||
/// <param name="name">Variable name</param>
|
||||
/// <param name="value">Variable value</param>
|
||||
public void AddVariable(string name, float value)
|
||||
{
|
||||
Variables.Add(name, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Parser
|
||||
|
||||
private void AnalyzeLex()
|
||||
@ -205,6 +253,9 @@ namespace GraphingCalculator
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses the expression
|
||||
/// </summary>
|
||||
public void ParseExpression()
|
||||
{
|
||||
if (!parsed)
|
||||
@ -217,6 +268,10 @@ namespace GraphingCalculator
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates the expression, and parses it if not parsed yet.
|
||||
/// </summary>
|
||||
/// <returns>Result of evaluation</returns>
|
||||
public float Evaluate()
|
||||
{
|
||||
// Parse expression first
|
||||
@ -303,6 +358,7 @@ namespace GraphingCalculator
|
||||
|
||||
return stack.Pop();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper routines
|
||||
|
12
Game/Assets/Scripts/Utils/Expression.cs.meta
Normal file
12
Game/Assets/Scripts/Utils/Expression.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9376fb88fdf55af4db8c52c23079dad3
|
||||
timeCreated: 1431078213
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user