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