Skip to content
Snippets Groups Projects
Commit f030903b authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Amélioration du script de génération de terrain

parent e5095bf2
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481694, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
......@@ -214,10 +214,13 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 86c7bfd105ba6e14180f833234cbc0b8, type: 3}
m_Name:
m_EditorClassIdentifier:
depth: 20
width: 256
height: 256
nombreDePointsAGenerer: 10
terrain: {fileID: 0}
terrainDeph: 20
heightmapWidth: 256
heightmapHeight: 256
scale: 2
ptProches:
--- !u!1 &705507993
GameObject:
m_ObjectHideFlags: 0
......
/*
République de Clément Geraudie
*/
using UnityEngine;
public class V7 : MonoBehaviour
{
public int depth = 20;
[Header("Initialisation")]
public int nombreDePointsAGenerer = 5;
[Header("Terrain")]
[SerializeField] private Terrain terrain;
public int terrainDeph = 20;
// Constantes pour la heightmap
[SerializeField] private int heightmapWidth = 256;
[SerializeField] private int heightmapHeight = 256;
public int width = 256;
public int height = 256;
public float scale = 2f;
public int[,] values;
public int[,] pointsDuChemin;
public int[] ptProches;
int dim1;
int dim2;
int ptsCheminX;
int ptsCheminY;
//public float offsetX = 100f;
......@@ -19,71 +30,80 @@ public class V7 : MonoBehaviour
void Start()
{
values = new int[2, 5];
dim1 = values.GetLength(0);
dim2 = values.GetLength(1);
for (int i = 0; i < dim1; i++)
// Création du tableau des points aléatoires (chemin)
pointsDuChemin = new int[2, nombreDePointsAGenerer];
// Récupère les dimensions du tableau de manière optimisée
ptsCheminX = pointsDuChemin.GetLength(0);
ptsCheminY = pointsDuChemin.GetLength(1);
// On va générer les points de sorte à ce qu'il ne puissent pas se croiser
// On va découper le terrain en N partie, n étant le nombre de pts à générer
int lastBorder = 0;
int newBorder;
for (int x = 0; x < ptsCheminX; x++)
{
for (int j = 0; j < dim2; j++)
for (int y = 0; y < ptsCheminY; y++)
{
values[i, j] = Random.Range(0, width);
if(x==1){
// Si i = 0, ça veut dire qu'on est sur l'axe Y du terrain
newBorder = (heightmapWidth / nombreDePointsAGenerer) + lastBorder; // On calcule dynamiquement la taille de la zone de terrain où l'on va générer le pt
pointsDuChemin[x, y] = Random.Range(lastBorder, newBorder); // On génère un pt aléatoire dans la zone définie
// Debug.Log("Point Y=" + pointsDuChemin[x, y]);
lastBorder = newBorder; // On met à jour la zone de terrain où l'on va générer le pt
}else{
// Normalement, on est sur 0 (c'set un tableau de taille 2)
pointsDuChemin[x, y] = Random.Range(0, heightmapWidth);
// Debug.Log("Point X=" + pointsDuChemin[x, y]);
}
}
}
void Update()
{
Terrain terrain = GetComponent<Terrain>();
// On va récupérer le terrain et on va générer sa forme
terrain = GetComponent<Terrain>();
terrain.terrainData = GenerateTerrain(terrain.terrainData);
// On va faire quelques vérifications
if(terrain == null)
{
Debug.LogError("Terrain non trouvé");
return;
}
if(terrain.terrainData == null)
{
Debug.LogError("TerrainData non trouvé");
return;
}
// On va tracer le chemin
tracerChemin();
}
TerrainData GenerateTerrain(TerrainData terrainData)
{
terrainData.heightmapResolution = width + 1;
terrainData.heightmapResolution = heightmapWidth + 1;
terrainData.size = new Vector3(width, depth, height);
terrainData.size = new Vector3(heightmapWidth, terrainDeph, heightmapHeight);
terrainData.SetHeights(0, 0, GenerateHeights());
// offsetX += Time.deltaTime * 5f;
return terrainData;
}
bool PisteTest(int x, int y)
{
int ptLePlusProche;
for (int i = 0; i < dim2; i++)
{
if (x == values[0, i] && y == values[1, i])
{
int x1 = values[0, i];
int y1 = values[1, i];
int x2 = values[0, ptProches[i]];
int y2 = values[1, ptProches[i]];
return true;
}
}
return false;
}
void ptLePlusProche()
/*void ptLePlusProche()
{
int ptProche = -1;
double resultPtProche = 256;
double resultPtActuel = 256;
for (int i = 0; i < dim2; i++)
ptProches = new int[ptsCheminY];
for (int i = 0; i < ptsCheminY; i++)
{
for (int j = 0; j < dim2; j++)
for (int j = 0; j < ptsCheminY; j++)
{
if (values[0, j] != values[0, i] || values[1, j] != values[1, i])
if (pointsDuChemin[0, j] != pointsDuChemin[0, i] || pointsDuChemin[1, j] != pointsDuChemin[1, i])
{
int x1 = values[0, i];
int y1 = values[1, i];
int x2 = values[0, j];
int y2 = values[1, j];
int x1 = pointsDuChemin[0, i];
int y1 = pointsDuChemin[1, i];
int x2 = pointsDuChemin[0, j];
int y2 = pointsDuChemin[1, j];
resultPtActuel = Mathf.Sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2);
if (resultPtActuel < resultPtProche)
{
......@@ -94,40 +114,49 @@ public class V7 : MonoBehaviour
}
ptProches[i] = ptProche;
}
// Debug liste de ptProches
for (int i = 0; i < ptsCheminY; i++)
{
Debug.Log("ptProches[" + i + "] = " + ptProches[i]);
}
}*/
bool tracerChemin(int x1, int y1, int x2, int y2, int x, int y)
// Tracer chemin va se charger de dessinner le chemin au sol
private void tracerChemin()
{
double m = (y2 - y1) / (x2 - x1);
double p = y - m * x;
bool equation = m == (p - y) / x;
return equation;
// On va parcourir les pointsDuChemin et tracer le chemin entre ces points
}
float[,] GenerateHeights()
{
float[,] heights = new float[width, height];
// On initialise le tableau
float[,] terrainHeightsList = new float[heightmapWidth, heightmapHeight];
for (int x = 0; x < width; x++)
// On remplit le tableau
for (int widthIndex = 0; widthIndex < heightmapWidth; widthIndex++)
{
for (int y = 0; y < height; y++)
for (int heightIndex = 0; heightIndex < heightmapHeight; heightIndex++)
{
heights[x, y] = CalculateHeight(x, y);
// On calcule hauteur
terrainHeightsList[widthIndex, heightIndex] = CalculateHeight(widthIndex, heightIndex);
}
}
for (int i = 0; i < dim2; i++)
// Pour chaque point à générer, on va définir la hauteur à 0
for (int i = 0; i < nombreDePointsAGenerer; i++)
{
heights[values[0, i], values[1, i]] = 0;
terrainHeightsList[pointsDuChemin[0, i], pointsDuChemin[1, i]] = 0; // terrainHeightsList[ ptPosX, ptPosY ] = 0;
Debug.Log("Point: "+pointsDuChemin[0, i] + ";" + pointsDuChemin[1, i]);
}
return heights;
return terrainHeightsList;
}
// Renvoie une valeur semi-aléaoite (perlin noise) entre un point x et y
float CalculateHeight(int x, int y)
{
float xCoord = (float)x / width * scale;//+ offsetX;
float yCoord = (float)y / height * scale;//+ offsetY;
float xCoord = (float)x / heightmapWidth * scale;//+ offsetX;
float yCoord = (float)y / heightmapHeight * scale;//+ offsetY;
return Mathf.PerlinNoise(xCoord, yCoord);
}
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment