Skip to content

Commit 8532406

Browse files
committed
Added seed generation
1 parent 55821e0 commit 8532406

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ MonoBehaviour:
470470
m_Script: {fileID: 11500000, guid: ece66b02246203c45951653135a8fb29, type: 3}
471471
m_Name:
472472
m_EditorClassIdentifier:
473+
inputSeed:
474+
seed: 0
473475
blockList: {fileID: 11400000, guid: e11aa234015dacb49ab4a67b64d1ccce, type: 2}
474476
contenentalnessToHeight: {fileID: 11400000, guid: e27670e7568ea9f4c91c9d63ed3db114, type: 2}
475477
useGreedyMeshing: 1

Assets/Voxels/Blocks/Block.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
[CreateAssetMenu(menuName = "VoxelStuff/Block")]
44
public class Block : ScriptableObject
55
{
6-
public byte block_ID;
6+
[HideInInspector] public byte block_ID;
77
public Color vertexColor;
88
}

Assets/Voxels/Scripts/Generation.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
using UnityEngine;
2+
using UnityEngine.InputSystem.Controls;
23

34
public class Generation : MonoBehaviour
45
{
56
public static Generation instance;
7+
[SerializeField] string inputSeed;
8+
[HideInInspector] public long seed;
69

10+
711
public static float BLOCK_SIZE = 0.25f;
812
public BlockList blockList;
913
public Spline contenentalnessToHeight;
@@ -19,19 +23,42 @@ private void Awake()
1923
}
2024
private void Start()
2125
{
26+
GenerateSeed();
27+
2228
contentalness_1 = new Noise(Chunk.CHUNK_WIDTH, Chunk.CHUNK_LENGTH, 0.075f, Noise.NoiseType.PERLIN);
2329
contentalness_2 = new Noise(Chunk.CHUNK_WIDTH, Chunk.CHUNK_LENGTH, 0.1f, Noise.NoiseType.SIMPLEX);
2430
contentalness_3 = new Noise(Chunk.CHUNK_WIDTH, Chunk.CHUNK_LENGTH, 1f, Noise.NoiseType.SIMPLEX);
2531

2632
Chunk.InititalizeChunks();
2733

2834
}
29-
35+
36+
public void GenerateSeed()
37+
{
38+
if(string.IsNullOrWhiteSpace(inputSeed))
39+
{
40+
System.Random random = new System.Random();
41+
seed = random.Next();
42+
inputSeed = seed.ToString();
43+
}
44+
else
45+
{
46+
int intSeed;
47+
if(int.TryParse(inputSeed, out intSeed))
48+
{
49+
seed = intSeed;
50+
}
51+
else
52+
{
53+
seed = inputSeed.GetHashCode();
54+
}
55+
}
56+
}
3057

3158
public static float GetContenentalness(float xCoord, float zCoord)
3259
{
3360
float low = contentalness_1.GetNoise(xCoord, zCoord, 10); // [0, 10]
34-
float mid = contentalness_2.GetNoise(xCoord, zCoord, 7); // less impact
61+
float mid = contentalness_2.GetNoise(xCoord, zCoord, 8); // less impact
3562
float high = contentalness_3.GetNoise(xCoord,zCoord, 2); // subtle variation
3663

3764
float combined = (low * 0.75f) + (mid * 0.5f) + (high * 0.15f);

Assets/Voxels/Scripts/Noise.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using Unity.Mathematics;
3+
using UnityEngine;
24

35
public class Noise
46
{
@@ -20,14 +22,20 @@ public Noise(float width, float length, float scale, NoiseType noiseType)
2022

2123
public float GetNoise(float x, float y, int expansion)
2224
{
25+
System.Random rand = new System.Random(Generation.instance.seed.GetHashCode()); // Convert the long seed into a unique int
26+
float randNumX = rand.Next(-10000, 10000); // First number in sequence
27+
float randNumY = rand.Next(-10000, 10000); // Second number in sequence
28+
float2 offset = new float2(randNumX, randNumY);
29+
2330
float raw = 0;
31+
float2 value = new float2(x / width, y / length) * scale + offset;
2432
switch (chosenNoiseType)
2533
{
2634
case NoiseType.PERLIN:
27-
raw = noise.cnoise(new float2(x / width * scale, y / length * scale)); // Returns [-1, 1];
35+
raw = noise.cnoise(value); // Returns [-1, 1];
2836
break;
2937
case NoiseType.SIMPLEX:
30-
raw = noise.snoise(new float2(x / width * scale, y / length * scale));
38+
raw = noise.snoise(value);
3139
break;
3240
}
3341

0 commit comments

Comments
 (0)