hooray! simplex noise

This commit is contained in:
zoe 2022-05-13 18:45:53 +02:00
parent c2c9d9b3e3
commit b6fbb204f1
10 changed files with 189 additions and 108 deletions

View file

Binary file not shown.

Binary file not shown.

View file

@ -3,7 +3,7 @@ extends Node
export var xsize = 256
export var ysize = 256
export var zsize = 3
export var cityname = "night city"
export var cityname = "cute little kitties"
func _ready():
StateServer.generate_world(xsize, ysize, zsize, cityname)

View file

@ -2,7 +2,7 @@ extends TileMap
func set_tile_graphics(pos: Vector2, id: int):
if id == 0: set_cellv(pos, -1)
set_cellv(pos,0,false,false,false,get_atlas_vec_for_id(id))
set_cellv(pos,id,false,false,false,get_variant())
func get_atlas_vec_for_id(id: int) -> Vector2:
return Vector2(randi() % 4, id)
func get_variant() -> Vector2:
return Vector2(randi() % 4, 0)

View file

@ -1,81 +1,14 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=3 format=2]
[ext_resource path="res://world/terrain/terrain.tres" type="TileSet" id=1]
[ext_resource path="res://world/Tilemap.gd" type="Script" id=2]
[sub_resource type="Shader" id=1]
code = "// My custom shader for implementing dithered alpha
shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx;
// Texture maps
uniform sampler2D texture_albedo : hint_albedo;
uniform sampler2D texture_masks : hint_white;
uniform sampler2D texture_normal : hint_normal;
// Parameters
uniform vec4 albedo : hint_color;
uniform float specular = 0.5;
uniform float metallic = 0.0;
uniform float roughness : hint_range(0,1) = 0.5;
uniform float normal_strength : hint_range(0,2) = 1.0;
void fragment() {
vec4 albedo_tex = texture(texture_albedo, UV);
vec4 masks_tex = texture(texture_masks, UV);
float alpha = albedo_tex.a;
ALBEDO = albedo.rgb * albedo_tex.rgb;
METALLIC = metallic * masks_tex.r;
ROUGHNESS = roughness * masks_tex.g;
NORMALMAP = texture(texture_normal, UV).rgb;
NORMALMAP_DEPTH = normal_strength;
// Fancy dithered alpha stuff
float opacity = albedo_tex.a;
int x = int(FRAGCOORD.x) % 4;
int y = int(FRAGCOORD.y) % 4;
int index = x + y * 4;
float limit = 0.0;
if (x < 8) {
if (index == 0) limit = 0.0625;
if (index == 1) limit = 0.5625;
if (index == 2) limit = 0.1875;
if (index == 3) limit = 0.6875;
if (index == 4) limit = 0.8125;
if (index == 5) limit = 0.3125;
if (index == 6) limit = 0.9375;
if (index == 7) limit = 0.4375;
if (index == 8) limit = 0.25;
if (index == 9) limit = 0.75;
if (index == 10) limit = 0.125;
if (index == 11) limit = 0.625;
if (index == 12) limit = 1.0;
if (index == 13) limit = 0.5;
if (index == 14) limit = 0.875;
if (index == 15) limit = 0.375;
}
// Is this pixel below the opacity limit? Skip drawing it
if (opacity < limit)
discard;
}"
[sub_resource type="ShaderMaterial" id=2]
shader = SubResource( 1 )
shader_param/albedo = null
shader_param/specular = 0.5
shader_param/metallic = 0.0
shader_param/roughness = 0.5
shader_param/normal_strength = 1.0
[node name="Tilemap" type="TileMap"]
process_priority = 124
material = SubResource( 2 )
position = Vector2( -2, 0 )
mode = 1
tile_set = ExtResource( 1 )
cell_size = Vector2( 32, 16 )
centered_textures = true
collision_layer = 0
collision_mask = 0
format = 1

View file

