Started populating generated islands.

WorldChunkRefactoring
Martin Felis 2023-06-11 22:04:22 +02:00
parent fdddc7d2ef
commit f21db2ac4f
37 changed files with 11934 additions and 16 deletions

View File

@ -0,0 +1,5 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/grass.glb" type="PackedScene" id=1]
[node name="grass" instance=ExtResource( 1 )]

View File

@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/grassLarge.glb" type="PackedScene" id=1]
[node name="grassLarge" instance=ExtResource( 1 )]
[node name="grassLarge" parent="." index="0"]
transform = Transform( 1.5, 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0, 0, 0 )

View File

@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/rockA.glb" type="PackedScene" id=1]
[sub_resource type="CylinderShape" id=1]
height = 0.736755
radius = 0.4
[node name="rockA" instance=ExtResource( 1 )]
[node name="rockA" parent="." index="0"]
transform = Transform( 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 )
[node name="StaticBody" type="StaticBody" parent="." index="1"]
input_ray_pickable = false
[node name="CollisionShape" type="CollisionShape" parent="StaticBody" index="0"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.373712, 0 )
shape = SubResource( 1 )

View File

@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/rockB.glb" type="PackedScene" id=1]
[sub_resource type="CylinderShape" id=1]
height = 0.679261
radius = 0.4
[node name="rockB" instance=ExtResource( 1 )]
[node name="rockB" parent="." index="0"]
transform = Transform( 1.5, 0, 0, 0, 1.5, 0, 0, 0, 1.6, 0, 0, 0 )
[node name="StaticBody" type="StaticBody" parent="." index="1"]
input_ray_pickable = false
[node name="CollisionShape" type="CollisionShape" parent="StaticBody" index="0"]
transform = Transform( 1, 0, 0, 0, 0.999997, -0.00240855, 0, 0.00240855, 0.999997, 0, 0.376176, 0 )
shape = SubResource( 1 )

View File

@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/rockC.glb" type="PackedScene" id=1]
[sub_resource type="CylinderShape" id=1]
height = 1.38848
radius = 0.4
[node name="rockC" instance=ExtResource( 1 )]
[node name="rockC" parent="." index="0"]
transform = Transform( 1.7, 0, 0, 0, 1.7, 0, 0, 0, 1.7, 0, 0, 0 )
[node name="StaticBody" type="StaticBody" parent="." index="1"]
input_ray_pickable = false
[node name="CollisionShape" type="CollisionShape" parent="StaticBody" index="0"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.746043, 0 )
shape = SubResource( 1 )

View File

@ -0,0 +1,22 @@
Survival Kit 1.1
Created/distributed by Kenney (www.kenney.nl)
Creation date: 25-05-2022 11:00
------------------------------
License: (Creative Commons Zero, CC0)
http://creativecommons.org/publicdomain/zero/1.0/
This content is free to use in personal, educational and commercial projects.
Support us by crediting Kenney or www.kenney.nl (this is not mandatory)
------------------------------
Donate: http://support.kenney.nl
Patreon: http://patreon.com/kenney/
Follow on Twitter for updates:
http://twitter.com/KenneyNL

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -16,12 +16,10 @@ vec3 color_ramp(float h) {
return vec3(0.8, 0.8, 0.1);
} else if (h < 0.6){
return vec3(0, 0.6, 0);
} else if (h < 0.68){
} else if (h < 0.78){
return vec3(0, 0.4, 0);
} else if (h <= 0.78){
return vec3(0.6, 0.6, 0.6);
} else if (h < 1.0){
return vec3(0.8, 0.8, 0.8);
return vec3(0.5, 0.5, 0.4);
}
return vec3(1.0);

View File

