1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 | /**************************************************************************/
/* noise_texture.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "noise_texture.h"
#include "core/core_string_names.h"
NoiseTexture::NoiseTexture() {
update_queued = false;
regen_queued = false;
first_time = true;
size = Vector2i(512, 512);<--- Variable 'size' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'size' a value by passing the value to the constructor in the initialization list.
seamless = false;
as_normalmap = false;
bump_strength = 8.0;
flags = FLAGS_DEFAULT;
noise = Ref<OpenSimplexNoise>();
texture = RID_PRIME(VS::get_singleton()->texture_create());
_queue_update();
}
NoiseTexture::~NoiseTexture() {
VS::get_singleton()->free(texture);
noise_thread.wait_to_finish();
}
void NoiseTexture::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width);
ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height);
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise);
ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise);
ClassDB::bind_method(D_METHOD("set_noise_offset", "noise_offset"), &NoiseTexture::set_noise_offset);
ClassDB::bind_method(D_METHOD("get_noise_offset"), &NoiseTexture::get_noise_offset);
ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless);
ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
ClassDB::bind_method(D_METHOD("set_as_normalmap", "as_normalmap"), &NoiseTexture::set_as_normalmap);
ClassDB::bind_method(D_METHOD("is_normalmap"), &NoiseTexture::is_normalmap);
ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength);
ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength);
ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
ClassDB::bind_method(D_METHOD("_queue_update"), &NoiseTexture::_queue_update);
ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normalmap"), "set_as_normalmap", "is_normalmap");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "noise_offset"), "set_noise_offset", "get_noise_offset");
}
void NoiseTexture::_validate_property(PropertyInfo &property) const {
if (property.name == "bump_strength") {
if (!as_normalmap) {
property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
}
}
}
void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
data = p_image;
if (data.is_valid()) {
VS::get_singleton()->texture_allocate(texture, size.x, size.y, 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, flags);
VS::get_singleton()->texture_set_data(texture, p_image);
}
emit_changed();
}
void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
_set_texture_data(p_image);
noise_thread.wait_to_finish();
if (regen_queued) {
noise_thread.start(_thread_function, this);
regen_queued = false;
}
}
void NoiseTexture::_thread_function(void *p_ud) {
NoiseTexture *tex = (NoiseTexture *)p_ud;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
tex->call_deferred("_thread_done", tex->_generate_texture());
}
void NoiseTexture::_queue_update() {
if (update_queued) {
return;
}
update_queued = true;
call_deferred("_update_texture");
}
Ref<Image> NoiseTexture::_generate_texture() {
// Prevent memdelete due to unref() on other thread.
Ref<OpenSimplexNoise> ref_noise = noise;
if (ref_noise.is_null()) {
return Ref<Image>();
}
Ref<Image> image;
if (seamless) {
image = ref_noise->get_seamless_image(size.x);
} else {
image = ref_noise->get_image(size.x, size.y, noise_offset);
}
if (as_normalmap) {
image->bumpmap_to_normalmap(bump_strength);
}
return image;
}
void NoiseTexture::_update_texture() {
bool use_thread = true;
if (first_time) {
use_thread = false;<--- use_thread is assigned
first_time = false;
}
#ifdef NO_THREADS
use_thread = false;<--- Assignment 'use_thread=false', assigned value is 0<--- use_thread is overwritten
#endif
if (use_thread) {<--- Condition 'use_thread' is always false
if (!noise_thread.is_started()) {
noise_thread.start(_thread_function, this);
regen_queued = false;
} else {
regen_queued = true;
}
} else {
Ref<Image> image = _generate_texture();
_set_texture_data(image);
}
update_queued = false;
}
void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) {
if (p_noise == noise) {
return;
}
if (noise.is_valid()) {
noise->disconnect(CoreStringNames::get_singleton()->changed, this, "_queue_update");
}
noise = p_noise;
if (noise.is_valid()) {
noise->connect(CoreStringNames::get_singleton()->changed, this, "_queue_update");
}
_queue_update();
}
Ref<OpenSimplexNoise> NoiseTexture::get_noise() {
return noise;
}
void NoiseTexture::set_width(int p_width) {
if (p_width == size.x) {
return;
}
size.x = p_width;
_queue_update();
}
void NoiseTexture::set_height(int p_height) {
if (p_height == size.y) {
return;
}
size.y = p_height;
_queue_update();
}
void NoiseTexture::set_noise_offset(Vector2 p_noise_offset) {
if (noise_offset == p_noise_offset) {
return;
}
noise_offset = p_noise_offset;
_queue_update();
}
void NoiseTexture::set_seamless(bool p_seamless) {
if (p_seamless == seamless) {
return;
}
seamless = p_seamless;
_queue_update();
}
bool NoiseTexture::get_seamless() {
return seamless;
}
void NoiseTexture::set_as_normalmap(bool p_as_normalmap) {
if (p_as_normalmap == as_normalmap) {
return;
}
as_normalmap = p_as_normalmap;
_queue_update();
_change_notify();
}
bool NoiseTexture::is_normalmap() {
return as_normalmap;
}
void NoiseTexture::set_bump_strength(float p_bump_strength) {
if (p_bump_strength == bump_strength) {
return;
}
bump_strength = p_bump_strength;
if (as_normalmap) {
_queue_update();
}
}
float NoiseTexture::get_bump_strength() {
return bump_strength;
}
int NoiseTexture::get_width() const {
return size.x;
}
int NoiseTexture::get_height() const {
return size.y;
}
Vector2 NoiseTexture::get_noise_offset() const {
return noise_offset;
}
void NoiseTexture::set_flags(uint32_t p_flags) {
flags = p_flags;
VS::get_singleton()->texture_set_flags(texture, flags);
}
uint32_t NoiseTexture::get_flags() const {
return flags;
}
Ref<Image> NoiseTexture::get_data() const {
return data;
}
|