Skip to content

Commit 8355d86

Browse files
author
Hoimar
committed
Initial commit
0 parents  commit 8355d86

18 files changed

+565
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Godot-specific ignores
3+
.import/
4+
export.cfg
5+
export_presets.cfg
6+
7+
# Mono-specific ignores
8+
.mono/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Hoimar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Planet-Generator
2+

default_env.tres

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
sky_top_color = Color( 0.0862745, 0.0862745, 0.141176, 1 )
5+
sky_horizon_color = Color( 0.0431373, 0.141176, 0.223529, 1 )
6+
sky_energy = 2.0
7+
ground_bottom_color = Color( 0.168627, 0.203922, 0.239216, 1 )
8+
ground_horizon_color = Color( 0.0431373, 0.141176, 0.223529, 1 )
9+
sun_latitude = 33.0
10+
sun_longitude = 46.0
11+
sun_energy = 5.0
12+
13+
[resource]
14+
background_mode = 2
15+
background_sky = SubResource( 1 )
16+
ambient_light_color = Color( 0.137255, 0.156863, 0.184314, 1 )

project.godot

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ {
12+
"base": "Spatial",
13+
"class": "NoiseGenerator",
14+
"language": "GDScript",
15+
"path": "res://scripts/NoiseGenerator.gd"
16+
}, {
17+
"base": "MeshInstance",
18+
"class": "TerrainFace",
19+
"language": "GDScript",
20+
"path": "res://scripts/TerrainFace.gd"
21+
} ]
22+
_global_script_class_icons={
23+
"NoiseGenerator": "",
24+
"TerrainFace": ""
25+
}
26+
27+
[application]
28+
29+
config/name="Planet Generator"
30+
run/main_scene="res://scenes/Main.tscn"
31+
config/icon="res://resources/icon.png"
32+
33+
[rendering]
34+
35+
quality/filters/msaa=3
36+
environment/default_environment="res://resources/default_env.tres"

resources/Planet.tres

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[gd_resource type="SpatialMaterial" format=2]
2+
3+
[resource]
4+
albedo_color = Color( 0.498039, 0.372549, 0, 1 )
5+
roughness = 0.35

resources/default_env.tres

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
sky_top_color = Color( 0.0862745, 0.0862745, 0.141176, 1 )
5+
sky_horizon_color = Color( 0.0431373, 0.141176, 0.223529, 1 )
6+
sky_energy = 2.0
7+
ground_bottom_color = Color( 0.168627, 0.203922, 0.239216, 1 )
8+
ground_horizon_color = Color( 0.0431373, 0.141176, 0.223529, 1 )
9+
sun_latitude = 33.0
10+
sun_longitude = 46.0
11+
sun_energy = 5.0
12+
13+
[resource]
14+
background_mode = 2
15+
background_sky = SubResource( 1 )
16+
ambient_light_color = Color( 0.137255, 0.156863, 0.184314, 1 )

resources/icon.png

3.35 KB
Loading

resources/icon.png.import

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-e9340f3172b6e3151758cbd23c21cb71.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://resources/icon.png"
13+
dest_files=[ "res://.import/icon.png-e9340f3172b6e3151758cbd23c21cb71.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0

scenes/3DNoisePlanet.tscn

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
[gd_scene load_steps=6 format=2]
2+
3+
[ext_resource path="res://resources/default_env.tres" type="Environment" id=1]
4+
[ext_resource path="res://scripts/3DNoisePlanet.gd" type="Script" id=2]
5+
[ext_resource path="res://scenes/Ship.tscn" type="PackedScene" id=3]
6+
7+
[sub_resource type="SpatialMaterial" id=1]
8+
albedo_color = Color( 0.388235, 0.258824, 0, 1 )
9+
roughness = 0.5
10+
11+
[sub_resource type="SphereMesh" id=2]
12+
material = SubResource( 1 )
13+
radial_segments = 512
14+
rings = 256
15+
16+
[node name="Spatial" type="Spatial"]
17+
script = ExtResource( 2 )
18+
19+
[node name="gui" type="CanvasLayer" parent="."]
20+
21+
[node name="PanelContainer" type="PanelContainer" parent="gui"]
22+
anchor_right = 0.2
23+
anchor_bottom = 0.2
24+
__meta__ = {
25+
"_edit_use_anchors_": false
26+
}
27+
28+
[node name="GridContainer" type="GridContainer" parent="gui/PanelContainer"]
29+
margin_left = 7.0
30+
margin_top = 7.0
31+
margin_right = 197.8
32+
margin_bottom = 114.0
33+
custom_constants/vseparation = 5
34+
columns = 2
35+
36+
[node name="Label" type="Label" parent="gui/PanelContainer/GridContainer"]
37+
margin_top = 5.0
38+
margin_right = 75.0
39+
margin_bottom = 19.0
40+
text = "Seed"
41+
42+
[node name="spn_seed" type="SpinBox" parent="gui/PanelContainer/GridContainer"]
43+
margin_left = 79.0
44+
margin_right = 190.0
45+
margin_bottom = 24.0
46+
size_flags_horizontal = 3
47+
48+
[node name="Label2" type="Label" parent="gui/PanelContainer/GridContainer"]
49+
margin_top = 34.0
50+
margin_right = 75.0
51+
margin_bottom = 48.0
52+
text = "water level"
53+
54+
[node name="spn_water" type="SpinBox" parent="gui/PanelContainer/GridContainer"]
55+
margin_left = 79.0
56+
margin_top = 29.0
57+
margin_right = 190.0
58+
margin_bottom = 53.0
59+
size_flags_horizontal = 3
60+
min_value = -1.0
61+
max_value = 1.0
62+
step = 0.02
63+
value = 0.1
64+
65+
[node name="Label3" type="Label" parent="gui/PanelContainer/GridContainer"]
66+
margin_top = 63.0
67+
margin_right = 75.0
68+
margin_bottom = 77.0
69+
text = "influence"
70+
71+
[node name="spn_influence" type="SpinBox" parent="gui/PanelContainer/GridContainer"]
72+
margin_left = 79.0
73+
margin_top = 58.0
74+
margin_right = 190.0
75+
margin_bottom = 82.0
76+
size_flags_horizontal = 3
77+
step = 0.002
78+
value = 5.0
79+
suffix = "%"
80+
81+
[node name="btn_generate" type="Button" parent="gui/PanelContainer/GridContainer"]
82+
margin_top = 87.0
83+
margin_right = 75.0
84+
margin_bottom = 107.0
85+
text = "Generate!"
86+
87+
[node name="Ship" parent="." instance=ExtResource( 3 )]
88+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 33.3719 )
89+
90+
[node name="Planet" type="MeshInstance" parent="."]
91+
mesh = SubResource( 2 )
92+
material/0 = null
93+
94+
[node name="OmniLight" type="OmniLight" parent="."]
95+
transform = Transform( 0.412129, -0.897543, -0.156738, 0.253923, -0.052066, 0.965822, -0.875027, -0.437843, 0.206449, 0.80857, 1.55902, 2.23497 )
96+
shadow_enabled = true
97+
omni_range = 1000.0
98+
99+
[node name="Camera" type="Camera" parent="."]
100+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2 )
101+
current = true
102+
near = 0.01
103+
far = 1000.0
104+
105+
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
106+
environment = ExtResource( 1 )
107+
[connection signal="pressed" from="gui/PanelContainer/GridContainer/btn_generate" to="." method="_on_btn_generate_pressed"]

scenes/Main.tscn

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://resources/default_env.tres" type="Environment" id=1]
4+
[ext_resource path="res://scripts/Main.gd" type="Script" id=2]
5+
6+
[node name="Main" type="WorldEnvironment"]
7+
environment = ExtResource( 1 )
8+
script = ExtResource( 2 )
9+
10+
[node name="PlanetContainer" type="Spatial" parent="."]
11+
12+
[node name="Camera" type="Camera" parent="."]
13+
transform = Transform( 0.801504, 0.0806397, -0.592527, 0, 0.990866, 0.134851, 0.597989, -0.108084, 0.794183, -1.23852, 0.248043, 1.50765 )
14+
15+
[node name="OmniLight" type="OmniLight" parent="."]
16+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5.12803, 4.42749, 6.41141 )
17+
omni_range = 100.0

