gpu_particles_collision_3d.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*************************************************************************/
  2. /* gpu_particles_collision_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "gpu_particles_collision_3d.h"
  31. #include "mesh_instance_3d.h"
  32. #include "scene/3d/camera_3d.h"
  33. #include "scene/main/viewport.h"
  34. void GPUParticlesCollision3D::set_cull_mask(uint32_t p_cull_mask) {
  35. cull_mask = p_cull_mask;
  36. RS::get_singleton()->particles_collision_set_cull_mask(collision, p_cull_mask);
  37. }
  38. uint32_t GPUParticlesCollision3D::get_cull_mask() const {
  39. return cull_mask;
  40. }
  41. void GPUParticlesCollision3D::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &GPUParticlesCollision3D::set_cull_mask);
  43. ClassDB::bind_method(D_METHOD("get_cull_mask"), &GPUParticlesCollision3D::get_cull_mask);
  44. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  45. }
  46. GPUParticlesCollision3D::GPUParticlesCollision3D(RS::ParticlesCollisionType p_type) {
  47. collision = RS::get_singleton()->particles_collision_create();
  48. RS::get_singleton()->particles_collision_set_collision_type(collision, p_type);
  49. set_base(collision);
  50. }
  51. GPUParticlesCollision3D::~GPUParticlesCollision3D() {
  52. RS::get_singleton()->free(collision);
  53. }
  54. /////////////////////////////////
  55. void GPUParticlesCollisionSphere::_bind_methods() {
  56. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GPUParticlesCollisionSphere::set_radius);
  57. ClassDB::bind_method(D_METHOD("get_radius"), &GPUParticlesCollisionSphere::get_radius);
  58. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_radius", "get_radius");
  59. }
  60. void GPUParticlesCollisionSphere::set_radius(real_t p_radius) {
  61. radius = p_radius;
  62. RS::get_singleton()->particles_collision_set_sphere_radius(_get_collision(), radius);
  63. update_gizmos();
  64. }
  65. real_t GPUParticlesCollisionSphere::get_radius() const {
  66. return radius;
  67. }
  68. AABB GPUParticlesCollisionSphere::get_aabb() const {
  69. return AABB(Vector3(-radius, -radius, -radius), Vector3(radius * 2, radius * 2, radius * 2));
  70. }
  71. GPUParticlesCollisionSphere::GPUParticlesCollisionSphere() :
  72. GPUParticlesCollision3D(RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE) {
  73. }
  74. GPUParticlesCollisionSphere::~GPUParticlesCollisionSphere() {
  75. }
  76. ///////////////////////////
  77. void GPUParticlesCollisionBox::_bind_methods() {
  78. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GPUParticlesCollisionBox::set_extents);
  79. ClassDB::bind_method(D_METHOD("get_extents"), &GPUParticlesCollisionBox::get_extents);
  80. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_extents", "get_extents");
  81. }
  82. void GPUParticlesCollisionBox::set_extents(const Vector3 &p_extents) {
  83. extents = p_extents;
  84. RS::get_singleton()->particles_collision_set_box_extents(_get_collision(), extents);
  85. update_gizmos();
  86. }
  87. Vector3 GPUParticlesCollisionBox::get_extents() const {
  88. return extents;
  89. }
  90. AABB GPUParticlesCollisionBox::get_aabb() const {
  91. return AABB(-extents, extents * 2);
  92. }
  93. GPUParticlesCollisionBox::GPUParticlesCollisionBox() :
  94. GPUParticlesCollision3D(RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE) {
  95. }
  96. GPUParticlesCollisionBox::~GPUParticlesCollisionBox() {
  97. }
  98. ///////////////////////////////
  99. ///////////////////////////
  100. void GPUParticlesCollisionSDF::_find_meshes(const AABB &p_aabb, Node *p_at_node, List<PlotMesh> &plot_meshes) {
  101. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node);
  102. if (mi && mi->is_visible_in_tree()) {
  103. Ref<Mesh> mesh = mi->get_mesh();
  104. if (mesh.is_valid()) {
  105. AABB aabb = mesh->get_aabb();
  106. Transform3D xf = get_global_transform().affine_inverse() * mi->get_global_transform();
  107. if (p_aabb.intersects(xf.xform(aabb))) {
  108. PlotMesh pm;
  109. pm.local_xform = xf;
  110. pm.mesh = mesh;
  111. plot_meshes.push_back(pm);
  112. }
  113. }
  114. }
  115. Node3D *s = Object::cast_to<Node3D>(p_at_node);
  116. if (s) {
  117. if (s->is_visible_in_tree()) {
  118. Array meshes = p_at_node->call("get_meshes");
  119. for (int i = 0; i < meshes.size(); i += 2) {
  120. Transform3D mxf = meshes[i];
  121. Ref<Mesh> mesh = meshes[i + 1];
  122. if (!mesh.is_valid()) {
  123. continue;
  124. }
  125. AABB aabb = mesh->get_aabb();
  126. Transform3D xf = get_global_transform().affine_inverse() * (s->get_global_transform() * mxf);
  127. if (p_aabb.intersects(xf.xform(aabb))) {
  128. PlotMesh pm;
  129. pm.local_xform = xf;
  130. pm.mesh = mesh;
  131. plot_meshes.push_back(pm);
  132. }
  133. }
  134. }
  135. }
  136. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  137. Node *child = p_at_node->get_child(i);
  138. _find_meshes(p_aabb, child, plot_meshes);
  139. }
  140. }
  141. uint32_t GPUParticlesCollisionSDF::_create_bvh(LocalVector<BVH> &bvh_tree, FacePos *p_faces, uint32_t p_face_count, const Face3 *p_triangles, float p_thickness) {
  142. if (p_face_count == 1) {
  143. return BVH::LEAF_BIT | p_faces[0].index;
  144. }
  145. uint32_t index = bvh_tree.size();
  146. {
  147. BVH bvh;
  148. for (uint32_t i = 0; i < p_face_count; i++) {
  149. const Face3 &f = p_triangles[p_faces[i].index];
  150. AABB aabb(f.vertex[0], Vector3());
  151. aabb.expand_to(f.vertex[1]);
  152. aabb.expand_to(f.vertex[2]);
  153. if (p_thickness > 0.0) {
  154. Vector3 normal = p_triangles[p_faces[i].index].get_plane().normal;
  155. aabb.expand_to(f.vertex[0] - normal * p_thickness);
  156. aabb.expand_to(f.vertex[1] - normal * p_thickness);
  157. aabb.expand_to(f.vertex[2] - normal * p_thickness);
  158. }
  159. if (i == 0) {
  160. bvh.bounds = aabb;
  161. } else {
  162. bvh.bounds.merge_with(aabb);
  163. }
  164. }
  165. bvh_tree.push_back(bvh);
  166. }
  167. uint32_t middle = p_face_count / 2;
  168. SortArray<FacePos, FaceSort> s;
  169. s.compare.axis = bvh_tree[index].bounds.get_longest_axis_index();
  170. s.sort(p_faces, p_face_count);
  171. uint32_t left = _create_bvh(bvh_tree, p_faces, middle, p_triangles, p_thickness);
  172. uint32_t right = _create_bvh(bvh_tree, p_faces + middle, p_face_count - middle, p_triangles, p_thickness);
  173. bvh_tree[index].children[0] = left;
  174. bvh_tree[index].children[1] = right;
  175. return index;
  176. }
  177. static _FORCE_INLINE_ real_t Vector3_dot2(const Vector3 &p_vec3) {
  178. return p_vec3.dot(p_vec3);
  179. }
  180. void GPUParticlesCollisionSDF::_find_closest_distance(const Vector3 &p_pos, const BVH *bvh, uint32_t p_bvh_cell, const Face3 *triangles, float thickness, float &closest_distance) {
  181. if (p_bvh_cell & BVH::LEAF_BIT) {
  182. p_bvh_cell &= BVH::LEAF_MASK; //remove bit
  183. Vector3 point = p_pos;
  184. Plane p = triangles[p_bvh_cell].get_plane();
  185. float d = p.distance_to(point);
  186. float inside_d = 1e20;
  187. if (d < 0 && d > -thickness) {
  188. //inside planes, do this in 2D
  189. Vector3 x_axis = (triangles[p_bvh_cell].vertex[0] - triangles[p_bvh_cell].vertex[1]).normalized();
  190. Vector3 y_axis = p.normal.cross(x_axis).normalized();
  191. Vector2 points[3];
  192. for (int i = 0; i < 3; i++) {
  193. points[i] = Vector2(x_axis.dot(triangles[p_bvh_cell].vertex[i]), y_axis.dot(triangles[p_bvh_cell].vertex[i]));
  194. }
  195. Vector2 p2d = Vector2(x_axis.dot(point), y_axis.dot(point));
  196. {
  197. // https://www.shadertoy.com/view/XsXSz4
  198. Vector2 e0 = points[1] - points[0];
  199. Vector2 e1 = points[2] - points[1];
  200. Vector2 e2 = points[0] - points[2];
  201. Vector2 v0 = p2d - points[0];
  202. Vector2 v1 = p2d - points[1];
  203. Vector2 v2 = p2d - points[2];
  204. Vector2 pq0 = v0 - e0 * CLAMP(v0.dot(e0) / e0.dot(e0), 0.0, 1.0);
  205. Vector2 pq1 = v1 - e1 * CLAMP(v1.dot(e1) / e1.dot(e1), 0.0, 1.0);
  206. Vector2 pq2 = v2 - e2 * CLAMP(v2.dot(e2) / e2.dot(e2), 0.0, 1.0);
  207. float s = SGN(e0.x * e2.y - e0.y * e2.x);
  208. Vector2 d2 = Vector2(pq0.dot(pq0), s * (v0.x * e0.y - v0.y * e0.x)).min(Vector2(pq1.dot(pq1), s * (v1.x * e1.y - v1.y * e1.x))).min(Vector2(pq2.dot(pq2), s * (v2.x * e2.y - v2.y * e2.x)));
  209. inside_d = -Math::sqrt(d2.x) * SGN(d2.y);
  210. }
  211. //make sure distance to planes is not shorter if inside
  212. if (inside_d < 0) {
  213. inside_d = MAX(inside_d, d);
  214. inside_d = MAX(inside_d, -(thickness + d));
  215. }
  216. closest_distance = MIN(closest_distance, inside_d);
  217. } else {
  218. if (d < 0) {
  219. point -= p.normal * thickness; //flatten
  220. }
  221. // https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
  222. Vector3 a = triangles[p_bvh_cell].vertex[0];
  223. Vector3 b = triangles[p_bvh_cell].vertex[1];
  224. Vector3 c = triangles[p_bvh_cell].vertex[2];
  225. Vector3 ba = b - a;
  226. Vector3 pa = point - a;
  227. Vector3 cb = c - b;
  228. Vector3 pb = point - b;
  229. Vector3 ac = a - c;
  230. Vector3 pc = point - c;
  231. Vector3 nor = ba.cross(ac);
  232. inside_d = Math::sqrt(
  233. (SGN(ba.cross(nor).dot(pa)) +
  234. SGN(cb.cross(nor).dot(pb)) +
  235. SGN(ac.cross(nor).dot(pc)) <
  236. 2.0) ?
  237. MIN(MIN(
  238. Vector3_dot2(ba * CLAMP(ba.dot(pa) / Vector3_dot2(ba), 0.0, 1.0) - pa),
  239. Vector3_dot2(cb * CLAMP(cb.dot(pb) / Vector3_dot2(cb), 0.0, 1.0) - pb)),
  240. Vector3_dot2(ac * CLAMP(ac.dot(pc) / Vector3_dot2(ac), 0.0, 1.0) - pc)) :
  241. nor.dot(pa) * nor.dot(pa) / Vector3_dot2(nor));
  242. closest_distance = MIN(closest_distance, inside_d);
  243. }
  244. } else {
  245. bool pass = true;
  246. if (!bvh[p_bvh_cell].bounds.has_point(p_pos)) {
  247. //outside, find closest point
  248. Vector3 he = bvh[p_bvh_cell].bounds.size * 0.5;
  249. Vector3 center = bvh[p_bvh_cell].bounds.position + he;
  250. Vector3 rel = (p_pos - center).abs();
  251. Vector3 closest(MIN(rel.x, he.x), MIN(rel.y, he.y), MIN(rel.z, he.z));
  252. float d = rel.distance_to(closest);
  253. if (d >= closest_distance) {
  254. pass = false; //already closer than this aabb, discard
  255. }
  256. }
  257. if (pass) {
  258. _find_closest_distance(p_pos, bvh, bvh[p_bvh_cell].children[0], triangles, thickness, closest_distance);
  259. _find_closest_distance(p_pos, bvh, bvh[p_bvh_cell].children[1], triangles, thickness, closest_distance);
  260. }
  261. }
  262. }
  263. void GPUParticlesCollisionSDF::_compute_sdf_z(uint32_t p_z, ComputeSDFParams *params) {
  264. int32_t z_ofs = p_z * params->size.y * params->size.x;
  265. for (int32_t y = 0; y < params->size.y; y++) {
  266. int32_t y_ofs = z_ofs + y * params->size.x;
  267. for (int32_t x = 0; x < params->size.x; x++) {
  268. int32_t x_ofs = y_ofs + x;
  269. float &cell = params->cells[x_ofs];
  270. Vector3 pos = params->cell_offset + Vector3(x, y, p_z) * params->cell_size;
  271. cell = 1e20;
  272. _find_closest_distance(pos, params->bvh, 0, params->triangles, params->thickness, cell);
  273. }
  274. }
  275. }
  276. void GPUParticlesCollisionSDF::_compute_sdf(ComputeSDFParams *params) {
  277. ThreadWorkPool work_pool;
  278. work_pool.init();
  279. work_pool.begin_work(params->size.z, this, &GPUParticlesCollisionSDF::_compute_sdf_z, params);
  280. while (!work_pool.is_done_dispatching()) {
  281. OS::get_singleton()->delay_usec(10000);
  282. bake_step_function(work_pool.get_work_index() * 100 / params->size.z, "Baking SDF");
  283. }
  284. work_pool.end_work();
  285. work_pool.finish();
  286. }
  287. Vector3i GPUParticlesCollisionSDF::get_estimated_cell_size() const {
  288. static const int subdivs[RESOLUTION_MAX] = { 16, 32, 64, 128, 256, 512 };
  289. int subdiv = subdivs[get_resolution()];
  290. AABB aabb(-extents, extents * 2);
  291. float cell_size = aabb.get_longest_axis_size() / float(subdiv);
  292. Vector3i sdf_size = Vector3i(aabb.size / cell_size);
  293. sdf_size.x = MAX(1, sdf_size.x);
  294. sdf_size.y = MAX(1, sdf_size.y);
  295. sdf_size.z = MAX(1, sdf_size.z);
  296. return sdf_size;
  297. }
  298. Ref<Image> GPUParticlesCollisionSDF::bake() {
  299. static const int subdivs[RESOLUTION_MAX] = { 16, 32, 64, 128, 256, 512 };
  300. int subdiv = subdivs[get_resolution()];
  301. AABB aabb(-extents, extents * 2);
  302. float cell_size = aabb.get_longest_axis_size() / float(subdiv);
  303. Vector3i sdf_size = Vector3i(aabb.size / cell_size);
  304. sdf_size.x = MAX(1, sdf_size.x);
  305. sdf_size.y = MAX(1, sdf_size.y);
  306. sdf_size.z = MAX(1, sdf_size.z);
  307. if (bake_begin_function) {
  308. bake_begin_function(100);
  309. }
  310. aabb.size = Vector3(sdf_size) * cell_size;
  311. List<PlotMesh> plot_meshes;
  312. _find_meshes(aabb, get_parent(), plot_meshes);
  313. LocalVector<Face3> faces;
  314. if (bake_step_function) {
  315. bake_step_function(0, "Finding Meshes");
  316. }
  317. for (const PlotMesh &pm : plot_meshes) {
  318. for (int i = 0; i < pm.mesh->get_surface_count(); i++) {
  319. if (pm.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  320. continue; //only triangles
  321. }
  322. Array a = pm.mesh->surface_get_arrays(i);
  323. Vector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  324. const Vector3 *vr = vertices.ptr();
  325. Vector<int> index = a[Mesh::ARRAY_INDEX];
  326. if (index.size()) {
  327. int facecount = index.size() / 3;
  328. const int *ir = index.ptr();
  329. for (int j = 0; j < facecount; j++) {
  330. Face3 face;
  331. for (int k = 0; k < 3; k++) {
  332. face.vertex[k] = pm.local_xform.xform(vr[ir[j * 3 + k]]);
  333. }
  334. //test against original bounds
  335. if (!Geometry3D::triangle_box_overlap(aabb.get_center(), aabb.size * 0.5, face.vertex)) {
  336. continue;
  337. }
  338. faces.push_back(face);
  339. }
  340. } else {
  341. int facecount = vertices.size() / 3;
  342. for (int j = 0; j < facecount; j++) {
  343. Face3 face;
  344. for (int k = 0; k < 3; k++) {
  345. face.vertex[k] = pm.local_xform.xform(vr[j * 3 + k]);
  346. }
  347. //test against original bounds
  348. if (!Geometry3D::triangle_box_overlap(aabb.get_center(), aabb.size * 0.5, face.vertex)) {
  349. continue;
  350. }
  351. faces.push_back(face);
  352. }
  353. }
  354. }
  355. }
  356. //compute bvh
  357. ERR_FAIL_COND_V(faces.size() <= 1, Ref<Image>());
  358. LocalVector<FacePos> face_pos;
  359. face_pos.resize(faces.size());
  360. float th = cell_size * thickness;
  361. for (uint32_t i = 0; i < faces.size(); i++) {
  362. face_pos[i].index = i;
  363. face_pos[i].center = (faces[i].vertex[0] + faces[i].vertex[1] + faces[i].vertex[2]) / 2;
  364. if (th > 0.0) {
  365. face_pos[i].center -= faces[i].get_plane().normal * th * 0.5;
  366. }
  367. }
  368. if (bake_step_function) {
  369. bake_step_function(0, "Creating BVH");
  370. }
  371. LocalVector<BVH> bvh;
  372. _create_bvh(bvh, face_pos.ptr(), face_pos.size(), faces.ptr(), th);
  373. Vector<uint8_t> data;
  374. data.resize(sdf_size.z * sdf_size.y * sdf_size.x * sizeof(float));
  375. if (bake_step_function) {
  376. bake_step_function(0, "Baking SDF");
  377. }
  378. ComputeSDFParams params;
  379. params.cells = (float *)data.ptrw();
  380. params.size = sdf_size;
  381. params.cell_size = cell_size;
  382. params.cell_offset = aabb.position + Vector3(cell_size * 0.5, cell_size * 0.5, cell_size * 0.5);
  383. params.bvh = bvh.ptr();
  384. params.triangles = faces.ptr();
  385. params.thickness = th;
  386. _compute_sdf(&params);
  387. Ref<Image> ret;
  388. ret.instantiate();
  389. ret->create(sdf_size.x, sdf_size.y * sdf_size.z, false, Image::FORMAT_RF, data);
  390. ret->convert(Image::FORMAT_RH); //convert to half, save space
  391. ret->set_meta("depth", sdf_size.z); //hack, make sure to add to the docs of this function
  392. if (bake_end_function) {
  393. bake_end_function();
  394. }
  395. return ret;
  396. }
  397. void GPUParticlesCollisionSDF::_bind_methods() {
  398. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GPUParticlesCollisionSDF::set_extents);
  399. ClassDB::bind_method(D_METHOD("get_extents"), &GPUParticlesCollisionSDF::get_extents);
  400. ClassDB::bind_method(D_METHOD("set_resolution", "resolution"), &GPUParticlesCollisionSDF::set_resolution);
  401. ClassDB::bind_method(D_METHOD("get_resolution"), &GPUParticlesCollisionSDF::get_resolution);
  402. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticlesCollisionSDF::set_texture);
  403. ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticlesCollisionSDF::get_texture);
  404. ClassDB::bind_method(D_METHOD("set_thickness", "thickness"), &GPUParticlesCollisionSDF::set_thickness);
  405. ClassDB::bind_method(D_METHOD("get_thickness"), &GPUParticlesCollisionSDF::get_thickness);
  406. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_extents", "get_extents");
  407. ADD_PROPERTY(PropertyInfo(Variant::INT, "resolution", PROPERTY_HINT_ENUM, "16,32,64,128,256,512"), "set_resolution", "get_resolution");
  408. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "thickness", PROPERTY_HINT_RANGE, "0.0,2.0,0.01"), "set_thickness", "get_thickness");
  409. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture3D"), "set_texture", "get_texture");
  410. BIND_ENUM_CONSTANT(RESOLUTION_16);
  411. BIND_ENUM_CONSTANT(RESOLUTION_32);
  412. BIND_ENUM_CONSTANT(RESOLUTION_64);
  413. BIND_ENUM_CONSTANT(RESOLUTION_128);
  414. BIND_ENUM_CONSTANT(RESOLUTION_256);
  415. BIND_ENUM_CONSTANT(RESOLUTION_512);
  416. BIND_ENUM_CONSTANT(RESOLUTION_MAX);
  417. }
  418. void GPUParticlesCollisionSDF::set_thickness(float p_thickness) {
  419. thickness = p_thickness;
  420. }
  421. float GPUParticlesCollisionSDF::get_thickness() const {
  422. return thickness;
  423. }
  424. void GPUParticlesCollisionSDF::set_extents(const Vector3 &p_extents) {
  425. extents = p_extents;
  426. RS::get_singleton()->particles_collision_set_box_extents(_get_collision(), extents);
  427. update_gizmos();
  428. }
  429. Vector3 GPUParticlesCollisionSDF::get_extents() const {
  430. return extents;
  431. }
  432. void GPUParticlesCollisionSDF::set_resolution(Resolution p_resolution) {
  433. resolution = p_resolution;
  434. update_gizmos();
  435. }
  436. GPUParticlesCollisionSDF::Resolution GPUParticlesCollisionSDF::get_resolution() const {
  437. return resolution;
  438. }
  439. void GPUParticlesCollisionSDF::set_texture(const Ref<Texture3D> &p_texture) {
  440. texture = p_texture;
  441. RID tex = texture.is_valid() ? texture->get_rid() : RID();
  442. RS::get_singleton()->particles_collision_set_field_texture(_get_collision(), tex);
  443. }
  444. Ref<Texture3D> GPUParticlesCollisionSDF::get_texture() const {
  445. return texture;
  446. }
  447. AABB GPUParticlesCollisionSDF::get_aabb() const {
  448. return AABB(-extents, extents * 2);
  449. }
  450. GPUParticlesCollisionSDF::BakeBeginFunc GPUParticlesCollisionSDF::bake_begin_function = nullptr;
  451. GPUParticlesCollisionSDF::BakeStepFunc GPUParticlesCollisionSDF::bake_step_function = nullptr;
  452. GPUParticlesCollisionSDF::BakeEndFunc GPUParticlesCollisionSDF::bake_end_function = nullptr;
  453. GPUParticlesCollisionSDF::GPUParticlesCollisionSDF() :
  454. GPUParticlesCollision3D(RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE) {
  455. }
  456. GPUParticlesCollisionSDF::~GPUParticlesCollisionSDF() {
  457. }
  458. ////////////////////////////
  459. ////////////////////////////
  460. void GPUParticlesCollisionHeightField::_notification(int p_what) {
  461. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  462. if (update_mode == UPDATE_MODE_ALWAYS) {
  463. RS::get_singleton()->particles_collision_height_field_update(_get_collision());
  464. }
  465. if (follow_camera_mode && get_viewport()) {
  466. Camera3D *cam = get_viewport()->get_camera_3d();
  467. if (cam) {
  468. Transform3D xform = get_global_transform();
  469. Vector3 x_axis = xform.basis.get_axis(Vector3::AXIS_X).normalized();
  470. Vector3 z_axis = xform.basis.get_axis(Vector3::AXIS_Z).normalized();
  471. float x_len = xform.basis.get_scale().x;
  472. float z_len = xform.basis.get_scale().z;
  473. Vector3 cam_pos = cam->get_global_transform().origin;
  474. Transform3D new_xform = xform;
  475. while (x_axis.dot(cam_pos - new_xform.origin) > x_len) {
  476. new_xform.origin += x_axis * x_len;
  477. }
  478. while (x_axis.dot(cam_pos - new_xform.origin) < -x_len) {
  479. new_xform.origin -= x_axis * x_len;
  480. }
  481. while (z_axis.dot(cam_pos - new_xform.origin) > z_len) {
  482. new_xform.origin += z_axis * z_len;
  483. }
  484. while (z_axis.dot(cam_pos - new_xform.origin) < -z_len) {
  485. new_xform.origin -= z_axis * z_len;
  486. }
  487. if (new_xform != xform) {
  488. set_global_transform(new_xform);
  489. RS::get_singleton()->particles_collision_height_field_update(_get_collision());
  490. }
  491. }
  492. }
  493. }
  494. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  495. RS::get_singleton()->particles_collision_height_field_update(_get_collision());
  496. }
  497. }
  498. void GPUParticlesCollisionHeightField::_bind_methods() {
  499. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GPUParticlesCollisionHeightField::set_extents);
  500. ClassDB::bind_method(D_METHOD("get_extents"), &GPUParticlesCollisionHeightField::get_extents);
  501. ClassDB::bind_method(D_METHOD("set_resolution", "resolution"), &GPUParticlesCollisionHeightField::set_resolution);
  502. ClassDB::bind_method(D_METHOD("get_resolution"), &GPUParticlesCollisionHeightField::get_resolution);
  503. ClassDB::bind_method(D_METHOD("set_update_mode", "update_mode"), &GPUParticlesCollisionHeightField::set_update_mode);
  504. ClassDB::bind_method(D_METHOD("get_update_mode"), &GPUParticlesCollisionHeightField::get_update_mode);
  505. ClassDB::bind_method(D_METHOD("set_follow_camera_mode", "enabled"), &GPUParticlesCollisionHeightField::set_follow_camera_mode);
  506. ClassDB::bind_method(D_METHOD("is_follow_camera_mode_enabled"), &GPUParticlesCollisionHeightField::is_follow_camera_mode_enabled);
  507. ClassDB::bind_method(D_METHOD("set_follow_camera_push_ratio", "ratio"), &GPUParticlesCollisionHeightField::set_follow_camera_push_ratio);
  508. ClassDB::bind_method(D_METHOD("get_follow_camera_push_ratio"), &GPUParticlesCollisionHeightField::get_follow_camera_push_ratio);
  509. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_extents", "get_extents");
  510. ADD_PROPERTY(PropertyInfo(Variant::INT, "resolution", PROPERTY_HINT_ENUM, "256,512,1024,2048,4096,8192"), "set_resolution", "get_resolution");
  511. ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "WhenMoved,Always"), "set_update_mode", "get_update_mode");
  512. ADD_GROUP("Follow Camera", "follow_camera_");
  513. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "follow_camera_enabled"), "set_follow_camera_mode", "is_follow_camera_mode_enabled");
  514. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "follow_camera_push_ratio", PROPERTY_HINT_RANGE, "0.01,1,0.01"), "set_follow_camera_push_ratio", "get_follow_camera_push_ratio");
  515. BIND_ENUM_CONSTANT(RESOLUTION_256);
  516. BIND_ENUM_CONSTANT(RESOLUTION_512);
  517. BIND_ENUM_CONSTANT(RESOLUTION_1024);
  518. BIND_ENUM_CONSTANT(RESOLUTION_2048);
  519. BIND_ENUM_CONSTANT(RESOLUTION_4096);
  520. BIND_ENUM_CONSTANT(RESOLUTION_8192);
  521. BIND_ENUM_CONSTANT(RESOLUTION_MAX);
  522. BIND_ENUM_CONSTANT(UPDATE_MODE_WHEN_MOVED);
  523. BIND_ENUM_CONSTANT(UPDATE_MODE_ALWAYS);
  524. }
  525. void GPUParticlesCollisionHeightField::set_follow_camera_push_ratio(float p_follow_camera_push_ratio) {
  526. follow_camera_push_ratio = p_follow_camera_push_ratio;
  527. }
  528. float GPUParticlesCollisionHeightField::get_follow_camera_push_ratio() const {
  529. return follow_camera_push_ratio;
  530. }
  531. void GPUParticlesCollisionHeightField::set_extents(const Vector3 &p_extents) {
  532. extents = p_extents;
  533. RS::get_singleton()->particles_collision_set_box_extents(_get_collision(), extents);
  534. update_gizmos();
  535. RS::get_singleton()->particles_collision_height_field_update(_get_collision());
  536. }
  537. Vector3 GPUParticlesCollisionHeightField::get_extents() const {
  538. return extents;
  539. }
  540. void GPUParticlesCollisionHeightField::set_resolution(Resolution p_resolution) {
  541. resolution = p_resolution;
  542. RS::get_singleton()->particles_collision_set_height_field_resolution(_get_collision(), RS::ParticlesCollisionHeightfieldResolution(resolution));
  543. update_gizmos();
  544. RS::get_singleton()->particles_collision_height_field_update(_get_collision());
  545. }
  546. GPUParticlesCollisionHeightField::Resolution GPUParticlesCollisionHeightField::get_resolution() const {
  547. return resolution;
  548. }
  549. void GPUParticlesCollisionHeightField::set_update_mode(UpdateMode p_update_mode) {
  550. update_mode = p_update_mode;
  551. set_process_internal(follow_camera_mode || update_mode == UPDATE_MODE_ALWAYS);
  552. }
  553. GPUParticlesCollisionHeightField::UpdateMode GPUParticlesCollisionHeightField::get_update_mode() const {
  554. return update_mode;
  555. }
  556. void GPUParticlesCollisionHeightField::set_follow_camera_mode(bool p_enabled) {
  557. follow_camera_mode = p_enabled;
  558. set_process_internal(follow_camera_mode || update_mode == UPDATE_MODE_ALWAYS);
  559. }
  560. bool GPUParticlesCollisionHeightField::is_follow_camera_mode_enabled() const {
  561. return follow_camera_mode;
  562. }
  563. AABB GPUParticlesCollisionHeightField::get_aabb() const {
  564. return AABB(-extents, extents * 2);
  565. }
  566. GPUParticlesCollisionHeightField::GPUParticlesCollisionHeightField() :
  567. GPUParticlesCollision3D(RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE) {
  568. }
  569. GPUParticlesCollisionHeightField::~GPUParticlesCollisionHeightField() {
  570. }
  571. ////////////////////////////
  572. ////////////////////////////
  573. void GPUParticlesAttractor3D::set_cull_mask(uint32_t p_cull_mask) {
  574. cull_mask = p_cull_mask;
  575. RS::get_singleton()->particles_collision_set_cull_mask(collision, p_cull_mask);
  576. }
  577. uint32_t GPUParticlesAttractor3D::get_cull_mask() const {
  578. return cull_mask;
  579. }
  580. void GPUParticlesAttractor3D::set_strength(real_t p_strength) {
  581. strength = p_strength;
  582. RS::get_singleton()->particles_collision_set_attractor_strength(collision, p_strength);
  583. }
  584. real_t GPUParticlesAttractor3D::get_strength() const {
  585. return strength;
  586. }
  587. void GPUParticlesAttractor3D::set_attenuation(real_t p_attenuation) {
  588. attenuation = p_attenuation;
  589. RS::get_singleton()->particles_collision_set_attractor_attenuation(collision, p_attenuation);
  590. }
  591. real_t GPUParticlesAttractor3D::get_attenuation() const {
  592. return attenuation;
  593. }
  594. void GPUParticlesAttractor3D::set_directionality(real_t p_directionality) {
  595. directionality = p_directionality;
  596. RS::get_singleton()->particles_collision_set_attractor_directionality(collision, p_directionality);
  597. update_gizmos();
  598. }
  599. real_t GPUParticlesAttractor3D::get_directionality() const {
  600. return directionality;
  601. }
  602. void GPUParticlesAttractor3D::_bind_methods() {
  603. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &GPUParticlesAttractor3D::set_cull_mask);
  604. ClassDB::bind_method(D_METHOD("get_cull_mask"), &GPUParticlesAttractor3D::get_cull_mask);
  605. ClassDB::bind_method(D_METHOD("set_strength", "strength"), &GPUParticlesAttractor3D::set_strength);
  606. ClassDB::bind_method(D_METHOD("get_strength"), &GPUParticlesAttractor3D::get_strength);
  607. ClassDB::bind_method(D_METHOD("set_attenuation", "attenuation"), &GPUParticlesAttractor3D::set_attenuation);
  608. ClassDB::bind_method(D_METHOD("get_attenuation"), &GPUParticlesAttractor3D::get_attenuation);
  609. ClassDB::bind_method(D_METHOD("set_directionality", "amount"), &GPUParticlesAttractor3D::set_directionality);
  610. ClassDB::bind_method(D_METHOD("get_directionality"), &GPUParticlesAttractor3D::get_directionality);
  611. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "-128,128,0.01,or_greater,or_lesser"), "set_strength", "get_strength");
  612. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "attenuation", PROPERTY_HINT_EXP_EASING, "0,8,0.01"), "set_attenuation", "get_attenuation");
  613. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "directionality", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_directionality", "get_directionality");
  614. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  615. }
  616. GPUParticlesAttractor3D::GPUParticlesAttractor3D(RS::ParticlesCollisionType p_type) {
  617. collision = RS::get_singleton()->particles_collision_create();
  618. RS::get_singleton()->particles_collision_set_collision_type(collision, p_type);
  619. set_base(collision);
  620. }
  621. GPUParticlesAttractor3D::~GPUParticlesAttractor3D() {
  622. RS::get_singleton()->free(collision);
  623. }
  624. /////////////////////////////////
  625. void GPUParticlesAttractorSphere::_bind_methods() {
  626. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GPUParticlesAttractorSphere::set_radius);
  627. ClassDB::bind_method(D_METHOD("get_radius"), &GPUParticlesAttractorSphere::get_radius);
  628. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_radius", "get_radius");
  629. }
  630. void GPUParticlesAttractorSphere::set_radius(real_t p_radius) {
  631. radius = p_radius;
  632. RS::get_singleton()->particles_collision_set_sphere_radius(_get_collision(), radius);
  633. update_gizmos();
  634. }
  635. real_t GPUParticlesAttractorSphere::get_radius() const {
  636. return radius;
  637. }
  638. AABB GPUParticlesAttractorSphere::get_aabb() const {
  639. return AABB(Vector3(-radius, -radius, -radius), Vector3(radius * 2, radius * 2, radius * 2));
  640. }
  641. GPUParticlesAttractorSphere::GPUParticlesAttractorSphere() :
  642. GPUParticlesAttractor3D(RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT) {
  643. }
  644. GPUParticlesAttractorSphere::~GPUParticlesAttractorSphere() {
  645. }
  646. ///////////////////////////
  647. void GPUParticlesAttractorBox::_bind_methods() {
  648. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GPUParticlesAttractorBox::set_extents);
  649. ClassDB::bind_method(D_METHOD("get_extents"), &GPUParticlesAttractorBox::get_extents);
  650. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_extents", "get_extents");
  651. }
  652. void GPUParticlesAttractorBox::set_extents(const Vector3 &p_extents) {
  653. extents = p_extents;
  654. RS::get_singleton()->particles_collision_set_box_extents(_get_collision(), extents);
  655. update_gizmos();
  656. }
  657. Vector3 GPUParticlesAttractorBox::get_extents() const {
  658. return extents;
  659. }
  660. AABB GPUParticlesAttractorBox::get_aabb() const {
  661. return AABB(-extents, extents * 2);
  662. }
  663. GPUParticlesAttractorBox::GPUParticlesAttractorBox() :
  664. GPUParticlesAttractor3D(RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT) {
  665. }
  666. GPUParticlesAttractorBox::~GPUParticlesAttractorBox() {
  667. }
  668. ///////////////////////////
  669. void GPUParticlesAttractorVectorField::_bind_methods() {
  670. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GPUParticlesAttractorVectorField::set_extents);
  671. ClassDB::bind_method(D_METHOD("get_extents"), &GPUParticlesAttractorVectorField::get_extents);
  672. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &GPUParticlesAttractorVectorField::set_texture);
  673. ClassDB::bind_method(D_METHOD("get_texture"), &GPUParticlesAttractorVectorField::get_texture);
  674. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater"), "set_extents", "get_extents");
  675. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture3D"), "set_texture", "get_texture");
  676. }
  677. void GPUParticlesAttractorVectorField::set_extents(const Vector3 &p_extents) {
  678. extents = p_extents;
  679. RS::get_singleton()->particles_collision_set_box_extents(_get_collision(), extents);
  680. update_gizmos();
  681. }
  682. Vector3 GPUParticlesAttractorVectorField::get_extents() const {
  683. return extents;
  684. }
  685. void GPUParticlesAttractorVectorField::set_texture(const Ref<Texture3D> &p_texture) {
  686. texture = p_texture;
  687. RID tex = texture.is_valid() ? texture->get_rid() : RID();
  688. RS::get_singleton()->particles_collision_set_field_texture(_get_collision(), tex);
  689. }
  690. Ref<Texture3D> GPUParticlesAttractorVectorField::get_texture() const {
  691. return texture;
  692. }
  693. AABB GPUParticlesAttractorVectorField::get_aabb() const {
  694. return AABB(-extents, extents * 2);
  695. }
  696. GPUParticlesAttractorVectorField::GPUParticlesAttractorVectorField() :
  697. GPUParticlesAttractor3D(RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) {
  698. }
  699. GPUParticlesAttractorVectorField::~GPUParticlesAttractorVectorField() {
  700. }