curve_texture.cpp 11 KB

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