scenes/Ship.tscn

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[gd_scene load_steps=3 format=2]
2+
3+
[ext_resource path="res://scripts/Ship.gd" type="Script" id=1]
4+
5+
6+
[sub_resource type="CubeMesh" id=1]
7+
8+
[node name="Ship" type="Spatial"]
9+
script = ExtResource( 1 )
10+
11+
[node name="MeshInstance" type="MeshInstance" parent="."]
12+
transform = Transform( 0.6, 0, 0, 0, 0.5, 0, 0, 0, 1, 0, 0, 0 )
13+
mesh = SubResource( 1 )
14+
material/0 = null
15+
16+
[node name="Camera" type="Camera" parent="."]
17+
transform = Transform( 1, 0, 0, 0, 0.936764, 0.349962, 0, -0.349962, 0.936764, 0, 2.997, 4.737 )
18+
far = 10000.0

scripts/3DNoisePlanet.gd

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
extends Spatial
2+
3+
var noise: OpenSimplexNoise
4+
5+
onready var planet = $Planet
6+
onready var org_mesh: Mesh = planet.mesh.duplicate(true)
7+
onready var spn_seed = $gui/PanelContainer/GridContainer/spn_seed
8+
onready var spn_water = $gui/PanelContainer/GridContainer/spn_water
9+
onready var spn_influence = $gui/PanelContainer/GridContainer/spn_influence
10+
11+
# Called when the node enters the scene tree for the first time.
12+
func _ready():
13+
randomize()
14+
spn_seed.value = randi()
15+
generate()
16+
17+
func generate():
18+
noise = OpenSimplexNoise.new()
19+
noise.seed = spn_seed.value
20+
noise.octaves = 4.0
21+
noise.period = .3
22+
noise.persistence = 0.8
23+
24+
var surf: MeshDataTool = MeshDataTool.new()
25+
var array_mesh = ArrayMesh.new()
26+
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, org_mesh.get_mesh_arrays())
27+
var result: int = surf.create_from_surface(array_mesh, 0)
28+
if result != OK:
29+
print("Error while creating MeshDataTool, exiting. Error: %d" % [result])
30+
return
31+
32+
var influence = org_mesh.radius*spn_influence.value/100
33+
var water_level = spn_water.value
34+
35+
for i in range(0, surf.get_vertex_count()):
36+
var v: Vector3 = surf.get_vertex(i)
37+
38+
var value = noise.get_noise_3dv(v)
39+
if value > water_level:
40+
v += v.normalized() * value * influence
41+
else:
42+
pass #v += v.normalized() * influence
43+
surf.set_vertex(i, v)
44+
45+
var min_dist = 0.9 #radius-1/radius
46+
var max_dist = 1.1 #radius+1/radius
47+
48+
for i in range(surf.get_vertex_count()):
49+
var v = surf.get_vertex(i)
50+
var dist = v.length()
51+
var dist_normalized = range_lerp(dist, min_dist, max_dist, 0, 1) # bring dist to 0..1 range
52+
53+
var uv = Vector2(dist_normalized, 0)
54+
surf.set_vertex_uv(i, uv)
55+
56+
# recalculate face normals (TODO smooth them!)
57+
for i in range(surf.get_face_count()):
58+
59+
var v1i = surf.get_face_vertex(i,0)
60+
var v2i = surf.get_face_vertex(i,1)
61+
var v3i = surf.get_face_vertex(i,2)
62+
63+
var v1 = surf.get_vertex(v1i)
64+
var v2 = surf.get_vertex(v2i)
65+
var v3 = surf.get_vertex(v3i)
66+
67+
# calculate normal for this face
68+
var norm = -(v2 - v1).normalized().cross((v3 - v1).normalized()).normalized()
69+
70+
surf.set_vertex_normal(v1i, norm)
71+
surf.set_vertex_normal(v2i, norm)
72+
surf.set_vertex_normal(v3i, norm)
73+
74+
# commit the mesh
75+
var mmesh = ArrayMesh.new()
76+
surf.commit_to_surface(mmesh)
77+
planet.mesh = mmesh
78+
planet.mesh.surface_set_material(0, org_mesh.surface_get_material(0))
79+
80+
81+
# Called every frame. 'delta' is the elapsed time since the previous frame.
82+
func _physics_process(delta):
83+
planet.rotation.y += PI / (360*2)
84+
85+
func _on_btn_generate_pressed():
86+
generate()

0 commit comments

Comments
 (0)