Skip to content

Commit

Permalink
improved chunk generation (now 1 chunk at a time)
Browse files Browse the repository at this point in the history
  • Loading branch information
tddebart committed Mar 31, 2022
1 parent d8b521a commit 95cf531
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 87 deletions.
170 changes: 143 additions & 27 deletions Assets/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,120 @@ RectTransform:
m_AnchoredPosition: {x: 20, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &647558308
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 647558313}
- component: {fileID: 647558312}
- component: {fileID: 647558311}
- component: {fileID: 647558310}
- component: {fileID: 647558309}
m_Layer: 0
m_Name: Plane (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &647558309
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647558308}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6b46bf24d89a16b42914d86f13716d3a, type: 3}
m_Name:
m_EditorClassIdentifier:
settings: {fileID: 11400000, guid: 0e63362b226ea1b4aaf0207cff8a5abd, type: 2}
resolution: 250
_renderer: {fileID: 647558311}
--- !u!64 &647558310
MeshCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647558308}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 4
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &647558311
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647558308}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 257
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &647558312
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647558308}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &647558313
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647558308}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 500, y: 100, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &739261251
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -3020,7 +3134,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e113280f9ab044168541f6fef84d3095, type: 3}
m_Name:
m_EditorClassIdentifier:
renderDistance: 2
renderDistance: 1
chunkSize: 16
chunkHeight: 16
worldHeight: 96
Expand All @@ -3035,6 +3149,7 @@ MonoBehaviour:
blockLightMultiplier: 1.18
dayColor: {r: 0.46666667, g: 0.8156863, b: 0.9137255, a: 1}
nightColor: {r: 0.12591669, g: 0.22711694, b: 0.254717, a: 1}
disabled: 0
OnWorldCreated:
m_PersistentCalls:
m_Calls:
Expand Down Expand Up @@ -3065,6 +3180,7 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
validateDone: 1
chunksToUpdate: []
--- !u!4 &1422854026
Transform:
Expand Down Expand Up @@ -3444,7 +3560,7 @@ MonoBehaviour:
playerChunkPosition: {x: 0, y: 0, z: 0}
playerSpawned: 0
world: {fileID: 1422854025}
detectionTime: 1
detectionTime: 0.01
--- !u!4 &1493713240
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -3733,31 +3849,31 @@ MonoBehaviour:
m_EditorClassIdentifier:
biomeGenerator: {fileID: 975012410}
biomeCenters:
- {x: 78, y: 0, z: 77}
- {x: -203, y: 0, z: -180}
- {x: -159, y: 0, z: -70}
- {x: -179, y: 0, z: 10}
- {x: -152, y: 0, z: 84}
- {x: -143, y: 0, z: 200}
- {x: -121, y: 0, z: -188}
- {x: -67, y: 0, z: -81}
- {x: -87, y: 0, z: -28}
- {x: -83, y: 0, z: 87}
- {x: -61, y: 0, z: 138}
- {x: -11, y: 0, z: -159}
- {x: 0, y: 0, z: -99}
- {x: -3, y: 0, z: -10}
- {x: -5, y: 0, z: 75}
- {x: -19, y: 0, z: 185}
- {x: 78, y: 0, z: -172}
- {x: 83, y: 0, z: -68}
- {x: 91, y: 0, z: -44}
- {x: 92, y: 0, z: 154}
- {x: 162, y: 0, z: -190}
- {x: 171, y: 0, z: -109}
- {x: 158, y: 0, z: 1}
- {x: 150, y: 0, z: 79}
- {x: 146, y: 0, z: 139}
- {x: 77, y: 0, z: 77}
- {x: -163, y: 0, z: -163}
- {x: -163, y: 0, z: -89}
- {x: -163, y: 0, z: -35}
- {x: -163, y: 0, z: 77}
- {x: -163, y: 0, z: 139}
- {x: -83, y: 0, z: -177}
- {x: -83, y: 0, z: -74}
- {x: -83, y: 0, z: -27}
- {x: -83, y: 0, z: 75}
- {x: -83, y: 0, z: 173}
- {x: -3, y: 0, z: -163}
- {x: -3, y: 0, z: -68}
- {x: -3, y: 0, z: 36}
- {x: -3, y: 0, z: 60}
- {x: -3, y: 0, z: 169}
- {x: 77, y: 0, z: -163}
- {x: 77, y: 0, z: -66}
- {x: 77, y: 0, z: 29}
- {x: 77, y: 0, z: 131}
- {x: 157, y: 0, z: -176}
- {x: 157, y: 0, z: -45}
- {x: 157, y: 0, z: -17}
- {x: 157, y: 0, z: 74}
- {x: 157, y: 0, z: 182}
temperatureNoiseSettings: {fileID: 11400000, guid: 0e63362b226ea1b4aaf0207cff8a5abd,
type: 2}
domainWarping: {fileID: 1658937130}
Expand Down
8 changes: 8 additions & 0 deletions Assets/_Scripts/Entity's/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public void CalculateVelocity()
velocity.y = CheckUpCollision(velocity.y);
}

