Browse Source

Merge pull request #642 from nathanfranke/opensimplexnoise-improvements

Simplify and improve OpenSimplexNoise viewer
Aaron Franke 4 years ago
parent
commit
5fb81434e7

+ 14 - 37
misc/opensimplexnoise/OpenSimplexNoise_Viewer.gd

@@ -1,36 +1,26 @@
 extends Control
 
 # The OpenSimplexNoise object.
-var noise = OpenSimplexNoise.new()
-var noise_texture = NoiseTexture.new()
+onready var noise: OpenSimplexNoise = $SeamlessNoiseTexture.texture.noise
 
 # Various noise parameters.
 var min_noise = -1
 var max_noise = 1
 
-# Are we using a NoiseTexture instead?
-# Noise textures automatically grab and apply the noise data to an ImageTexture, instead of manually.
-const use_noise_texture = false
-
 # Called when the node enters the scene tree for the first time.
 func _ready():
 	# Set up noise with basic info.
 	$ParameterContainer/SeedSpinBox.value = noise.seed
 	$ParameterContainer/LacunaritySpinBox.value = noise.lacunarity
-	$ParameterContainer/OctavesSpinBox.value = noise.octaves
 	$ParameterContainer/PeriodSpinBox.value = noise.period
 	$ParameterContainer/PersistenceSpinBox.value = noise.persistence
+	$ParameterContainer/OctavesSpinBox.value = noise.octaves
 
 	# Render the noise.
-	_refresh_noise_images()
+	_refresh_shader_params()
 
-	# Do we need to set up a noise texture?
-	if use_noise_texture:
-		noise_texture.noise = noise
-		$SeamlessNoiseTexture.texture = noise_texture
 
-
-func _refresh_noise_images():
+func _refresh_shader_params():
 	# Adjust min/max for shader.
 	var _min = (min_noise + 1) / 2
 	var _max = (max_noise + 1) / 2
@@ -38,54 +28,41 @@ func _refresh_noise_images():
 	_material.set_shader_param("min_value", _min)
 	_material.set_shader_param("max_value", _max)
 
-	# Are we using noise textures instead?
-	if use_noise_texture:
-		return
-
-	# Get a new image if we aren't using a NoiseTexture.
-	var image = noise.get_seamless_image(500)
-	var image_texture = ImageTexture.new()
-
-	# Draw it.
-	image_texture.create_from_image(image)
-	$SeamlessNoiseTexture.texture = image_texture
-
 
 func _on_DocumentationButton_pressed():
 	#warning-ignore:return_value_discarded
 	OS.shell_open("https://docs.godotengine.org/en/latest/classes/class_opensimplexnoise.html")
 
 
+func _on_RandomSeedButton_pressed():
+	$ParameterContainer/SeedSpinBox.value = floor(rand_range(-2147483648, 2147483648))
+
+
 func _on_SeedSpinBox_value_changed(value):
 	noise.seed = value
-	_refresh_noise_images()
 
 
 func _on_LacunaritySpinBox_value_changed(value):
 	noise.lacunarity = value
-	_refresh_noise_images()
-
-
-func _on_OctavesSpinBox_value_changed(value):
-	noise.octaves = value
-	_refresh_noise_images()
 
 
 func _on_PeriodSpinBox_value_changed(value):
 	noise.period = value
-	_refresh_noise_images()
 
 
 func _on_PersistenceSpinBox_value_changed(value):
 	noise.persistence = value
-	_refresh_noise_images()
+
+
+func _on_OctavesSpinBox_value_changed(value):
+	noise.octaves = value
 
 
 func _on_MinClipSpinBox_value_changed(value):
 	min_noise = value
-	_refresh_noise_images()
+	_refresh_shader_params()
 
 
 func _on_MaxClipSpinBox_value_changed(value):
 	max_noise = value
-	_refresh_noise_images()
+	_refresh_shader_params()

+ 59 - 43
misc/opensimplexnoise/OpenSimplexNoise_Viewer.tscn

@@ -1,54 +1,73 @@
-[gd_scene load_steps=3 format=2]
+[gd_scene load_steps=5 format=2]
 
 [ext_resource path="res://OpenSimplexNoise_Viewer.gd" type="Script" id=1]
 [ext_resource path="res://OpenSimplexNoise_Viewer.tres" type="Material" id=2]
 
+[sub_resource type="OpenSimplexNoise" id=1]
+
+[sub_resource type="NoiseTexture" id=2]
+noise = SubResource( 1 )
+
 [node name="OpenSimplexNoise Viewer" type="Control"]
 anchor_right = 1.0
 anchor_bottom = 1.0
-margin_left = 8.42108
-margin_top = -5.26315
-margin_right = 8.42114
-margin_bottom = -5.26318
+margin_left = 24.0
+margin_top = 24.0
+margin_right = -24.0
+margin_bottom = -24.0
 script = ExtResource( 1 )
 __meta__ = {
 "_edit_use_anchors_": false
 }
 
