shape_3d.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* shape_3d.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 "shape_3d.h"
  31. #include "scene/main/scene_tree.h"
  32. #include "scene/resources/mesh.h"
  33. #include "servers/physics_server_3d.h"
  34. void Shape3D::add_vertices_to_array(Vector<Vector3> &array, const Transform3D &p_xform) {
  35. Vector<Vector3> toadd = get_debug_mesh_lines();
  36. if (toadd.size()) {
  37. int base = array.size();
  38. array.resize(base + toadd.size());
  39. Vector3 *w = array.ptrw();
  40. for (int i = 0; i < toadd.size(); i++) {
  41. w[i + base] = p_xform.xform(toadd[i]);
  42. }
  43. }
  44. }
  45. void Shape3D::set_custom_solver_bias(real_t p_bias) {
  46. custom_bias = p_bias;
  47. PhysicsServer3D::get_singleton()->shape_set_custom_solver_bias(shape, custom_bias);
  48. }
  49. real_t Shape3D::get_custom_solver_bias() const {
  50. return custom_bias;
  51. }
  52. real_t Shape3D::get_margin() const {
  53. return margin;
  54. }
  55. void Shape3D::set_margin(real_t p_margin) {
  56. margin = p_margin;
  57. PhysicsServer3D::get_singleton()->shape_set_margin(shape, margin);
  58. }
  59. void Shape3D::set_debug_color(const Color &p_color) {
  60. if (p_color == debug_color) {
  61. return;
  62. }
  63. debug_color = p_color;
  64. #ifdef DEBUG_ENABLED
  65. debug_properties_edited = true;
  66. #endif // DEBUG_ENABLED
  67. _update_shape();
  68. }
  69. Color Shape3D::get_debug_color() const {
  70. return debug_color;
  71. }
  72. void Shape3D::set_debug_fill(bool p_fill) {
  73. if (p_fill == debug_fill) {
  74. return;
  75. }
  76. debug_fill = p_fill;
  77. #ifdef DEBUG_ENABLED
  78. debug_properties_edited = true;
  79. #endif // DEBUG_ENABLED
  80. _update_shape();
  81. }
  82. bool Shape3D::get_debug_fill() const {
  83. return debug_fill;
  84. }
  85. Ref<ArrayMesh> Shape3D::get_debug_mesh() {
  86. if (debug_mesh_cache.is_valid()) {
  87. return debug_mesh_cache;
  88. }
  89. Vector<Vector3> lines = get_debug_mesh_lines();
  90. debug_mesh_cache.instantiate();
  91. if (!lines.is_empty()) {
  92. //make mesh
  93. Vector<Vector3> array;
  94. array.resize(lines.size());
  95. Vector3 *v = array.ptrw();
  96. Vector<Color> arraycol;
  97. arraycol.resize(lines.size());
  98. Color *c = arraycol.ptrw();
  99. for (int i = 0; i < lines.size(); i++) {
  100. v[i] = lines[i];
  101. c[i] = debug_color;
  102. }
  103. Array lines_array;
  104. lines_array.resize(Mesh::ARRAY_MAX);
  105. lines_array[Mesh::ARRAY_VERTEX] = array;
  106. lines_array[Mesh::ARRAY_COLOR] = arraycol;
  107. Ref<StandardMaterial3D> material = get_debug_collision_material();
  108. debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, lines_array);
  109. debug_mesh_cache->surface_set_material(0, material);
  110. if (debug_fill) {
  111. Ref<ArrayMesh> array_mesh = get_debug_arraymesh_faces(debug_color * Color(1.0, 1.0, 1.0, 0.0625));
  112. if (array_mesh.is_valid() && array_mesh->get_surface_count() > 0) {
  113. Array solid_array = array_mesh->surface_get_arrays(0);
  114. debug_mesh_cache->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, solid_array);
  115. debug_mesh_cache->surface_set_material(1, material);
  116. }
  117. }
  118. }
  119. return debug_mesh_cache;
  120. }
  121. Ref<Material> Shape3D::get_debug_collision_material() {
  122. if (collision_material.is_valid()) {
  123. return collision_material;
  124. }
  125. Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
  126. material->set_albedo(Color(1.0, 1.0, 1.0));
  127. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  128. material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  129. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
  130. material->set_cull_mode(StandardMaterial3D::CULL_BACK);
  131. material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  132. material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  133. material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  134. collision_material = material;
  135. return collision_material;
  136. }
  137. void Shape3D::_update_shape() {
  138. emit_changed();
  139. debug_mesh_cache.unref();
  140. }
  141. void Shape3D::_bind_methods() {
  142. ClassDB::bind_method(D_METHOD("set_custom_solver_bias", "bias"), &Shape3D::set_custom_solver_bias);
  143. ClassDB::bind_method(D_METHOD("get_custom_solver_bias"), &Shape3D::get_custom_solver_bias);
  144. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &Shape3D::set_margin);
  145. ClassDB::bind_method(D_METHOD("get_margin"), &Shape3D::get_margin);
  146. ClassDB::bind_method(D_METHOD("get_debug_mesh"), &Shape3D::get_debug_mesh);
  147. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias");
  148. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater,suffix:m"), "set_margin", "get_margin");
  149. }
  150. Shape3D::Shape3D() {
  151. ERR_PRINT("Default constructor must not be called!");
  152. }
  153. Shape3D::Shape3D(RID p_shape) :
  154. shape(p_shape) {}
  155. Shape3D::~Shape3D() {
  156. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  157. PhysicsServer3D::get_singleton()->free(shape);
  158. }