OpenSimplexNoise_Viewer.gd 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. extends Control
  2. # The OpenSimplexNoise object.
  3. var noise = OpenSimplexNoise.new()
  4. var noise_texture = NoiseTexture.new()
  5. # Various noise parameters.
  6. var min_noise = -1
  7. var max_noise = 1
  8. # Are we using a NoiseTexture instead?
  9. # Noise textures automatically grab and apply the noise data to an ImageTexture, instead of manually.
  10. const use_noise_texture = false
  11. # Called when the node enters the scene tree for the first time.
  12. func _ready():
  13. # Set up noise with basic info.
  14. $ParameterContainer/SeedSpinBox.value = noise.seed
  15. $ParameterContainer/LacunaritySpinBox.value = noise.lacunarity
  16. $ParameterContainer/OctavesSpinBox.value = noise.octaves
  17. $ParameterContainer/PeriodSpinBox.value = noise.period
  18. $ParameterContainer/PersistenceSpinBox.value = noise.persistence
  19. # Render the noise.
  20. _refresh_noise_images()
  21. # Do we need to set up a noise texture?
  22. if use_noise_texture:
  23. noise_texture.noise = noise
  24. $SeamlessNoiseTexture.texture = noise_texture
  25. func _refresh_noise_images():
  26. # Adjust min/max for shader.
  27. var _min = ((min_noise + 1)/2)
  28. var _max = ((max_noise + 1)/2)
  29. var _material = $SeamlessNoiseTexture.material
  30. _material.set_shader_param("min_value", _min)
  31. _material.set_shader_param("max_value", _max)
  32. # Are we using noise textures instead?
  33. if use_noise_texture:
  34. return
  35. # Get a new image if we aren't using a NoiseTexture.
  36. var image = noise.get_seamless_image(500)
  37. var image_texture = ImageTexture.new()
  38. # Draw it.
  39. image_texture.create_from_image(image)
  40. $SeamlessNoiseTexture.texture = image_texture
  41. func _on_DocumentationButton_pressed():
  42. #warning-ignore:return_value_discarded
  43. OS.shell_open("https://docs.godotengine.org/en/latest/classes/class_opensimplexnoise.html")
  44. func _on_SeedSpinBox_value_changed(value):
  45. noise.seed = value
  46. _refresh_noise_images()
  47. func _on_LacunaritySpinBox_value_changed(value):
  48. noise.lacunarity = value
  49. _refresh_noise_images()
  50. func _on_OctavesSpinBox_value_changed(value):
  51. noise.octaves = value
  52. _refresh_noise_images()
  53. func _on_PeriodSpinBox_value_changed(value):
  54. noise.period = value
  55. _refresh_noise_images()
  56. func _on_PersistenceSpinBox_value_changed(value):
  57. noise.persistence = value
  58. _refresh_noise_images()
  59. func _on_MinClipSpinBox_value_changed(value):
  60. min_noise = value
  61. _refresh_noise_images()
  62. func _on_MaxClipSpinBox_value_changed(value):
  63. max_noise = value
  64. _refresh_noise_images()