Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/gradient_texture.cpp
9903 views
1
/**************************************************************************/
2
/* gradient_texture.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 "gradient_texture.h"
32
33
#include "core/math/geometry_2d.h"
34
35
GradientTexture1D::GradientTexture1D() {
36
_queue_update();
37
}
38
39
GradientTexture1D::~GradientTexture1D() {
40
if (texture.is_valid()) {
41
ERR_FAIL_NULL(RenderingServer::get_singleton());
42
RS::get_singleton()->free(texture);
43
}
44
}
45
46
void GradientTexture1D::_bind_methods() {
47
ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);
48
ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);
49
50
ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);
51
// The `get_width()` method is already exposed by the parent class Texture2D.
52
53
ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);
54
ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);
55
56
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
57
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");
58
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
59
}
60
61
void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {
62
if (p_gradient == gradient) {
63
return;
64
}
65
if (gradient.is_valid()) {
66
gradient->disconnect_changed(callable_mp(this, &GradientTexture1D::_queue_update));
67
}
68
gradient = p_gradient;
69
if (gradient.is_valid()) {
70
gradient->connect_changed(callable_mp(this, &GradientTexture1D::_queue_update));
71
}
72
_queue_update();
73
emit_changed();
74
}
75
76
Ref<Gradient> GradientTexture1D::get_gradient() const {
77
return gradient;
78
}
79
80
void GradientTexture1D::_queue_update() {
81
if (update_pending) {
82
return;
83
}
84
update_pending = true;
85
callable_mp(this, &GradientTexture1D::update_now).call_deferred();
86
}
87
88
void GradientTexture1D::_update() const {
89
update_pending = false;
90
91
if (gradient.is_null()) {
92
return;
93
}
94
95
if (use_hdr) {
96
// High dynamic range.
97
Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));
98
Gradient &g = **gradient;
99
// `create()` isn't available for non-uint8_t data, so fill in the data manually.
100
for (int i = 0; i < width; i++) {
101
float ofs = float(i) / (width - 1);
102
image->set_pixel(i, 0, g.get_color_at_offset(ofs));
103
}
104
105
if (texture.is_valid()) {
106
RID new_texture = RS::get_singleton()->texture_2d_create(image);
107
RS::get_singleton()->texture_replace(texture, new_texture);
108
} else {
109
texture = RS::get_singleton()->texture_2d_create(image);
110
}
111
} else {
112
// Low dynamic range. "Overbright" colors will be clamped.
113
Vector<uint8_t> data;
114
data.resize(width * 4);
115
{
116
uint8_t *wd8 = data.ptrw();
117
Gradient &g = **gradient;
118
119
for (int i = 0; i < width; i++) {
120
float ofs = float(i) / (width - 1);
121
Color color = g.get_color_at_offset(ofs);
122
123
wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));
124
wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));
125
wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));
126
wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));
127
}
128
}
129
130
Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));
131
132
if (texture.is_valid()) {
133
RID new_texture = RS::get_singleton()->texture_2d_create(image);
134
RS::get_singleton()->texture_replace(texture, new_texture);
135
} else {
136
texture = RS::get_singleton()->texture_2d_create(image);
137
}
138
}
139
RS::get_singleton()->texture_set_path(texture, get_path());
140
}
141
142
void GradientTexture1D::set_width(int p_width) {
143
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
144
width = p_width;
145
_queue_update();
146
emit_changed();
147
}
148
149
int GradientTexture1D::get_width() const {
150
return width;
151
}
152
153
void GradientTexture1D::set_use_hdr(bool p_enabled) {
154
if (p_enabled == use_hdr) {
155
return;
156
}
157
158
use_hdr = p_enabled;
159
_queue_update();
160
emit_changed();
161
}
162
163
bool GradientTexture1D::is_using_hdr() const {
164
return use_hdr;
165
}
166
167
RID GradientTexture1D::get_rid() const {
168
if (!texture.is_valid()) {
169
texture = RS::get_singleton()->texture_2d_placeholder_create();
170
}
171
return texture;
172
}
173
174
Ref<Image> GradientTexture1D::get_image() const {
175
update_now();
176
if (!texture.is_valid()) {
177
return Ref<Image>();
178
}
179
return RenderingServer::get_singleton()->texture_2d_get(texture);
180
}
181
182
void GradientTexture1D::update_now() const {
183
if (update_pending) {
184
_update();
185
}
186
}
187
188
//////////////////
189
190
GradientTexture2D::GradientTexture2D() {
191
_queue_update();
192
}
193
194
GradientTexture2D::~GradientTexture2D() {
195
if (texture.is_valid()) {
196
ERR_FAIL_NULL(RenderingServer::get_singleton());
197
RS::get_singleton()->free(texture);
198
}
199
}
200
201
void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {
202
if (gradient == p_gradient) {
203
return;
204
}
205
if (gradient.is_valid()) {
206
gradient->disconnect_changed(callable_mp(this, &GradientTexture2D::_queue_update));
207
}
208
gradient = p_gradient;
209
if (gradient.is_valid()) {
210
gradient->connect_changed(callable_mp(this, &GradientTexture2D::_queue_update));
211
}
212
_queue_update();
213
emit_changed();
214
}
215
216
Ref<Gradient> GradientTexture2D::get_gradient() const {
217
return gradient;
218
}
219
220
void GradientTexture2D::_queue_update() {
221
if (update_pending) {
222
return;
223
}
224
update_pending = true;
225
callable_mp(this, &GradientTexture2D::update_now).call_deferred();
226
}
227
228
void GradientTexture2D::_update() const {
229
update_pending = false;
230
231
if (gradient.is_null()) {
232
return;
233
}
234
Ref<Image> image;
235
image.instantiate();
236
237
if (gradient->get_point_count() <= 1) { // No need to interpolate.
238
image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);
239
image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));
240
} else {
241
if (use_hdr) {
242
image->initialize_data(width, height, false, Image::FORMAT_RGBAF);
243
Gradient &g = **gradient;
244
// `create()` isn't available for non-uint8_t data, so fill in the data manually.
245
for (int y = 0; y < height; y++) {
246
for (int x = 0; x < width; x++) {
247
float ofs = _get_gradient_offset_at(x, y);
248
image->set_pixel(x, y, g.get_color_at_offset(ofs));
249
}
250
}
251
} else {
252
Vector<uint8_t> data;
253
data.resize(width * height * 4);
254
{
255
uint8_t *wd8 = data.ptrw();
256
Gradient &g = **gradient;
257
for (int y = 0; y < height; y++) {
258
for (int x = 0; x < width; x++) {
259
float ofs = _get_gradient_offset_at(x, y);
260
const Color &c = g.get_color_at_offset(ofs);
261
262
wd8[(x + (y * width)) * 4 + 0] = uint8_t(CLAMP(c.r * 255.0, 0, 255));
263
wd8[(x + (y * width)) * 4 + 1] = uint8_t(CLAMP(c.g * 255.0, 0, 255));
264
wd8[(x + (y * width)) * 4 + 2] = uint8_t(CLAMP(c.b * 255.0, 0, 255));
265
wd8[(x + (y * width)) * 4 + 3] = uint8_t(CLAMP(c.a * 255.0, 0, 255));
266
}
267
}
268
}
269
image->set_data(width, height, false, Image::FORMAT_RGBA8, data);
270
}
271
}
272
273
if (texture.is_valid()) {
274
RID new_texture = RS::get_singleton()->texture_2d_create(image);
275
RS::get_singleton()->texture_replace(texture, new_texture);
276
} else {
277
texture = RS::get_singleton()->texture_2d_create(image);
278
}
279
RS::get_singleton()->texture_set_path(texture, get_path());
280
}
281
282
float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {
283
if (fill_to == fill_from) {
284
return 0;
285
}
286
float ofs = 0;
287
Vector2 pos;
288
if (width > 1) {
289
pos.x = static_cast<float>(x) / (width - 1);
290
}
291
if (height > 1) {
292
pos.y = static_cast<float>(y) / (height - 1);
293
}
294
if (fill == Fill::FILL_LINEAR) {
295
const Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, fill_from, fill_to);
296
ofs = (closest - fill_from).length() / (fill_to - fill_from).length();
297
if ((closest - fill_from).dot(fill_to - fill_from) < 0) {
298
ofs *= -1;
299
}
300
} else if (fill == Fill::FILL_RADIAL) {
301
ofs = (pos - fill_from).length() / (fill_to - fill_from).length();
302
} else if (fill == Fill::FILL_SQUARE) {
303
ofs = MAX(Math::abs(pos.x - fill_from.x), Math::abs(pos.y - fill_from.y)) / MAX(Math::abs(fill_to.x - fill_from.x), Math::abs(fill_to.y - fill_from.y));
304
}
305
if (repeat == Repeat::REPEAT_NONE) {
306
ofs = CLAMP(ofs, 0.0, 1.0);
307
} else if (repeat == Repeat::REPEAT) {
308
ofs = Math::fmod(ofs, 1.0f);
309
if (ofs < 0) {
310
ofs = 1 + ofs;
311
}
312
} else if (repeat == Repeat::REPEAT_MIRROR) {
313
ofs = Math::abs(ofs);
314
ofs = Math::fmod(ofs, 2.0f);
315
if (ofs > 1.0) {
316
ofs = 2.0 - ofs;
317
}
318
}
319
return ofs;
320
}
321
322
void GradientTexture2D::set_width(int p_width) {
323
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
324
width = p_width;
325
_queue_update();
326
emit_changed();
327
}
328
329
int GradientTexture2D::get_width() const {
330
return width;
331
}
332
333
void GradientTexture2D::set_height(int p_height) {
334
ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");
335
height = p_height;
336
_queue_update();
337
emit_changed();
338
}
339
int GradientTexture2D::get_height() const {
340
return height;
341
}
342
343
void GradientTexture2D::set_use_hdr(bool p_enabled) {
344
if (p_enabled == use_hdr) {
345
return;
346
}
347
348
use_hdr = p_enabled;
349
_queue_update();
350
emit_changed();
351
}
352
353
bool GradientTexture2D::is_using_hdr() const {
354
return use_hdr;
355
}
356
357
void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {
358
fill_from = p_fill_from;
359
_queue_update();
360
emit_changed();
361
}
362
363
Vector2 GradientTexture2D::get_fill_from() const {
364
return fill_from;
365
}
366
367
void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {
368
fill_to = p_fill_to;
369
_queue_update();
370
emit_changed();
371
}
372
373
Vector2 GradientTexture2D::get_fill_to() const {
374
return fill_to;
375
}
376
377
void GradientTexture2D::set_fill(Fill p_fill) {
378
fill = p_fill;
379
_queue_update();
380
emit_changed();
381
}
382
383
GradientTexture2D::Fill GradientTexture2D::get_fill() const {
384
return fill;
385
}
386
387
void GradientTexture2D::set_repeat(Repeat p_repeat) {
388
repeat = p_repeat;
389
_queue_update();
390
emit_changed();
391
}
392
393
GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {
394
return repeat;
395
}
396
397
RID GradientTexture2D::get_rid() const {
398
if (!texture.is_valid()) {
399
texture = RS::get_singleton()->texture_2d_placeholder_create();
400
}
401
return texture;
402
}
403
404
Ref<Image> GradientTexture2D::get_image() const {
405
update_now();
406
if (!texture.is_valid()) {
407
return Ref<Image>();
408
}
409
return RenderingServer::get_singleton()->texture_2d_get(texture);
410
}
411
412
void GradientTexture2D::update_now() const {
413
if (update_pending) {
414
_update();
415
}
416
}
417
418
void GradientTexture2D::_bind_methods() {
419
ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);
420
ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);
421
422
ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);
423
ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);
424
425
ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);
426
ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);
427
428
ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);
429
ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);
430
ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);
431
ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);
432
ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);
433
ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);
434
435
ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);
436
ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);
437
438
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
439
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
440
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");
441
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");
442
443
ADD_GROUP("Fill", "fill_");
444
ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial,Square"), "set_fill", "get_fill");
445
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");
446
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");
447
448
ADD_GROUP("Repeat", "repeat_");
449
ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");
450
451
BIND_ENUM_CONSTANT(FILL_LINEAR);
452
BIND_ENUM_CONSTANT(FILL_RADIAL);
453
BIND_ENUM_CONSTANT(FILL_SQUARE);
454
455
BIND_ENUM_CONSTANT(REPEAT_NONE);
456
BIND_ENUM_CONSTANT(REPEAT);
457
BIND_ENUM_CONSTANT(REPEAT_MIRROR);
458
}
459
460