Fixed world height and color texture lookup.

WorldChunkRefactoring
Martin Felis 2023-06-10 17:09:24 +02:00
parent 555982262f
commit 4b153bb6b5
18 changed files with 316 additions and 150 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/4x4checkerPattern.png-73d552959f1dc927e6542b979f612974.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/4x4checkerPattern.png"
dest_files=[ "res://.import/4x4checkerPattern.png-73d552959f1dc927e6542b979f612974.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=true
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=1
process/fix_alpha_border=false
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=false
svg/scale=1.0

BIN
assets/5x5checkerColor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/5x5checkerColor.png-0b102dcaa34ad617f92db40e12de058f.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/5x5checkerColor.png"
dest_files=[ "res://.import/5x5checkerColor.png-0b102dcaa34ad617f92db40e12de058f.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=1
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=1
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=false
svg/scale=1.0

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/5x5checkerPattern.png-1235e6d8801ed94a1c7fab425cf83b94.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://assets/5x5checkerPattern.png"
dest_files=[ "res://.import/5x5checkerPattern.png-1235e6d8801ed94a1c7fab425cf83b94.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=1
process/fix_alpha_border=false
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=false
svg/scale=1.0

View File

@ -1,6 +1,6 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=2] [gd_resource type="ShaderMaterial" load_steps=3 format=2]
[ext_resource path="res://assets/4x4checkerColor.png" type="Texture" id=1] [ext_resource path="res://assets/4x4checkerPattern.png" type="Texture" id=1]
[ext_resource path="res://materials/shader/HexToTexture.gdshader" type="Shader" id=2] [ext_resource path="res://materials/shader/HexToTexture.gdshader" type="Shader" id=2]
[resource] [resource]

View File

@ -0,0 +1,79 @@
shader_type spatial;
render_mode specular_schlick_ggx, async_visible;
uniform sampler2D MapAlbedoTexture : hint_black_albedo;
uniform int TextureSize: hint_range(0, 1024, 4);
varying vec2 map_coord;
varying vec3 obj_coord;
const mat2 _HexAffineInverse = mat2(vec2(1.333333, -0.6666667), vec2(0, -1.154701));
vec3 axial_to_cube(vec2 axial) {
return vec3(axial.x, axial.y, -axial.x - axial.y);
}
ivec3 round_cube_coords(vec3 cube) {
ivec3 rounded = ivec3(round(cube));
vec3 diffs = abs(vec3(rounded) - cube);
if (diffs.x > diffs.y && diffs.x > diffs.z) {
rounded.x = -rounded.y - rounded.z;
} else if (diffs.y > diffs.z) {
rounded.y = -rounded.x - rounded.z;
} else {
rounded.z = -rounded.x - rounded.y;
}
return rounded;
}
vec2 axial_to_offset(vec2 axial) {
ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial));
int x = cubeCoords.x;
int y = cubeCoords.y;
int off_y = y + (x - (x & 1)) / 2;
return vec2(float(x), float(off_y));
}
void vertex() {
// Input:2
mat4 model_matrix = WORLD_MATRIX;
vec3 origin = vec4(WORLD_MATRIX * vec4(0, 0, 0, 1)).xyz;
vec3 axial_coords = vec3(_HexAffineInverse * origin.xz, 0);
vec2 offset_coords = axial_to_offset(axial_coords.xy);
//map_coord = mod(axial_to_offset(axial_coords.xy), vec2(float(TextureSize)));
map_coord = offset_coords;
map_coord.y = -map_coord.y;
float map_size = 4.0;
map_coord = mod(origin.xz, map_size) / (map_size);
//map_coord = mod((round(origin.xz * map_size)) / (map_size) , map_size);
// if (map_coord.x > map_size) {
// map_coord.x = map_coord.x - (map_size + 1.0);
// }
// map_coord.x = map_coord.x / map_size;
// map_coord.x = (mod (map_coord.x, map_size) / (map_size + 1.0));
// map_coord.y = (mod (map_coord.y, map_size) / (map_size + 1.0));
// ivec2 map_coord_i = ivec2(map_coord);
// map_coord_i.x = map_coord_i.x % TextureSize;
// map_coord_i.y = map_coord_i.y % TextureSize;
// map_coord = vec2(map_coord_i) / float(TextureSize);
obj_coord = origin;
}
void fragment() {
//ALBEDO = pow(texture(MapAlbedoTexture, map_coord).rgb, vec3(2.4));
ALBEDO = texture(MapAlbedoTexture, map_coord).rgb;
}
void light() {
// Output:0
}

