gpu_particles_collision_3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. uint32_t heightfield_mask = (1 << 20) - 1; // Only the first 20 bits are set by default to ignore editor layers.
  180. Vector3 size = Vector3(2, 2, 2);
  181. Resolution resolution = RESOLUTION_1024;
  182. bool follow_camera_mode = false;
  183. UpdateMode update_mode = UPDATE_MODE_WHEN_MOVED;
  184. protected:
  185. void _notification(int p_what);
  186. static void _bind_methods();
  187. #ifndef DISABLE_DEPRECATED
  188. bool _set(const StringName &p_name, const Variant &p_value);
  189. bool _get(const StringName &p_name, Variant &r_property) const;
  190. #endif // DISABLE_DEPRECATED
  191. public:
  192. void set_size(const Vector3 &p_size);
  193. Vector3 get_size() const;
  194. void set_resolution(Resolution p_resolution);
  195. Resolution get_resolution() const;
  196. void set_update_mode(UpdateMode p_update_mode);
  197. UpdateMode get_update_mode() const;
  198. void set_heightfield_mask(uint32_t p_heightfield_mask);
  199. uint32_t get_heightfield_mask() const;
  200. void set_heightfield_mask_value(int p_layer_number, bool p_value);
  201. bool get_heightfield_mask_value(int p_layer_number) const;
  202. void set_follow_camera_enabled(bool p_enabled);
  203. bool is_follow_camera_enabled() const;
  204. virtual AABB get_aabb() const override;
  205. GPUParticlesCollisionHeightField3D();
  206. ~GPUParticlesCollisionHeightField3D();
  207. };
  208. VARIANT_ENUM_CAST(GPUParticlesCollisionHeightField3D::Resolution)
  209. VARIANT_ENUM_CAST(GPUParticlesCollisionHeightField3D::UpdateMode)
  210. class GPUParticlesAttractor3D : public VisualInstance3D {
  211. GDCLASS(GPUParticlesAttractor3D, VisualInstance3D);
  212. uint32_t cull_mask = 0xFFFFFFFF;
  213. RID collision;
  214. real_t strength = 1.0;
  215. real_t attenuation = 1.0;
  216. real_t directionality = 0.0;
  217. protected:
  218. _FORCE_INLINE_ RID _get_collision() { return collision; }
  219. static void _bind_methods();
  220. GPUParticlesAttractor3D(RS::ParticlesCollisionType p_type);
  221. public:
  222. void set_cull_mask(uint32_t p_cull_mask);
  223. uint32_t get_cull_mask() const;
  224. void set_strength(real_t p_strength);
  225. real_t get_strength() const;
  226. void set_attenuation(real_t p_attenuation);
  227. real_t get_attenuation() const;
  228. void set_directionality(real_t p_directionality);
  229. real_t get_directionality() const;
  230. ~GPUParticlesAttractor3D();
  231. };
  232. class GPUParticlesAttractorSphere3D : public GPUParticlesAttractor3D {
  233. GDCLASS(GPUParticlesAttractorSphere3D, GPUParticlesAttractor3D);
  234. real_t radius = 1.0;
  235. protected:
  236. static void _bind_methods();
  237. public:
  238. void set_radius(real_t p_radius);
  239. real_t get_radius() const;
  240. virtual AABB get_aabb() const override;
  241. GPUParticlesAttractorSphere3D();
  242. ~GPUParticlesAttractorSphere3D();
  243. };
  244. class GPUParticlesAttractorBox3D : public GPUParticlesAttractor3D {
  245. GDCLASS(GPUParticlesAttractorBox3D, GPUParticlesAttractor3D);
  246. Vector3 size = Vector3(2, 2, 2);
  247. protected:
  248. static void _bind_methods();
  249. #ifndef DISABLE_DEPRECATED
  250. bool _set(const StringName &p_name, const Variant &p_value);
  251. bool _get(const StringName &p_name, Variant &r_property) const;
  252. #endif // DISABLE_DEPRECATED
  253. public:
  254. void set_size(const Vector3 &p_size);
  255. Vector3 get_size() const;
  256. virtual AABB get_aabb() const override;
  257. GPUParticlesAttractorBox3D();
  258. ~GPUParticlesAttractorBox3D();
  259. };
  260. class GPUParticlesAttractorVectorField3D : public GPUParticlesAttractor3D {
  261. GDCLASS(GPUParticlesAttractorVectorField3D, GPUParticlesAttractor3D);
  262. Vector3 size = Vector3(2, 2, 2);
  263. Ref<Texture3D> texture;
  264. protected:
  265. static void _bind_methods();
  266. #ifndef DISABLE_DEPRECATED
  267. bool _set(const StringName &p_name, const Variant &p_value);
  268. bool _get(const StringName &p_name, Variant &r_property) const;
  269. #endif // DISABLE_DEPRECATED
  270. public:
  271. void set_size(const Vector3 &p_size);
  272. Vector3 get_size() const;
  273. void set_texture(const Ref<Texture3D> &p_texture);
  274. Ref<Texture3D> get_texture() const;
  275. virtual AABB get_aabb() const override;
  276. GPUParticlesAttractorVectorField3D();
  277. ~GPUParticlesAttractorVectorField3D();
  278. };
  279. #endif // GPU_PARTICLES_COLLISION_3D_H