| 1 | /**************************************************************************/ |
| 2 | /* curve_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 "curve_texture.h" |
| 32 | |
| 33 | #include "core/core_string_names.h" |
| 34 | |
| 35 | void CurveTexture::_bind_methods() { |
| 36 | ClassDB::bind_method(D_METHOD("set_width" , "width" ), &CurveTexture::set_width); |
| 37 | |
| 38 | ClassDB::bind_method(D_METHOD("set_curve" , "curve" ), &CurveTexture::set_curve); |
| 39 | ClassDB::bind_method(D_METHOD("get_curve" ), &CurveTexture::get_curve); |
| 40 | |
| 41 | ClassDB::bind_method(D_METHOD("set_texture_mode" , "texture_mode" ), &CurveTexture::set_texture_mode); |
| 42 | ClassDB::bind_method(D_METHOD("get_texture_mode" ), &CurveTexture::get_texture_mode); |
| 43 | |
| 44 | ClassDB::bind_method(D_METHOD("_update" ), &CurveTexture::_update); |
| 45 | |
| 46 | ADD_PROPERTY(PropertyInfo(Variant::INT, "width" , PROPERTY_HINT_RANGE, "1,4096,suffix:px" ), "set_width" , "get_width" ); |
| 47 | ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode" , PROPERTY_HINT_ENUM, "RGB,Red" ), "set_texture_mode" , "get_texture_mode" ); |
| 48 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve" , PROPERTY_HINT_RESOURCE_TYPE, "Curve" ), "set_curve" , "get_curve" ); |
| 49 | |
| 50 | BIND_ENUM_CONSTANT(TEXTURE_MODE_RGB); |
| 51 | BIND_ENUM_CONSTANT(TEXTURE_MODE_RED); |
| 52 | } |
| 53 | |
| 54 | void CurveTexture::set_width(int p_width) { |
| 55 | ERR_FAIL_COND(p_width < 32 || p_width > 4096); |
| 56 | |
| 57 | if (_width == p_width) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | _width = p_width; |
| 62 | _update(); |
| 63 | } |
| 64 | |
| 65 | int CurveTexture::get_width() const { |
| 66 | return _width; |
| 67 | } |
| 68 | |
| 69 | void CurveTexture::ensure_default_setup(float p_min, float p_max) { |
| 70 | if (_curve.is_null()) { |
| 71 | Ref<Curve> curve = Ref<Curve>(memnew(Curve)); |
| 72 | curve->add_point(Vector2(0, 1)); |
| 73 | curve->add_point(Vector2(1, 1)); |
| 74 | curve->set_min_value(p_min); |
| 75 | curve->set_max_value(p_max); |
| 76 | set_curve(curve); |
| 77 | // Min and max is 0..1 by default |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CurveTexture::set_curve(Ref<Curve> p_curve) { |
| 82 | if (_curve != p_curve) { |
| 83 | if (_curve.is_valid()) { |
| 84 | _curve->disconnect_changed(callable_mp(this, &CurveTexture::_update)); |
| 85 | } |
| 86 | _curve = p_curve; |
| 87 | if (_curve.is_valid()) { |
| 88 | _curve->connect_changed(callable_mp(this, &CurveTexture::_update)); |
| 89 | } |
| 90 | _update(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void CurveTexture::_update() { |
| 95 | Vector<uint8_t> data; |
| 96 | data.resize(_width * sizeof(float) * (texture_mode == TEXTURE_MODE_RGB ? 3 : 1)); |
| 97 | |
| 98 | // The array is locked in that scope |
| 99 | { |
| 100 | uint8_t *wd8 = data.ptrw(); |
| 101 | float *wd = (float *)wd8; |
| 102 | |
| 103 | if (_curve.is_valid()) { |
| 104 | Curve &curve = **_curve; |
| 105 | for (int i = 0; i < _width; ++i) { |
| 106 | float t = i / static_cast<float>(_width); |
| 107 | if (texture_mode == TEXTURE_MODE_RGB) { |
| 108 | wd[i * 3 + 0] = curve.sample_baked(t); |
| 109 | wd[i * 3 + 1] = wd[i * 3 + 0]; |
| 110 | wd[i * 3 + 2] = wd[i * 3 + 0]; |
| 111 | } else { |
| 112 | wd[i] = curve.sample_baked(t); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } else { |
| 117 | for (int i = 0; i < _width; ++i) { |
| 118 | if (texture_mode == TEXTURE_MODE_RGB) { |
| 119 | wd[i * 3 + 0] = 0; |
| 120 | wd[i * 3 + 1] = 0; |
| 121 | wd[i * 3 + 2] = 0; |
| 122 | } else { |
| 123 | wd[i] = 0; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | Ref<Image> image = memnew(Image(_width, 1, false, texture_mode == TEXTURE_MODE_RGB ? Image::FORMAT_RGBF : Image::FORMAT_RF, data)); |
| 130 | |
| 131 | if (_texture.is_valid()) { |
| 132 | if (_current_texture_mode != texture_mode || _current_width != _width) { |
| 133 | RID new_texture = RS::get_singleton()->texture_2d_create(image); |
| 134 | RS::get_singleton()->texture_replace(_texture, new_texture); |
| 135 | } else { |
| 136 | RS::get_singleton()->texture_2d_update(_texture, image); |
| 137 | } |
| 138 | } else { |
| 139 | _texture = RS::get_singleton()->texture_2d_create(image); |
| 140 | } |
| 141 | _current_texture_mode = texture_mode; |
| 142 | _current_width = _width; |
| 143 | |
| 144 | emit_changed(); |
| 145 | } |
| 146 | |
| 147 | Ref<Curve> CurveTexture::get_curve() const { |
| 148 | return _curve; |
| 149 | } |
| 150 | |
| 151 | void CurveTexture::set_texture_mode(TextureMode p_mode) { |
| 152 | ERR_FAIL_COND(p_mode < TEXTURE_MODE_RGB || p_mode > TEXTURE_MODE_RED); |
| 153 | if (texture_mode == p_mode) { |
| 154 | return; |
| 155 | } |
| 156 | texture_mode = p_mode; |
| 157 | _update(); |
| 158 | } |
| 159 | CurveTexture::TextureMode CurveTexture::get_texture_mode() const { |
| 160 | return texture_mode; |
| 161 | } |
| 162 | |
| 163 | RID CurveTexture::get_rid() const { |
| 164 | if (!_texture.is_valid()) { |
| 165 | _texture = RS::get_singleton()->texture_2d_placeholder_create(); |
| 166 | } |
| 167 | return _texture; |
| 168 | } |
| 169 | |
| 170 | CurveTexture::CurveTexture() {} |
| 171 | |
| 172 | CurveTexture::~CurveTexture() { |
| 173 | if (_texture.is_valid()) { |
| 174 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 175 | RS::get_singleton()->free(_texture); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ////////////////// |
| 180 | |
| 181 | void CurveXYZTexture::_bind_methods() { |
| 182 | ClassDB::bind_method(D_METHOD("set_width" , "width" ), &CurveXYZTexture::set_width); |
| 183 | |
| 184 | ClassDB::bind_method(D_METHOD("set_curve_x" , "curve" ), &CurveXYZTexture::set_curve_x); |
| 185 | ClassDB::bind_method(D_METHOD("get_curve_x" ), &CurveXYZTexture::get_curve_x); |
| 186 | |
| 187 | ClassDB::bind_method(D_METHOD("set_curve_y" , "curve" ), &CurveXYZTexture::set_curve_y); |
| 188 | ClassDB::bind_method(D_METHOD("get_curve_y" ), &CurveXYZTexture::get_curve_y); |
| 189 | |
| 190 | ClassDB::bind_method(D_METHOD("set_curve_z" , "curve" ), &CurveXYZTexture::set_curve_z); |
| 191 | ClassDB::bind_method(D_METHOD("get_curve_z" ), &CurveXYZTexture::get_curve_z); |
| 192 | |
| 193 | ClassDB::bind_method(D_METHOD("_update" ), &CurveXYZTexture::_update); |
| 194 | |
| 195 | ADD_PROPERTY(PropertyInfo(Variant::INT, "width" , PROPERTY_HINT_RANGE, "1,4096,suffix:px" ), "set_width" , "get_width" ); |
| 196 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_x" , PROPERTY_HINT_RESOURCE_TYPE, "Curve" ), "set_curve_x" , "get_curve_x" ); |
| 197 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_y" , PROPERTY_HINT_RESOURCE_TYPE, "Curve" ), "set_curve_y" , "get_curve_y" ); |
| 198 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_z" , PROPERTY_HINT_RESOURCE_TYPE, "Curve" ), "set_curve_z" , "get_curve_z" ); |
| 199 | } |
| 200 | |
| 201 | void CurveXYZTexture::set_width(int p_width) { |
| 202 | ERR_FAIL_COND(p_width < 32 || p_width > 4096); |
| 203 | |
| 204 | if (_width == p_width) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | _width = p_width; |
| 209 | _update(); |
| 210 | } |
| 211 | |
| 212 | int CurveXYZTexture::get_width() const { |
| 213 | return _width; |
| 214 | } |
| 215 | |
| 216 | void CurveXYZTexture::ensure_default_setup(float p_min, float p_max) { |
| 217 | if (_curve_x.is_null()) { |
| 218 | Ref<Curve> curve = Ref<Curve>(memnew(Curve)); |
| 219 | curve->add_point(Vector2(0, 1)); |
| 220 | curve->add_point(Vector2(1, 1)); |
| 221 | curve->set_min_value(p_min); |
| 222 | curve->set_max_value(p_max); |
| 223 | set_curve_x(curve); |
| 224 | } |
| 225 | |
| 226 | if (_curve_y.is_null()) { |
| 227 | Ref<Curve> curve = Ref<Curve>(memnew(Curve)); |
| 228 | curve->add_point(Vector2(0, 1)); |
| 229 | curve->add_point(Vector2(1, 1)); |
| 230 | curve->set_min_value(p_min); |
| 231 | curve->set_max_value(p_max); |
| 232 | set_curve_y(curve); |
| 233 | } |
| 234 | |
| 235 | if (_curve_z.is_null()) { |
| 236 | Ref<Curve> curve = Ref<Curve>(memnew(Curve)); |
| 237 | curve->add_point(Vector2(0, 1)); |
| 238 | curve->add_point(Vector2(1, 1)); |
| 239 | curve->set_min_value(p_min); |
| 240 | curve->set_max_value(p_max); |
| 241 | set_curve_z(curve); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void CurveXYZTexture::set_curve_x(Ref<Curve> p_curve) { |
| 246 | if (_curve_x != p_curve) { |
| 247 | if (_curve_x.is_valid()) { |
| 248 | _curve_x->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update)); |
| 249 | } |
| 250 | _curve_x = p_curve; |
| 251 | if (_curve_x.is_valid()) { |
| 252 | _curve_x->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED); |
| 253 | } |
| 254 | _update(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void CurveXYZTexture::set_curve_y(Ref<Curve> p_curve) { |
| 259 | if (_curve_y != p_curve) { |
| 260 | if (_curve_y.is_valid()) { |
| 261 | _curve_y->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update)); |
| 262 | } |
| 263 | _curve_y = p_curve; |
| 264 | if (_curve_y.is_valid()) { |
| 265 | _curve_y->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED); |
| 266 | } |
| 267 | _update(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void CurveXYZTexture::set_curve_z(Ref<Curve> p_curve) { |
| 272 | if (_curve_z != p_curve) { |
| 273 | if (_curve_z.is_valid()) { |
| 274 | _curve_z->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update)); |
| 275 | } |
| 276 | _curve_z = p_curve; |
| 277 | if (_curve_z.is_valid()) { |
| 278 | _curve_z->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED); |
| 279 | } |
| 280 | _update(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void CurveXYZTexture::_update() { |
| 285 | Vector<uint8_t> data; |
| 286 | data.resize(_width * sizeof(float) * 3); |
| 287 | |
| 288 | // The array is locked in that scope |
| 289 | { |
| 290 | uint8_t *wd8 = data.ptrw(); |
| 291 | float *wd = (float *)wd8; |
| 292 | |
| 293 | if (_curve_x.is_valid()) { |
| 294 | Curve &curve_x = **_curve_x; |
| 295 | for (int i = 0; i < _width; ++i) { |
| 296 | float t = i / static_cast<float>(_width); |
| 297 | wd[i * 3 + 0] = curve_x.sample_baked(t); |
| 298 | } |
| 299 | |
| 300 | } else { |
| 301 | for (int i = 0; i < _width; ++i) { |
| 302 | wd[i * 3 + 0] = 0; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (_curve_y.is_valid()) { |
| 307 | Curve &curve_y = **_curve_y; |
| 308 | for (int i = 0; i < _width; ++i) { |
| 309 | float t = i / static_cast<float>(_width); |
| 310 | wd[i * 3 + 1] = curve_y.sample_baked(t); |
| 311 | } |
| 312 | |
| 313 | } else { |
| 314 | for (int i = 0; i < _width; ++i) { |
| 315 | wd[i * 3 + 1] = 0; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (_curve_z.is_valid()) { |
| 320 | Curve &curve_z = **_curve_z; |
| 321 | for (int i = 0; i < _width; ++i) { |
| 322 | float t = i / static_cast<float>(_width); |
| 323 | wd[i * 3 + 2] = curve_z.sample_baked(t); |
| 324 | } |
| 325 | |
| 326 | } else { |
| 327 | for (int i = 0; i < _width; ++i) { |
| 328 | wd[i * 3 + 2] = 0; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RGBF, data)); |
| 334 | |
| 335 | if (_texture.is_valid()) { |
| 336 | if (_current_width != _width) { |
| 337 | RID new_texture = RS::get_singleton()->texture_2d_create(image); |
| 338 | RS::get_singleton()->texture_replace(_texture, new_texture); |
| 339 | } else { |
| 340 | RS::get_singleton()->texture_2d_update(_texture, image); |
| 341 | } |
| 342 | } else { |
| 343 | _texture = RS::get_singleton()->texture_2d_create(image); |
| 344 | } |
| 345 | _current_width = _width; |
| 346 | |
| 347 | emit_changed(); |
| 348 | } |
| 349 | |
| 350 | Ref<Curve> CurveXYZTexture::get_curve_x() const { |
| 351 | return _curve_x; |
| 352 | } |
| 353 | |
| 354 | Ref<Curve> CurveXYZTexture::get_curve_y() const { |
| 355 | return _curve_y; |
| 356 | } |
| 357 | |
| 358 | Ref<Curve> CurveXYZTexture::get_curve_z() const { |
| 359 | return _curve_z; |
| 360 | } |
| 361 | |
| 362 | RID CurveXYZTexture::get_rid() const { |
| 363 | if (!_texture.is_valid()) { |
| 364 | _texture = RS::get_singleton()->texture_2d_placeholder_create(); |
| 365 | } |
| 366 | return _texture; |
| 367 | } |
| 368 | |
| 369 | CurveXYZTexture::CurveXYZTexture() {} |
| 370 | |
| 371 | CurveXYZTexture::~CurveXYZTexture() { |
| 372 | if (_texture.is_valid()) { |
| 373 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 374 | RS::get_singleton()->free(_texture); |
| 375 | } |
| 376 | } |
| 377 | |