Skip to content
Snippets Groups Projects
Commit 2730f7fe authored by raffytaro's avatar raffytaro Committed by Sofiane Lasri
Browse files

ajout de GenTerrain

ajout de genTerrain(version non aleatoire de la route)
parent 8add2701
No related branches found
No related tags found
No related merge requests found
using UnityEngine;
using System.Collections;
public class genTerrainRoute : MonoBehaviour
{
public int depth = 20;
public int width = 256;
public int height = 256;
public float scale = 20f;
public float offsetX = 100f;
public float offsetY = 100f;
[SerializeField] private Texture2D[] mask;
private Terrain terrain;
void Start()
{
offsetX = Random.Range(0f, 9999f);
offsetY = Random.Range(0f, 9999f);
terrain = GetComponent<Terrain>();
}
Terrain oldTerrain;
void Update()
{
if(oldTerrain != terrain)
{
terrain.terrainData = GenerateTerrain(terrain.terrainData);
oldTerrain = terrain;
}
}
TerrainData GenerateTerrain(TerrainData terrainData)
{
terrainData.heightmapResolution = width + 1;
terrainData.size = new Vector3(width, depth, height);
terrainData.SetHeights(0, 0, useMask(GenerateHeights()));
return terrainData;
}
float[,] GenerateHeights()
{
float[,] heights = new float[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y ++)
{
heights[x, y] = CalculateHeight(x, y);
}
}
return heights;
}
float CalculateHeight(int x, int y)
{
float xCoord = (float)x / width * scale + offsetX;
float yCoord = (float)y / height * scale + offsetY;
return Mathf.PerlinNoise(xCoord, yCoord);
}
float[,] useMask(float[,] heights)
{
int index = Random.Range(0, mask.Length);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < width; y++)
{
heights[x, y] = CalculateHeight(x, y);
float grayScale = (float)mask[index].GetPixel((width - 1) - x, y).grayscale;
heights[x, y] *= grayScale;
}
}
return heights;
}
ArrayList drawChemin()
{
ArrayList x = new ArrayList();
ArrayList y = new ArrayList();
ArrayList points = new ArrayList();
for (int i = 0; i < 6; i++)
{
if(i == 0)
{
x.Add(Random.Range(0, 10000));
y.Add(Random.Range(0, 10000));
}
else
{
x.Add(Random.Range((int) x[i]+1, 10000));
y.Add(Random.Range((int)y[i]+1, 10000));
}
}
points.Add(x);
points.Add(y);
return points;
}
}
fileFormatVersion: 2
guid: 2d1ade1b23e121a49afc591b8e28e1ef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment