gpu_particles_collision_3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**************************************************************************/
  2. /* gpu_particles_collision_3d.h */
  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. #pragma once
  31. #include "core/templates/local_vector.h"
  32. #include "scene/3d/visual_instance_3d.h"
  33. class GPUParticlesCollision3D : public VisualInstance3D {
  34. GDCLASS(GPUParticlesCollision3D, VisualInstance3D);
  35. uint32_t cull_mask = 0xFFFFFFFF;
  36. RID collision;
  37. protected:
  38. _FORCE_INLINE_ RID _get_collision() { return collision; }
  39. static void _bind_methods();
  40. GPUParticlesCollision3D(RS::ParticlesCollisionType p_type);
  41. public:
  42. void set_cull_mask(uint32_t p_cull_mask);
  43. uint32_t get_cull_mask() const;
  44. ~GPUParticlesCollision3D();
  45. };
  46. class GPUParticlesCollisionSphere3D : public GPUParticlesCollision3D {
  47. GDCLASS(GPUParticlesCollisionSphere3D, GPUParticlesCollision3D);
  48. real_t radius = 1.0;
  49. protected:
  50. static void _bind_methods();
  51. public:
  52. void set_radius(real_t p_radius);
  53. real_t get_radius() const;
  54. virtual AABB get_aabb() const override;
  55. GPUParticlesCollisionSphere3D();
  56. ~GPUParticlesCollisionSphere3D();
  57. };
  58. class GPUParticlesCollisionBox3D : public GPUParticlesCollision3D {
  59. GDCLASS(GPUParticlesCollisionBox3D, GPUParticlesCollision3D);
  60. Vector3 size = Vector3(2, 2, 2);
  61. protected:
  62. static void _bind_methods();
  63. #ifndef DISABLE_DEPRECATED
  64. bool _set(const StringName &p_name, const Variant &p_value);
  65. bool _get(const StringName &p_name, Variant &r_property) const;
  66. #endif // DISABLE_DEPRECATED
  67. public:
  68. void set_size(const Vector3 &p_size);
  69. Vector3 get_size() const;
  70. virtual AABB get_aabb() const override;
  71. GPUParticlesCollisionBox3D();
  72. ~GPUParticlesCollisionBox3D();
  73. };
  74. class GPUParticlesCollisionSDF3D : public GPUParticlesCollision3D {
  75. GDCLASS(GPUParticlesCollisionSDF3D, GPUParticlesCollision3D);
  76. public:
  77. enum Resolution {
  78. RESOLUTION_16,
  79. RESOLUTION_32,
  80. RESOLUTION_64,
  81. RESOLUTION_128,
  82. RESOLUTION_256,
  83. RESOLUTION_512,
  84. RESOLUTION_MAX,
  85. };
  86. typedef void (*BakeBeginFunc)(int);
  87. typedef void (*BakeStepFunc)(int, const String &);
  88. typedef void (*BakeEndFunc)();
  89. private:
  90. Vector3 size = Vector3(2, 2, 2);
  91. Resolution resolution = RESOLUTION_64;
  92. uint32_t bake_mask = 0xFFFFFFFF;
  93. Ref<Texture3D> texture;
  94. float thickness = 1.0;
  95. struct PlotMesh {
  96. Ref<Mesh> mesh;
  97. Transform3D local_xform;
  98. };
  99. void _find_meshes(const AABB &p_aabb, Node *p_at_node, List<PlotMesh> &plot_meshes);
  100. struct BVH {
  101. enum {
  102. LEAF_BIT = 1 << 30,
  103. LEAF_MASK = LEAF_BIT - 1
  104. };
  105. AABB bounds;
  106. uint32_t children[2] = {};
  107. };
  108. struct FacePos {
  109. Vector3 center;
  110. uint32_t index = 0;
  111. };
  112. struct FaceSort {
  113. uint32_t axis = 0;
  114. bool operator()(const FacePos &p_left, const FacePos &p_right) const {
  115. return p_left.center[axis] < p_right.center[axis];
  116. }
  117. };
  118. uint32_t _create_bvh(LocalVector<BVH> &bvh_tree, FacePos *p_faces, uint32_t p_face_count, const Face3 *p_triangles, float p_thickness);
  119. struct ComputeSDFParams {
  120. float *cells = nullptr;
  121. Vector3i size;
  122. float cell_size = 0.0;
  123. Vector3 cell_offset;
  124. const BVH *bvh = nullptr;
  125. const Face3 *triangles = nullptr;
  126. float thickness = 0.0;
  127. };
  128. void _find_closest_distance(const Vector3 &p_pos, const BVH *p_bvh, uint32_t p_bvh_cell, const Face3 *p_triangles, float p_thickness, float &r_closest_distance);
  129. void _compute_sdf_z(uint32_t p_z, ComputeSDFParams *params);
  130. void _compute_sdf(ComputeSDFParams *params);
  131. protected:
  132. static void _bind_methods();
  133. #ifndef DISABLE_DEPRECATED
  134. bool _set(const StringName &p_name, const Variant &p_value);
  135. bool _get(const StringName &p_name, Variant &r_property) const;
  136. #endif // DISABLE_DEPRECATED
  137. public:
  138. virtual PackedStringArray get_configuration_warnings() const override;
  139. void set_thickness(float p_thickness);
  140. float get_thickness() const;
  141. void set_size(const Vector3 &p_size);
  142. Vector3 get_size() const;
  143. void set_resolution(Resolution p_resolution);
  144. Resolution get_resolution() const;
  145. void set_bake_mask(uint32_t p_mask);
  146. uint32_t get_bake_mask() const;
  147. void set_bake_mask_value(int p_layer_number, bool p_enable);
  148. bool get_bake_mask_value(int p_layer_number) const;
  149. void set_texture(const Ref<Texture3D> &p_texture);
  150. Ref<Texture3D> get_texture() const;
  151. Vector3i get_estimated_cell_size() const;
  152. Ref<Image> bake();
  153. virtual AABB get_aabb() const override;
  154. static BakeBeginFunc bake_begin_function;
  155. static BakeStepFunc bake_step_function;
  156. static BakeEndFunc bake_end_function;
  157. GPUParticlesCollisionSDF3D();
  158. ~GPUParticlesCollisionSDF3D();
  159. };
  160. VARIANT_ENUM_CAST(GPUParticlesCollisionSDF3D::Resolution)
  161. class GPUParticlesCollisionHeightField3D : public GPUParticlesCollision3D {
  162. GDCLASS(GPUParticlesCollisionHeightField3D, GPUParticlesCollision3D);
  163. public:
  164. enum Resolution {
  165. RESOLUTION_256,
  166. RESOLUTION_512,
  167. RESOLUTION_1024,
  168. RESOLUTION_2048,
  169. RESOLUTION_4096,
  170. RESOLUTION_8192,
  171. RESOLUTION_MAX,
  172. };
  173. enum UpdateMode {
  174. UPDATE_MODE_WHEN_MOVED,
  175. UPDATE_MODE_ALWAYS,
  176. };
  177. private:
  178. uint32_t heightfield_mask = (1 << 20) - 1; // Only the first 20 bits are set by default to ignore editor layers.
  179. Vector3 size = Vector3(2, 2, 2);
  180. Resolution resolution = RESOLUTION_1024;
  181. bool follow_camera_mode = false;
  182. UpdateMode update_mode = UPDATE_MODE_WHEN_MOVED;
  183. protected:
  184. void _notification(int p_what);
  185. static void _bind_methods();
  186. #ifndef DISABLE_DEPRECATED
  187. bool _set(const StringName &p_name, const Variant &p_value);
  188. bool _get(const StringName &p_name, Variant &r_property) const;
  189. #endif // DISABLE_DEPRECATED
  190. public:
  191. void set_size(const Vector3 &p_size);
  192. Vector3 get_size() const;
  193. void set_resolution(Resolution p_resolution);
  194. Resolution get_resolution() const;
  195. void set_update_mode(UpdateMode p_update_mode);
  196. UpdateMode get_update_mode() const;
  197. void set_heightfield_mask(uint32_t p_heightfield_mask);
  198. uint32_t get_heightfield_mask() const;
  199. void set_heightfield_mask_value(int p_layer_number, bool p_value);
  200. bool get_heightfield_mask_value(int p_layer_number) const;
  201. void set_follow_camera_enabled(bool p_enabled);
  202. bool is_follow_camera_enabled() const;
  203. virtual AABB get_aabb() const override;
  204. GPUParticlesCollisionHeightField3D();
  205. ~GPUParticlesCollisionHeightField3D();
  206. };
  207. VARIANT_ENUM_CAST(GPUParticlesCollisionHeightField3D::Resolution)
  208. VARIANT_ENUM_CAST(GPUParticlesCollisionHeightField3D::UpdateMode)
  209. class GPUParticlesAttractor3D : public VisualInstance3D {
  210. GDCLASS(GPUParticlesAttractor3D, VisualInstance3D);
  211. uint32_t cull_mask = 0xFFFFFFFF;
  212. RID collision;
  213. real_t strength = 1.0;
  214. real_t attenuation = 1.0;
  215. real_t directionality = 0.0;
  216. protected:
  217. _FORCE_INLINE_ RID _get_collision() { return collision; }
  218. static void _bind_methods();
  219. GPUParticlesAttractor3D(RS::ParticlesCollisionType p_type);
  220. public:
  221. void set_cull_mask(uint32_t p_cull_mask);
  222. uint32_t get_cull_mask() const;
  223. void set_strength(real_t p_strength);
  224. real_t get_strength() const;
  225. void set_attenuation(real_t p_attenuation);
  226. real_t get_attenuation() const;
  227. void set_directionality(real_t p_directionality);
  228. real_t get_directionality() const;
  229. ~GPUParticlesAttractor3D();
  230. };
  231. class GPUParticlesAttractorSphere3D : public GPUParticlesAttractor3D {
  232. GDCLASS(GPUParticlesAttractorSphere3D, GPUParticlesAttractor3D);
  233. real_t radius = 1.0;
  234. protected:
  235. static void _bind_methods();
  236. public:
  237. void set_radius(real_t p_radius);
  238. real_t get_radius() const;
  239. virtual AABB get_aabb() const override;
  240. GPUParticlesAttractorSphere3D();
  241. ~GPUParticlesAttractorSphere3D();
  242. };
  243. class GPUParticlesAttractorBox3D : public GPUParticlesAttractor3D {
  244. GDCLASS(GPUParticlesAttractorBox3D, GPUParticlesAttractor3D);
  245. Vector3 size = Vector3(2, 2, 2);
  246. protected:
  247. static void _bind_methods();
  248. #ifndef DISABLE_DEPRECATED
  249. bool _set(const StringName &p_name, const Variant &p_value);
  250. bool _get(const StringName &p_name, Variant &r_property) const;
  251. #endif // DISABLE_DEPRECATED
  252. public:
  253. void set_size(const Vector3 &p_size);
  254. Vector3 get_size() const;
  255. virtual AABB get_aabb() const override;
  256. GPUParticlesAttractorBox3D();
  257. ~GPUParticlesAttractorBox3D();
  258. };
  259. class GPUParticlesAttractorVectorField3D : public GPUParticlesAttractor3D {
  260. GDCLASS(GPUParticlesAttractorVectorField3D, GPUParticlesAttractor3D);
  261. Vector3 size = Vector3(2, 2, 2);
  262. Ref<Texture3D> texture;
  263. protected:
  264. static void _bind_methods();
  265. #ifndef DISABLE_DEPRECATED
  266. bool _set(const StringName &p_name, const Variant &p_value);
  267. bool _get(const StringName &p_name, Variant &r_property) const;
  268. #endif // DISABLE_DEPRECATED
  269. public:
  270. void set_size(const Vector3 &p_size);
  271. Vector3 get_size() const;
  272. void set_texture(const Ref<Texture3D> &p_texture);
  273. Ref<Texture3D> get_texture() const;
  274. virtual AABB get_aabb() const override;
  275. GPUParticlesAttractorVectorField3D();
  276. ~GPUParticlesAttractorVectorField3D();
  277. };