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