View File

@ -31,23 +31,26 @@ vec2 axial_to_offset(vec2 axial) {
ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial)); ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial));
int x = cubeCoords.x; int x = cubeCoords.x;
int y = cubeCoords.y; int y = cubeCoords.y;
int off_y = y + (x - (x % 2)) / 2; int off_y = y + (x - (x & 1)) / 2;
return vec2(float(x), float(off_y)); return vec2(float(x), float(off_y));
} }
void vertex() { void vertex() {
// Input:2
mat4 model_matrix = WORLD_MATRIX; mat4 model_matrix = WORLD_MATRIX;
vec3 origin = vec4(WORLD_MATRIX * vec4(0, 0, 0, 1)).xyz; vec3 origin = vec4(WORLD_MATRIX * vec4(0, 0, 0, 1)).xyz;
vec3 axial_coords = vec3(_HexAffineInverse * origin.xz, 0); vec3 axial_coords = vec3(_HexAffineInverse * origin.xz, 0);
map_coord = origin.xz * 1. / float(TextureSize); map_coord = axial_to_offset(axial_coords.xy);
map_coord = axial_to_offset(axial_coords.xy) / float(TextureSize) - vec2(0.5);
} }
void fragment() { void fragment() {
ALBEDO = pow(texture(MapAlbedoTexture, map_coord).rgb, vec3(2.4)); float size = float(TextureSize);
vec2 texel_offset = vec2(ceil(size / 2.0));
ivec2 texel_coord = ivec2(mod(map_coord.xy - texel_offset, vec2(size)));
vec4 texel_value = texelFetch(MapAlbedoTexture, texel_coord, 0);
ALBEDO = texelFetch(MapAlbedoTexture, texel_coord, 0).rgb;
} }
void light() { void light() {

View File

@ -90,6 +90,7 @@ window/stretch/aspect="expand"
[global] [global]
collision=false collision=false
srgb=false
[input] [input]

View File

@ -65,7 +65,7 @@ public class Game : Spatial
_player = GetNode<Player>("Player"); _player = GetNode<Player>("Player");
_chest = GetNode<Chest>("Entities/Chest"); _chest = GetNode<Chest>("Entities/Chest");
_tileWorld = GetNode<TileWorld>("TileWorld"); _tileWorld = GetNode<TileWorld>("TileWorld");
_camera = GetNode<Camera>("Camera"); _camera = GetNode<Camera>("StreamContainer/Camera");
_cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation; _cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation;
Debug.Assert(_tileWorld != null); Debug.Assert(_tileWorld != null);
@ -114,7 +114,7 @@ public class Game : Spatial
worldInfoComponent.SetWorld(_tileWorld); worldInfoComponent.SetWorld(_tileWorld);
} }
_tileWorld.Generate(); _tileWorld.Generate(_tileWorld.Size);
UpdateCurrentTile(); UpdateCurrentTile();
_streamContainer.SetCenterTile(_currentTile); _streamContainer.SetCenterTile(_currentTile);
} }
@ -177,15 +177,21 @@ public class Game : Spatial
Transform cameraTransform = _camera.Transform; Transform cameraTransform = _camera.Transform;
cameraTransform.origin = _player.GlobalTranslation + _cameraOffset; cameraTransform.origin = _player.GlobalTranslation + _cameraOffset;
_camera.Transform = cameraTransform; // _camera.Transform = cameraTransform;
} }
public void OnGenerateButton() public void OnGenerateButton()
{ {
GD.Print("Generating"); GD.Print("Generating");
Slider worldSizeSlider = (Slider)FindNode("WorldSizeSlider");
if (worldSizeSlider == null)
{
GD.PrintErr("Could not find WorldSizeSlider!");
return;
}
_tileWorld.Seed = _tileWorld.Seed + 1; _tileWorld.Seed = _tileWorld.Seed + 1;
_tileWorld.Generate(); _tileWorld.Generate((int)worldSizeSlider.Value);
} }
@ -266,7 +272,7 @@ public class Game : Spatial
public void OnWorldGenerated() public void OnWorldGenerated()
{ {
GD.Print("Using new map"); GD.Print("Using new map. Size: " + (int)_tileWorld.Colormap.GetSize().x);
ImageTexture newWorldTexture = new ImageTexture(); ImageTexture newWorldTexture = new ImageTexture();
newWorldTexture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat)); newWorldTexture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=22 format=2] [gd_scene load_steps=19 format=2]
[ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=1] [ext_resource path="res://scenes/StreamContainer.cs" type="Script" id=1]
[ext_resource path="res://components/NavigationComponent.cs" type="Script" id=2] [ext_resource path="res://components/NavigationComponent.cs" type="Script" id=2]
@ -11,6 +11,7 @@
[ext_resource path="res://scenes/Game.cs" type="Script" id=9] [ext_resource path="res://scenes/Game.cs" type="Script" id=9]
[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=10] [ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=10]
[ext_resource path="res://assets/CreatusPiratePack/Models/Characters/gltf/Pirate1final.glb" type="PackedScene" id=11] [ext_resource path="res://assets/CreatusPiratePack/Models/Characters/gltf/Pirate1final.glb" type="PackedScene" id=11]
[ext_resource path="res://ui/WorldGeneratorUI.gd" type="Script" id=12]
[sub_resource type="CubeMesh" id=1] [sub_resource type="CubeMesh" id=1]
size = Vector3( 1, 1, 1 ) size = Vector3( 1, 1, 1 )
@ -26,19 +27,6 @@ extents = Vector3( 20, 1, 20 )
radius = 0.3 radius = 0.3
height = 0.2 height = 0.2
[sub_resource type="SpatialMaterial" id=20]
albedo_color = Color( 0.741176, 0.529412, 0.32549, 1 )
[sub_resource type="CapsuleMesh" id=8]
material = SubResource( 20 )
radius = 0.2
mid_height = 0.5
radial_segments = 16
[sub_resource type="CubeMesh" id=14]
[sub_resource type="PrismMesh" id=15]
[sub_resource type="CylinderShape" id=24] [sub_resource type="CylinderShape" id=24]
height = 0.2 height = 0.2
@ -68,19 +56,20 @@ margin_bottom = 158.0
mouse_filter = 2 mouse_filter = 2
alignment = 2 alignment = 2
[node name="VBoxContainer" type="VBoxContainer" parent="Control/HBoxContainer"] [node name="WorldGeneratorUI" type="VBoxContainer" parent="Control/HBoxContainer"]
margin_right = 100.0 margin_right = 100.0
margin_bottom = 228.0 margin_bottom = 248.0
alignment = 2 alignment = 2
script = ExtResource( 12 )
[node name="WorldTextureRect" type="TextureRect" parent="Control/HBoxContainer/VBoxContainer"] [node name="WorldTextureRect" type="TextureRect" parent="Control/HBoxContainer/WorldGeneratorUI"]
margin_right = 100.0 margin_right = 100.0
margin_bottom = 100.0 margin_bottom = 100.0
rect_min_size = Vector2( 100, 100 ) rect_min_size = Vector2( 100, 100 )
stretch_mode = 1 stretch_mode = 1
flip_v = true flip_v = true
[node name="HeightTextureRect" type="TextureRect" parent="Control/HBoxContainer/VBoxContainer"] [node name="HeightTextureRect" type="TextureRect" parent="Control/HBoxContainer/WorldGeneratorUI"]
margin_top = 104.0 margin_top = 104.0
margin_right = 100.0 margin_right = 100.0
margin_bottom = 204.0 margin_bottom = 204.0
@ -88,16 +77,36 @@ rect_min_size = Vector2( 100, 100 )
stretch_mode = 1 stretch_mode = 1
flip_v = true flip_v = true
[node name="WorldGenerateButton" type="Button" parent="Control/HBoxContainer/VBoxContainer"] [node name="WorldGenerateButton" type="Button" parent="Control/HBoxContainer/WorldGeneratorUI"]
margin_top = 208.0 margin_top = 208.0
margin_right = 100.0 margin_right = 100.0
margin_bottom = 228.0 margin_bottom = 228.0
text = "Generate" text = "Generate"
[node name="HBoxContainer" type="HBoxContainer" parent="Control/HBoxContainer/WorldGeneratorUI"]
margin_top = 232.0
margin_right = 100.0
margin_bottom = 248.0
[node name="WorldSizeSlider" type="HSlider" parent="Control/HBoxContainer/WorldGeneratorUI/HBoxContainer"]
margin_right = 88.0
margin_bottom = 16.0
size_flags_horizontal = 3
min_value = 1.0
max_value = 512.0
value = 1.0
[node name="WorldSizeLabel" type="Label" parent="Control/HBoxContainer/WorldGeneratorUI/HBoxContainer"]
margin_left = 92.0
margin_top = 1.0
margin_right = 100.0
margin_bottom = 15.0
text = "4"
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"] [node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
margin_left = 104.0 margin_left = 104.0
margin_right = 231.0 margin_right = 231.0
margin_bottom = 228.0 margin_bottom = 248.0
mouse_filter = 2 mouse_filter = 2
columns = 2 columns = 2
@ -222,7 +231,7 @@ text = "0"
[node name="StreamContainer" type="Spatial" parent="."] [node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 1 ) script = ExtResource( 1 )
Dimensions = Vector2( 18, 17 ) Dimensions = Vector2( 30, 30 )
World = NodePath("../TileWorld") World = NodePath("../TileWorld")
[node name="ActiveTiles" type="Spatial" parent="StreamContainer"] [node name="ActiveTiles" type="Spatial" parent="StreamContainer"]
@ -240,8 +249,8 @@ material/0 = SubResource( 2 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) 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="StreamContainer"]
transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 4.79787, 2.99071 ) transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, 1.7053e-13, 10.6428, 6.65615 )
current = true current = true
fov = 60.0 fov = 60.0
script = ExtResource( 10 ) script = ExtResource( 10 )
@ -256,26 +265,6 @@ script = ExtResource( 3 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 ) transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
shape = SubResource( 7 ) shape = SubResource( 7 )
[node name="MeshInstance" type="MeshInstance" parent="Player"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
mesh = SubResource( 8 )
[node name="Arrow" type="Spatial" parent="Player/MeshInstance"]
transform = Transform( 3, -1.51264e-15, -9.91796e-22, 0, -8.74228e-09, 3, -2.26897e-14, -0.2, -1.31134e-07, 0, -0.253289, 0.32602 )
visible = false
[node name="MeshInstance" type="MeshInstance" parent="Player/MeshInstance/Arrow"]
transform = Transform( 0.1, 0, 3.72529e-09, 0, 0.1, 0, -3.72529e-09, 0, 0.1, 0, 0, 0 )
visible = false
mesh = SubResource( 14 )
skeleton = NodePath("../../..")
[node name="MeshInstance2" type="MeshInstance" parent="Player/MeshInstance/Arrow"]
transform = Transform( 0.1, 3.72529e-09, -1.62838e-16, 0, -4.37114e-09, -0.1, -3.72529e-09, 0.1, -4.37114e-09, 0, 0, 0.191322 )
visible = false
mesh = SubResource( 15 )
skeleton = NodePath("../../..")
[node name="Movable" parent="Player" instance=ExtResource( 4 )] [node name="Movable" parent="Player" instance=ExtResource( 4 )]
[node name="Geometry" type="Spatial" parent="Player"] [node name="Geometry" type="Spatial" parent="Player"]
@ -318,4 +307,6 @@ transform = Transform( 0.550568, 0, -0.83479, 0, 1, 0, 0.83479, 0, 0.550568, 4.8
[node name="Chest2" parent="Entities" instance=ExtResource( 7 )] [node name="Chest2" parent="Entities" instance=ExtResource( 7 )]
transform = Transform( 0.793576, 0, -0.608471, 0, 1, 0, 0.608471, 0, 0.793576, 2.79265, 0, -5.36551 ) transform = Transform( 0.793576, 0, -0.608471, 0, 1, 0, 0.608471, 0, 0.793576, 2.79265, 0, -5.36551 )
[connection signal="value_changed" from="Control/HBoxContainer/WorldGeneratorUI/HBoxContainer/WorldSizeSlider" to="Control/HBoxContainer/WorldGeneratorUI" method="_on_HSlider_value_changed"]
[editable path="Entities/Chest"] [editable path="Entities/Chest"]

View File

@ -19,7 +19,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0096302, 0, 0 )
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="Mesh" type="MeshInstance" parent="."] [node name="Mesh" type="MeshInstance" parent="."]
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0.00399733, -5, 0 ) transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0.0325004, -5, -0.0786072 )
mesh = SubResource( 6 ) mesh = SubResource( 6 )
material/0 = ExtResource( 2 ) material/0 = ExtResource( 2 )