@ -1,13 +1,14 @@
[gd_resource type="TileSet" load_steps=2 format=2]
[gd_resource type="TileSet" load_steps=3 format=2]
[ext_resource path="res://sprite/tiles/terrain/default.png" type="Texture" id=1]
[ext_resource path="res://materials/water.material" type="Material" id=2]
[resource]
0/name = "default.png 0"
0/name = "air"
0/texture = ExtResource( 1 )
0/tex_offset = Vector2( 0, 0 )
0/modulate = Color( 1, 1, 1, 1 )
0/region = Rect2( 0, 0, 320, 160 )
0/region = Rect2( 0, 0, 128, 32 )
0/tile_mode = 2
0/autotile/icon_coordinate = Vector2( 0, 0 )
0/autotile/tile_size = Vector2( 32, 32 )
@ -24,3 +25,151 @@
0/shape_one_way_margin = 0.0
0/shapes = [ ]
0/z_index = 0
1/name = "water"
1/texture = ExtResource( 1 )
1/tex_offset = Vector2( 0, 0 )
1/modulate = Color( 1, 1, 1, 1 )
1/region = Rect2( 0, 32, 128, 32 )
1/tile_mode = 2
1/autotile/icon_coordinate = Vector2( 0, 0 )
1/autotile/tile_size = Vector2( 32, 32 )
1/autotile/spacing = 0
1/autotile/occluder_map = [ ]
1/autotile/navpoly_map = [ ]
1/autotile/priority_map = [ ]
1/autotile/z_index_map = [ ]
1/occluder_offset = Vector2( 0, 0 )
1/navigation_offset = Vector2( 0, 0 )
1/shape_offset = Vector2( 0, 0 )
1/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
1/shape_one_way = false
1/shape_one_way_margin = 0.0
1/shapes = [ ]
1/z_index = 0
2/name = "grass"
2/texture = ExtResource( 1 )
2/tex_offset = Vector2( 0, 0 )
2/modulate = Color( 1, 1, 1, 1 )
2/region = Rect2( 0, 64, 128, 32 )
2/tile_mode = 2
2/autotile/icon_coordinate = Vector2( 0, 0 )
2/autotile/tile_size = Vector2( 32, 32 )
2/autotile/spacing = 0
2/autotile/occluder_map = [ ]
2/autotile/navpoly_map = [ ]
2/autotile/priority_map = [ ]
2/autotile/z_index_map = [ ]
2/occluder_offset = Vector2( 0, 0 )
2/navigation_offset = Vector2( 0, 0 )
2/shape_offset = Vector2( 0, 0 )
2/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
2/shape_one_way = false
2/shape_one_way_margin = 0.0
2/shapes = [ ]
2/z_index = 0
3/name = "dirt"
3/texture = ExtResource( 1 )
3/tex_offset = Vector2( 0, 0 )
3/modulate = Color( 1, 1, 1, 1 )
3/region = Rect2( 0, 96, 128, 32 )
3/tile_mode = 2
3/autotile/icon_coordinate = Vector2( 0, 0 )
3/autotile/tile_size = Vector2( 32, 32 )
3/autotile/spacing = 0
3/autotile/occluder_map = [ ]
3/autotile/navpoly_map = [ ]
3/autotile/priority_map = [ ]
3/autotile/z_index_map = [ ]
3/occluder_offset = Vector2( 0, 0 )
3/navigation_offset = Vector2( 0, 0 )
3/shape_offset = Vector2( 0, 0 )
3/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
3/shape_one_way = false
3/shape_one_way_margin = 0.0
3/shapes = [ ]
3/z_index = 0
4/name = "sand"
4/texture = ExtResource( 1 )
4/tex_offset = Vector2( 0, 0 )
4/modulate = Color( 1, 1, 1, 1 )
4/region = Rect2( 0, 128, 128, 32 )
4/tile_mode = 2
4/autotile/icon_coordinate = Vector2( 0, 0 )
4/autotile/tile_size = Vector2( 32, 32 )
4/autotile/spacing = 0
4/autotile/occluder_map = [ ]
4/autotile/navpoly_map = [ ]
4/autotile/priority_map = [ ]
4/autotile/z_index_map = [ ]
4/occluder_offset = Vector2( 0, 0 )
4/navigation_offset = Vector2( 0, 0 )
4/shape_offset = Vector2( 0, 0 )
4/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
4/shape_one_way = false
4/shape_one_way_margin = 0.0
4/shapes = [ ]
4/z_index = 0
5/name = "rock"
5/texture = ExtResource( 1 )
5/tex_offset = Vector2( 0, 0 )
5/modulate = Color( 1, 1, 1, 1 )
5/region = Rect2( 0, 160, 128, 32 )
5/tile_mode = 2
5/autotile/icon_coordinate = Vector2( 0, 0 )
5/autotile/tile_size = Vector2( 32, 32 )
5/autotile/spacing = 0
5/autotile/occluder_map = [ ]
5/autotile/navpoly_map = [ ]
5/autotile/priority_map = [ ]
5/autotile/z_index_map = [ ]
5/occluder_offset = Vector2( 0, 0 )
5/navigation_offset = Vector2( 0, 0 )
5/shape_offset = Vector2( 0, 0 )
5/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
5/shape_one_way = false
5/shape_one_way_margin = 0.0
5/shapes = [ ]
5/z_index = 0
6/name = "water_slab"
6/texture = ExtResource( 1 )
6/tex_offset = Vector2( 0, 0 )
6/material = ExtResource( 2 )
6/modulate = Color( 1, 1, 1, 1 )
6/region = Rect2( 0, 192, 128, 32 )
6/tile_mode = 2
6/autotile/icon_coordinate = Vector2( 0, 0 )
6/autotile/tile_size = Vector2( 32, 32 )
6/autotile/spacing = 0
6/autotile/occluder_map = [ ]
6/autotile/navpoly_map = [ ]
6/autotile/priority_map = [ ]
6/autotile/z_index_map = [ ]
6/occluder_offset = Vector2( 0, 0 )
6/navigation_offset = Vector2( 0, 0 )
6/shape_offset = Vector2( 0, 0 )
6/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
6/shape_one_way = false
6/shape_one_way_margin = 0.0
6/shapes = [ ]
6/z_index = 0
7/name = "grass_slab"
7/texture = ExtResource( 1 )
7/tex_offset = Vector2( 0, 0 )
7/modulate = Color( 1, 1, 1, 1 )
7/region = Rect2( 0, 224, 128, 32 )
7/tile_mode = 2
7/autotile/icon_coordinate = Vector2( 0, 0 )
7/autotile/tile_size = Vector2( 32, 32 )
7/autotile/spacing = 0
7/autotile/occluder_map = [ ]
7/autotile/navpoly_map = [ ]
7/autotile/priority_map = [ ]
7/autotile/z_index_map = [ ]
7/occluder_offset = Vector2( 0, 0 )
7/navigation_offset = Vector2( 0, 0 )
7/shape_offset = Vector2( 0, 0 )
7/shape_transform = Transform2D( 1, 0, 0, 1, 0, 0 )
7/shape_one_way = false
7/shape_one_way_margin = 0.0
7/shapes = [ ]
7/z_index = 0