// If our feet are in a block we we shouldn't fall
if (world.GetBlock(transform.position).BlockData.generateCollider)
{
velocity.y = 0;
isGrounded = true;
verticalMomentum = 0;
}

}


Expand Down
7 changes: 6 additions & 1 deletion Assets/_Scripts/Entity's/Player/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ private void SetCurrentChunkCenter()
currentChunkCenter.y = playerChunkPosition.y + world.chunkHeight / 2;
}

public Vector3Int GetCurrentChunkCenter()
{
return WorldDataHelper.GetChunkPosition(world, Vector3Int.RoundToInt(localPlayer.transform.position));
}

private IEnumerator CheckForChunkLoading()
{
yield return new WaitForSeconds(detectionTime);
yield return null;
if (
Mathf.Abs(currentChunkCenter.x - localPlayer.transform.position.x) > world.chunkSize / 2 ||
Mathf.Abs(currentChunkCenter.z - localPlayer.transform.position.z) > world.chunkSize / 2 ||
Expand Down
1 change: 1 addition & 0 deletions Assets/_Scripts/Entity's/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public Block TargetedBlock(float tReach,out Vector3Int lastGlobalPos)
{
var pos = cam.position + cam.forward * step;
var block = world.GetBlock(pos);
if (block == null) return null;
if (BlockDataManager.blockTypeDataDictionary[(int)block.type].generateCollider)
{
return block;
Expand Down
7 changes: 5 additions & 2 deletions Assets/_Scripts/F3MenuManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public void Update()
CreateF3Text("Target", "Targeted Block: " + blockPos.x + ", " + blockPos.y + ", " + blockPos.z, false);
CreateF3Text("BlockType", targetedBlock.type.ToString(), false);
var frontBlock = World.Instance.GetBlock(frontGlobalBlockPos);
CreateF3Text("BlockLight", "Block light level: " + frontBlock.GetBlockLight(), false);
CreateF3Text("SkyLight", "Sky light level: " + frontBlock.GetSkyLight(), false);
if (frontBlock != null)
{
CreateF3Text("BlockLight", "Block light level: " + frontBlock.GetBlockLight(), false);
CreateF3Text("SkyLight", "Sky light level: " + frontBlock.GetSkyLight(), false);
}
}
else
{
Expand Down
7 changes: 6 additions & 1 deletion Assets/_Scripts/NoiseVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public void UpdateTexture()
{
for (var y = 0; y < resolution; y++)
{
var color = Color.Lerp(Color.black, Color.white, MyNoise.Redistribution(MyNoise.OctavePerlin(x, y, settings), settings));
var value = MyNoise.OctavePerlin(x, y, settings);
if (value > 0.9f)
{
var g = 0;
}
var color = new Color(value, value, value);
pixs[y * resolution + x] = color;
}
}
Expand Down
15 changes: 11 additions & 4 deletions Assets/_Scripts/World/ChunkData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,17 @@ public MeshData GetMeshData()

public void UpdateLight()
{
Lighting.CalculateSkyLightRemove(this);
Lighting.CalculateSkyLightExtend(this);

Lighting.CalculateLight(this);
try
{
Lighting.CalculateSkyLightRemove(this);
Lighting.CalculateSkyLightExtend(this);

Lighting.CalculateLight(this);
}
catch (Exception e)
{
Debug.LogError(e);
}
}

public void CalculateBlockLight()
Expand Down
2 changes: 2 additions & 0 deletions Assets/_Scripts/World/Lighting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public static void BlockSunRay(ChunkData data, Block block)

public static void RemoveSkyLight(ChunkData data, Block block, int oldLightValue)
{
if (block == null) return;

if (oldLightValue > 0)
{
oldLightValue--;
Expand Down
8 changes: 5 additions & 3 deletions Assets/_Scripts/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public partial class World : MonoBehaviour
[Space]
public Color dayColor;
public Color nightColor;
private bool disabled;
[HideInInspector]
public bool disabled;

private CancellationTokenSource taskTokenSource = new CancellationTokenSource();

Expand All @@ -54,7 +55,7 @@ public partial class World : MonoBehaviour

public Dictionary<Vector3Int, BlockType> blocksToPlaceAfterGeneration = new Dictionary<Vector3Int, BlockType>();

private bool validateDone;
public bool validateDone;


private void Awake()
Expand Down Expand Up @@ -164,6 +165,7 @@ public void SetBlock(Vector3Int blockPos, BlockType blockType)
{
var chunkPos = WorldDataHelper.GetChunkPosition(this, blockPos);
var chunk = WorldDataHelper.GetChunk(this, chunkPos);
if (chunk == null) return;
chunk.ModifiedByPlayer = true;
SetBlock(chunk, blockPos, blockType);
}
Expand Down Expand Up @@ -249,7 +251,7 @@ public Block GetBlock(Vector3Int globalPos)

if (containerChunk == null)
{
return null;
return BlockHelper.NOTHING;
}

var blockPos = containerChunk.GetLocalBlockCoords(new Vector3Int(globalPos.x, globalPos.y, globalPos.z));
Expand Down
20 changes: 10 additions & 10 deletions Assets/_Scripts/World/WorldDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public static Vector3Int GetChunkPosition(World world, Vector3Int worldSpacePos)

public static List<Vector3Int> GetChunkPositionsInRenderDistance(World world, Vector3Int playerPos)
{
var startX = playerPos.x - world.renderDistance * world.chunkSize;
var startZ = playerPos.z - world.renderDistance * world.chunkSize;
var endX = playerPos.x + world.renderDistance * world.chunkSize;
var endZ = playerPos.z + world.renderDistance * world.chunkSize;
var startX = playerPos.x - Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8) * world.chunkSize;
var startZ = playerPos.z - Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8) * world.chunkSize;
var endX = playerPos.x + Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8) * world.chunkSize;
var endZ = playerPos.z + Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8) * world.chunkSize;

return GetPositionsInRenderDistance(world,playerPos, startX, startZ, endX, endZ);
}

public static List<Vector3Int> GetDataPositionsInRenderDistance(World world, Vector3Int playerPos)
{
var startX = playerPos.x - (world.renderDistance+1) * world.chunkSize;
var startZ = playerPos.z - (world.renderDistance+1) * world.chunkSize;
var endX = playerPos.x + (world.renderDistance+1) * world.chunkSize;
var endZ = playerPos.z + (world.renderDistance+1) * world.chunkSize;
var startX = playerPos.x - (Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8)+1) * world.chunkSize;
var startZ = playerPos.z - (Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8)+1) * world.chunkSize;
var endX = playerPos.x + (Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8)+1) * world.chunkSize;
var endZ = playerPos.z + (Mathf.Min(world.renderDistance, World.Instance.IsWorldCreated ? world.renderDistance : 8)+1) * world.chunkSize;