View File

@ -1203,3 +1203,6 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.762809, 0, -1.31108 )
[node name="HexTile3D400" parent="." instance=ExtResource( 1 )] [node name="HexTile3D400" parent="." instance=ExtResource( 1 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.762809, 0, -9.08685 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.762809, 0, -9.08685 )
[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 20, 0 )

View File

@ -1,9 +1,7 @@
using Godot; using Godot;
using System; using System;
using System.Linq; using System.Linq;
using System.Numerics;
using System.Diagnostics; using System.Diagnostics;
using GoDotLog;
using Vector2 = Godot.Vector2; using Vector2 = Godot.Vector2;
using Vector3 = Godot.Vector3; using Vector3 = Godot.Vector3;
@ -16,15 +14,15 @@ public class TileWorld : Spatial
Done Done
} }
private GenerationState CurrentGenerationState = GenerationState.Heightmap; private GenerationState _currentGenerationState = GenerationState.Heightmap;
// signals // signals
[Signal] [Signal]
delegate void WorldGenerated(); delegate void WorldGenerated();
// public members // public members
public float Size = 100; public int Size = 11;
public float HeightScale = 10.0f; public float HeightScale = 2.0f;
public Image Heightmap; public Image Heightmap;
public Image Colormap; public Image Colormap;
public int Seed = 0; public int Seed = 0;
@ -58,72 +56,54 @@ public class TileWorld : Spatial
Debug.Assert(_heightmapOffscreenTextureRect != null); Debug.Assert(_heightmapOffscreenTextureRect != null);
_heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size)); _heightmapOffscreenTextureRect.SetSize(new Vector2(Size, Size));
Generate(); Generate(Size);
} }
public void Generate() public void Generate(int size)
{ {
GD.Print("Triggering generation for size: " + size);
Size = size;
_worldOffscreenViewport.Size = new Vector2(size, size);
_heightmapOffscreenViewport.Size = new Vector2(size, size);
GenerateNoiseMap(); GenerateNoiseMap();
//GenerateDebugMap(); // GenerateDebugMap();
} }
private void GenerateDebugMap() private void GenerateDebugMap()
{ {
Colormap = new Image(); Colormap = new Image();
Colormap.Create((int)Size, (int)Size, false, Image.Format.Rgba8); Colormap.Create(Size, Size, false, Image.Format.Rgba8);
Heightmap = new Image(); Heightmap = new Image();
Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rf); Heightmap.Create(Size, Size, false, Image.Format.Rf);
Heightmap.Lock(); Heightmap.Lock();
Colormap.Lock(); Colormap.Lock();
foreach (int coord_x in Enumerable.Range(0, (int)Size))
foreach (int coord_x in Enumerable.Range(0, Size))
{ {
foreach (int coord_y in Enumerable.Range(0, (int)Size)) foreach (int coord_y in Enumerable.Range(0, Size))
{ {
// Colormap.SetPixel(coord_x, coord_y, new Color((float) coord_x, (float) coord_y, 0, 1)); Colormap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1));
float coord_to_height = Heightmap.SetPixel(coord_x, coord_y, new Color((float) Mathf.Min(coord_x, coord_y) / Size, (float) 0, 0, 1));
(float)coord_y / Size * 0f;
if (coord_x == 1 && coord_y == 4)
{
coord_to_height = 1;
}
SetHeightAtOffset(new Vector2(coord_x, coord_y), coord_to_height);
} }
} }
Colormap.SetPixel(Size - 1, Size -1, new Color(1, 1, 1, 1));
Colormap.Unlock(); Colormap.Unlock();
_currentGenerationState = GenerationState.Done;;
ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(Heightmap); EmitSignal("WorldGenerated");
imageTexture.Flags = 0;
_worldOffscreenTextureRect.Texture = imageTexture;
Colormap.CopyFrom(imageTexture.GetData());
} }
private void GenerateSimpleMap()
{
Heightmap = new Image();
Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rf);
Heightmap.Lock();
foreach (int coord_x in Enumerable.Range(-(int)Size / 2, (int)Size))
{
foreach (int coord_y in Enumerable.Range(-(int)Size / 2, (int)Size))
{
SetHeightAtOffset(new Vector2(coord_x, coord_y), 5f);
}
}
}
private void GenerateNoiseMap() private void GenerateNoiseMap()
{ {
Heightmap = new Image(); Heightmap = new Image();
Heightmap.Create((int)Size, (int)Size, false, Image.Format.Rgba8); Heightmap.Create(Size, Size, false, Image.Format.Rgba8);
OpenSimplexNoise noiseGenerator = new OpenSimplexNoise(); OpenSimplexNoise noiseGenerator = new OpenSimplexNoise();
@ -134,54 +114,38 @@ public class TileWorld : Spatial
noiseGenerator.Lacunarity = 4; noiseGenerator.Lacunarity = 4;
ImageTexture heightmapTexture = new ImageTexture(); ImageTexture heightmapTexture = new ImageTexture();
// heightmapTexture.CreateFromImage(noiseGenerator.GetImage((int)Size, (int)Size));
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());
CurrentGenerationState = GenerationState.Heightmap; _currentGenerationState = GenerationState.Heightmap;
} }
public override void _Process(float delta) public override void _Process(float delta)
{ {
if (CurrentGenerationState == GenerationState.Heightmap) if (_currentGenerationState == GenerationState.Heightmap)
{ {
CurrentGenerationState = GenerationState.Color; _currentGenerationState = GenerationState.Color;
ImageTexture imageTexture = new ImageTexture(); ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(Heightmap); imageTexture.CreateFromImage(Heightmap);
imageTexture.Flags = 0; imageTexture.Flags = 0;
_worldOffscreenTextureRect.Texture = imageTexture; _worldOffscreenTextureRect.Texture = imageTexture;
} }
else if (CurrentGenerationState == GenerationState.Color) else if (_currentGenerationState == GenerationState.Color)
{ {
Colormap = new Image(); Colormap = new Image();
Colormap.Create((int)Size, (int)Size, false, Image.Format.Rgba8); Colormap.Create(Size, Size, false, Image.Format.Rgba8);
Colormap.CopyFrom(_worldOffscreenViewport.GetTexture().GetData()); Colormap.CopyFrom(_worldOffscreenViewport.GetTexture().GetData());
Heightmap.Lock(); Heightmap.Lock();
CurrentGenerationState = GenerationState.Done; _currentGenerationState = GenerationState.Done;
EmitSignal("WorldGenerated"); EmitSignal("WorldGenerated");
} }
} }
public void ApplyHeightmap(Image heightmap)
{
foreach (int coord_x in Enumerable.Range(-(int)Size / 2, (int)Size))
{
foreach (int coord_y in Enumerable.Range(-(int)Size / 2, (int)Size))
{
Vector2 textureCoord = OffsetToTextureCoord(new Vector2(coord_x, coord_y));
float height = heightmap.GetPixel((int)textureCoord.x, (int)textureCoord.y).r;
SetHeightAtOffset(new Vector2(coord_x, coord_y), height);
}
}
}
public bool IsOffsetCoordValid(Vector2 offsetCoord) public bool IsOffsetCoordValid(Vector2 offsetCoord)
{ {
return ((int)Math.Clamp(offsetCoord.x, -Size / 2, Size / 2 - 1) == (int)offsetCoord.x return ((int)Math.Clamp(offsetCoord.x, -Size / 2, Size / 2 - 1) == (int)offsetCoord.x
@ -202,17 +166,9 @@ public class TileWorld : Spatial
public Vector2 OffsetToTextureCoord(Vector2 offsetCoord) public Vector2 OffsetToTextureCoord(Vector2 offsetCoord)
{ {
Vector2 textureCoord = (offsetCoord - Vector2.One * (Mathf.Floor(Size / 2) + 1)) % (Vector2.One * Size); // Vector2 textureCoord = (offsetCoord - Vector2.One * (Mathf.Floor(Size / 2))) % (Vector2.One * Size);
if (textureCoord[0] < 0) Vector2 mapSize = Vector2.One * Size;
{ Vector2 textureCoord = (offsetCoord + mapSize / 2).PosMod(mapSize);
textureCoord[0] += Size;
}
if (textureCoord[1] < 0)
{
textureCoord[1] += Size;
}
return textureCoord; return textureCoord;
} }
@ -225,14 +181,20 @@ public class TileWorld : Spatial
public float GetHeightAtOffset(Vector2 offsetCoord) public float GetHeightAtOffset(Vector2 offsetCoord)
{ {
if (CurrentGenerationState != GenerationState.Done) if (_currentGenerationState != GenerationState.Done)
{ {
return 0f; return 0f;
} }
Vector2 textureCoord = OffsetToTextureCoord(offsetCoord); Vector2 textureCoord = OffsetToTextureCoord(offsetCoord);
return Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r * HeightScale; float heightmapHeight = Heightmap.GetPixel((int)textureCoord.x, (int)(textureCoord.y)).r * HeightScale;
// heightmapHeight = Mathf.Floor(heightmapHeight);
// heightmapHeight = heightmapHeight * 10)
// heightmapHeight = Mathf.Clamp(heightmapHeight, -1f, 5);
return heightmapHeight;
} }

View File

@ -35,7 +35,7 @@ ivec2 axial_to_offset(vec2 axial) {
ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial)); ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial));
int x = cubeCoords.x; int x = cubeCoords.x;
int y = cubeCoords.y; int y = cubeCoords.y;
int off_y = y + (x - (x % 2)) / 2; int off_y = y + (x - (x & 1)) / 2;
return ivec2(x, off_y); return ivec2(x, off_y);
} }
@ -72,20 +72,23 @@ vec3 vertex_world_to_color(vec3 vertex_world) {
vec3 vertex_world_to_hex_center(vec3 vertex_world) { vec3 vertex_world_to_hex_center(vec3 vertex_world) {
vec3 axial_coords_f = vec3(_HexAffineInverse * vertex_world.xz, 0); vec3 axial_coords_f = vec3(_HexAffineInverse * vertex_world.xz, 0);
// Output:0
ivec2 offset_coord_f = axial_to_offset(axial_coords_f.xy); ivec2 offset_coord_f = axial_to_offset(axial_coords_f.xy);
vec2 mod_result = mod (vec2(offset_coord_f), vec2(2.0));
vec3 result_color = vec3(0); vec3 result_color = vec3(0);
if (offset_coord_f.x % 2 == 0) { if (mod_result.x == 0.0) {
result_color.r = 1.0; result_color.r = 1.0;
} }
if (offset_coord_f.y % 2 == 0) { if (mod_result.y == 0.0) {
result_color.b = 1.0; result_color.b = 1.0;
} }
return result_color; result_color = vec3(float(offset_coord_f.x), float(offset_coord_f.y), 0.);
result_color.x = result_color.y;
return mod(result_color / 4.0, vec3(1.0));
} }
ivec2 cube_to_axial(ivec3 hex_i) { ivec2 cube_to_axial(ivec3 hex_i) {
@ -125,7 +128,7 @@ vec3 vertex_world_to_hex_center_amid (vec3 vertex_world) {
void fragment() { void fragment() {
ALBEDO = vertex_world_to_hex_center(vertex_coord); ALBEDO = vertex_world_to_hex_center(vertex_coord);
ALBEDO = vertex_world_to_hex_center_amid(vertex_coord); // ALBEDO = vertex_world_to_hex_center_amid(vertex_coord);
} }
void light() { void light() {

View File

@ -1,8 +1,12 @@
[gd_scene load_steps=6 format=2] [gd_scene load_steps=7 format=2]
[ext_resource path="res://scenes/tests/FragmentShaderHexTile.tres" type="Shader" id=1] [ext_resource path="res://scenes/tests/FragmentShaderHexTile.tres" type="Shader" id=1]
[sub_resource type="ShaderMaterial" id=5]
shader = ExtResource( 1 )
[sub_resource type="PlaneMesh" id=1] [sub_resource type="PlaneMesh" id=1]
material = SubResource( 5 )
[sub_resource type="ShaderMaterial" id=2] [sub_resource type="ShaderMaterial" id=2]
shader = ExtResource( 1 ) shader = ExtResource( 1 )
@ -12,7 +16,6 @@ radius = 0.1
height = 0.2 height = 0.2
[sub_resource type="SpatialMaterial" id=4] [sub_resource type="SpatialMaterial" id=4]
albedo_color = Color( 1, 0, 0, 1 )
[node name="Spatial" type="Spatial"] [node name="Spatial" type="Spatial"]
@ -20,7 +23,7 @@ albedo_color = Color( 1, 0, 0, 1 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 3.34454, 0 ) transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 3.34454, 0 )
[node name="MeshInstance" type="MeshInstance" parent="."] [node name="MeshInstance" type="MeshInstance" parent="."]
transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, -0.00971127, 0, -0.00242263 ) transform = Transform( 50, 0, 0, 0, 50, 0, 0, 0, 50, -0.00971127, 3.65894, -0.00242263 )
mesh = SubResource( 1 ) mesh = SubResource( 1 )
material/0 = SubResource( 2 ) material/0 = SubResource( 2 )
@ -34,11 +37,7 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2.08117 )
visible = false visible = false
mesh = SubResource( 1 ) mesh = SubResource( 1 )
[node name="MeshInstance4" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2.07165, 0, 2.08117 )
visible = false
mesh = SubResource( 1 )
[node name="Center" type="MeshInstance" parent="."] [node name="Center" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.60949, 0 )
mesh = SubResource( 3 ) mesh = SubResource( 3 )
material/0 = SubResource( 4 ) material/0 = SubResource( 4 )

14
ui/WorldGeneratorUI.gd Normal file
View File

@ -0,0 +1,14 @@
extends VBoxContainer
var world_size_label = null
var world_size_slider = null
# Called when the node enters the scene tree for the first time.
func _ready():
world_size_label = find_node("WorldSizeLabel")
world_size_slider = find_node("WorldSizeSlider");
world_size_slider.value = 4
func _on_HSlider_value_changed(value):
world_size_label.text = str(value)