Skip to content

Commit

Permalink
Puntos de interes por altura y UN bioma
Browse files Browse the repository at this point in the history
  • Loading branch information
sarais02 committed May 21, 2024
1 parent 5808f2e commit b8c46e2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 44 deletions.
73 changes: 39 additions & 34 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ MonoBehaviour:
m_LightCookieSize: {x: 1, y: 1}
m_LightCookieOffset: {x: 0, y: 0}
m_SoftShadowQuality: 1
--- !u!1 &464554083
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 464554084}
m_Layer: 0
m_Name: Mapa3D
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &464554084
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 464554083}
m_LocalRotation: {x: 0, y: -0, z: -0.0000000074505797, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1874351697}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &574417061
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -445,37 +476,6 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 1821906454}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1492810307
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1492810308}
m_Layer: 0
m_Name: Mapa3D
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1492810308
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1492810307}
m_LocalRotation: {x: 0, y: -0, z: -0.0000000074505797, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1874351697}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1821906454
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -622,7 +622,7 @@ PrefabInstance:
type: 3}
propertyPath: gameObjectMap3D
value:
objectReference: {fileID: 1492810307}
objectReference: {fileID: 0}
- target: {fileID: 1822892296130974850, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
propertyPath: generateObjects
Expand All @@ -636,7 +636,7 @@ PrefabInstance:
- target: {fileID: 1822892296130974850, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
propertyPath: generateInterestPoints
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1822892296130974850, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
Expand Down Expand Up @@ -778,6 +778,11 @@ PrefabInstance:
propertyPath: m_Name
value: Map Generator
objectReference: {fileID: 0}
- target: {fileID: 3386608439140938058, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
propertyPath: m_TagString
value: MapGenerator
objectReference: {fileID: 0}
- target: {fileID: 5056276323852144433, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
propertyPath: m_Name
Expand Down Expand Up @@ -855,6 +860,6 @@ PrefabInstance:
- targetCorrespondingSourceObject: {fileID: 5234133774820441089, guid: 8472f537986b14f4eb239dfd57fe774d,
type: 3}
insertIndex: -1
addedObject: {fileID: 1492810308}
addedObject: {fileID: 464554084}
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 8472f537986b14f4eb239dfd57fe774d, type: 3}
3 changes: 3 additions & 0 deletions Assets/ScriptableObjects/Interest Points/Castillo.asset
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ MonoBehaviour:
type: 3}
amount: 3
radius: 50
maxHeight: 3
minHeight: 2
biome: {fileID: 11400000, guid: c354ecbb187b1fd419c79003be6d009c, type: 2}
interpolation: 0
50 changes: 47 additions & 3 deletions Assets/Scripts/Procedural/Algorithm/PoissonDiscSampler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using UnityEditor.PackageManager.UI;
using UnityEngine;
using UnityEngine.Profiling;


/// <summary>
Expand Down Expand Up @@ -27,7 +29,11 @@ public class PoissonDiscSampler
/// </summary>
private readonly float cellSize;


private int maxHeight;
private int minHeight;
private MapGenerator mapGen;
private MapInfo mapInfo;
private Biome biome;
/// <summary>
/// Lista de posiciones para los objetos generados
/// </summary>
Expand All @@ -39,15 +45,20 @@ public class PoissonDiscSampler
/// <param name="size"> Anchura del mapa generado</param>
/// <param name="size"> Longitud de mapa generado</param>
/// <param name="radius_"> Cada objeto estará a una distancia mínima de `radio` de cualquier otra muestra, y como máximo a 2 * `radio`.</param>
public PoissonDiscSampler(float size, float radius_, int amount)
public PoissonDiscSampler(float size, float radius_, int amount, int maxHeight_, int minHeight_, MapInfo mapInfo_, MapGenerator mapGen_, Biome biome_)
{
this.amount = amount;
rect = new Rect(0, 0, size, size);
radius = radius_;
cellSize = radius_ / Mathf.Sqrt(2);
grid = new int[Mathf.CeilToInt(size / cellSize), Mathf.CeilToInt(size / cellSize)];
maxHeight = maxHeight_*10;
minHeight = minHeight_*10;
mapInfo = mapInfo_;
mapGen = mapGen_;

spawnPoints.Add(new Vector2(size/2, size/2));
spawnPoints.Add(new Vector2(size / 2, size / 2));
biome = biome_;
}

/// <summary>
Expand Down Expand Up @@ -84,15 +95,48 @@ public List<Vector2> Samples()

return points;
}

public Vector2 NewPoint(Vector2 p)
{
points.Remove(p);
spawnPoints.Remove(p);

int spawnIndex = Random.Range(0, spawnPoints.Count);
Vector2 spawnCentre = spawnPoints[spawnIndex];
bool candidateAccepted = false;

for (int i = 0; i < amount; i++)
{
float angle = Random.value * Mathf.PI * 2;
Vector2 dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
Vector2 candidate = spawnCentre + dir * Random.Range(radius, 2 * radius);
if (IsValid(candidate) && points.Count < amount)
{
points.Add(candidate);
spawnPoints.Add(candidate);
grid[(int)(candidate.x / cellSize), (int)(candidate.y / cellSize)] = points.Count;
candidateAccepted = true;
return candidate;
}
}
if (!candidateAccepted)
{
spawnPoints.RemoveAt(spawnIndex);
}
return p;
}
/// <summary>
/// Comprobar la disponibilidad y proximidad de la posicion candidata
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
bool IsValid(Vector2 candidate)
{

if (candidate.x >= 0 && candidate.x < rect.width && candidate.y >= 0 && candidate.y < rect.height)
{
float z = mapInfo.HeightMap[(int)candidate.x, (int)candidate.y];
if (z > maxHeight || z < minHeight || biome != mapGen.biomeGenerator.GetBiomeAt((int)candidate.x, (int)candidate.y)) return false;
int cellX = (int)(candidate.x / cellSize);
int cellY = (int)(candidate.y / cellSize);
int searchStartX = Mathf.Max(0, cellX - 2);
Expand Down
25 changes: 20 additions & 5 deletions Assets/Scripts/Procedural/BasicElements/InterestPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public enum Type_of_POI
[SerializeField]
public float radius;

[Tooltip("Altura maxima a la que se va a generar")]
[SerializeField]
public int maxHeight;

[Tooltip("Altura minima a la que se va a generar")]
[SerializeField]
public int minHeight;

[Tooltip("Bioma en el que aparecerá ese objeto")]
[SerializeField]
public Biome biome;

/// <summary>
/// Interpolación entre Puntos de Interés
/// </summary>
Expand All @@ -41,21 +53,24 @@ public enum Type_of_POI

private PoissonDiscSampler poissonDisc;


//Se deberia generar el objeto dentro del Chunk correspondiente dependiendo de la posicion
public void Generate(int mapSize, MapInfo map, float sizePerBlock, int chunkSize, Transform grandParent)
{
HashSet<Vector2> objectPositions = map.getObjects();
poissonDisc = new PoissonDiscSampler(mapSize, radius,amount);
MapGenerator mapGen = GameObject.FindGameObjectWithTag("MapGenerator").GetComponent<MapGenerator>();
poissonDisc = new PoissonDiscSampler(mapSize, radius,amount, maxHeight, minHeight, map, mapGen, biome);
List<Vector2> points = poissonDisc.Samples();
var parent = new GameObject("InterestPoint_" + this.name);
parent.transform.parent = grandParent;
foreach (Vector2 sample in points)
{
float z = map.HeightMap[(int)sample.x, (int)sample.y];
Vector3 pos = new Vector3(sample.x * sizePerBlock - chunkSize / 2 + 1,
map.HeightMap[(int)sample.x,
(int)sample.y], -sample.y * sizePerBlock + chunkSize / 2 - 1);
Vector3 pos = new Vector3(sample.x * sizePerBlock - chunkSize / 2 + 1,
map.HeightMap[(int)sample.x, (int)sample.y],
-sample.y * sizePerBlock + chunkSize / 2 - 1);

objectPositions.Add(pos);

Instantiate(objectInstance, pos, Quaternion.identity, parent.transform);
}
map.SetObjectsMap(objectPositions);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Procedural/MapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public enum ALgorithm
public Vector2 offset;

[SerializeField]
BiomeGenerator biomeGenerator;
public BiomeGenerator biomeGenerator;

public InterestPoint[] interestPoints;

Expand Down
3 changes: 2 additions & 1 deletion ProjectSettings/TagManager.asset
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
--- !u!78 &1
TagManager:
serializedVersion: 2
tags: []
tags:
- MapGenerator
layers:
- Default
- TransparentFX
Expand Down

0 comments on commit b8c46e2

Please sign in to comment.