From 39b83ba9ea83d29c7cf0bda4bd825b851de3bb31 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Mon, 24 Jun 2024 22:15:11 -0400 Subject: [PATCH] Initial commit, fun geodesic random testing --- .gitattributes | 2 + .gitignore | 2 + DrawLine3D.gd | 97 ++++++++++++++++++++++++++++++++++++++++++++++ geodesic_dome.gd | 24 ++++++++++++ geodesic_dome.tscn | 21 ++++++++++ ico_vertex.gd | 34 ++++++++++++++++ ico_vertex.tscn | 16 ++++++++ icon.svg | 1 + icon.svg.import | 37 ++++++++++++++++++ project.godot | 20 ++++++++++ world.gd | 14 +++++++ world.tscn | 17 ++++++++ 12 files changed, 285 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 DrawLine3D.gd create mode 100644 geodesic_dome.gd create mode 100644 geodesic_dome.tscn create mode 100644 ico_vertex.gd create mode 100644 ico_vertex.tscn create mode 100644 icon.svg create mode 100644 icon.svg.import create mode 100644 project.godot create mode 100644 world.gd create mode 100644 world.tscn diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4709183 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Godot 4+ specific ignores +.godot/ diff --git a/DrawLine3D.gd b/DrawLine3D.gd new file mode 100644 index 0000000..0225363 --- /dev/null +++ b/DrawLine3D.gd @@ -0,0 +1,97 @@ +extends Node2D + +class Line: + var Start + var End + var LineColor + var time + + func _init(Start, End, LineColor, time): + self.Start = Start + self.End = End + self.LineColor = LineColor + self.time = time + +var Lines = [] +var RemovedLine = false + +func _process(delta): + for i in range(len(Lines)): + Lines[i].time -= delta + + if(len(Lines) > 0 || RemovedLine): + queue_redraw() #Calls _draw + RemovedLine = false + +func _draw(): + var Cam = get_viewport().get_camera_3d() + for i in range(len(Lines)): + var ScreenPointStart = Cam.unproject_position(Lines[i].Start) + var ScreenPointEnd = Cam.unproject_position(Lines[i].End) + + #Dont draw line if either start or end is considered behind the camera + #this causes the line to not be drawn sometimes but avoids a bug where the + #line is drawn incorrectly + if(Cam.is_position_behind(Lines[i].Start) || + Cam.is_position_behind(Lines[i].End)): + continue + + draw_line(ScreenPointStart, ScreenPointEnd, Lines[i].LineColor) + + #Remove lines that have timed out + var i = Lines.size() - 1 + while (i >= 0): + if(Lines[i].time < 0.0): + Lines.remove_at(i) + RemovedLine = true + i -= 1 + +func DrawLine(Start, End, LineColor, time = 0.0): + Lines.append(Line.new(Start, End, LineColor, time)) + +func DrawRay(Start, Ray, LineColor, time = 0.0): + Lines.append(Line.new(Start, Start + Ray, LineColor, time)) + +func DrawCube(Center, HalfExtents, LineColor, time = 0.0): + #Start at the 'top left' + var LinePointStart = Center + LinePointStart.x -= HalfExtents + LinePointStart.y += HalfExtents + LinePointStart.z -= HalfExtents + + #Draw top square + var LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + + #Draw bottom square + LinePointStart = LinePointEnd + Vector3(0, -HalfExtents * 2.0, 0) + LinePointEnd = LinePointStart + Vector3(0, 0, HalfExtents * 2.0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(HalfExtents * 2.0, 0, 0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(0, 0, -HalfExtents * 2.0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + LinePointStart = LinePointEnd + LinePointEnd = LinePointStart + Vector3(-HalfExtents * 2.0, 0, 0) + DrawLine(LinePointStart, LinePointEnd, LineColor, time); + + #Draw vertical lines + LinePointStart = LinePointEnd + DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time) + LinePointStart += Vector3(0, 0, HalfExtents * 2.0) + DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time) + LinePointStart += Vector3(HalfExtents * 2.0, 0, 0) + DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time) + LinePointStart += Vector3(0, 0, -HalfExtents * 2.0) + DrawRay(LinePointStart, Vector3(0, HalfExtents * 2.0, 0), LineColor, time) diff --git a/geodesic_dome.gd b/geodesic_dome.gd new file mode 100644 index 0000000..b6c3ffa --- /dev/null +++ b/geodesic_dome.gd @@ -0,0 +1,24 @@ +extends Node3D + +@export var vertex_scene: PackedScene + +func random_position(): + return Vector3( + randf_range(-2,2), + randf_range(-2,2), + randf_range(-2,2) + ) + +func add_vertex(p : Vector3): + var vertex = vertex_scene.instantiate() + vertex.position = p + add_child(vertex) + +# Called when the node enters the scene tree for the first time. +func _ready(): + for i in range(11): + add_vertex(random_position()) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass diff --git a/geodesic_dome.tscn b/geodesic_dome.tscn new file mode 100644 index 0000000..56f1092 --- /dev/null +++ b/geodesic_dome.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=4 format=3 uid="uid://cr06eruxv1iyp"] + +[ext_resource type="Script" path="res://geodesic_dome.gd" id="1_ewsyw"] +[ext_resource type="PackedScene" uid="uid://bn7wa6in76pce" path="res://ico_vertex.tscn" id="2_loxxg"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_dw2k7"] +transparency = 1 +cull_mode = 2 +albedo_color = Color(1, 1, 1, 0.254902) + +[node name="GeodesicDome" type="Node3D"] +script = ExtResource("1_ewsyw") +vertex_scene = ExtResource("2_loxxg") + +[node name="UnitSphere" type="CSGSphere3D" parent="."] +radius = 0.9 +material = SubResource("StandardMaterial3D_dw2k7") + +[node name="TopVertex" parent="." instance=ExtResource("2_loxxg")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) +fixed = true diff --git a/ico_vertex.gd b/ico_vertex.gd new file mode 100644 index 0000000..9406e98 --- /dev/null +++ b/ico_vertex.gd @@ -0,0 +1,34 @@ +class_name IsoVertex extends RigidBody3D + +@export var fixed : bool = false + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + get_tree().call_group("vertex", "maybe_draw_line", self) + +func maybe_draw_line(other : IsoVertex): + if self == other: return + var v1 := self.position + var v2 := other.position + var midpoint = (v1 + v2) / 2 + if midpoint.length() > 0.75: # If it doesn't pass through the 'middle' + get_tree().current_scene.draw_line(v1, v2) + +func push_away_from(other : IsoVertex, delta : float): + if self == other: return + if fixed: return + var displacement = self.position - other.position + var forceDirection = displacement.normalized() + var forceStrength = 10 / displacement.length_squared() # Some function of + apply_force(forceDirection * forceStrength * delta) + +func _physics_process(delta): + # Step 1, get a list of all other vertices + # Step 2, apply force to each + get_tree().call_group("vertex", "push_away_from", self, delta) + # Step 3, normalize current position + position = position.normalized() diff --git a/ico_vertex.tscn b/ico_vertex.tscn new file mode 100644 index 0000000..56455d4 --- /dev/null +++ b/ico_vertex.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://bn7wa6in76pce"] + +[ext_resource type="Script" path="res://ico_vertex.gd" id="1_31hj6"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2qrn4"] +albedo_color = Color(0.495227, 0.615034, 0.939684, 1) + +[node name="IsoVertex" type="RigidBody3D" groups=["vertex"]] +collision_layer = 0 +collision_mask = 0 +gravity_scale = 0.0 +script = ExtResource("1_31hj6") + +[node name="Dot" type="CSGSphere3D" parent="."] +radius = 0.05 +material = SubResource("StandardMaterial3D_2qrn4") diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..3fe4f4a --- /dev/null +++ b/icon.svg @@ -0,0 +1 @@ + diff --git a/icon.svg.import b/icon.svg.import new file mode 100644 index 0000000..70be774 --- /dev/null +++ b/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://bwn5xbopjnidj" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..858ed5e --- /dev/null +++ b/project.godot @@ -0,0 +1,20 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="The Dome" +run/main_scene="res://world.tscn" +config/features=PackedStringArray("4.2", "Forward Plus") +config/icon="res://icon.svg" + +[autoload] + +DrawLine3d="*res://DrawLine3D.gd" diff --git a/world.gd b/world.gd new file mode 100644 index 0000000..8183573 --- /dev/null +++ b/world.gd @@ -0,0 +1,14 @@ +extends Node3D + +var LineDrawer = preload("res://DrawLine3D.gd").new() + +func draw_line(v1 : Vector3, v2 : Vector3): + LineDrawer.DrawLine(v1, v2, Color.BROWN, 0.1) + +# Called when the node enters the scene tree for the first time. +func _ready(): + add_child(LineDrawer) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass diff --git a/world.tscn b/world.tscn new file mode 100644 index 0000000..645362c --- /dev/null +++ b/world.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=3 uid="uid://cotgytv6xac3l"] + +[ext_resource type="PackedScene" uid="uid://cr06eruxv1iyp" path="res://geodesic_dome.tscn" id="1_j3sin"] +[ext_resource type="Script" path="res://world.gd" id="1_lxwr8"] + +[node name="World" type="Node3D"] +script = ExtResource("1_lxwr8") + +[node name="CameraPivot" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.846797, 0.531916, 0, -0.531916, 0.846797, 0.0169442, 1.0738, 1.64421) + +[node name="Camera3D" type="Camera3D" parent="CameraPivot"] + +[node name="GeodesicDome" parent="." instance=ExtResource("1_j3sin")] + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, -0.258819, 0.965926, 0, -0.965926, -0.258819, 0, 2.14709, 3) -- 2.47.3