Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/noise/noise_texture_3d.cpp
21175 views
1
/**************************************************************************/
2
/* noise_texture_3d.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_3d.h"
32
33
#include "noise.h"
34
35
#include "servers/rendering/rendering_server.h"
36
37
NoiseTexture3D::NoiseTexture3D() {
38
noise = Ref<Noise>();
39
40
_queue_update();
41
}
42
43
NoiseTexture3D::~NoiseTexture3D() {
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 NoiseTexture3D::_bind_methods() {
54
ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture3D::set_width);
55
ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture3D::set_height);
56
ClassDB::bind_method(D_METHOD("set_depth", "depth"), &NoiseTexture3D::set_depth);
57
58
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture3D::set_noise);
59
ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::get_noise);
60
61
ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);
62
ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);
63
64
ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);
65
ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);
66
67
ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);
68
ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);
69
70
ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);
71
ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);
72
73
ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);
74
ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);
75
76
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");
77
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");
78
ADD_PROPERTY(PropertyInfo(Variant::INT, "depth", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_depth", "get_depth");
79
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, Noise::get_class_static()), "set_noise", "get_noise");
80
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_ramp", "get_color_ramp");
81
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
82
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");
83
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");
84
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");
85
}
86
87
void NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {
88
if (!Engine::get_singleton()->is_editor_hint()) {
89
return;
90
}
91
if (p_property.name == "seamless_blend_skirt") {
92
if (!seamless) {
93
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
94
}
95
}
96
}
97
98
void NoiseTexture3D::_set_texture_data(const TypedArray<Image> &p_data) {
99
if (!p_data.is_empty()) {
100
Vector<Ref<Image>> data;
101
102
data.resize(p_data.size());
103
104
for (int i = 0; i < data.size(); i++) {
105
data.write[i] = p_data[i];
106
}
107
108
if (texture.is_valid()) {
109
RID new_texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
110
RS::get_singleton()->texture_replace(texture, new_texture);
111
} else {
112
texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);
113
}
114
format = data[0]->get_format();
115
}
116
emit_changed();
117
}
118
119
void NoiseTexture3D::_thread_done(const TypedArray<Image> &p_data) {
120
_set_texture_data(p_data);
121
noise_thread.wait_to_finish();
122
if (regen_queued) {
123
noise_thread.start(_thread_function, this);
124
regen_queued = false;
125
}
126
}
127
128
void NoiseTexture3D::_thread_function(void *p_ud) {
129
NoiseTexture3D *tex = static_cast<NoiseTexture3D *>(p_ud);
130
callable_mp(tex, &NoiseTexture3D::_thread_done).call_deferred(tex->_generate_texture());
131
}
132
133
void NoiseTexture3D::_queue_update() {
134
if (update_queued) {
135
return;
136
}
137
138
update_queued = true;
139
callable_mp(this, &NoiseTexture3D::_update_texture).call_deferred();
140
}
141
142
TypedArray<Image> NoiseTexture3D::_generate_texture() {
143
// Prevent memdelete due to unref() on other thread.
144
Ref<Noise> ref_noise = noise;
145
146
if (ref_noise.is_null()) {
147
return TypedArray<Image>();
148
}
149
150
ERR_FAIL_COND_V_MSG((int64_t)width * height * depth > Image::MAX_PIXELS, TypedArray<Image>(), "The NoiseTexture3D is too big, consider lowering its width, height, or depth.");
151
152
Vector<Ref<Image>> images;
153
154
if (seamless) {
155
images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize);
156
} else {
157
images = ref_noise->_get_image(width, height, depth, invert, true, normalize);
158
}
159
160
if (color_ramp.is_valid()) {
161
for (int i = 0; i < images.size(); i++) {
162
images.write[i] = _modulate_with_gradient(images[i], color_ramp);
163
}
164
}
165
166
TypedArray<Image> new_data;
167
new_data.resize(images.size());
168
169
for (int i = 0; i < new_data.size(); i++) {
170
new_data[i] = images[i];
171
}
172
173
return new_data;
174
}
175
176
Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {
177
int w = p_image->get_width();
178
int h = p_image->get_height();
179
180
Ref<Image> new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);
181
182
for (int row = 0; row < h; row++) {
183
for (int col = 0; col < w; col++) {
184
Color pixel_color = p_image->get_pixel(col, row);
185
Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());
186
new_image->set_pixel(col, row, ramp_color);
187
}
188
}
189
190
return new_image;
191
}
192
193
void NoiseTexture3D::_update_texture() {
194
bool use_thread = true;
195
#ifndef THREADS_ENABLED
196
use_thread = false;
197
#endif
198
if (first_time) {
199
use_thread = false;
200
first_time = false;
201
}
202
if (use_thread) {
203
if (!noise_thread.is_started()) {
204
noise_thread.start(_thread_function, this);
205
regen_queued = false;
206
} else {
207
regen_queued = true;
208
}
209
210
} else {
211
TypedArray<Image> new_data = _generate_texture();
212
_set_texture_data(new_data);
213
}
214
update_queued = false;
215
}
216
217
void NoiseTexture3D::set_noise(Ref<Noise> p_noise) {
218
if (p_noise == noise) {
219
return;
220
}
221
if (noise.is_valid()) {
222
noise->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
223
}
224
noise = p_noise;
225
if (noise.is_valid()) {
226
noise->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
227
}
228
_queue_update();
229
}
230
231
Ref<Noise> NoiseTexture3D::get_noise() {
232
return noise;
233
}
234
235
void NoiseTexture3D::set_width(int p_width) {
236
ERR_FAIL_COND(p_width <= 0);
237
if (p_width == width) {
238
return;
239
}
240
width = p_width;
241
_queue_update();
242
}
243
244
void NoiseTexture3D::set_height(int p_height) {
245
ERR_FAIL_COND(p_height <= 0);
246
if (p_height == height) {
247
return;
248
}
249
height = p_height;
250
_queue_update();
251
}
252
253
void NoiseTexture3D::set_depth(int p_depth) {
254
ERR_FAIL_COND(p_depth <= 0);
255
if (p_depth == depth) {
256
return;
257
}
258
depth = p_depth;
259
_queue_update();
260
}
261
262
void NoiseTexture3D::set_invert(bool p_invert) {
263
if (p_invert == invert) {
264
return;
265
}
266
invert = p_invert;
267
_queue_update();
268
}
269
270
bool NoiseTexture3D::get_invert() const {
271
return invert;
272
}
273
274
void NoiseTexture3D::set_seamless(bool p_seamless) {
275
if (p_seamless == seamless) {
276
return;
277
}
278
seamless = p_seamless;
279
_queue_update();
280
notify_property_list_changed();
281
}
282
283
bool NoiseTexture3D::get_seamless() {
284
return seamless;
285
}
286
287
void NoiseTexture3D::set_seamless_blend_skirt(real_t p_blend_skirt) {
288
ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1);
289
290
if (p_blend_skirt == seamless_blend_skirt) {
291
return;
292
}
293
seamless_blend_skirt = p_blend_skirt;
294
_queue_update();
295
}
296
real_t NoiseTexture3D::get_seamless_blend_skirt() {
297
return seamless_blend_skirt;
298
}
299
300
void NoiseTexture3D::set_color_ramp(const Ref<Gradient> &p_gradient) {
301
if (p_gradient == color_ramp) {
302
return;
303
}
304
if (color_ramp.is_valid()) {
305
color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
306
}
307
color_ramp = p_gradient;
308
if (color_ramp.is_valid()) {
309
color_ramp->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));
310
}
311
_queue_update();
312
}
313
314
void NoiseTexture3D::set_normalize(bool p_normalize) {
315
if (normalize == p_normalize) {
316
return;
317
}
318
normalize = p_normalize;
319
_queue_update();
320
}
321
322
bool NoiseTexture3D::is_normalized() const {
323
return normalize;
324
}
325
326
Ref<Gradient> NoiseTexture3D::get_color_ramp() const {
327
return color_ramp;
328
}
329
330
int NoiseTexture3D::get_width() const {
331
return width;
332
}
333
334
int NoiseTexture3D::get_height() const {
335
return height;
336
}
337
338
int NoiseTexture3D::get_depth() const {
339
return depth;
340
}
341
342
bool NoiseTexture3D::has_mipmaps() const {
343
return false;
344
}
345
346
RID NoiseTexture3D::get_rid() const {
347
if (!texture.is_valid()) {
348
texture = RS::get_singleton()->texture_3d_placeholder_create();
349
}
350
351
return texture;
352
}
353
354
Vector<Ref<Image>> NoiseTexture3D::get_data() const {
355
ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());
356
return RS::get_singleton()->texture_3d_get(texture);
357
}
358
359
Image::Format NoiseTexture3D::get_format() const {
360
return format;
361
}
362
363