From 987cb61c1fbe88bfa483fb2deaf42823ff7e7799 Mon Sep 17 00:00:00 2001 From: Valentin Stark Date: Wed, 17 Aug 2022 20:38:28 +0200 Subject: [PATCH] add base 3d --- project.godot | 2 +- utils/terrain/Terrain.gd | 1 + world/World.gd | 35 ++++++++++++++++++++++ default_env.tres => world/default_env.tres | 0 world/game.gd | 2 +- world/game.tscn | 26 +++++++++++++--- world/world.tres | 4 +++ 7 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 world/World.gd rename default_env.tres => world/default_env.tres (100%) create mode 100644 world/world.tres diff --git a/project.godot b/project.godot index 5fbb305..2e3eafe 100644 --- a/project.godot +++ b/project.godot @@ -46,4 +46,4 @@ common/enable_pause_aware_picking=true [rendering] -environment/default_environment="res://default_env.tres" +environment/default_environment="res://world/default_env.tres" diff --git a/utils/terrain/Terrain.gd b/utils/terrain/Terrain.gd index 77e46ee..fce7e9a 100644 --- a/utils/terrain/Terrain.gd +++ b/utils/terrain/Terrain.gd @@ -62,6 +62,7 @@ class Triangle: var list_points = [] for edge in edges(): list_points.append(Point.new(_terrain._triangles[edge._idx], _terrain)) + list_points.invert() return list_points func triangles_adjacent(): diff --git a/world/World.gd b/world/World.gd new file mode 100644 index 0000000..d273cb0 --- /dev/null +++ b/world/World.gd @@ -0,0 +1,35 @@ +extends Spatial + +var terrain + +func _ready(): + pass + + +func draw_world(): + var st = SurfaceTool.new() + + st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.add_smooth_group(true) + for triangle in terrain.get_triangles(): + for point in triangle.points(): + st.add_vertex(point.point3d()) + + st.generate_normals() + st.generate_tangents() + st.index() + # Commit to a mesh. + var mesh = st.commit() + + var mi = MeshInstance.new() + mi.mesh = mesh + var material = load("res://world/world.tres") + mi.set_surface_material(0, material) + mi.create_trimesh_collision() + mi.cast_shadow = GeometryInstance.SHADOW_CASTING_SETTING_ON + print(mi) + add_child(mi) + +func _on_Game_world_loaded(game_terrain): + terrain = game_terrain + draw_world() diff --git a/default_env.tres b/world/default_env.tres similarity index 100% rename from default_env.tres rename to world/default_env.tres diff --git a/world/game.gd b/world/game.gd index 7f17b5e..b1afcf0 100644 --- a/world/game.gd +++ b/world/game.gd @@ -1,4 +1,4 @@ -extends Spatial +extends Node signal world_loaded diff --git a/world/game.tscn b/world/game.tscn index 3c60c82..2f32e71 100644 --- a/world/game.tscn +++ b/world/game.tscn @@ -1,16 +1,34 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://ui/ui.tscn" type="PackedScene" id=1] [ext_resource path="res://world/game.gd" type="Script" id=2] +[ext_resource path="res://world/default_env.tres" type="Environment" id=3] +[ext_resource path="res://world/World.gd" type="Script" id=4] -[node name="Game" type="Spatial"] +[sub_resource type="PlaneMesh" id=1] +size = Vector2( 2000, 2000 ) + +[node name="Game" type="Node"] script = ExtResource( 2 ) [node name="UI" parent="." instance=ExtResource( 1 )] -[node name="Map" parent="UI" index="0"] -scale = Vector2( 0.5, 0.5 ) +[node name="World" type="Spatial" parent="."] +script = ExtResource( 4 ) + +[node name="Water" type="MeshInstance" parent="World"] +visible = false +mesh = SubResource( 1 ) + +[node name="WorldEnvironment" type="WorldEnvironment" parent="World"] +environment = ExtResource( 3 ) + +[node name="Camera" type="Camera" parent="World"] +transform = Transform( 1, 0, 0, 0, 0.509837, 0.860271, 0, -0.860271, 0.509837, 0, 5.05008, 66.6125 ) +near = 0.01 +far = 8192.0 [connection signal="world_loaded" from="." to="UI/Map" method="_on_Game_world_loaded"] +[connection signal="world_loaded" from="." to="World" method="_on_Game_world_loaded"] [editable path="UI"] diff --git a/world/world.tres b/world/world.tres new file mode 100644 index 0000000..f8baa3f --- /dev/null +++ b/world/world.tres @@ -0,0 +1,4 @@ +[gd_resource type="SpatialMaterial" format=2] + +[resource] +albedo_color = Color( 0.109804, 0.305882, 0.0745098, 1 )