-[node name="DocumentationButton" type="Button" parent="."]
-anchor_left = 1.0
-anchor_right = 1.0
-margin_left = -170.0
-margin_top = 30.0
-margin_right = -33.0
-margin_bottom = 50.0
-grow_horizontal = 0
-text = "API Documentation"
-__meta__ = {
-"_edit_use_anchors_": false
-}
-
 [node name="SeamlessNoiseTexture" type="TextureRect" parent="."]
 material = ExtResource( 2 )
 anchor_left = 0.5
 anchor_top = 0.5
 anchor_right = 0.5
 anchor_bottom = 0.5
-margin_left = 40.0
-margin_top = -20.0
-margin_right = 80.0
-margin_bottom = 20.0
+margin_left = -196.0
+margin_top = -256.0
+margin_right = 316.0
+margin_bottom = 256.0
 grow_horizontal = 2
 grow_vertical = 2
+texture = SubResource( 2 )
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="ButtonsContainer" type="VBoxContainer" parent="."]
+anchor_left = 1.0
+anchor_right = 1.0
+margin_left = -137.0
+margin_bottom = 44.0
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="DocumentationButton" type="Button" parent="ButtonsContainer"]
+margin_right = 137.0
+margin_bottom = 20.0
+grow_horizontal = 0
+text = "API Documentation"
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
+[node name="RandomSeedButton" type="Button" parent="ButtonsContainer"]
+margin_top = 24.0
+margin_right = 137.0
+margin_bottom = 44.0
+grow_horizontal = 0
+text = "Random Seed"
 __meta__ = {
 "_edit_use_anchors_": false
 }
 
 [node name="ParameterContainer" type="VBoxContainer" parent="."]
-margin_left = 20.0
-margin_top = 30.0
-margin_right = 300.0
-margin_bottom = 166.0
+margin_right = 280.0
+margin_bottom = 136.0
 __meta__ = {
 "_edit_use_anchors_": false
 }
@@ -56,9 +75,8 @@ __meta__ = {
 [node name="SeedSpinBox" type="SpinBox" parent="ParameterContainer"]
 margin_right = 280.0
 margin_bottom = 24.0
-min_value = -1.53049e+09
-max_value = 1.53049e+09
-rounded = true
+min_value = -2.14748e+09
+max_value = 2.14748e+09
 allow_greater = true
 allow_lesser = true
 prefix = "Seed:"
@@ -67,8 +85,7 @@ prefix = "Seed:"
 margin_top = 28.0
 margin_right = 280.0
 margin_bottom = 52.0
-max_value = 1e+08
-step = 0.01
+step = 0.1
 allow_greater = true
 prefix = "Lacunarity:"
 
@@ -76,9 +93,8 @@ prefix = "Lacunarity:"
 margin_top = 56.0
 margin_right = 280.0
 margin_bottom = 80.0
-min_value = -1e+08
-max_value = 1e+08
-step = 0.01
+min_value = -100000.0
+max_value = 100000.0
 allow_greater = true
 prefix = "Period:"
 
@@ -86,8 +102,7 @@ prefix = "Period:"
 margin_top = 84.0
 margin_right = 280.0
 margin_bottom = 108.0
-max_value = 1e+08
-step = 0.01
+max_value = 1000.0
 allow_greater = true
 prefix = "Persistance:"
 
@@ -96,19 +111,19 @@ margin_top = 112.0
 margin_right = 280.0
 margin_bottom = 136.0
 min_value = 1.0
-max_value = 10.0
+max_value = 9.0
 value = 1.0
-allow_greater = true
 prefix = "Octaves:"
 
 [node name="ClipContainer" type="VBoxContainer" parent="."]
 anchor_top = 1.0
 anchor_bottom = 1.0
-margin_left = 20.0
-margin_top = -72.0
-margin_right = 300.0
-margin_bottom = -20.0
+margin_top = -52.0
+margin_right = 280.0
 grow_vertical = 0
+__meta__ = {
+"_edit_use_anchors_": false
+}
 
 [node name="MinClipSpinBox" type="SpinBox" parent="ClipContainer"]
 margin_right = 280.0
@@ -129,7 +144,8 @@ step = 0.01
 value = 1.0
 prefix = "Max:"
 
-[connection signal="pressed" from="DocumentationButton" to="." method="_on_DocumentationButton_pressed"]
+[connection signal="pressed" from="ButtonsContainer/DocumentationButton" to="." method="_on_DocumentationButton_pressed"]
+[connection signal="pressed" from="ButtonsContainer/RandomSeedButton" to="." method="_on_RandomSeedButton_pressed"]
 [connection signal="value_changed" from="ParameterContainer/SeedSpinBox" to="." method="_on_SeedSpinBox_value_changed"]
 [connection signal="value_changed" from="ParameterContainer/LacunaritySpinBox" to="." method="_on_LacunaritySpinBox_value_changed"]
 [connection signal="value_changed" from="ParameterContainer/PeriodSpinBox" to="." method="_on_PeriodSpinBox_value_changed"]