open_simplex_noise.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. osn_context contexts[6];
  40. int seed;
  41. float persistence; // Controls details, value in [0,1]. Higher increases grain, lower increases smoothness.
  42. int octaves; // Number of noise layers
  43. float period; // Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain.
  44. float lacunarity; // Controls period change across octaves. 2 is usually a good value to address all detail levels.
  45. public:
  46. OpenSimplexNoise();
  47. ~OpenSimplexNoise();
  48. void _init_seeds();
  49. void set_seed(int seed);
  50. int get_seed();
  51. void set_octaves(int p_octaves);
  52. int get_octaves() const { return octaves; }
  53. void set_period(float p_period);
  54. float get_period() const { return period; }
  55. void set_persistence(float p_persistence);
  56. float get_persistence() const { return persistence; }
  57. void set_lacunarity(float p_lacunarity);
  58. float get_lacunarity() const { return lacunarity; }
  59. Ref<Image> get_image(int p_width, int p_height);
  60. Ref<Image> get_seamless_image(int p_size);
  61. float get_noise_2d(float x, float y);
  62. float get_noise_3d(float x, float y, float z);
  63. float get_noise_4d(float x, float y, float z, float w);
  64. _FORCE_INLINE_ float _get_octave_noise_2d(int octave, float x, float y) { return open_simplex_noise2(&(contexts[octave]), x, y); }
  65. _FORCE_INLINE_ float _get_octave_noise_3d(int octave, float x, float y, float z) { return open_simplex_noise3(&(contexts[octave]), x, y, z); }
  66. _FORCE_INLINE_ float _get_octave_noise_4d(int octave, float x, float y, float z, float w) { return open_simplex_noise4(&(contexts[octave]), x, y, z, w); }
  67. // Convenience
  68. _FORCE_INLINE_ float get_noise_2dv(Vector2 v) { return get_noise_2d(v.x, v.y); }
  69. _FORCE_INLINE_ float get_noise_3dv(Vector3 v) { return get_noise_3d(v.x, v.y, v.z); }
  70. protected:
  71. static void _bind_methods();
  72. };
  73. #endif // OPEN_SIMPLEX_NOISE_H