Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/noise/noise_texture_2d.cpp
10277 views
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
NoiseTexture2D::NoiseTexture2D() {
36
noise = Ref<Noise>();
37
38
_queue_update();
39
}
40
41
NoiseTexture2D::~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
51
void NoiseTexture2D::_bind_methods() {
52
ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);
53
ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);
54
55
ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);
56
ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);
57
58
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);
59
ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);
60
61
ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);
62
ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);
63
64
ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);
65
ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);
66
67
ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);
68
ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);
69
70
ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);
71
ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);
72
73
ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);
74
ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);
75
76
ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);
77
ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);
78
79
ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);
80
ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);
81
82
ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);
83
ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);
84
85
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
86
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
87
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");
88
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");
89
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");
90
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
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, "as_normal_map"), "set_as_normal_map", "is_normal_map");
94
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
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::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
97
}
98
99
void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {
100
if (!Engine::get_singleton()->is_editor_hint()) {
101
return;
102
}
103
if (p_property.name == "bump_strength") {
104
if (!as_normal_map) {
105
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
106
}
107
}
108
109
if (p_property.name == "seamless_blend_skirt") {
110
if (!seamless) {
111
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
112
}
113
}
114
}
115
116
void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {
117
image = p_image;
118
if (image.is_valid()) {
119
if (texture.is_valid()) {
120
RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
121
RS::get_singleton()->texture_replace(texture, new_texture);
122
} else {
123
texture = RS::get_singleton()->texture_2d_create(p_image);
124
}
125
RS::get_singleton()->texture_set_path(texture, get_path());
126
}
127
emit_changed();
128
}
129
130
void 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
139
void NoiseTexture2D::_thread_function(void *p_ud) {
140
NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);
141
callable_mp(tex, &NoiseTexture2D::_thread_done).call_deferred(tex->_generate_texture());
142
}
143
144
void NoiseTexture2D::_queue_update() {
145
if (update_queued) {
146
return;
147
}
148
149
update_queued = true;
150
callable_mp(this, &NoiseTexture2D::_update_texture).call_deferred();
151
}
152
153
Ref<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
181
Ref<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
198
void NoiseTexture2D::_update_texture() {
199
bool use_thread = true;
200
#ifndef THREADS_ENABLED
201
use_thread = false;
202
#endif
203
if (first_time) {
204
use_thread = false;
205
first_time = false;
206
}
207
if (use_thread) {
208
if (!noise_thread.is_started()) {
209
noise_thread.start(_thread_function, this);
210
regen_queued = false;
211
} else {
212
regen_queued = true;
213
}
214
215
} else {
216
Ref<Image> new_image = _generate_texture();
217
_set_texture_image(new_image);
218
}
219
update_queued = false;
220
}
221
222
void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {
223
if (p_noise == noise) {
224
return;
225
}
226
if (noise.is_valid()) {
227
noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
228
}
229
noise = p_noise;
230
if (noise.is_valid()) {
231
noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
232
}
233
_queue_update();
234
}
235
236
Ref<Noise> NoiseTexture2D::get_noise() {
237
return noise;
238
}
239
240
void NoiseTexture2D::set_width(int p_width) {
241
ERR_FAIL_COND(p_width <= 0);
242
if (p_width == size.x) {
243
return;
244
}
245
size.x = p_width;
246
_queue_update();
247
}
248
249
void NoiseTexture2D::set_height(int p_height) {
250
ERR_FAIL_COND(p_height <= 0);
251
if (p_height == size.y) {
252
return;
253
}
254
size.y = p_height;
255
_queue_update();
256
}
257
258
void NoiseTexture2D::set_invert(bool p_invert) {
259
if (p_invert == invert) {
260
return;
261
}
262
invert = p_invert;
263
_queue_update();
264
}
265
266
bool NoiseTexture2D::get_invert() const {
267
return invert;
268
}
269
270
void NoiseTexture2D::set_in_3d_space(bool p_enable) {
271
if (p_enable == in_3d_space) {
272
return;
273
}
274
in_3d_space = p_enable;
275
_queue_update();
276
}
277
bool NoiseTexture2D::is_in_3d_space() const {
278
return in_3d_space;
279
}
280
281
void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {
282
if (p_enable == generate_mipmaps) {
283
return;
284
}
285
generate_mipmaps = p_enable;
286
_queue_update();
287
}
288
289
bool NoiseTexture2D::is_generating_mipmaps() const {
290
return generate_mipmaps;
291
}
292
293
void NoiseTexture2D::set_seamless(bool p_seamless) {
294
if (p_seamless == seamless) {
295
return;
296
}
297
seamless = p_seamless;
298
_queue_update();
299
notify_property_list_changed();
300
}
301
302
bool NoiseTexture2D::get_seamless() {
303
return seamless;
304
}
305
306
void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {
307
ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);
308
309
if (p_blend_skirt == seamless_blend_skirt) {
310
return;
311
}
312
seamless_blend_skirt = p_blend_skirt;
313
_queue_update();
314
}
315
real_t NoiseTexture2D::get_seamless_blend_skirt() {
316
return seamless_blend_skirt;
317
}
318
319
void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {
320
if (p_as_normal_map == as_normal_map) {
321
return;
322
}
323
as_normal_map = p_as_normal_map;
324
_queue_update();
325
notify_property_list_changed();
326
}
327
328
bool NoiseTexture2D::is_normal_map() {
329
return as_normal_map;
330
}
331
332
void NoiseTexture2D::set_bump_strength(float p_bump_strength) {
333
if (p_bump_strength == bump_strength) {
334
return;
335
}
336
bump_strength = p_bump_strength;
337
if (as_normal_map) {
338
_queue_update();
339
}
340
}
341
342
float NoiseTexture2D::get_bump_strength() {
343
return bump_strength;
344
}
345
346
void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {
347
if (p_gradient == color_ramp) {
348
return;
349
}
350
if (color_ramp.is_valid()) {
351
color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
352
}
353
color_ramp = p_gradient;
354
if (color_ramp.is_valid()) {
355
color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));
356
}
357
_queue_update();
358
}
359
360
void NoiseTexture2D::set_normalize(bool p_normalize) {
361
if (normalize == p_normalize) {
362
return;
363
}
364
normalize = p_normalize;
365
_queue_update();
366
}
367
368
bool NoiseTexture2D::is_normalized() const {
369
return normalize;
370
}
371
372
Ref<Gradient> NoiseTexture2D::get_color_ramp() const {
373
return color_ramp;
374
}
375
376
int NoiseTexture2D::get_width() const {
377
return size.x;
378
}
379
380
int NoiseTexture2D::get_height() const {
381
return size.y;
382
}
383
384
RID NoiseTexture2D::get_rid() const {
385
if (!texture.is_valid()) {
386
texture = RS::get_singleton()->texture_2d_placeholder_create();
387
}
388
389
return texture;
390
}
391
392
Ref<Image> NoiseTexture2D::get_image() const {
393
return image;
394
}
395
396