return GetPositionsInRenderDistance(world,playerPos, startX, startZ, endX, endZ);
}
Expand Down Expand Up @@ -68,15 +68,15 @@ public static HashSet<Vector3Int> GetPositionsToCreate(WorldData worldData, List
{
return allChunkPositionsNeeded
.Where(pos => !worldData.chunkDict.ContainsKey(pos))
.OrderBy(pos => Vector3.Distance(playerPos, pos))
.OrderBy(pos => Vector3.Distance(playerPos, pos)).Take(World.Instance.IsWorldCreated ? World.Instance.chunksGenerationPerFrame : allChunkPositionsNeeded.Count)
.ToHashSet();
}

public static HashSet<Vector3Int> GetDataPositionsToCreate(WorldData worldData, List<Vector3Int> allChunkDataPositionsNeeded, Vector3Int playerPos)
{
return allChunkDataPositionsNeeded
.Where(pos => !worldData.chunkDataDict.ContainsKey(pos))
.OrderBy(pos => Vector3.Distance(playerPos, pos))
.OrderBy(pos => Vector3.Distance(playerPos, pos)).Take(World.Instance.IsWorldCreated ? 9 + World.Instance.chunksGenerationPerFrame*3 : allChunkDataPositionsNeeded.Count)
.ToHashSet();
}

Expand Down
Loading

0 comments on commit 95cf531

Please sign in to comment.