gpu_particles_collision_3d.h 10 KB

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