quad.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* quad.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "quad.h"
  31. #include "servers/visual_server.h"
  32. void Quad::_update() {
  33. if (!is_inside_tree())
  34. return;
  35. Vector3 normal;
  36. normal[axis] = 1.0;
  37. const int axis_order_1[3] = { 1, 2, 0 };
  38. const int axis_order_2[3] = { 2, 0, 1 };
  39. const int a1 = axis_order_1[axis];
  40. const int a2 = axis_order_2[axis];
  41. DVector<Vector3> points;
  42. points.resize(4);
  43. DVector<Vector3>::Write pointsw = points.write();
  44. Vector2 s2 = size * 0.5;
  45. Vector2 o = offset;
  46. if (!centered)
  47. o += s2;
  48. pointsw[0][a1] = -s2.x + offset.x;
  49. pointsw[0][a2] = s2.y + offset.y;
  50. pointsw[1][a1] = s2.x + offset.x;
  51. pointsw[1][a2] = s2.y + offset.y;
  52. pointsw[2][a1] = s2.x + offset.x;
  53. pointsw[2][a2] = -s2.y + offset.y;
  54. pointsw[3][a1] = -s2.x + offset.x;
  55. pointsw[3][a2] = -s2.y + offset.y;
  56. aabb = AABB(pointsw[0], Vector3());
  57. for (int i = 1; i < 4; i++)
  58. aabb.expand_to(pointsw[i]);
  59. pointsw = DVector<Vector3>::Write();
  60. DVector<Vector3> normals;
  61. normals.resize(4);
  62. DVector<Vector3>::Write normalsw = normals.write();
  63. for (int i = 0; i < 4; i++)
  64. normalsw[i] = normal;
  65. normalsw = DVector<Vector3>::Write();
  66. DVector<Vector2> uvs;
  67. uvs.resize(4);
  68. DVector<Vector2>::Write uvsw = uvs.write();
  69. uvsw[0] = Vector2(0, 0);
  70. uvsw[1] = Vector2(1, 0);
  71. uvsw[2] = Vector2(1, 1);
  72. uvsw[3] = Vector2(0, 1);
  73. uvsw = DVector<Vector2>::Write();
  74. DVector<int> indices;
  75. indices.resize(6);
  76. DVector<int>::Write indicesw = indices.write();
  77. indicesw[0] = 0;
  78. indicesw[1] = 1;
  79. indicesw[2] = 2;
  80. indicesw[3] = 2;
  81. indicesw[4] = 3;
  82. indicesw[5] = 0;
  83. indicesw = DVector<int>::Write();
  84. Array arr;
  85. arr.resize(VS::ARRAY_MAX);
  86. arr[VS::ARRAY_VERTEX] = points;
  87. arr[VS::ARRAY_NORMAL] = normals;
  88. arr[VS::ARRAY_TEX_UV] = uvs;
  89. arr[VS::ARRAY_INDEX] = indices;
  90. if (configured) {
  91. VS::get_singleton()->mesh_remove_surface(mesh, 0);
  92. } else {
  93. configured = true;
  94. }
  95. VS::get_singleton()->mesh_add_surface(mesh, VS::PRIMITIVE_TRIANGLES, arr);
  96. pending_update = false;
  97. }
  98. void Quad::set_axis(Vector3::Axis p_axis) {
  99. axis = p_axis;
  100. _update();
  101. }
  102. Vector3::Axis Quad::get_axis() const {
  103. return axis;
  104. }
  105. void Quad::set_size(const Vector2 &p_size) {
  106. size = p_size;
  107. _update();
  108. }
  109. Vector2 Quad::get_size() const {
  110. return size;
  111. }
  112. void Quad::set_offset(const Vector2 &p_offset) {
  113. offset = p_offset;
  114. _update();
  115. }
  116. Vector2 Quad::get_offset() const {
  117. return offset;
  118. }
  119. void Quad::set_centered(bool p_enabled) {
  120. centered = p_enabled;
  121. _update();
  122. }
  123. bool Quad::is_centered() const {
  124. return centered;
  125. }
  126. void Quad::_notification(int p_what) {
  127. switch (p_what) {
  128. case NOTIFICATION_ENTER_TREE: {
  129. if (pending_update)
  130. _update();
  131. } break;
  132. case NOTIFICATION_EXIT_TREE: {
  133. pending_update = true;
  134. } break;
  135. }
  136. }
  137. DVector<Face3> Quad::get_faces(uint32_t p_usage_flags) const {
  138. return DVector<Face3>();
  139. }
  140. AABB Quad::get_aabb() const {
  141. return aabb;
  142. }
  143. void Quad::_bind_methods() {
  144. ObjectTypeDB::bind_method(_MD("set_axis", "axis"), &Quad::set_axis);
  145. ObjectTypeDB::bind_method(_MD("get_axis"), &Quad::get_axis);
  146. ObjectTypeDB::bind_method(_MD("set_size", "size"), &Quad::set_size);
  147. ObjectTypeDB::bind_method(_MD("get_size"), &Quad::get_size);
  148. ObjectTypeDB::bind_method(_MD("set_centered", "centered"), &Quad::set_centered);
  149. ObjectTypeDB::bind_method(_MD("is_centered"), &Quad::is_centered);
  150. ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &Quad::set_offset);
  151. ObjectTypeDB::bind_method(_MD("get_offset"), &Quad::get_offset);
  152. ADD_PROPERTY(PropertyInfo(Variant::INT, "quad/axis", PROPERTY_HINT_ENUM, "X,Y,Z"), _SCS("set_axis"), _SCS("get_axis"));
  153. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad/size"), _SCS("set_size"), _SCS("get_size"));
  154. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "quad/offset"), _SCS("set_offset"), _SCS("get_offset"));
  155. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quad/centered"), _SCS("set_centered"), _SCS("is_centered"));
  156. }
  157. Quad::Quad() {
  158. pending_update = true;
  159. centered = true;
  160. //offset=0;
  161. size = Vector2(1, 1);
  162. axis = Vector3::AXIS_Z;
  163. mesh = VisualServer::get_singleton()->mesh_create();
  164. set_base(mesh);
  165. configured = false;
  166. }
  167. Quad::~Quad() {
  168. VisualServer::get_singleton()->free(mesh);
  169. }