open_simplex_noise.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************/
  2. /* open_simplex_noise.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef OPEN_SIMPLEX_NOISE_H
  31. #define OPEN_SIMPLEX_NOISE_H
  32. #include "core/image.h"
  33. #include "core/reference.h"
  34. #include "scene/resources/texture.h"
  35. #include "thirdparty/misc/open-simplex-noise.h"
  36. class OpenSimplexNoise : public Resource {
  37. GDCLASS(OpenSimplexNoise, Resource);
  38. OBJ_SAVE_TYPE(OpenSimplexNoise);
  39. // The maximum number of octaves allowed. Note that these are statically allocated.
  40. // Higher values become exponentially slower, so this shouldn't be set too high
  41. // to avoid freezing the editor for long periods of time.
  42. static const int MAX_OCTAVES = 9;
  43. osn_context contexts[MAX_OCTAVES];
  44. int seed;
  45. float persistence; // Controls details, value in [0,1]. Higher increases grain, lower increases smoothness.
  46. int octaves; // Number of noise layers
  47. float period; // Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain.
  48. float lacunarity; // Controls period change across octaves. 2 is usually a good value to address all detail levels.
  49. public:
  50. OpenSimplexNoise();
  51. ~OpenSimplexNoise();
  52. void _init_seeds();
  53. void set_seed(int seed);
  54. int get_seed() const;
  55. void set_octaves(int p_octaves);
  56. int get_octaves() const { return octaves; }
  57. void set_period(float p_period);
  58. float get_period() const { return period; }
  59. void set_persistence(float p_persistence);
  60. float get_persistence() const { return persistence; }
  61. void set_lacunarity(float p_lacunarity);
  62. float get_lacunarity() const { return lacunarity; }
  63. Ref<Image> get_image(int p_width, int p_height, const Vector2 &p_noise_offset = Vector2()) const;
  64. Ref<Image> get_seamless_image(int p_size) const;
  65. float get_noise_1d(float x) const;
  66. float get_noise_2d(float x, float y) const;
  67. float get_noise_3d(float x, float y, float z) const;
  68. float get_noise_4d(float x, float y, float z, float w) const;
  69. _FORCE_INLINE_ float _get_octave_noise_2d(int octave, float x, float y) const { return open_simplex_noise2(&(contexts[octave]), x, y); }
  70. _FORCE_INLINE_ float _get_octave_noise_3d(int octave, float x, float y, float z) const { return open_simplex_noise3(&(contexts[octave]), x, y, z); }
  71. _FORCE_INLINE_ float _get_octave_noise_4d(int octave, float x, float y, float z, float w) const { return open_simplex_noise4(&(contexts[octave]), x, y, z, w); }
  72. // Convenience
  73. _FORCE_INLINE_ float get_noise_2dv(const Vector2 &v) const { return get_noise_2d(v.x, v.y); }
  74. _FORCE_INLINE_ float get_noise_3dv(const Vector3 &v) const { return get_noise_3d(v.x, v.y, v.z); }
  75. protected:
  76. static void _bind_methods();
  77. };
  78. #endif // OPEN_SIMPLEX_NOISE_H