Added trees

WorldChunkRefactoring
Martin Felis 2023-06-22 18:34:39 +02:00
parent b672d4556a
commit 9896d0896a
4 changed files with 68 additions and 30 deletions

View File

@ -0,0 +1,18 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://assets/KenneySurvivalKit/Models/tree.glb" type="PackedScene" id=1]
[sub_resource type="CapsuleShape" id=1]
radius = 0.329139
height = 0.936801
[node name="tree" instance=ExtResource( 1 )]
[node name="tree" parent="." index="0"]
transform = Transform( 1.5, 0, 0, 0, 1, 0, 0, 0, 1.5, 0, 0, 0 )
[node name="StaticBody" type="StaticBody" parent="." index="1"]
[node name="CollisionShape" type="CollisionShape" parent="StaticBody" index="0"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.519998, 0 )
shape = SubResource( 1 )

View File

@ -266,7 +266,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 )
shape = SubResource( 9 ) shape = SubResource( 9 )
[node name="Camera" type="Camera" parent="."] [node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.60042, 0.799685, 0, -0.799685, 0.60042, -4.76837e-07, 8.3759, 5.70105 ) transform = Transform( 1, 0, 0, 0, 0.60042, 0.799685, 0, -0.799685, 0.60042, -4.76837e-07, 6.37557, 4.57224 )
current = true current = true
fov = 60.0 fov = 60.0
script = ExtResource( 10 ) script = ExtResource( 10 )

View File

