curve_texture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if (_texture.is_valid()) {
  145. ERR_FAIL_NULL(RenderingServer::get_singleton());
  146. RS::get_singleton()->free(_texture);
  147. }
  148. }
  149. //////////////////
  150. void CurveXYZTexture::_bind_methods() {
  151. ClassDB::bind_method(D_METHOD("set_width", "width"), &CurveXYZTexture::set_width);
  152. ClassDB::bind_method(D_METHOD("set_curve_x", "curve"), &CurveXYZTexture::set_curve_x);
  153. ClassDB::bind_method(D_METHOD("get_curve_x"), &CurveXYZTexture::get_curve_x);
  154. ClassDB::bind_method(D_METHOD("set_curve_y", "curve"), &CurveXYZTexture::set_curve_y);
  155. ClassDB::bind_method(D_METHOD("get_curve_y"), &CurveXYZTexture::get_curve_y);
  156. ClassDB::bind_method(D_METHOD("set_curve_z", "curve"), &CurveXYZTexture::set_curve_z);
  157. ClassDB::bind_method(D_METHOD("get_curve_z"), &CurveXYZTexture::get_curve_z);
  158. ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,4096,suffix:px"), "set_width", "get_width");
  159. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_x", "get_curve_x");
  160. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_y", "get_curve_y");
  161. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve_z", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_curve_z", "get_curve_z");
  162. }
  163. void CurveXYZTexture::set_width(int p_width) {
  164. ERR_FAIL_COND(p_width < 32 || p_width > 4096);
  165. if (_width == p_width) {
  166. return;
  167. }
  168. _width = p_width;
  169. _update();
  170. }
  171. int CurveXYZTexture::get_width() const {
  172. return _width;
  173. }
  174. void CurveXYZTexture::ensure_default_setup(float p_min, float p_max) {
  175. if (_curve_x.is_null()) {
  176. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  177. curve->add_point(Vector2(0, 1));
  178. curve->add_point(Vector2(1, 1));
  179. curve->set_min_value(p_min);
  180. curve->set_max_value(p_max);
  181. set_curve_x(curve);
  182. }
  183. if (_curve_y.is_null()) {
  184. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  185. curve->add_point(Vector2(0, 1));
  186. curve->add_point(Vector2(1, 1));
  187. curve->set_min_value(p_min);
  188. curve->set_max_value(p_max);
  189. set_curve_y(curve);
  190. }
  191. if (_curve_z.is_null()) {
  192. Ref<Curve> curve = Ref<Curve>(memnew(Curve));
  193. curve->add_point(Vector2(0, 1));
  194. curve->add_point(Vector2(1, 1));
  195. curve->set_min_value(p_min);
  196. curve->set_max_value(p_max);
  197. set_curve_z(curve);
  198. }
  199. }
  200. void CurveXYZTexture::set_curve_x(Ref<Curve> p_curve) {
  201. if (_curve_x != p_curve) {
  202. if (_curve_x.is_valid()) {
  203. _curve_x->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update));
  204. }
  205. _curve_x = p_curve;
  206. if (_curve_x.is_valid()) {
  207. _curve_x->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  208. }
  209. _update();
  210. }
  211. }
  212. void CurveXYZTexture::set_curve_y(Ref<Curve> p_curve) {
  213. if (_curve_y != p_curve) {
  214. if (_curve_y.is_valid()) {
  215. _curve_y->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update));
  216. }
  217. _curve_y = p_curve;
  218. if (_curve_y.is_valid()) {
  219. _curve_y->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  220. }
  221. _update();
  222. }
  223. }
  224. void CurveXYZTexture::set_curve_z(Ref<Curve> p_curve) {
  225. if (_curve_z != p_curve) {
  226. if (_curve_z.is_valid()) {
  227. _curve_z->disconnect_changed(callable_mp(this, &CurveXYZTexture::_update));
  228. }
  229. _curve_z = p_curve;
  230. if (_curve_z.is_valid()) {
  231. _curve_z->connect_changed(callable_mp(this, &CurveXYZTexture::_update), CONNECT_REFERENCE_COUNTED);
  232. }
  233. _update();
  234. }
  235. }
  236. void CurveXYZTexture::_update() {
  237. Vector<uint8_t> data;
  238. data.resize(_width * sizeof(float) * 3);
  239. // The array is locked in that scope
  240. {
  241. uint8_t *wd8 = data.ptrw();
  242. float *wd = (float *)wd8;
  243. if (_curve_x.is_valid()) {
  244. Curve &curve_x = **_curve_x;
  245. for (int i = 0; i < _width; ++i) {
  246. float t = i / static_cast<float>(_width);
  247. wd[i * 3 + 0] = curve_x.sample_baked(t);
  248. }
  249. } else {
  250. for (int i = 0; i < _width; ++i) {
  251. wd[i * 3 + 0] = 0;
  252. }
  253. }
  254. if (_curve_y.is_valid()) {
  255. Curve &curve_y = **_curve_y;
  256. for (int i = 0; i < _width; ++i) {
  257. float t = i / static_cast<float>(_width);
  258. wd[i * 3 + 1] = curve_y.sample_baked(t);
  259. }
  260. } else {
  261. for (int i = 0; i < _width; ++i) {
  262. wd[i * 3 + 1] = 0;
  263. }
  264. }
  265. if (_curve_z.is_valid()) {
  266. Curve &curve_z = **_curve_z;
  267. for (int i = 0; i < _width; ++i) {
  268. float t = i / static_cast<float>(_width);
  269. wd[i * 3 + 2] = curve_z.sample_baked(t);
  270. }
  271. } else {
  272. for (int i = 0; i < _width; ++i) {
  273. wd[i * 3 + 2] = 0;
  274. }
  275. }
  276. }
  277. Ref<Image> image = memnew(Image(_width, 1, false, Image::FORMAT_RGBF, data));
  278. if (_texture.is_valid()) {
  279. if (_current_width != _width) {
  280. RID new_texture = RS::get_singleton()->texture_2d_create(image);
  281. RS::get_singleton()->texture_replace(_texture, new_texture);
  282. } else {
  283. RS::get_singleton()->texture_2d_update(_texture, image);
  284. }
  285. } else {
  286. _texture = RS::get_singleton()->texture_2d_create(image);
  287. }
  288. _current_width = _width;
  289. emit_changed();
  290. }
  291. Ref<Curve> CurveXYZTexture::get_curve_x() const {
  292. return _curve_x;
  293. }
  294. Ref<Curve> CurveXYZTexture::get_curve_y() const {
  295. return _curve_y;
  296. }
  297. Ref<Curve> CurveXYZTexture::get_curve_z() const {
  298. return _curve_z;
  299. }
  300. RID CurveXYZTexture::get_rid() const {
  301. if (!_texture.is_valid()) {
  302. _texture = RS::get_singleton()->texture_2d_placeholder_create();
  303. }
  304. return _texture;
  305. }
  306. CurveXYZTexture::~CurveXYZTexture() {
  307. if (_texture.is_valid()) {
  308. ERR_FAIL_NULL(RenderingServer::get_singleton());
  309. RS::get_singleton()->free(_texture);
  310. }
  311. }