Skip to content
Snippets Groups Projects
Commit c9ec0d73 authored by Ryan Chang's avatar Ryan Chang
Browse files

Chunking

parent 73c11567
No related branches found
No related tags found
No related merge requests found
......@@ -497,9 +497,23 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d189f22f5738f604690940b663082fa7, type: 3}
m_Name:
m_EditorClassIdentifier:
octaves: []
octaves:
- amplitude: 2
frequency: 0.002
type: 1
- amplitude: 0.5
frequency: 0.01
type: 3
- amplitude: 0.1
frequency: 0.05
type: 3
- amplitude: 0.02
frequency: 0.1
type: 1
initialCrawlPosition: {x: 0, y: 0}
terrainHeight: 1800
generateNewCrawlPosition: 0
refresh: 0
--- !u!4 &438166963
Transform:
m_ObjectHideFlags: 0
......
......@@ -30,8 +30,15 @@ public class TerrainChunker : MonoBehaviour
private TerrainData data;
private int heightResolution = 513;
private float terrainWidth = 1000;
private float heightScale;
private ShapeGeneratorJob shapeGenerator;
private JobHandle runningShape;
private bool lookForCompletion = false;
public void Initialize(TerrainMaster master, Vector2Int gridPosition,
Vector2 crawlOffset)
......@@ -43,17 +50,30 @@ public class TerrainChunker : MonoBehaviour
gameObject.name = $"Chunker ({gridPosition})";
data = Instantiate(Resources.Load<TerrainData>("Template"));
terrain = gameObject.AddComponent<Terrain>();
UpdateTerrain();
heightScale = data.size.y / master.terrainHeight;
data.size = new(data.size.x, master.terrainHeight, data.size.z);
var terrainGO = Terrain.CreateTerrainGameObject(data);
terrainGO.transform.parent = transform;
heightResolution = data.heightmapResolution;
terrainWidth = data.size.x;
UpdatePositions(gridPosition, true);
}
public void UpdatePositions(Vector2Int newGridPosition)
public void UpdatePositions(Vector2Int newGridPosition, bool forceUpdate = false)
{
if (newGridPosition != gridPosition)
print($"Updating {gameObject.name} position.");
if (newGridPosition != gridPosition || forceUpdate)
{
gridPosition = newGridPosition;
Vector2 worldPosition = (Vector2)gridPosition * terrainWidth;
transform.position = new(worldPosition.x, 0, worldPosition.y);
// Now we update the terrain
UpdateTerrain();
}
......@@ -61,26 +81,33 @@ public class TerrainChunker : MonoBehaviour
private void UpdateTerrain()
{
print($"Updating {gameObject.name} terrain.");
if (!runningShape.IsCompleted)
{
// Cancel thread if its running.
shapeGenerator.Cancel();
print($"{gameObject.name} has running job, which has been cancelled.");
}
shapeGenerator = new(crawlOffset, gridPosition, 513, master.octaves);
shapeGenerator = new(crawlOffset, gridPosition, heightResolution,
heightScale, master.octaves);
runningShape = shapeGenerator.Schedule();
lookForCompletion = true;
}
private void ApplyTerrain(float[,] heights)
{
print($"Apply {gameObject.name} terrain.");
data.SetHeights(0, 0, heights);
}
private void Update()
{
if (shapeGenerator.Finished)
if (lookForCompletion && runningShape.IsCompleted)
{
runningShape.Complete();
ApplyTerrain(shapeGenerator.Retrieve());
lookForCompletion = false;
}
}
......
......@@ -11,34 +11,33 @@ public struct ShapeGeneratorJob : IJob
#region Generation variables
private Vector2 crawlOffset;
private Vector2Int gridOffset;
[ReadOnly]
[DeallocateOnJobCompletion]
private NativeArray<Octave> octaves;
private int samples;
private float heightScale;
#endregion
#region Output variables
private NativeArray<float> heights;
#endregion
#region Control variables
public bool Finished { get; private set; }
// public bool Finished { get; private set; }
public bool Cancelled { get; private set; }
#endregion
public ShapeGeneratorJob(Vector2 crawlOffset, Vector2Int gridOffset,
int samples, IEnumerable<Octave> octaves)
public ShapeGeneratorJob(Vector2 initialCrawlPosition, Vector2Int gridOffset,
int samples, float heightScale, IEnumerable<Octave> octaves)
{
this.crawlOffset = crawlOffset;
this.gridOffset = gridOffset;
this.octaves = new(octaves.ToArray(), Allocator.TempJob);
this.crawlOffset = initialCrawlPosition + gridOffset;
this.octaves = new(octaves.ToArray(), Allocator.Persistent);
this.samples = samples;
this.heightScale = heightScale;
heights = new(samples, Allocator.Persistent);
heights = new(samples * samples, Allocator.Persistent);
Finished = false;
// Finished = false;
Cancelled = false;
}
......@@ -53,28 +52,24 @@ public struct ShapeGeneratorJob : IJob
Cancelled = true;
// Why the hell can I not deallocate them in a job.
// octaves.Dispose();
heights.Dispose();
if (heights.IsCreated)
heights.Dispose();
}
public float[,] Retrieve()
{
if (!Finished)
throw new System.ArgumentException("Not finished");
else
float[,] arrHeights = new float[samples, samples];
for (int y = 0; y < samples; y++)
{
float[,] arrHeights = new float[samples, samples];
for (int y = 0; y < samples; y++)
for (int x = 0; x < samples; x++)
{
for (int x = 0; x < samples; x++)
{
arrHeights[y, x] = heights[y * samples + x];
}
arrHeights[y, x] = heights[y * samples + x];
}
heights.Dispose();
return arrHeights;
}
heights.Dispose();
return arrHeights;
}
#endregion
......@@ -110,12 +105,12 @@ public struct ShapeGeneratorJob : IJob
addition = noise.cellular(crawlPos).x / 2.3f + 0.5f;
break;
}
heights[y * samples + x] += addition * octave.amplitude;
heights[y * samples + x] += addition * octave.amplitude * heightScale;
}
}
}
// octaves.Dispose();
Finished = true;
// Finished = true;
}
}
\ No newline at end of file
......@@ -9,10 +9,16 @@ public class TerrainMaster : MonoBehaviour
public List<Octave> octaves;
public Vector2 initialCrawlPosition;
[Tooltip("How tall the terrain is. May be changed to allow for higher terrains.")]
public float terrainHeight = 600;
[SerializeField]
private bool generateNewCrawlPosition;
[SerializeField]
private bool refresh;
[Header("Autogenerated values")]
public Dictionary<Vector2Int, TerrainChunker> chunkers = new();
......@@ -56,5 +62,15 @@ public class TerrainMaster : MonoBehaviour
generateNewCrawlPosition = false;
initialCrawlPosition = RNG.GetRandomVector2(-2500, 2500);
}
if (refresh && Application.isPlaying)
{
refresh = false;
foreach (var chunker in chunkers.Values)
{
chunker.UpdatePositions(chunker.gridPosition, true);
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment