curve_texture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. void CurveTexture::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveTexture::set_width);
  33. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &CurveTexture::set_curve);
  34. ClassDB::bind_method(D_METHOD("get_curve"), &CurveTexture::get_curve);
  35. ClassDB::bind_method(D_METHOD("set_texture_mode", "texture_mode"), &CurveTexture::set_texture_mode);
  36. ClassDB::bind_method(D_METHOD("get_texture_mode"), &CurveTexture::get_texture_mode);
  37. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
  38. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "RGB,Red"), "set_texture_mode", "get_texture_mode");
  39. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve", "get_curve");
  40. BIND_ENUM_CONSTANT(TEXTURE_MODE_RGB);
  41. BIND_ENUM_CONSTANT(TEXTURE_MODE_RED);
  42. }
  43. void CurveTexture::set_width(int p_width) {
  44. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  45. if (_width == p_width) {
  46. return;
  47. }
  48. _width = p_width;
  49. _update();
  50. }
  51. int CurveTexture::get_width() const {
  52. return _width;
  53. }
  54. void CurveTexture::ensure_default_setup(float p_min, float p_max) {
  55. if (_curve.is_null()) {
  56. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  57. curve->add_point(Vector2(0, 1));
  58. curve->add_point(Vector2(1, 1));
  59. curve->set_min_value(p_min);
  60. curve->set_max_value(p_max);
  61. set_curve(curve);
  62. // Min and max is 0..1 by default
  63. }
  64. }
  65. void CurveTexture::set_curve(Ref<Curve> p_curve) {
  66. if (_curve != p_curve) {
  67. if (_curve.is_valid()) {
  68. _curve->disconnect_changed(callable_mp(this, &CurveTexture::_update));
  69. }
  70. _curve = p_curve;
  71. if (_curve.is_valid()) {
  72. _curve->connect_changed(callable_mp(this, &CurveTexture::_update));
  73. }
  74. _update();
  75. }
  76. }
  77. void CurveTexture::_update() {
  78. Vector<uint8_t> data;
  79. data.resize(_width * sizeof(float) * (texture_mode == TEXTURE_MODE_RGB ? 3 : 1));
  80. // The array is locked in that scope
  81. {
  82. uint8_t *wd8 = data.ptrw();
  83. float *wd = (float *)wd8;
  84. if (_curve.is_valid()) {
  85. Curve &curve = **_curve;
  86. for (int i = 0; i < _width; ++i) {
  87. float t = i / static_cast<float>(_width);
  88. if (texture_mode == TEXTURE_MODE_RGB) {
  89. wd[i * 3 + 0] = curve.sample_baked(t);
  90. wd[i * 3 + 1] = wd[i * 3 + 0];
  91. wd[i * 3 + 2] = wd[i * 3 + 0];
  92. } else {
  93. wd[i] = curve.sample_baked(t);
  94. }
  95. }
  96. } else {
  97. for (int i = 0; i < _width; ++i) {
  98. if (texture_mode == TEXTURE_MODE_RGB) {
  99. wd[i * 3 + 0] = 0;
  100. wd[i * 3 + 1] = 0;
  101. wd[i * 3 + 2] = 0;
  102. } else {
  103. wd[i] = 0;
  104. }
  105. }
  106. }
  107. }
  108. Ref<Image> image = memnew(Image(_width, 1, false, texture_mode == TEXTURE_MODE_RGB ? Image::FORMAT_RGBF : Image::FORMAT_RF, data));
  109. if (_texture.is_valid()) {
  110. if (_current_texture_mode != texture_mode || _current_width != _width) {
  111. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  112. RS::get_singleton()->texture_replace(_texture, new_texture);
  113. } else {
  114. RS::get_singleton()->texture_2d_update(_texture, image);
  115. }
  116. } else {
  117. _texture = RS::get_singleton()->texture_2d_create(image);
  118. }
  119. _current_texture_mode = texture_mode;
  120. _current_width = _width;
  121. emit_changed();
  122. }
  123. Ref<Curve> CurveTexture::get_curve() const {
  124. return _curve;
  125. }
  126. void CurveTexture::set_texture_mode(TextureMode p_mode) {
  127. ERR_FAIL_COND(p_mode < TEXTURE_MODE_RGB || p_mode > TEXTURE_MODE_RED);
  128. if (texture_mode == p_mode) {
  129. return;
  130. }
  131. texture_mode = p_mode;
  132. _update();
  133. }
  134. CurveTexture::TextureMode CurveTexture::get_texture_mode() const {
  135. return texture_mode;
  136. }
  137. RID CurveTexture::get_rid() const {
  138. if (!_texture.is_valid()) {
  139. _texture = RS::get_singleton()->texture_2d_placeholder_create();
  140. }
  141. return _texture;
  142. }
  143. CurveTexture::CurveTexture() {}
  144. CurveTexture::~CurveTexture() {
  145. if (_texture.is_valid()) {
  146. ERR_FAIL_NULL(RenderingServer::get_singleton());
  147. RS::get_singleton()->free(_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. CurveXYZTexture::~CurveXYZTexture() {
  309. if (_texture.is_valid()) {
  310. ERR_FAIL_NULL(RenderingServer::get_singleton());
  311. RS::get_singleton()->free(_texture);
  312. }
  313. }