| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | /*  open_simplex_noise.h                                                 */ | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | #ifndef OPEN_SIMPLEX_NOISE_H
 | 
					
						
							|  |  |  | #define OPEN_SIMPLEX_NOISE_H
 | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/io/image.h"
 | 
					
						
							|  |  |  | #include "core/object/reference.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | #include "scene/resources/texture.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "thirdparty/misc/open-simplex-noise.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | class OpenSimplexNoise : public Resource { | 
					
						
							| 
									
										
										
										
											2019-03-19 14:35:57 -04:00
										 |  |  | 	GDCLASS(OpenSimplexNoise, Resource); | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | 	OBJ_SAVE_TYPE(OpenSimplexNoise); | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 21:18:20 +01:00
										 |  |  | 	// The maximum number of octaves allowed. Note that these are statically allocated.
 | 
					
						
							|  |  |  | 	// Higher values become exponentially slower, so this shouldn't be set too high
 | 
					
						
							|  |  |  | 	// to avoid freezing the editor for long periods of time.
 | 
					
						
							|  |  |  | 	static const int MAX_OCTAVES = 9; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 23:49:17 +01:00
										 |  |  | 	osn_context contexts[MAX_OCTAVES]; | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int seed; | 
					
						
							| 
									
										
										
										
											2018-09-19 21:00:10 +02:00
										 |  |  | 	float persistence; // Controls details, value in [0,1]. Higher increases grain, lower increases smoothness.
 | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 	int octaves; // Number of noise layers
 | 
					
						
							|  |  |  | 	float period; // Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain.
 | 
					
						
							|  |  |  | 	float lacunarity; // Controls period change across octaves. 2 is usually a good value to address all detail levels.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | 	OpenSimplexNoise(); | 
					
						
							|  |  |  | 	~OpenSimplexNoise(); | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void _init_seeds(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void set_seed(int seed); | 
					
						
							| 
									
										
										
										
											2020-12-04 09:38:42 +01:00
										 |  |  | 	int get_seed() const; | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_octaves(int p_octaves); | 
					
						
							|  |  |  | 	int get_octaves() const { return octaves; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void set_period(float p_period); | 
					
						
							|  |  |  | 	float get_period() const { return period; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 21:00:10 +02:00
										 |  |  | 	void set_persistence(float p_persistence); | 
					
						
							|  |  |  | 	float get_persistence() const { return persistence; } | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_lacunarity(float p_lacunarity); | 
					
						
							|  |  |  | 	float get_lacunarity() const { return lacunarity; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 09:38:42 +01:00
										 |  |  | 	Ref<Image> get_image(int p_width, int p_height) const; | 
					
						
							|  |  |  | 	Ref<Image> get_seamless_image(int p_size) const; | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 09:38:42 +01:00
										 |  |  | 	float get_noise_1d(float x) const; | 
					
						
							|  |  |  | 	float get_noise_2d(float x, float y) const; | 
					
						
							|  |  |  | 	float get_noise_3d(float x, float y, float z) const; | 
					
						
							|  |  |  | 	float get_noise_4d(float x, float y, float z, float w) const; | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 09:38:42 +01:00
										 |  |  | 	_FORCE_INLINE_ float _get_octave_noise_2d(int octave, float x, float y) const { return open_simplex_noise2(&(contexts[octave]), x, y); } | 
					
						
							|  |  |  | 	_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); } | 
					
						
							|  |  |  | 	_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); } | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Convenience
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 09:38:42 +01:00
										 |  |  | 	_FORCE_INLINE_ float get_noise_2dv(const Vector2 &v) const { return get_noise_2d(v.x, v.y); } | 
					
						
							|  |  |  | 	_FORCE_INLINE_ float get_noise_3dv(const Vector3 &v) const { return get_noise_3d(v.x, v.y, v.z); } | 
					
						
							| 
									
										
										
										
											2018-08-25 00:25:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 	static void _bind_methods(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-28 14:30:07 +03:00
										 |  |  | #endif // OPEN_SIMPLEX_NOISE_H
 |