@ -18,12 +18,10 @@ vec3 color_ramp(float h) {
return vec3(0.8, 0.8, 0.1);
} else if (h < 0.65){
return vec3(0, 0.6, 0);
} else if (h < 0.75){
return vec3(0, 0.4, 0);
} else if (h <= 0.85){
return vec3(0.6, 0.6, 0.6);
return vec3(0, 0.4, 0);
} else if (h < 1.0){
return vec3(0.8, 0.8, 0.8);
return vec3(0.6, 0.6, 0.6);
}
return vec3(1.0);

View File

@ -199,7 +199,6 @@ margin_left = 10.0
margin_top = 10.0
margin_right = 110.0
margin_bottom = 258.0
alignment = 2
script = ExtResource( 12 )
[node name="HBoxContainer" type="HBoxContainer" parent="Generator Container/WorldGeneratorContainer"]
@ -228,6 +227,7 @@ margin_bottom = 40.0
text = "Generate"
[node name="WorldTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"]
visible = false
margin_top = 44.0
margin_right = 100.0
margin_bottom = 144.0
@ -236,6 +236,7 @@ stretch_mode = 1
flip_v = true
[node name="HeightTextureRect" type="TextureRect" parent="Generator Container/WorldGeneratorContainer"]
visible = false
margin_top = 148.0
margin_right = 100.0
margin_bottom = 248.0

View File

@ -2,6 +2,8 @@ using Godot;
using System;
using System.Linq;
using System.Diagnostics;
using Godot.Collections;
using Array = Godot.Collections.Array;
using Vector2 = Godot.Vector2;
using Vector3 = Godot.Vector3;
@ -11,9 +13,14 @@ public class TileWorld : Spatial
{
Heightmap,
Color,
Objects,
Done
}
// constants
private static readonly Color RockColor = new Color (0.5f, 0.5f, 0.4f, 1f);
private static readonly Color GrassColor = new Color (0, 0.4f, 0, 1f);
private GenerationState _currentGenerationState = GenerationState.Heightmap;
// signals
@ -34,7 +41,9 @@ public class TileWorld : Spatial
private TextureRect _worldOffscreenTextureRect;
private Viewport _heightmapOffscreenViewport;
private TextureRect _heightmapOffscreenTextureRect;
private Array<Spatial> _rockAssets = new Array<Spatial>();
private Array<Spatial> _grassAssets = new Array<Spatial>();
private Spatial _environmentNode;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
@ -56,6 +65,21 @@ public class TileWorld : Spatial
Debug.Assert(_heightmapOffscreenTextureRect != null);
_heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size));
GetNode<Spatial>("Assets").Visible = false;
foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren())
{
_rockAssets.Add(asset);
}
foreach (Spatial asset in GetNode<Node>("Assets/Grass").GetChildren())
{
_grassAssets.Add(asset);
}
_environmentNode = GetNode<Spatial>("Environment");
Generate(Size);
}
@ -66,6 +90,8 @@ public class TileWorld : Spatial
Size = size;
_worldOffscreenViewport.Size = new Vector2(size, size);
_heightmapOffscreenViewport.Size = new Vector2(size, size);
OnMapGenerationStart();
GenerateNoiseMap();
// GenerateDebugMap();
@ -122,7 +148,14 @@ public class TileWorld : Spatial
OnHeightMapChanged();
}
private void OnMapGenerationStart()
{
foreach (Node child in _environmentNode.GetChildren())
{
child.QueueFree();
}
}
private void OnHeightMapChanged()
{
_currentGenerationState = GenerationState.Heightmap;
@ -134,6 +167,62 @@ public class TileWorld : Spatial
EmitSignal("WorldGenerated");
}
Spatial SelectAsset(Vector2 offsetCoord, Array<Spatial> assets, Random randomGenerator, double probability)
{
if (randomGenerator.NextDouble() < 1.0 - probability)
{
return null;
}
int assetIndex = randomGenerator.Next(assets.Count);
Spatial assetInstance = (Spatial) assets[assetIndex].Duplicate();
Transform assetTransform = Transform.Identity;
assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord);
assetTransform.origin.y += 1.2f;
assetTransform.basis = assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2));
assetInstance.Transform = assetTransform;
return assetInstance;
}
bool IsColorEqualApprox(Color colorA, Color colorB)
{
Vector3 colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b);
return colorDifference.LengthSquared() < 0.1 * 0.1;
}
private void PopulateEnvironment()
{
Random environmentRandom = new Random(Seed);
Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size))
{
foreach (int coord_y in Enumerable.Range(0, Size))
{
Vector2 offsetCoord = new Vector2(coord_x, coord_y);
Color colorValue = Colormap.GetPixel(coord_x, coord_y);
if (IsColorEqualApprox(colorValue, RockColor))
{
Spatial rockAsset = SelectAsset(offsetCoord, _rockAssets, environmentRandom, 0.15);
if (rockAsset != null)
{
_environmentNode.AddChild(rockAsset);
}
} else if (IsColorEqualApprox(colorValue, GrassColor))
{
Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35);
if (grassAsset != null)
{
_environmentNode.AddChild(grassAsset);
}
}
}
}
Colormap.Unlock();
}
public override void _Process(float delta)
{
if (_currentGenerationState == GenerationState.Heightmap)
@ -147,14 +236,17 @@ public class TileWorld : Spatial
else if (_currentGenerationState == GenerationState.Color)
{
Colormap = new Image();
Colormap.Create(Size, Size, false, Image.Format.Rgba8);
Colormap.Create(Size, Size, false, Image.Format.Rgb8);
Colormap.CopyFrom(_worldOffscreenViewport.GetTexture().GetData());
Heightmap.Lock();
_currentGenerationState = GenerationState.Done;
_currentGenerationState = GenerationState.Objects;
PopulateEnvironment();
OnMapGenerationComplete();
}
}
public bool IsOffsetCoordValid(Vector2 offsetCoord)
@ -223,9 +315,10 @@ public class TileWorld : Spatial
{
Vector2 tileCenter = _hexGrid.GetHexCenterFromOffset(offsetCoord);
// TODO: coordinates do not match for bigger maps
return new Vector3(
tileCenter.x,
tileCenter.x - Size * 0.75f * 0.5f,
GetHeightAtOffset(offsetCoord),
tileCenter.y);
tileCenter.y + (Mathf.Sqrt(3) / 2) * Size * 0.5f);
}
}

