Debian Codegraph @

modules/noise/noise.cpp

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