@ -18,9 +18,9 @@ public class TileWorld : Spatial
} }
// constants // constants
private static readonly Color RockColor = new Color (0.5f, 0.5f, 0.4f, 1f); 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 static readonly Color GrassColor = new Color(0, 0.4f, 0, 1f);
private GenerationState _currentGenerationState = GenerationState.Heightmap; private GenerationState _currentGenerationState = GenerationState.Heightmap;
// signals // signals
@ -43,6 +43,7 @@ public class TileWorld : Spatial
private TextureRect _heightmapOffscreenTextureRect; private TextureRect _heightmapOffscreenTextureRect;
private Array<Spatial> _rockAssets = new Array<Spatial>(); private Array<Spatial> _rockAssets = new Array<Spatial>();
private Array<Spatial> _grassAssets = new Array<Spatial>(); private Array<Spatial> _grassAssets = new Array<Spatial>();
private Array<Spatial> _treeAssets = new Array<Spatial>();
private Spatial _environmentNode; private Spatial _environmentNode;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
@ -66,7 +67,7 @@ public class TileWorld : Spatial
_heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size)); _heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size));
GetNode<Spatial>("Assets").Visible = false; GetNode<Spatial>("Assets").Visible = false;
foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren()) foreach (Spatial asset in GetNode<Node>("Assets/Rocks").GetChildren())
{ {
_rockAssets.Add(asset); _rockAssets.Add(asset);
@ -77,9 +78,13 @@ public class TileWorld : Spatial
_grassAssets.Add(asset); _grassAssets.Add(asset);
} }
foreach (Spatial asset in GetNode<Node>("Assets/Trees").GetChildren())
{
_treeAssets.Add(asset);
}
_environmentNode = GetNode<Spatial>("Environment"); _environmentNode = GetNode<Spatial>("Environment");
Generate(Size); Generate(Size);
} }
@ -92,7 +97,7 @@ public class TileWorld : Spatial
_heightmapOffscreenViewport.Size = new Vector2(size, size); _heightmapOffscreenViewport.Size = new Vector2(size, size);
OnMapGenerationStart(); OnMapGenerationStart();
GenerateNoiseMap(); GenerateNoiseMap();
// GenerateDebugMap(); // GenerateDebugMap();
@ -109,23 +114,25 @@ public class TileWorld : Spatial
Heightmap.Lock(); Heightmap.Lock();
Colormap.Lock(); Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size)) foreach (int coord_x in Enumerable.Range(0, Size))
{ {
foreach (int coord_y in Enumerable.Range(0, Size)) foreach (int coord_y in Enumerable.Range(0, Size))
{ {
Colormap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1)); Colormap.SetPixel(coord_x, coord_y,
Heightmap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1)); new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
Heightmap.SetPixel(coord_x, coord_y,
new Color((float)Mathf.Min(coord_x, coord_y) / Size, (float)0, 0, 1));
} }
} }
Colormap.SetPixel(Size - 1, Size -1, new Color(1, 1, 1, 1)); Colormap.SetPixel(Size - 1, Size - 1, new Color(1, 1, 1, 1));
Colormap.Unlock(); Colormap.Unlock();
OnMapGenerationComplete(); OnMapGenerationComplete();
} }
private void GenerateNoiseMap() private void GenerateNoiseMap()
{ {
Heightmap = new Image(); Heightmap = new Image();
@ -140,11 +147,11 @@ public class TileWorld : Spatial
noiseGenerator.Lacunarity = 4; noiseGenerator.Lacunarity = 4;
ImageTexture heightmapTexture = new ImageTexture(); ImageTexture heightmapTexture = new ImageTexture();
heightmapTexture.CreateFromImage(noiseGenerator.GetSeamlessImage((int) Size)); heightmapTexture.CreateFromImage(noiseGenerator.GetSeamlessImage((int)Size));
heightmapTexture.Flags = 0; heightmapTexture.Flags = 0;
_heightmapOffscreenTextureRect.Texture = heightmapTexture; _heightmapOffscreenTextureRect.Texture = heightmapTexture;
Heightmap.CopyFrom(_heightmapOffscreenViewport.GetTexture().GetData()); Heightmap.CopyFrom(_heightmapOffscreenViewport.GetTexture().GetData());
OnHeightMapChanged(); OnHeightMapChanged();
} }
@ -155,7 +162,7 @@ public class TileWorld : Spatial
child.QueueFree(); child.QueueFree();
} }
} }
private void OnHeightMapChanged() private void OnHeightMapChanged()
{ {
_currentGenerationState = GenerationState.Heightmap; _currentGenerationState = GenerationState.Heightmap;
@ -174,13 +181,14 @@ public class TileWorld : Spatial
{ {
return null; return null;
} }
int assetIndex = randomGenerator.Next(assets.Count); int assetIndex = randomGenerator.Next(assets.Count);
Spatial assetInstance = (Spatial) assets[assetIndex].Duplicate(); Spatial assetInstance = (Spatial)assets[assetIndex].Duplicate();
Transform assetTransform = Transform.Identity; Transform assetTransform = Transform.Identity;
assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord); assetTransform.origin = GetTileWorldCenterFromOffset(offsetCoord);
assetTransform.origin.y += 1.2f; assetTransform.origin.y += 1.2f;
assetTransform.basis = assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2)); assetTransform.basis =
assetTransform.basis.Rotated(Vector3.Up, (float)(randomGenerator.NextDouble() * Mathf.Pi * 2));
assetInstance.Transform = assetTransform; assetInstance.Transform = assetTransform;
return assetInstance; return assetInstance;
@ -191,11 +199,11 @@ public class TileWorld : Spatial
Vector3 colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b); Vector3 colorDifference = new Vector3(colorA.r - colorB.r, colorA.g - colorB.g, colorA.b - colorB.b);
return colorDifference.LengthSquared() < 0.1 * 0.1; return colorDifference.LengthSquared() < 0.1 * 0.1;
} }
private void PopulateEnvironment() private void PopulateEnvironment()
{ {
Random environmentRandom = new Random(Seed); Random environmentRandom = new Random(Seed);
Colormap.Lock(); Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, Size)) foreach (int coord_x in Enumerable.Range(0, Size))
{ {
@ -211,16 +219,24 @@ public class TileWorld : Spatial
{ {
_environmentNode.AddChild(rockAsset); _environmentNode.AddChild(rockAsset);
} }
} else if (IsColorEqualApprox(colorValue, GrassColor)) }
else if (IsColorEqualApprox(colorValue, GrassColor))
{ {
Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35); Spatial grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.35);
if (grassAsset != null) if (grassAsset != null)
{ {
_environmentNode.AddChild(grassAsset); _environmentNode.AddChild(grassAsset);
} }
Spatial treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.10);
if (treeAsset != null)
{
_environmentNode.AddChild(treeAsset);
}
} }
} }
} }
Colormap.Unlock(); Colormap.Unlock();
} }
@ -243,12 +259,11 @@ public class TileWorld : Spatial
Heightmap.Lock(); Heightmap.Lock();
_currentGenerationState = GenerationState.Objects; _currentGenerationState = GenerationState.Objects;
PopulateEnvironment(); PopulateEnvironment();
OnMapGenerationComplete(); OnMapGenerationComplete();
} }
} }
public bool IsOffsetCoordValid(Vector2 offsetCoord) public bool IsOffsetCoordValid(Vector2 offsetCoord)
@ -290,7 +305,7 @@ public class TileWorld : Spatial
{ {
return 0f; return 0f;
} }
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord); Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
float heightmapHeight = Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r; float heightmapHeight = Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r;
@ -321,6 +336,6 @@ public class TileWorld : Spatial
return new Vector3( return new Vector3(
tileCenter.x - Mathf.Round(Size * 0.75f * 0.5f), tileCenter.x - Mathf.Round(Size * 0.75f * 0.5f),
GetHeightAtOffset(offsetCoord), GetHeightAtOffset(offsetCoord),
tileCenter.y + (Mathf.Sqrt(3) / 2) * Size * 0.5f); tileCenter.y + ((Mathf.Sqrt(3) / 2) * Mathf.Round(Size * 0.5f)));
} }
} }

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=2] [gd_scene load_steps=10 format=2]
[ext_resource path="res://scenes/TileWorld.cs" type="Script" id=1] [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://icon.png" type="Texture" id=2]
@ -8,6 +8,7 @@
[ext_resource path="res://assets/Environment/rockC.tscn" type="PackedScene" id=6] [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/rockA.tscn" type="PackedScene" id=7]
[ext_resource path="res://assets/Environment/grassLarge.tscn" type="PackedScene" id=8] [ext_resource path="res://assets/Environment/grassLarge.tscn" type="PackedScene" id=8]
[ext_resource path="res://assets/Environment/tree.tscn" type="PackedScene" id=9]
[node name="TileWorld" type="Spatial"] [node name="TileWorld" type="Spatial"]
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -64,4 +65,8 @@ visible = false
[node name="grassLarge" parent="Assets/Grass" instance=ExtResource( 8 )] [node name="grassLarge" parent="Assets/Grass" instance=ExtResource( 8 )]
[node name="Trees" type="Spatial" parent="Assets"]
[node name="tree" parent="Assets/Trees" instance=ExtResource( 9 )]
[node name="Environment" type="Spatial" parent="."] [node name="Environment" type="Spatial" parent="."]