Debian Codegraph @

modules/noise/noise_texture_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* noise_texture_2d.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_texture_2d.h"
32
33 #include "noise.h"
34
35 #include "servers/rendering/rendering_server.h"
36
37 NoiseTexture2D::NoiseTexture2D() {
38 noise = Ref<Noise>();
39
40 _queue_update();
41 }
42
43 NoiseTexture2D::~NoiseTexture2D() {
44 ERR_FAIL_NULL(RenderingServer::get_singleton());
45 if (texture.is_valid()) {
46 RS::get_singleton()->free_rid(texture);
47 }
48 if (noise_thread.is_started()) {
49 noise_thread.wait_to_finish();
50 }
51 }
52
53 void NoiseTexture2D::_bind_methods() {
54 ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);
55 ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);
56
57 ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);
58 ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);
59
60 ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);
61 ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);
62
63 ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);
64 ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);
65
66 ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);
67 ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);
68
69 ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);
70 ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);
71
72 ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);
73 ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);
74
75 ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);
76 ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);
77
78 ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);
79 ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);
80
81 ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);
82 ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);
83
84 ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);
85 ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);
86
87 ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
88 ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
89 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");
90 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
91 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
92 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
93 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
94 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space");
95 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");
96 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
97 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
98 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
99 }
100
101 void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {
102 if (!Engine::get_singleton()->is_editor_hint()) {
103 return;
104 }
105 if (p_property.name == "bump_strength") {
106 if (!as_normal_map) {
107 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
108 }
109 }
110
111 if (p_property.name == "seamless_blend_skirt") {
112 if (!seamless) {
113 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
114 }
115 }
116 }
117
118 void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {
119 image = p_image;
120 if (image.is_valid()) {
121 if (texture.is_valid()) {
122 RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
123 RS::get_singleton()->texture_replace(texture, new_texture);
124 } else {
125 texture = RS::get_singleton()->texture_2d_create(p_image);
126 }
127 RS::get_singleton()->texture_set_path(texture, get_path());
128 }
129 emit_changed();
130 }
131
132 void NoiseTexture2D::_thread_done(const Ref<Image> &p_image) {
133 _set_texture_image(p_image);
134 noise_thread.wait_to_finish();
135 if (regen_queued) {
136 noise_thread.start(_thread_function, this);
137 regen_queued = false;
138 }
139 }
140
141 void NoiseTexture2D::_thread_function(void *p_ud) {
142 NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);
143 callable_mp(tex, &NoiseTexture2D::_thread_done).call_deferred(tex->_generate_texture());
144 }
145
146 void NoiseTexture2D::_queue_update() {
147 if (update_queued) {
148 return;
149 }
150
151 update_queued = true;
152 callable_mp(this, &NoiseTexture2D::_update_texture).call_deferred();
153 }
154
155 Ref<Image> NoiseTexture2D::_generate_texture() {
156 // Prevent memdelete due to unref() on other thread.
157 Ref<Noise> ref_noise = noise;
158
159 if (ref_noise.is_null()) {
160 return Ref<Image>();
161 }
162
163 Ref<Image> new_image;
164
165 if (seamless) {
166 new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);
167 } else {
168 new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);
169 }
170 if (color_ramp.is_valid()) {
171 new_image = _modulate_with_gradient(new_image, color_ramp);
172 }
173 if (as_normal_map) {
174 new_image->bump_map_to_normal_map(bump_strength);
175 }
176 if (generate_mipmaps) {
177 new_image->generate_mipmaps();
178 }
179
180 return new_image;
181 }
182
183 Ref<Image> NoiseTexture2D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
184 int width = p_image->get_width();
185 int height = p_image->get_height();
186
187 Ref<Image> new_image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);
188
189 for (int row = 0; row < height; row++) {
190 for (int col = 0; col < width; col++) {
191 Color pixel_color = p_image->get_pixel(col, row);
192 Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
193 new_image->set_pixel(col, row, ramp_color);
194 }
195 }
196
197 return new_image;
198 }
199
200 void NoiseTexture2D::_update_texture() {
201 bool use_thread = true;
202 #ifndef THREADS_ENABLED
203 use_thread = false;
204 #endif
205 if (first_time) {
206 use_thread = false;
207 first_time = false;
208 }
209 if (use_thread) {
210 if (!noise_thread.is_started()) {
211 noise_thread.start(_thread_function, this);
212 regen_queued = false;
213 } else {
214 regen_queued = true;
215 }
216
217 } else {
218 Ref<Image> new_image = _generate_texture();
219 _set_texture_image(new_image);
220 }
221 update_queued = false;
222 }
223
224 void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {
225 if (p_noise == noise) {
226 return;
227 }
228 if (noise.is_valid()) {
229 noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
230 }
231 noise = p_noise;
232 if (noise.is_valid()) {
233 noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
234 }
235 _queue_update();
236 }
237
238 Ref<Noise> NoiseTexture2D::get_noise() {
239 return noise;
240 }
241
242 void NoiseTexture2D::set_width(int p_width) {
243 ERR_FAIL_COND(p_width <= 0);
244 if (p_width == size.x) {
245 return;
246 }
247 size.x = p_width;
248 _queue_update();
249 }
250
251 void NoiseTexture2D::set_height(int p_height) {
252 ERR_FAIL_COND(p_height <= 0);
253 if (p_height == size.y) {
254 return;
255 }
256 size.y = p_height;
257 _queue_update();
258 }
259
260 void NoiseTexture2D::set_invert(bool p_invert) {
261 if (p_invert == invert) {
262 return;
263 }
264 invert = p_invert;
265 _queue_update();
266 }
267
268 bool NoiseTexture2D::get_invert() const {
269 return invert;
270 }
271
272 void NoiseTexture2D::set_in_3d_space(bool p_enable) {
273 if (p_enable == in_3d_space) {
274 return;
275 }
276 in_3d_space = p_enable;
277 _queue_update();
278 }
279 bool NoiseTexture2D::is_in_3d_space() const {
280 return in_3d_space;
281 }
282
283 void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {
284 if (p_enable == generate_mipmaps) {
285 return;
286 }
287 generate_mipmaps = p_enable;
288 _queue_update();
289 }
290
291 bool NoiseTexture2D::is_generating_mipmaps() const {
292 return generate_mipmaps;
293 }
294
295 void NoiseTexture2D::set_seamless(bool p_seamless) {
296 if (p_seamless == seamless) {
297 return;
298 }
299 seamless = p_seamless;
300 _queue_update();
301 notify_property_list_changed();
302 }
303
304 bool NoiseTexture2D::get_seamless() {
305 return seamless;
306 }
307
308 void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {
309 ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);
310
311 if (p_blend_skirt == seamless_blend_skirt) {
312 return;
313 }
314 seamless_blend_skirt = p_blend_skirt;
315 _queue_update();
316 }
317 real_t NoiseTexture2D::get_seamless_blend_skirt() {
318 return seamless_blend_skirt;
319 }
320
321 void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {
322 if (p_as_normal_map == as_normal_map) {
323 return;
324 }
325 as_normal_map = p_as_normal_map;
326 _queue_update();
327 notify_property_list_changed();
328 }
329
330 bool NoiseTexture2D::is_normal_map() {
331 return as_normal_map;
332 }
333
334 void NoiseTexture2D::set_bump_strength(float p_bump_strength) {
335 if (p_bump_strength == bump_strength) {
336 return;
337 }
338 bump_strength = p_bump_strength;
339 if (as_normal_map) {
340 _queue_update();
341 }
342 }
343
344 float NoiseTexture2D::get_bump_strength() {
345 return bump_strength;
346 }
347
348 void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {
349 if (p_gradient == color_ramp) {
350 return;
351 }
352 if (color_ramp.is_valid()) {
353 color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
354 }
355 color_ramp = p_gradient;
356 if (color_ramp.is_valid()) {
357 color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
358 }
359 _queue_update();
360 }
361
362 void NoiseTexture2D::set_normalize(bool p_normalize) {
363 if (normalize == p_normalize) {
364 return;
365 }
366 normalize = p_normalize;
367 _queue_update();
368 }
369
370 bool NoiseTexture2D::is_normalized() const {
371 return normalize;
372 }
373
374 Ref<Gradient> NoiseTexture2D::get_color_ramp() const {
375 return color_ramp;
376 }
377
378 int NoiseTexture2D::get_width() const {
379 return size.x;
380 }
381
382 int NoiseTexture2D::get_height() const {
383 return size.y;
384 }
385
386 RID NoiseTexture2D::get_rid() const {
387 if (!texture.is_valid()) {
388 texture = RS::get_singleton()->texture_2d_placeholder_create();
389 }
390
391 return texture;
392 }
393
394 Ref<Image> NoiseTexture2D::get_image() const {
395 return image;
396 }