| 1 | /**************************************************************************/ |
| 2 | /* noise.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "noise.h" |
| 32 | |
| 33 | #include <float.h> |
| 34 | |
| 35 | Vector<Ref<Image>> Noise::_get_seamless_image(int p_width, int p_height, int p_depth, bool p_invert, bool p_in_3d_space, real_t p_blend_skirt, bool p_normalize) const { |
| 36 | ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0 || p_depth <= 0, Vector<Ref<Image>>()); |
| 37 | |
| 38 | int skirt_width = MAX(1, p_width * p_blend_skirt); |
| 39 | int skirt_height = MAX(1, p_height * p_blend_skirt); |
| 40 | int skirt_depth = MAX(1, p_depth * p_blend_skirt); |
| 41 | int src_width = p_width + skirt_width; |
| 42 | int src_height = p_height + skirt_height; |
| 43 | int src_depth = p_depth + skirt_depth; |
| 44 | |
| 45 | Vector<Ref<Image>> src = _get_image(src_width, src_height, src_depth, p_invert, p_in_3d_space, p_normalize); |
| 46 | bool grayscale = (src[0]->get_format() == Image::FORMAT_L8); |
| 47 | |
| 48 | if (grayscale) { |
| 49 | return _generate_seamless_image<uint8_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt); |
| 50 | } else { |
| 51 | return _generate_seamless_image<uint32_t>(src, p_width, p_height, p_depth, p_invert, p_blend_skirt); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | Ref<Image> Noise::get_seamless_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, real_t p_blend_skirt, bool p_normalize) const { |
| 56 | Vector<Ref<Image>> images = _get_seamless_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_blend_skirt, p_normalize); |
| 57 | return images[0]; |
| 58 | } |
| 59 | |
| 60 | TypedArray<Image> Noise::get_seamless_image_3d(int p_width, int p_height, int p_depth, bool p_invert, real_t p_blend_skirt, bool p_normalize) const { |
| 61 | Vector<Ref<Image>> images = _get_seamless_image(p_width, p_height, p_depth, p_invert, true, p_blend_skirt, p_normalize); |
| 62 | |
| 63 | TypedArray<Image> ret; |
| 64 | ret.resize(images.size()); |
| 65 | for (int i = 0; i < images.size(); i++) { |
| 66 | ret[i] = images[i]; |
| 67 | } |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | // Template specialization for faster grayscale blending. |
| 72 | template <> |
| 73 | uint8_t Noise::_alpha_blend<uint8_t>(uint8_t p_bg, uint8_t p_fg, int p_alpha) const { |
| 74 | uint16_t alpha = p_alpha + 1; |
| 75 | uint16_t inv_alpha = 256 - p_alpha; |
| 76 | |
| 77 | return (uint8_t)((alpha * p_fg + inv_alpha * p_bg) >> 8); |
| 78 | } |
| 79 | |
| 80 | Vector<Ref<Image>> Noise::_get_image(int p_width, int p_height, int p_depth, bool p_invert, bool p_in_3d_space, bool p_normalize) const { |
| 81 | ERR_FAIL_COND_V(p_width <= 0 || p_height <= 0 || p_depth <= 0, Vector<Ref<Image>>()); |
| 82 | |
| 83 | Vector<Ref<Image>> images; |
| 84 | images.resize(p_depth); |
| 85 | |
| 86 | if (p_normalize) { |
| 87 | // Get all values and identify min/max values. |
| 88 | LocalVector<real_t> values; |
| 89 | values.resize(p_width * p_height * p_depth); |
| 90 | |
| 91 | real_t min_val = FLT_MAX; |
| 92 | real_t max_val = -FLT_MAX; |
| 93 | int idx = 0; |
| 94 | for (int d = 0; d < p_depth; d++) { |
| 95 | for (int y = 0; y < p_height; y++) { |
| 96 | for (int x = 0; x < p_width; x++) { |
| 97 | values[idx] = p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y); |
| 98 | if (values[idx] > max_val) { |
| 99 | max_val = values[idx]; |
| 100 | } |
| 101 | if (values[idx] < min_val) { |
| 102 | min_val = values[idx]; |
| 103 | } |
| 104 | idx++; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | idx = 0; |
| 109 | // Normalize values and write to texture. |
| 110 | for (int d = 0; d < p_depth; d++) { |
| 111 | Vector<uint8_t> data; |
| 112 | data.resize(p_width * p_height); |
| 113 | |
| 114 | uint8_t *wd8 = data.ptrw(); |
| 115 | uint8_t ivalue; |
| 116 | |
| 117 | for (int y = 0; y < p_height; y++) { |
| 118 | for (int x = 0; x < p_width; x++) { |
| 119 | if (max_val == min_val) { |
| 120 | ivalue = 0; |
| 121 | } else { |
| 122 | ivalue = static_cast<uint8_t>(CLAMP((values[idx] - min_val) / (max_val - min_val) * 255.f, 0, 255)); |
| 123 | } |
| 124 | |
| 125 | if (p_invert) { |
| 126 | ivalue = 255 - ivalue; |
| 127 | } |
| 128 | |
| 129 | wd8[x + y * p_width] = ivalue; |
| 130 | idx++; |
| 131 | } |
| 132 | } |
| 133 | Ref<Image> img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data)); |
| 134 | images.write[d] = img; |
| 135 | } |
| 136 | } else { |
| 137 | // Without normalization, the expected range of the noise function is [-1, 1]. |
| 138 | |
| 139 | for (int d = 0; d < p_depth; d++) { |
| 140 | Vector<uint8_t> data; |
| 141 | data.resize(p_width * p_height); |
| 142 | |
| 143 | uint8_t *wd8 = data.ptrw(); |
| 144 | |
| 145 | uint8_t ivalue; |
| 146 | int idx = 0; |
| 147 | for (int y = 0; y < p_height; y++) { |
| 148 | for (int x = 0; x < p_width; x++) { |
| 149 | float value = (p_in_3d_space ? get_noise_3d(x, y, d) : get_noise_2d(x, y)); |
| 150 | ivalue = static_cast<uint8_t>(CLAMP(value * 127.5f + 127.5f, 0.0f, 255.0f)); |
| 151 | wd8[idx] = p_invert ? (255 - ivalue) : ivalue; |
| 152 | idx++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | Ref<Image> img = memnew(Image(p_width, p_height, false, Image::FORMAT_L8, data)); |
| 157 | images.write[d] = img; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return images; |
| 162 | } |
| 163 | |
| 164 | Ref<Image> Noise::get_image(int p_width, int p_height, bool p_invert, bool p_in_3d_space, bool p_normalize) const { |
| 165 | Vector<Ref<Image>> images = _get_image(p_width, p_height, 1, p_invert, p_in_3d_space, p_normalize); |
| 166 | return images[0]; |
| 167 | } |
| 168 | |
| 169 | TypedArray<Image> Noise::get_image_3d(int p_width, int p_height, int p_depth, bool p_invert, bool p_normalize) const { |
| 170 | Vector<Ref<Image>> images = _get_image(p_width, p_height, p_depth, p_invert, true, p_normalize); |
| 171 | |
| 172 | TypedArray<Image> ret; |
| 173 | ret.resize(images.size()); |
| 174 | for (int i = 0; i < images.size(); i++) { |
| 175 | ret[i] = images[i]; |
| 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | void Noise::_bind_methods() { |
| 181 | // Noise functions. |
| 182 | ClassDB::bind_method(D_METHOD("get_noise_1d" , "x" ), &Noise::get_noise_1d); |
| 183 | ClassDB::bind_method(D_METHOD("get_noise_2d" , "x" , "y" ), &Noise::get_noise_2d); |
| 184 | ClassDB::bind_method(D_METHOD("get_noise_2dv" , "v" ), &Noise::get_noise_2dv); |
| 185 | ClassDB::bind_method(D_METHOD("get_noise_3d" , "x" , "y" , "z" ), &Noise::get_noise_3d); |
| 186 | ClassDB::bind_method(D_METHOD("get_noise_3dv" , "v" ), &Noise::get_noise_3dv); |
| 187 | |
| 188 | // Textures. |
| 189 | ClassDB::bind_method(D_METHOD("get_image" , "width" , "height" , "invert" , "in_3d_space" , "normalize" ), &Noise::get_image, DEFVAL(false), DEFVAL(false), DEFVAL(true)); |
| 190 | ClassDB::bind_method(D_METHOD("get_seamless_image" , "width" , "height" , "invert" , "in_3d_space" , "skirt" , "normalize" ), &Noise::get_seamless_image, DEFVAL(false), DEFVAL(false), DEFVAL(0.1), DEFVAL(true)); |
| 191 | ClassDB::bind_method(D_METHOD("get_image_3d" , "width" , "height" , "depth" , "invert" , "normalize" ), &Noise::get_image_3d, DEFVAL(false), DEFVAL(true)); |
| 192 | ClassDB::bind_method(D_METHOD("get_seamless_image_3d" , "width" , "height" , "depth" , "invert" , "skirt" , "normalize" ), &Noise::get_seamless_image_3d, DEFVAL(false), DEFVAL(0.1), DEFVAL(true)); |
| 193 | } |
| 194 | |