View File

@ -1,9 +1,13 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=9 format=2]
[ext_resource path="res://scenes/TileWorld.cs" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]
[ext_resource path="res://materials/IslandColorRampShader.tres" type="Material" id=3]
[ext_resource path="res://materials/IslandHeightmapFalloffShader.tres" type="Material" id=4]
[ext_resource path="res://assets/Environment/rockB.tscn" type="PackedScene" id=5]
[ext_resource path="res://assets/Environment/rockC.tscn" type="PackedScene" id=6]
[ext_resource path="res://assets/Environment/rockA.tscn" type="PackedScene" id=7]
[ext_resource path="res://assets/Environment/grassLarge.tscn" type="PackedScene" id=8]
[node name="TileWorld" type="Spatial"]
script = ExtResource( 1 )
@ -44,3 +48,20 @@ material = ExtResource( 4 )
texture = ExtResource( 2 )
[node name="Camera2D" type="Camera2D" parent="HeightmapOffscreenViewport"]
[node name="Assets" type="Spatial" parent="."]
visible = false
[node name="Rocks" type="Spatial" parent="Assets"]
[node name="rockA" parent="Assets/Rocks" instance=ExtResource( 7 )]
[node name="rockB" parent="Assets/Rocks" instance=ExtResource( 5 )]
[node name="rockC" parent="Assets/Rocks" instance=ExtResource( 6 )]
[node name="Grass" type="Spatial" parent="Assets"]
[node name="grassLarge" parent="Assets/Grass" instance=ExtResource( 8 )]
[node name="Environment" type="Spatial" parent="."]