View file

@ -16,6 +16,7 @@ derive_builder = "0.11.2"
rand_seeder = "0.2.3"
rand = "0.8.5"
toml = "0.5.9"
noise = "0.7.0"
[profile.dev.package."*"]
opt-level = 3

View file

@ -1,8 +1,6 @@
use gdnative::prelude::*;
use rand::prelude::StdRng;
use rand::seq::SliceRandom;
use rand_seeder::Seeder;
mod gen;
mod tiles;
pub struct World {
@ -32,40 +30,26 @@ impl World {
}
pub fn generate(&mut self) -> Vector3Array {
let mut rng: StdRng = Seeder::from(self.seed.to_owned()).make_rng();
self.tiles = get_vec3(self.xsize, self.ysize, self.zsize);
let mut rng = gen::get_rng(self.seed.to_owned());
let noisemap = gen::get_noise(&mut rng, (self.xsize, self.ysize));
// fill edges with water
for x in 0..self.ysize {
self.tiles[x][0][0] = tiles::Tiletypes::WaterSlab;
self.tiles[x][self.ysize - 1][0] = tiles::Tiletypes::WaterSlab;
}
for y in 0..self.xsize {
self.tiles[0][y][0] = tiles::Tiletypes::WaterSlab;
self.tiles[self.xsize - 1][y][0] = tiles::Tiletypes::WaterSlab;
}
for x in 1..self.xsize - 1 {
for y in 1..self.ysize - 1 {
if self.tile_is_next_to(x, y, 0, tiles::Tiletypes::WaterSlab){
let available = vec![tiles::Tiletypes::WaterSlab, tiles::Tiletypes::Sand];
self.tiles[x][y][0] = available.choose(&mut rng).unwrap().to_owned();
}
else if self.tile_is_next_to(x, y, 0, tiles::Tiletypes::Sand) {
let available = vec![tiles::Tiletypes::Grass, tiles::Tiletypes::Sand];
self.tiles[x][y][0] = available.choose(&mut rng).unwrap().to_owned();
}
else {
self.tiles[x][y][0] = tiles::Tiletypes::Air;
}
}
}
// return array with all changed tiles, needs rework
let mut ret: Vector3Array = Vector3Array::new();
for x in 0..self.xsize {
for y in 0..self.ysize {
for z in 0..self.zsize {
ret.push(Vector3::new(x as f32, y as f32, z as f32));
ret.push(Vector3::new(x as f32, y as f32, 0.0));
if noisemap.get_value(x, y) > 0.5 {
self.tiles[x][y][0] = tiles::Tiletypes::Rock;
}
else if noisemap.get_value(x, y) > 0.0 {
self.tiles[x][y][0] = tiles::Tiletypes::Grass;
}
else if noisemap.get_value(x, y) > -0.1 {
self.tiles[x][y][0] = tiles::Tiletypes::Sand;
}
else {
self.tiles[x][y][0] = tiles::Tiletypes::WaterSlab;
}
}
}

View file

@ -0,0 +1,14 @@
use rand::{prelude::StdRng, Rng};
use noise::{Seedable, utils::{PlaneMapBuilder, NoiseMapBuilder, NoiseMap}, SuperSimplex};
use rand_seeder::Seeder;
pub fn get_rng(seed: String) -> StdRng{
Seeder::from(seed).make_rng()
}
pub fn get_noise(rng: &mut StdRng, size: (usize, usize)) -> NoiseMap{
let noise = SuperSimplex::new().set_seed(rng.gen_range(0..u32::MAX));
PlaneMapBuilder::new(&noise)
.set_size(size.0, size.1)
.build()
}