summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-11-24 11:46:03 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-11-24 11:46:03 +0100
commitb1d9bfe7e80b4d95acd26d0ab6a1ce34ad18d91c (patch)
tree3bc9cce0c176057fcc2f8b4640a3e23de374875e
parent1a785420ec47c78ac3f95bd0ece08c819653f6e8 (diff)
next commitHEADmain
-rw-r--r--chunk.gd8
-rw-r--r--free-look-camera.gd11
-rw-r--r--main.gd250
-rw-r--r--main.tscn6
-rw-r--r--player.gd8
5 files changed, 29 insertions, 254 deletions
diff --git a/chunk.gd b/chunk.gd
index 8dcd416..24713a2 100644
--- a/chunk.gd
+++ b/chunk.gd
@@ -6,7 +6,7 @@ var chunk_size := Vector3(16, 16, 16)
@export var noise: FastNoiseLite
-var blocks: Array[Block] = []
+var blocks: Array = []
func generate_chunk(row, column, depth):
@@ -18,7 +18,7 @@ func generate_chunk(row, column, depth):
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_material(material)
- var blocks = []
+ blocks = []
for x in chunk_size.x:
#blocks[x] = []
@@ -66,7 +66,7 @@ func generate_chunk(row, column, depth):
st.set_uv(Vector2(0, 0))
if (blocks[x][y].size() > z+1 and blocks[x][y][z + 1].type == Block.Type.AIR) or blocks[x][y].size() == z+1:
- block.add_face(st, Block.Face.FRONT, block)
+ block.add_face(st, Block.Face.FRONT)
if (blocks.size() > x+1 and blocks[x + 1][y][z].type == Block.Type.AIR) or blocks.size() == x+1:
block.add_face(st, Block.Face.RIGHT)
@@ -88,3 +88,5 @@ func generate_chunk(row, column, depth):
var mesh = MeshInstance3D.new()
mesh.mesh = st.commit()
mesh.create_trimesh_collision()
+
+ return mesh
diff --git a/free-look-camera.gd b/free-look-camera.gd
index e35485f..50c2311 100644
--- a/free-look-camera.gd
+++ b/free-look-camera.gd
@@ -30,6 +30,14 @@ var _shift = false
var _alt = false
func _input(event):
+ if not current:
+ return
+
+ if event is InputEventMouse and event.is_pressed():
+ Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
+ if event.is_action_pressed("mouse_exit"):
+ Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
+
# Receives mouse motion
if event is InputEventMouseMotion:
_mouse_position = event.relative
@@ -66,6 +74,9 @@ func _input(event):
# Updates mouselook and movement every frame
func _process(delta):
+ if not current:
+ return
+
_update_mouselook()
_update_movement(delta)
diff --git a/main.gd b/main.gd
index ae29028..f1806e4 100644
--- a/main.gd
+++ b/main.gd
@@ -2,134 +2,8 @@ class_name Main
extends Node3D
-var chunk_size := Vector3(16, 16, 16)
-
@onready var noise := FastNoiseLite.new()
-var face_corners = [
- Vector2(0, 0), Vector2(1, 0),
- Vector2(0, 1), Vector2(1, 1),
-]
-
-enum BlockFace {
- FRONT,
- BACK,
- LEFT,
- RIGHT,
- TOP,
- BOTTOM,
-}
-var block_faces = [
- [ # FRONT
- [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
- [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0)],
- ],
- [ # BACK
- [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
- [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
- ],
- [ # LEFT
- [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, 1.0), Vector3(-1.0, -1.0, -1.0)],
- [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, 1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
- ],
- [ # RIGHT
- [Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(1.0, -1.0, 1.0)],
- [Vector3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
- ],
- [ # TOP
- [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0), Vector3(-1.0, 1.0, 1.0)],
- [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0)],
- ],
- [ # BOTTOM
- [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
- [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0)],
- ],
-]
-var block_face_uvs = [
- [ # FRONT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # BACK
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # LEFT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # RIGHT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # TOP
- [Vector2(0.0, 1.0), Vector2(1.0, 2.0), Vector2(0.0, 2.0)],
- [Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 2.0)],
- ],
- [ # BOTTOM
- [Vector2(1.0, 0.0), Vector2(2.0, 1.0), Vector2(1.0, 1.0)],
- [Vector2(1.0, 0.0), Vector2(2.0, 0.0), Vector2(2.0, 1.0)],
- ],
-]
-var block_face_normals = [
- Vector3(0, 0, 1), # FRONT
- Vector3(0, 0, -1), # BACK
- Vector3(-1, 0, 0), # LEFT
- Vector3(1, 0, 0), # RIGHT
- Vector3(0, 1, 0), # TOP
- Vector3(0, -1, 0), # BOTTOM
-]
-
-enum BlockType {
- AIR,
- GRASS,
- DIRT,
- LEAVES,
- STONE,
-}
-var block_types = {
- BlockType.GRASS: {
- "uv2s": [
- Vector2(0, 0), # FRONT
- Vector2(0, 0), # BACK
- Vector2(0, 0), # LEFT
- Vector2(0, 0), # RIGHT
- Vector2(0, 1), # TOP
- Vector2(1, 0), # BOTTOM
- ],
- },
- BlockType.DIRT: {
- "uv2s": [
- Vector2(1, 0), # FRONT
- Vector2(1, 0), # BACK
- Vector2(1, 0), # LEFT
- Vector2(1, 0), # RIGHT
- Vector2(1, 0), # TOP
- Vector2(1, 0), # BOTTOM
- ],
- },
- BlockType.LEAVES: {
- "uv2s": [
- Vector2(2, 2), # FRONT
- Vector2(2, 2), # BACK
- Vector2(2, 2), # LEFT
- Vector2(2, 2), # RIGHT
- Vector2(2, 2), # TOP
- Vector2(2, 2), # BOTTOM
- ],
- },
- BlockType.STONE: {
- "uv2s": [
- Vector2(2, 0), # FRONT
- Vector2(2, 0), # BACK
- Vector2(2, 0), # LEFT
- Vector2(2, 0), # RIGHT
- Vector2(2, 0), # TOP
- Vector2(2, 0), # BOTTOM
- ],
- },
-}
-
var chunks := []
@@ -153,125 +27,11 @@ func brrr():
chunks[row].append([])
for depth in range(0, 8):
chunks[row][column].append([])
- generate_chunk(row, column, depth)
+ var chunk = Chunk.new()
+ chunk.noise = noise
+ var mesh = chunk.generate_chunk(row, column, depth)
+ add_child.call_deferred(mesh)
+ chunks[row][column][depth] = chunk
thread.start(rrrb)
#thread.wait_to_finish()
-
-
-func generate_chunk(row, column, depth):
- #var material := StandardMaterial3D.new()
- #material.albedo_texture = preload("res://new_atlas_texture.tres")
- #material.vertex_color_use_as_albedo = true
- var material := preload("res://new_shader_material.tres")
- var st = SurfaceTool.new()
- st.begin(Mesh.PRIMITIVE_TRIANGLES)
- st.set_material(material)
-
- var blocks = []
-
- for x in chunk_size.x:
- #blocks[x] = []
- blocks.append([])
- for y in chunk_size.y:
- #blocks[x][y] = []
- blocks[x].append([])
- for z in chunk_size.z:
- var block_position = Vector3(
- row*chunk_size.x + x,
- column*chunk_size.y + y,
- depth*chunk_size.z + z
- #x,
- #y,
- #z
- ) * 2
- var value = noise.get_noise_3d(block_position.x, block_position.y, block_position.z) * 100
-
- var type = BlockType.AIR
- if value > -15:
- type = BlockType.GRASS
- elif value > -20:
- type = BlockType.STONE
-
- var block = {
- "position": block_position,
- "type": type,
- }
- #blocks[x][y][z] = block_position
- blocks[x][y].append(block)
-
- for x in blocks.size():
- for y in blocks[x].size():
- for z in blocks[x][y].size():
- var block = blocks[x][y][z]
- var block_position = block.position
-
- if not block.type == BlockType.AIR:
- if block.type == BlockType.GRASS and blocks[x].size() > y + 1 and not blocks[x][y + 1][z].type == BlockType.AIR:
- block.type = BlockType.DIRT
-
- var rng = RandomNumberGenerator.new()
- rng.seed = randi()
- st.set_color(Color(rng.randf(), rng.randf(), rng.randf()))
-
- st.set_uv(Vector2(0, 0))
-
- if (blocks[x][y].size() > z+1 and blocks[x][y][z + 1].type == BlockType.AIR) or blocks[x][y].size() == z+1:
- add_face(st, BlockFace.FRONT, block)
-
- if (blocks.size() > x+1 and blocks[x + 1][y][z].type == BlockType.AIR) or blocks.size() == x+1:
- add_face(st, BlockFace.RIGHT, block)
-
- if blocks[x][y].size() > z-1 and (blocks[x][y][z - 1].type == BlockType.AIR or z == 0):
- add_face(st, BlockFace.BACK, block)
-
- if blocks.size() > x-1 and (blocks[x - 1][y][z].type == BlockType.AIR or x == 0):
- add_face(st, BlockFace.LEFT, block)
-
- if (blocks[x].size() > y+1 and blocks[x][y + 1][z].type == BlockType.AIR) or blocks[x].size() == y+1:
- add_face(st, BlockFace.TOP, block)
-
- if blocks[x].size() > y-1 and (blocks[x][y - 1][z].type == BlockType.AIR or y == 0):
- add_face(st, BlockFace.BOTTOM, block)
-
- chunks[row][column][depth] = blocks
-
- #st.generate_normals()
- var mesh = MeshInstance3D.new()
- mesh.mesh = st.commit()
- mesh.create_trimesh_collision()
- add_child.call_deferred(mesh)
-
-
-# Called every frame. 'delta' is the elapsed time since the previous frame.
-func _process(_delta: float) -> void:
- pass
-
-
-func add_face(surface_tool: SurfaceTool, face: BlockFace, block: Dictionary):
- var block_position: Vector3 = block.position
- var type: BlockType = block.type
-
- var uv2s = []
- uv2s.resize(3)
- uv2s.fill(block_types[type].uv2s[face])
-
- var normals = []
- normals.resize(3)
- normals.fill(block_face_normals[face])
-
- for idx in block_faces[face].size():
- var triangle = block_faces[face][idx]
- var triangle_positions = triangle.map(func(item):
- return item + block_position
- )
-
- var uvs = block_face_uvs[face][idx]
-
- surface_tool.add_triangle_fan(
- PackedVector3Array(triangle_positions),
- PackedVector2Array(uvs),
- PackedColorArray(),
- PackedVector2Array(uv2s),
- PackedVector3Array(normals)
- )
diff --git a/main.tscn b/main.tscn
index b25da59..b712365 100644
--- a/main.tscn
+++ b/main.tscn
@@ -1,6 +1,7 @@
-[gd_scene load_steps=8 format=3 uid="uid://b7k6l3bm1f0db"]
+[gd_scene load_steps=9 format=3 uid="uid://b7k6l3bm1f0db"]
[ext_resource type="Script" path="res://main.gd" id="1_g4bmv"]
+[ext_resource type="Script" path="res://free-look-camera.gd" id="2_4jusq"]
[ext_resource type="Texture2D" uid="uid://dv842eajslxyw" path="res://icon.svg" id="3_7tj3h"]
[ext_resource type="Script" path="res://player.gd" id="3_tn7lj"]
@@ -34,10 +35,12 @@ directional_shadow_max_distance = 400.0
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.767031, 0.64161, 0, -0.64161, 0.767031, 0.387164, 18.7811, 29.7854)
current = true
+script = ExtResource("2_4jusq")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
[node name="CharacterBody3D" type="CharacterBody3D" parent="."]
+process_mode = 4
transform = Transform3D(-0.0770845, 0, 0.997025, 0, 1, 0, -0.997025, 0, -0.0770845, 0, 238.895, 0)
script = ExtResource("3_tn7lj")
@@ -48,7 +51,6 @@ shape = SubResource("CapsuleShape3D_cggfi")
transform = Transform3D(0.999999, 0, 0, 0, 1, 0, 7.45058e-09, 0, 1, 0, 0.87, 0)
[node name="Camera" type="Camera3D" parent="CharacterBody3D/CameraAnchor"]
-current = true
[node name="RayCast3D" type="RayCast3D" parent="CharacterBody3D/CameraAnchor/Camera"]
target_position = Vector3(0, 0, -5)
diff --git a/player.gd b/player.gd
index cf8e90e..d010294 100644
--- a/player.gd
+++ b/player.gd
@@ -40,10 +40,10 @@ func _process(delta: float) -> void:
acc = 0
print(block_chunk_idx)
print(chunk_idx)
- var chunk = get_tree().current_scene.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z]
- var block = chunk[block_chunk_idx.x][block_chunk_idx.y][block_chunk_idx.z]
- print(block)
- print(Main.BlockType.keys()[block.type])
+ var chunk: Chunk = get_tree().current_scene.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z]
+ var block: Block = chunk.blocks[block_chunk_idx.x][block_chunk_idx.y][block_chunk_idx.z]
+ print(point, "==", block.position) #??
+ print(Block.Type.keys()[block.type])
func _physics_process(delta: float) -> void: