csg_shape.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /*************************************************************************/
  2. /* csg_shape.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "csg_shape.h"
  31. #include "scene/3d/path.h"
  32. void CSGShape::set_use_collision(bool p_enable) {
  33. if (use_collision == p_enable)
  34. return;
  35. use_collision = p_enable;
  36. if (!is_inside_tree() || !is_root_shape())
  37. return;
  38. if (use_collision) {
  39. root_collision_shape.instance();
  40. root_collision_instance = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  41. PhysicsServer::get_singleton()->body_set_state(root_collision_instance, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  42. PhysicsServer::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  43. PhysicsServer::get_singleton()->body_set_space(root_collision_instance, get_world()->get_space());
  44. _make_dirty(); //force update
  45. } else {
  46. PhysicsServer::get_singleton()->free(root_collision_instance);
  47. root_collision_instance = RID();
  48. root_collision_shape.unref();
  49. }
  50. }
  51. bool CSGShape::is_using_collision() const {
  52. return use_collision;
  53. }
  54. bool CSGShape::is_root_shape() const {
  55. return !parent;
  56. }
  57. void CSGShape::set_snap(float p_snap) {
  58. snap = p_snap;
  59. }
  60. float CSGShape::get_snap() const {
  61. return snap;
  62. }
  63. void CSGShape::_make_dirty() {
  64. if (!is_inside_tree())
  65. return;
  66. if (dirty) {
  67. return;
  68. }
  69. dirty = true;
  70. if (parent) {
  71. parent->_make_dirty();
  72. } else {
  73. //only parent will do
  74. call_deferred("_update_shape");
  75. }
  76. }
  77. CSGBrush *CSGShape::_get_brush() {
  78. if (dirty) {
  79. if (brush) {
  80. memdelete(brush);
  81. }
  82. brush = NULL;
  83. CSGBrush *n = _build_brush();
  84. for (int i = 0; i < get_child_count(); i++) {
  85. CSGShape *child = Object::cast_to<CSGShape>(get_child(i));
  86. if (!child)
  87. continue;
  88. if (!child->is_visible_in_tree())
  89. continue;
  90. CSGBrush *n2 = child->_get_brush();
  91. if (!n2)
  92. continue;
  93. if (!n) {
  94. n = memnew(CSGBrush);
  95. n->copy_from(*n2, child->get_transform());
  96. } else {
  97. CSGBrush *nn = memnew(CSGBrush);
  98. CSGBrush *nn2 = memnew(CSGBrush);
  99. nn2->copy_from(*n2, child->get_transform());
  100. CSGBrushOperation bop;
  101. switch (child->get_operation()) {
  102. case CSGShape::OPERATION_UNION: bop.merge_brushes(CSGBrushOperation::OPERATION_UNION, *n, *nn2, *nn, snap); break;
  103. case CSGShape::OPERATION_INTERSECTION: bop.merge_brushes(CSGBrushOperation::OPERATION_INTERSECTION, *n, *nn2, *nn, snap); break;
  104. case CSGShape::OPERATION_SUBTRACTION: bop.merge_brushes(CSGBrushOperation::OPERATION_SUBSTRACTION, *n, *nn2, *nn, snap); break;
  105. }
  106. memdelete(n);
  107. memdelete(nn2);
  108. n = nn;
  109. }
  110. }
  111. if (n) {
  112. AABB aabb;
  113. for (int i = 0; i < n->faces.size(); i++) {
  114. for (int j = 0; j < 3; j++) {
  115. if (i == 0 && j == 0)
  116. aabb.position = n->faces[i].vertices[j];
  117. else
  118. aabb.expand_to(n->faces[i].vertices[j]);
  119. }
  120. }
  121. node_aabb = aabb;
  122. } else {
  123. node_aabb = AABB();
  124. }
  125. brush = n;
  126. dirty = false;
  127. }
  128. return brush;
  129. }
  130. void CSGShape::_update_shape() {
  131. //print_line("updating shape for " + String(get_path()));
  132. if (parent)
  133. return;
  134. set_base(RID());
  135. root_mesh.unref(); //byebye root mesh
  136. CSGBrush *n = _get_brush();
  137. ERR_FAIL_COND(!n);
  138. OAHashMap<Vector3, Vector3> vec_map;
  139. Vector<int> face_count;
  140. face_count.resize(n->materials.size() + 1);
  141. for (int i = 0; i < face_count.size(); i++) {
  142. face_count[i] = 0;
  143. }
  144. for (int i = 0; i < n->faces.size(); i++) {
  145. int mat = n->faces[i].material;
  146. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  147. int idx = mat == -1 ? face_count.size() - 1 : mat;
  148. if (n->faces[i].smooth) {
  149. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  150. for (int j = 0; j < 3; j++) {
  151. Vector3 v = n->faces[i].vertices[j];
  152. Vector3 add;
  153. if (vec_map.lookup(v, add)) {
  154. add += p.normal;
  155. } else {
  156. add = p.normal;
  157. }
  158. vec_map.set(v, add);
  159. }
  160. }
  161. face_count[idx]++;
  162. }
  163. Vector<ShapeUpdateSurface> surfaces;
  164. surfaces.resize(face_count.size());
  165. //create arrays
  166. for (int i = 0; i < surfaces.size(); i++) {
  167. surfaces[i].vertices.resize(face_count[i] * 3);
  168. surfaces[i].normals.resize(face_count[i] * 3);
  169. surfaces[i].uvs.resize(face_count[i] * 3);
  170. surfaces[i].last_added = 0;
  171. if (i != surfaces.size() - 1) {
  172. surfaces[i].material = n->materials[i];
  173. }
  174. surfaces[i].verticesw = surfaces[i].vertices.write();
  175. surfaces[i].normalsw = surfaces[i].normals.write();
  176. surfaces[i].uvsw = surfaces[i].uvs.write();
  177. }
  178. //fill arrays
  179. PoolVector<Vector3> physics_faces;
  180. bool fill_physics_faces = false;
  181. if (root_collision_shape.is_valid()) {
  182. physics_faces.resize(n->faces.size() * 3);
  183. fill_physics_faces = true;
  184. }
  185. {
  186. PoolVector<Vector3>::Write physicsw;
  187. if (fill_physics_faces) {
  188. physicsw = physics_faces.write();
  189. }
  190. for (int i = 0; i < n->faces.size(); i++) {
  191. int order[3] = { 0, 1, 2 };
  192. if (n->faces[i].invert) {
  193. SWAP(order[1], order[2]);
  194. }
  195. if (fill_physics_faces) {
  196. physicsw[i * 3 + 0] = n->faces[i].vertices[order[0]];
  197. physicsw[i * 3 + 1] = n->faces[i].vertices[order[1]];
  198. physicsw[i * 3 + 2] = n->faces[i].vertices[order[2]];
  199. }
  200. int mat = n->faces[i].material;
  201. ERR_CONTINUE(mat < -1 || mat >= face_count.size());
  202. int idx = mat == -1 ? face_count.size() - 1 : mat;
  203. int last = surfaces[idx].last_added;
  204. Plane p(n->faces[i].vertices[0], n->faces[i].vertices[1], n->faces[i].vertices[2]);
  205. for (int j = 0; j < 3; j++) {
  206. Vector3 v = n->faces[i].vertices[j];
  207. Vector3 normal = p.normal;
  208. if (n->faces[i].smooth && vec_map.lookup(v, normal)) {
  209. normal.normalize();
  210. }
  211. if (n->faces[i].invert) {
  212. normal = -normal;
  213. }
  214. surfaces[idx].verticesw[last + order[j]] = v;
  215. surfaces[idx].uvsw[last + order[j]] = n->faces[i].uvs[j];
  216. surfaces[idx].normalsw[last + order[j]] = normal;
  217. }
  218. surfaces[idx].last_added += 3;
  219. }
  220. }
  221. root_mesh.instance();
  222. //create surfaces
  223. for (int i = 0; i < surfaces.size(); i++) {
  224. surfaces[i].verticesw = PoolVector<Vector3>::Write();
  225. surfaces[i].normalsw = PoolVector<Vector3>::Write();
  226. surfaces[i].uvsw = PoolVector<Vector2>::Write();
  227. if (surfaces[i].last_added == 0)
  228. continue;
  229. Array array;
  230. array.resize(Mesh::ARRAY_MAX);
  231. array[Mesh::ARRAY_VERTEX] = surfaces[i].vertices;
  232. array[Mesh::ARRAY_NORMAL] = surfaces[i].normals;
  233. array[Mesh::ARRAY_TEX_UV] = surfaces[i].uvs;
  234. int idx = root_mesh->get_surface_count();
  235. root_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array);
  236. root_mesh->surface_set_material(idx, surfaces[i].material);
  237. }
  238. if (root_collision_shape.is_valid()) {
  239. root_collision_shape->set_faces(physics_faces);
  240. }
  241. set_base(root_mesh->get_rid());
  242. }
  243. AABB CSGShape::get_aabb() const {
  244. return node_aabb;
  245. }
  246. PoolVector<Vector3> CSGShape::get_brush_faces() {
  247. ERR_FAIL_COND_V(!is_inside_tree(), PoolVector<Vector3>());
  248. CSGBrush *b = _get_brush();
  249. if (!b) {
  250. return PoolVector<Vector3>();
  251. }
  252. PoolVector<Vector3> faces;
  253. int fc = b->faces.size();
  254. faces.resize(fc * 3);
  255. {
  256. PoolVector<Vector3>::Write w = faces.write();
  257. for (int i = 0; i < fc; i++) {
  258. w[i * 3 + 0] = b->faces[i].vertices[0];
  259. w[i * 3 + 1] = b->faces[i].vertices[1];
  260. w[i * 3 + 2] = b->faces[i].vertices[2];
  261. }
  262. }
  263. return faces;
  264. }
  265. PoolVector<Face3> CSGShape::get_faces(uint32_t p_usage_flags) const {
  266. return PoolVector<Face3>();
  267. }
  268. void CSGShape::_notification(int p_what) {
  269. if (p_what == NOTIFICATION_ENTER_TREE) {
  270. Node *parentn = get_parent();
  271. if (parentn) {
  272. parent = Object::cast_to<CSGShape>(parentn);
  273. if (parent) {
  274. set_base(RID());
  275. root_mesh.unref();
  276. }
  277. }
  278. if (use_collision && is_root_shape()) {
  279. root_collision_shape.instance();
  280. root_collision_instance = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
  281. PhysicsServer::get_singleton()->body_set_state(root_collision_instance, PhysicsServer::BODY_STATE_TRANSFORM, get_global_transform());
  282. PhysicsServer::get_singleton()->body_add_shape(root_collision_instance, root_collision_shape->get_rid());
  283. PhysicsServer::get_singleton()->body_set_space(root_collision_instance, get_world()->get_space());
  284. }
  285. _make_dirty();
  286. }
  287. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  288. //print_line("local xform changed");
  289. if (parent) {
  290. parent->_make_dirty();
  291. }
  292. }
  293. if (p_what == NOTIFICATION_EXIT_TREE) {
  294. if (parent)
  295. parent->_make_dirty();
  296. parent = NULL;
  297. if (use_collision && is_root_shape()) {
  298. PhysicsServer::get_singleton()->free(root_collision_instance);
  299. root_collision_instance = RID();
  300. root_collision_shape.unref();
  301. }
  302. _make_dirty();
  303. }
  304. }
  305. void CSGShape::set_operation(Operation p_operation) {
  306. operation = p_operation;
  307. _make_dirty();
  308. }
  309. CSGShape::Operation CSGShape::get_operation() const {
  310. return operation;
  311. }
  312. void CSGShape::_validate_property(PropertyInfo &property) const {
  313. if (is_inside_tree() && property.name.begins_with("use_collision") && !is_root_shape()) {
  314. //hide collision if not root
  315. property.usage = PROPERTY_USAGE_NOEDITOR;
  316. }
  317. }
  318. void CSGShape::_bind_methods() {
  319. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape::_update_shape);
  320. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape::is_root_shape);
  321. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape::set_operation);
  322. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape::get_operation);
  323. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape::set_use_collision);
  324. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape::is_using_collision);
  325. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape::set_snap);
  326. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape::get_snap);
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  328. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  329. ADD_PROPERTY(PropertyInfo(Variant::REAL, "snap", PROPERTY_HINT_RANGE, "0.0001,1,0.001"), "set_snap", "get_snap");
  330. BIND_ENUM_CONSTANT(OPERATION_UNION);
  331. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  332. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  333. }
  334. CSGShape::CSGShape() {
  335. brush = NULL;
  336. set_notify_local_transform(true);
  337. dirty = false;
  338. parent = NULL;
  339. use_collision = false;
  340. operation = OPERATION_UNION;
  341. snap = 0.001;
  342. }
  343. CSGShape::~CSGShape() {
  344. if (brush) {
  345. memdelete(brush);
  346. brush = NULL;
  347. }
  348. }
  349. //////////////////////////////////
  350. CSGBrush *CSGCombiner::_build_brush() {
  351. return NULL; //does not build anything
  352. }
  353. CSGCombiner::CSGCombiner() {
  354. }
  355. /////////////////////
  356. CSGBrush *CSGPrimitive::_create_brush_from_arrays(const PoolVector<Vector3> &p_vertices, const PoolVector<Vector2> &p_uv, const PoolVector<bool> &p_smooth, const PoolVector<Ref<Material> > &p_materials) {
  357. CSGBrush *brush = memnew(CSGBrush);
  358. PoolVector<bool> invert;
  359. invert.resize(p_vertices.size() / 3);
  360. {
  361. int ic = invert.size();
  362. PoolVector<bool>::Write w = invert.write();
  363. for (int i = 0; i < ic; i++) {
  364. w[i] = invert_faces;
  365. }
  366. }
  367. brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  368. return brush;
  369. }
  370. void CSGPrimitive::_bind_methods() {
  371. ClassDB::bind_method(D_METHOD("set_invert_faces", "invert_faces"), &CSGPrimitive::set_invert_faces);
  372. ClassDB::bind_method(D_METHOD("is_inverting_faces"), &CSGPrimitive::is_inverting_faces);
  373. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_faces"), "set_invert_faces", "is_inverting_faces");
  374. }
  375. void CSGPrimitive::set_invert_faces(bool p_invert) {
  376. if (invert_faces == p_invert)
  377. return;
  378. invert_faces = p_invert;
  379. _make_dirty();
  380. }
  381. bool CSGPrimitive::is_inverting_faces() {
  382. return invert_faces;
  383. }
  384. CSGPrimitive::CSGPrimitive() {
  385. invert_faces = false;
  386. }
  387. /////////////////////
  388. CSGBrush *CSGMesh::_build_brush() {
  389. if (!mesh.is_valid())
  390. return NULL;
  391. PoolVector<Vector3> vertices;
  392. PoolVector<bool> smooth;
  393. PoolVector<Ref<Material> > materials;
  394. PoolVector<Vector2> uvs;
  395. for (int i = 0; i < mesh->get_surface_count(); i++) {
  396. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  397. continue;
  398. }
  399. Array arrays = mesh->surface_get_arrays(i);
  400. PoolVector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  401. if (avertices.size() == 0)
  402. continue;
  403. PoolVector<Vector3>::Read vr = avertices.read();
  404. PoolVector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  405. PoolVector<Vector3>::Read nr;
  406. bool nr_used = false;
  407. if (anormals.size()) {
  408. nr = anormals.read();
  409. nr_used = true;
  410. }
  411. PoolVector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  412. PoolVector<Vector2>::Read uvr;
  413. bool uvr_used = false;
  414. if (auvs.size()) {
  415. uvr = auvs.read();
  416. uvr_used = true;
  417. }
  418. Ref<Material> mat = mesh->surface_get_material(i);
  419. PoolVector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  420. if (aindices.size()) {
  421. int as = vertices.size();
  422. int is = aindices.size();
  423. vertices.resize(as + is);
  424. smooth.resize((as + is) / 3);
  425. materials.resize((as + is) / 3);
  426. uvs.resize(as + is);
  427. PoolVector<Vector3>::Write vw = vertices.write();
  428. PoolVector<bool>::Write sw = smooth.write();
  429. PoolVector<Vector2>::Write uvw = uvs.write();
  430. PoolVector<Ref<Material> >::Write mw = materials.write();
  431. PoolVector<int>::Read ir = aindices.read();
  432. for (int j = 0; j < is; j += 3) {
  433. Vector3 vertex[3];
  434. Vector3 normal[3];
  435. Vector2 uv[3];
  436. for (int k = 0; k < 3; k++) {
  437. int idx = ir[j + k];
  438. vertex[k] = vr[idx];
  439. if (nr_used) {
  440. normal[k] = nr[idx];
  441. }
  442. if (uvr_used) {
  443. uv[k] = uvr[idx];
  444. }
  445. }
  446. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  447. vw[as + j + 0] = vertex[0];
  448. vw[as + j + 1] = vertex[1];
  449. vw[as + j + 2] = vertex[2];
  450. uvw[as + j + 0] = uv[0];
  451. uvw[as + j + 1] = uv[1];
  452. uvw[as + j + 2] = uv[2];
  453. sw[j / 3] = !flat;
  454. mw[j / 3] = mat;
  455. }
  456. } else {
  457. int is = vertices.size();
  458. int as = avertices.size();
  459. vertices.resize(as + is);
  460. smooth.resize((as + is) / 3);
  461. uvs.resize(as + is);
  462. materials.resize((as + is) / 3);
  463. PoolVector<Vector3>::Write vw = vertices.write();
  464. PoolVector<bool>::Write sw = smooth.write();
  465. PoolVector<Vector2>::Write uvw = uvs.write();
  466. PoolVector<Ref<Material> >::Write mw = materials.write();
  467. for (int j = 0; j < is; j += 3) {
  468. Vector3 vertex[3];
  469. Vector3 normal[3];
  470. Vector2 uv[3];
  471. for (int k = 0; k < 3; k++) {
  472. vertex[k] = vr[j + k];
  473. if (nr_used) {
  474. normal[k] = nr[j + k];
  475. }
  476. if (uvr_used) {
  477. uv[k] = uvr[j + k];
  478. }
  479. }
  480. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  481. vw[as + j + 0] = vertex[0];
  482. vw[as + j + 1] = vertex[1];
  483. vw[as + j + 2] = vertex[2];
  484. uvw[as + j + 0] = uv[0];
  485. uvw[as + j + 1] = uv[1];
  486. uvw[as + j + 2] = uv[2];
  487. sw[j / 3] = !flat;
  488. mw[j / 3] = mat;
  489. }
  490. }
  491. }
  492. //print_line("total vertices? " + itos(vertices.size()));
  493. if (vertices.size() == 0)
  494. return NULL;
  495. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  496. }
  497. void CSGMesh::_mesh_changed() {
  498. _make_dirty();
  499. update_gizmo();
  500. }
  501. void CSGMesh::_bind_methods() {
  502. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh::set_mesh);
  503. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh::get_mesh);
  504. ClassDB::bind_method(D_METHOD("_mesh_changed"), &CSGMesh::_mesh_changed);
  505. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  506. }
  507. void CSGMesh::set_mesh(const Ref<Mesh> &p_mesh) {
  508. if (mesh == p_mesh)
  509. return;
  510. if (mesh.is_valid()) {
  511. mesh->disconnect("changed", this, "_mesh_changed");
  512. }
  513. mesh = p_mesh;
  514. if (mesh.is_valid()) {
  515. mesh->connect("changed", this, "_mesh_changed");
  516. }
  517. _make_dirty();
  518. }
  519. Ref<Mesh> CSGMesh::get_mesh() {
  520. return mesh;
  521. }
  522. ////////////////////////////////
  523. CSGBrush *CSGSphere::_build_brush() {
  524. // set our bounding box
  525. CSGBrush *brush = memnew(CSGBrush);
  526. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  527. bool invert_val = is_inverting_faces();
  528. Ref<Material> material = get_material();
  529. PoolVector<Vector3> faces;
  530. PoolVector<Vector2> uvs;
  531. PoolVector<bool> smooth;
  532. PoolVector<Ref<Material> > materials;
  533. PoolVector<bool> invert;
  534. faces.resize(face_count * 3);
  535. uvs.resize(face_count * 3);
  536. smooth.resize(face_count);
  537. materials.resize(face_count);
  538. invert.resize(face_count);
  539. {
  540. PoolVector<Vector3>::Write facesw = faces.write();
  541. PoolVector<Vector2>::Write uvsw = uvs.write();
  542. PoolVector<bool>::Write smoothw = smooth.write();
  543. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  544. PoolVector<bool>::Write invertw = invert.write();
  545. int face = 0;
  546. for (int i = 1; i <= rings; i++) {
  547. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / rings);
  548. double z0 = Math::sin(lat0);
  549. double zr0 = Math::cos(lat0);
  550. double u0 = double(i - 1) / rings;
  551. double lat1 = Math_PI * (-0.5 + (double)i / rings);
  552. double z1 = Math::sin(lat1);
  553. double zr1 = Math::cos(lat1);
  554. double u1 = double(i) / rings;
  555. for (int j = radial_segments; j >= 1; j--) {
  556. double lng0 = 2 * Math_PI * (double)(j - 1) / radial_segments;
  557. double x0 = Math::cos(lng0);
  558. double y0 = Math::sin(lng0);
  559. double v0 = double(i - 1) / radial_segments;
  560. double lng1 = 2 * Math_PI * (double)(j) / radial_segments;
  561. double x1 = Math::cos(lng1);
  562. double y1 = Math::sin(lng1);
  563. double v1 = double(i) / radial_segments;
  564. Vector3 v[4] = {
  565. Vector3(x1 * zr0, z0, y1 * zr0) * radius,
  566. Vector3(x1 * zr1, z1, y1 * zr1) * radius,
  567. Vector3(x0 * zr1, z1, y0 * zr1) * radius,
  568. Vector3(x0 * zr0, z0, y0 * zr0) * radius
  569. };
  570. Vector2 u[4] = {
  571. Vector2(v1, u0),
  572. Vector2(v1, u1),
  573. Vector2(v0, u1),
  574. Vector2(v0, u0),
  575. };
  576. if (i < rings) {
  577. //face 1
  578. facesw[face * 3 + 0] = v[0];
  579. facesw[face * 3 + 1] = v[1];
  580. facesw[face * 3 + 2] = v[2];
  581. uvsw[face * 3 + 0] = u[0];
  582. uvsw[face * 3 + 1] = u[1];
  583. uvsw[face * 3 + 2] = u[2];
  584. smoothw[face] = smooth_faces;
  585. invertw[face] = invert_val;
  586. materialsw[face] = material;
  587. face++;
  588. }
  589. if (i > 1) {
  590. //face 2
  591. facesw[face * 3 + 0] = v[2];
  592. facesw[face * 3 + 1] = v[3];
  593. facesw[face * 3 + 2] = v[0];
  594. uvsw[face * 3 + 0] = u[2];
  595. uvsw[face * 3 + 1] = u[3];
  596. uvsw[face * 3 + 2] = u[0];
  597. smoothw[face] = smooth_faces;
  598. invertw[face] = invert_val;
  599. materialsw[face] = material;
  600. face++;
  601. }
  602. }
  603. }
  604. if (face != face_count) {
  605. ERR_PRINT("Face mismatch bug! fix code");
  606. }
  607. }
  608. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  609. return brush;
  610. }
  611. void CSGSphere::_bind_methods() {
  612. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere::set_radius);
  613. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere::get_radius);
  614. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere::set_radial_segments);
  615. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere::get_radial_segments);
  616. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere::set_rings);
  617. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere::get_rings);
  618. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere::set_smooth_faces);
  619. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere::get_smooth_faces);
  620. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere::set_material);
  621. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere::get_material);
  622. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001"), "set_radius", "get_radius");
  623. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  624. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  625. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  626. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  627. }
  628. void CSGSphere::set_radius(const float p_radius) {
  629. ERR_FAIL_COND(p_radius <= 0);
  630. radius = p_radius;
  631. _make_dirty();
  632. update_gizmo();
  633. }
  634. float CSGSphere::get_radius() const {
  635. return radius;
  636. }
  637. void CSGSphere::set_radial_segments(const int p_radial_segments) {
  638. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  639. _make_dirty();
  640. update_gizmo();
  641. }
  642. int CSGSphere::get_radial_segments() const {
  643. return radial_segments;
  644. }
  645. void CSGSphere::set_rings(const int p_rings) {
  646. rings = p_rings > 1 ? p_rings : 1;
  647. _make_dirty();
  648. update_gizmo();
  649. }
  650. int CSGSphere::get_rings() const {
  651. return rings;
  652. }
  653. void CSGSphere::set_smooth_faces(const bool p_smooth_faces) {
  654. smooth_faces = p_smooth_faces;
  655. _make_dirty();
  656. }
  657. bool CSGSphere::get_smooth_faces() const {
  658. return smooth_faces;
  659. }
  660. void CSGSphere::set_material(const Ref<Material> &p_material) {
  661. material = p_material;
  662. _make_dirty();
  663. }
  664. Ref<Material> CSGSphere::get_material() const {
  665. return material;
  666. }
  667. CSGSphere::CSGSphere() {
  668. // defaults
  669. radius = 1.0;
  670. radial_segments = 12;
  671. rings = 6;
  672. smooth_faces = true;
  673. }
  674. ///////////////
  675. CSGBrush *CSGBox::_build_brush() {
  676. // set our bounding box
  677. CSGBrush *brush = memnew(CSGBrush);
  678. int face_count = 12; //it's a cube..
  679. bool invert_val = is_inverting_faces();
  680. Ref<Material> material = get_material();
  681. PoolVector<Vector3> faces;
  682. PoolVector<Vector2> uvs;
  683. PoolVector<bool> smooth;
  684. PoolVector<Ref<Material> > materials;
  685. PoolVector<bool> invert;
  686. faces.resize(face_count * 3);
  687. uvs.resize(face_count * 3);
  688. smooth.resize(face_count);
  689. materials.resize(face_count);
  690. invert.resize(face_count);
  691. {
  692. PoolVector<Vector3>::Write facesw = faces.write();
  693. PoolVector<Vector2>::Write uvsw = uvs.write();
  694. PoolVector<bool>::Write smoothw = smooth.write();
  695. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  696. PoolVector<bool>::Write invertw = invert.write();
  697. int face = 0;
  698. Vector3 vertex_mul(width, height, depth);
  699. {
  700. for (int i = 0; i < 6; i++) {
  701. Vector3 face_points[4];
  702. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  703. for (int j = 0; j < 4; j++) {
  704. float v[3];
  705. v[0] = 1.0;
  706. v[1] = 1 - 2 * ((j >> 1) & 1);
  707. v[2] = v[1] * (1 - 2 * (j & 1));
  708. for (int k = 0; k < 3; k++) {
  709. if (i < 3)
  710. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  711. else
  712. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  713. }
  714. }
  715. Vector2 u[4];
  716. for (int j = 0; j < 4; j++) {
  717. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  718. }
  719. //face 1
  720. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  721. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  722. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  723. uvsw[face * 3 + 0] = u[0];
  724. uvsw[face * 3 + 1] = u[1];
  725. uvsw[face * 3 + 2] = u[2];
  726. smoothw[face] = false;
  727. invertw[face] = invert_val;
  728. materialsw[face] = material;
  729. face++;
  730. //face 1
  731. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  732. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  733. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  734. uvsw[face * 3 + 0] = u[2];
  735. uvsw[face * 3 + 1] = u[3];
  736. uvsw[face * 3 + 2] = u[0];
  737. smoothw[face] = false;
  738. invertw[face] = invert_val;
  739. materialsw[face] = material;
  740. face++;
  741. }
  742. }
  743. if (face != face_count) {
  744. ERR_PRINT("Face mismatch bug! fix code");
  745. }
  746. }
  747. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  748. return brush;
  749. }
  750. void CSGBox::_bind_methods() {
  751. ClassDB::bind_method(D_METHOD("set_width", "width"), &CSGBox::set_width);
  752. ClassDB::bind_method(D_METHOD("get_width"), &CSGBox::get_width);
  753. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGBox::set_height);
  754. ClassDB::bind_method(D_METHOD("get_height"), &CSGBox::get_height);
  755. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGBox::set_depth);
  756. ClassDB::bind_method(D_METHOD("get_depth"), &CSGBox::get_depth);
  757. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox::set_material);
  758. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox::get_material);
  759. ADD_PROPERTY(PropertyInfo(Variant::REAL, "width", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_width", "get_width");
  760. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  761. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  762. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  763. }
  764. void CSGBox::set_width(const float p_width) {
  765. width = p_width;
  766. _make_dirty();
  767. update_gizmo();
  768. }
  769. float CSGBox::get_width() const {
  770. return width;
  771. }
  772. void CSGBox::set_height(const float p_height) {
  773. height = p_height;
  774. _make_dirty();
  775. update_gizmo();
  776. }
  777. float CSGBox::get_height() const {
  778. return height;
  779. }
  780. void CSGBox::set_depth(const float p_depth) {
  781. depth = p_depth;
  782. _make_dirty();
  783. update_gizmo();
  784. }
  785. float CSGBox::get_depth() const {
  786. return depth;
  787. }
  788. void CSGBox::set_material(const Ref<Material> &p_material) {
  789. material = p_material;
  790. _make_dirty();
  791. update_gizmo();
  792. }
  793. Ref<Material> CSGBox::get_material() const {
  794. return material;
  795. }
  796. CSGBox::CSGBox() {
  797. // defaults
  798. width = 1.0;
  799. height = 1.0;
  800. depth = 1.0;
  801. }
  802. ///////////////
  803. CSGBrush *CSGCylinder::_build_brush() {
  804. // set our bounding box
  805. CSGBrush *brush = memnew(CSGBrush);
  806. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  807. bool invert_val = is_inverting_faces();
  808. Ref<Material> material = get_material();
  809. PoolVector<Vector3> faces;
  810. PoolVector<Vector2> uvs;
  811. PoolVector<bool> smooth;
  812. PoolVector<Ref<Material> > materials;
  813. PoolVector<bool> invert;
  814. faces.resize(face_count * 3);
  815. uvs.resize(face_count * 3);
  816. smooth.resize(face_count);
  817. materials.resize(face_count);
  818. invert.resize(face_count);
  819. {
  820. PoolVector<Vector3>::Write facesw = faces.write();
  821. PoolVector<Vector2>::Write uvsw = uvs.write();
  822. PoolVector<bool>::Write smoothw = smooth.write();
  823. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  824. PoolVector<bool>::Write invertw = invert.write();
  825. int face = 0;
  826. Vector3 vertex_mul(radius, height * 0.5, radius);
  827. {
  828. for (int i = 0; i < sides; i++) {
  829. float inc = float(i) / sides;
  830. float inc_n = float((i + 1)) / sides;
  831. float ang = inc * Math_PI * 2.0;
  832. float ang_n = inc_n * Math_PI * 2.0;
  833. Vector3 base(Math::cos(ang), 0, Math::sin(ang));
  834. Vector3 base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  835. Vector3 face_points[4] = {
  836. base + Vector3(0, -1, 0),
  837. base_n + Vector3(0, -1, 0),
  838. base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  839. base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  840. };
  841. Vector2 u[4] = {
  842. Vector2(inc, 0),
  843. Vector2(inc_n, 0),
  844. Vector2(inc_n, 1),
  845. Vector2(inc, 1),
  846. };
  847. //side face 1
  848. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  849. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  850. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  851. uvsw[face * 3 + 0] = u[0];
  852. uvsw[face * 3 + 1] = u[1];
  853. uvsw[face * 3 + 2] = u[2];
  854. smoothw[face] = smooth_faces;
  855. invertw[face] = invert_val;
  856. materialsw[face] = material;
  857. face++;
  858. if (!cone) {
  859. //side face 2
  860. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  861. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  862. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  863. uvsw[face * 3 + 0] = u[2];
  864. uvsw[face * 3 + 1] = u[3];
  865. uvsw[face * 3 + 2] = u[0];
  866. smoothw[face] = smooth_faces;
  867. invertw[face] = invert_val;
  868. materialsw[face] = material;
  869. face++;
  870. }
  871. //bottom face 1
  872. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  873. facesw[face * 3 + 1] = face_points[0] * vertex_mul;
  874. facesw[face * 3 + 2] = Vector3(0, -1, 0) * vertex_mul;
  875. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  876. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  877. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  878. smoothw[face] = false;
  879. invertw[face] = invert_val;
  880. materialsw[face] = material;
  881. face++;
  882. if (!cone) {
  883. //top face 1
  884. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  885. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  886. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  887. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  888. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  889. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  890. smoothw[face] = false;
  891. invertw[face] = invert_val;
  892. materialsw[face] = material;
  893. face++;
  894. }
  895. }
  896. }
  897. if (face != face_count) {
  898. ERR_PRINT("Face mismatch bug! fix code");
  899. }
  900. }
  901. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  902. return brush;
  903. }
  904. void CSGCylinder::_bind_methods() {
  905. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder::set_radius);
  906. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder::get_radius);
  907. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder::set_height);
  908. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder::get_height);
  909. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder::set_sides);
  910. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder::get_sides);
  911. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder::set_cone);
  912. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder::is_cone);
  913. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder::set_material);
  914. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder::get_material);
  915. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder::set_smooth_faces);
  916. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder::get_smooth_faces);
  917. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_radius", "get_radius");
  918. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  919. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  920. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  921. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  922. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  923. }
  924. void CSGCylinder::set_radius(const float p_radius) {
  925. radius = p_radius;
  926. _make_dirty();
  927. update_gizmo();
  928. }
  929. float CSGCylinder::get_radius() const {
  930. return radius;
  931. }
  932. void CSGCylinder::set_height(const float p_height) {
  933. height = p_height;
  934. _make_dirty();
  935. update_gizmo();
  936. }
  937. float CSGCylinder::get_height() const {
  938. return height;
  939. }
  940. void CSGCylinder::set_sides(const int p_sides) {
  941. ERR_FAIL_COND(p_sides < 3);
  942. sides = p_sides;
  943. _make_dirty();
  944. update_gizmo();
  945. }
  946. int CSGCylinder::get_sides() const {
  947. return sides;
  948. }
  949. void CSGCylinder::set_cone(const bool p_cone) {
  950. cone = p_cone;
  951. _make_dirty();
  952. update_gizmo();
  953. }
  954. bool CSGCylinder::is_cone() const {
  955. return cone;
  956. }
  957. void CSGCylinder::set_smooth_faces(const bool p_smooth_faces) {
  958. smooth_faces = p_smooth_faces;
  959. _make_dirty();
  960. }
  961. bool CSGCylinder::get_smooth_faces() const {
  962. return smooth_faces;
  963. }
  964. void CSGCylinder::set_material(const Ref<Material> &p_material) {
  965. material = p_material;
  966. _make_dirty();
  967. }
  968. Ref<Material> CSGCylinder::get_material() const {
  969. return material;
  970. }
  971. CSGCylinder::CSGCylinder() {
  972. // defaults
  973. radius = 1.0;
  974. height = 1.0;
  975. sides = 8;
  976. cone = false;
  977. smooth_faces = true;
  978. }
  979. ///////////////
  980. CSGBrush *CSGTorus::_build_brush() {
  981. // set our bounding box
  982. float min_radius = inner_radius;
  983. float max_radius = outer_radius;
  984. if (min_radius == max_radius)
  985. return NULL; //sorry, can't
  986. if (min_radius > max_radius) {
  987. SWAP(min_radius, max_radius);
  988. }
  989. float radius = (max_radius - min_radius) * 0.5;
  990. CSGBrush *brush = memnew(CSGBrush);
  991. int face_count = ring_sides * sides * 2;
  992. bool invert_val = is_inverting_faces();
  993. Ref<Material> material = get_material();
  994. PoolVector<Vector3> faces;
  995. PoolVector<Vector2> uvs;
  996. PoolVector<bool> smooth;
  997. PoolVector<Ref<Material> > materials;
  998. PoolVector<bool> invert;
  999. faces.resize(face_count * 3);
  1000. uvs.resize(face_count * 3);
  1001. smooth.resize(face_count);
  1002. materials.resize(face_count);
  1003. invert.resize(face_count);
  1004. {
  1005. PoolVector<Vector3>::Write facesw = faces.write();
  1006. PoolVector<Vector2>::Write uvsw = uvs.write();
  1007. PoolVector<bool>::Write smoothw = smooth.write();
  1008. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  1009. PoolVector<bool>::Write invertw = invert.write();
  1010. int face = 0;
  1011. {
  1012. for (int i = 0; i < sides; i++) {
  1013. float inci = float(i) / sides;
  1014. float inci_n = float((i + 1)) / sides;
  1015. float angi = inci * Math_PI * 2.0;
  1016. float angi_n = inci_n * Math_PI * 2.0;
  1017. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1018. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1019. for (int j = 0; j < ring_sides; j++) {
  1020. float incj = float(j) / ring_sides;
  1021. float incj_n = float((j + 1)) / ring_sides;
  1022. float angj = incj * Math_PI * 2.0;
  1023. float angj_n = incj_n * Math_PI * 2.0;
  1024. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1025. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1026. Vector3 face_points[4] = {
  1027. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1028. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1029. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1030. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1031. };
  1032. Vector2 u[4] = {
  1033. Vector2(inci, incj),
  1034. Vector2(inci, incj_n),
  1035. Vector2(inci_n, incj_n),
  1036. Vector2(inci_n, incj),
  1037. };
  1038. // face 1
  1039. facesw[face * 3 + 0] = face_points[0];
  1040. facesw[face * 3 + 1] = face_points[2];
  1041. facesw[face * 3 + 2] = face_points[1];
  1042. uvsw[face * 3 + 0] = u[0];
  1043. uvsw[face * 3 + 1] = u[2];
  1044. uvsw[face * 3 + 2] = u[1];
  1045. smoothw[face] = smooth_faces;
  1046. invertw[face] = invert_val;
  1047. materialsw[face] = material;
  1048. face++;
  1049. //face 2
  1050. facesw[face * 3 + 0] = face_points[3];
  1051. facesw[face * 3 + 1] = face_points[2];
  1052. facesw[face * 3 + 2] = face_points[0];
  1053. uvsw[face * 3 + 0] = u[3];
  1054. uvsw[face * 3 + 1] = u[2];
  1055. uvsw[face * 3 + 2] = u[0];
  1056. smoothw[face] = smooth_faces;
  1057. invertw[face] = invert_val;
  1058. materialsw[face] = material;
  1059. face++;
  1060. }
  1061. }
  1062. }
  1063. if (face != face_count) {
  1064. ERR_PRINT("Face mismatch bug! fix code");
  1065. }
  1066. }
  1067. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1068. return brush;
  1069. }
  1070. void CSGTorus::_bind_methods() {
  1071. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus::set_inner_radius);
  1072. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus::get_inner_radius);
  1073. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus::set_outer_radius);
  1074. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus::get_outer_radius);
  1075. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus::set_sides);
  1076. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus::get_sides);
  1077. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus::set_ring_sides);
  1078. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus::get_ring_sides);
  1079. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus::set_material);
  1080. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus::get_material);
  1081. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus::set_smooth_faces);
  1082. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus::get_smooth_faces);
  1083. ADD_PROPERTY(PropertyInfo(Variant::REAL, "inner_radius", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_inner_radius", "get_inner_radius");
  1084. ADD_PROPERTY(PropertyInfo(Variant::REAL, "outer_radius", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_outer_radius", "get_outer_radius");
  1085. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1086. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1087. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1088. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  1089. }
  1090. void CSGTorus::set_inner_radius(const float p_inner_radius) {
  1091. inner_radius = p_inner_radius;
  1092. _make_dirty();
  1093. update_gizmo();
  1094. }
  1095. float CSGTorus::get_inner_radius() const {
  1096. return inner_radius;
  1097. }
  1098. void CSGTorus::set_outer_radius(const float p_outer_radius) {
  1099. outer_radius = p_outer_radius;
  1100. _make_dirty();
  1101. update_gizmo();
  1102. }
  1103. float CSGTorus::get_outer_radius() const {
  1104. return outer_radius;
  1105. }
  1106. void CSGTorus::set_sides(const int p_sides) {
  1107. ERR_FAIL_COND(p_sides < 3);
  1108. sides = p_sides;
  1109. _make_dirty();
  1110. update_gizmo();
  1111. }
  1112. int CSGTorus::get_sides() const {
  1113. return sides;
  1114. }
  1115. void CSGTorus::set_ring_sides(const int p_ring_sides) {
  1116. ERR_FAIL_COND(p_ring_sides < 3);
  1117. ring_sides = p_ring_sides;
  1118. _make_dirty();
  1119. update_gizmo();
  1120. }
  1121. int CSGTorus::get_ring_sides() const {
  1122. return ring_sides;
  1123. }
  1124. void CSGTorus::set_smooth_faces(const bool p_smooth_faces) {
  1125. smooth_faces = p_smooth_faces;
  1126. _make_dirty();
  1127. }
  1128. bool CSGTorus::get_smooth_faces() const {
  1129. return smooth_faces;
  1130. }
  1131. void CSGTorus::set_material(const Ref<Material> &p_material) {
  1132. material = p_material;
  1133. _make_dirty();
  1134. }
  1135. Ref<Material> CSGTorus::get_material() const {
  1136. return material;
  1137. }
  1138. CSGTorus::CSGTorus() {
  1139. // defaults
  1140. inner_radius = 2.0;
  1141. outer_radius = 3.0;
  1142. sides = 8;
  1143. ring_sides = 6;
  1144. smooth_faces = true;
  1145. }
  1146. ///////////////
  1147. CSGBrush *CSGPolygon::_build_brush() {
  1148. // set our bounding box
  1149. if (polygon.size() < 3)
  1150. return NULL;
  1151. Vector<Point2> final_polygon = polygon;
  1152. if (Triangulate::get_area(final_polygon) > 0) {
  1153. final_polygon.invert();
  1154. }
  1155. Vector<int> triangles = Geometry::triangulate_polygon(final_polygon);
  1156. if (triangles.size() < 3)
  1157. return NULL;
  1158. Path *path = NULL;
  1159. Ref<Curve3D> curve;
  1160. if (mode == MODE_PATH) {
  1161. if (!has_node(path_node))
  1162. return NULL;
  1163. Node *n = get_node(path_node);
  1164. if (!n)
  1165. return NULL;
  1166. path = Object::cast_to<Path>(n);
  1167. if (!path)
  1168. return NULL;
  1169. if (path != path_cache) {
  1170. if (path_cache) {
  1171. path_cache->disconnect("tree_exited", this, "_path_exited");
  1172. path_cache->disconnect("curve_changed", this, "_path_changed");
  1173. path_cache = NULL;
  1174. }
  1175. path_cache = path;
  1176. if (path_cache) {
  1177. path_cache->connect("tree_exited", this, "_path_exited");
  1178. path_cache->connect("curve_changed", this, "_path_changed");
  1179. path_cache = NULL;
  1180. }
  1181. }
  1182. curve = path->get_curve();
  1183. if (curve.is_null())
  1184. return NULL;
  1185. if (curve->get_baked_length() <= 0)
  1186. return NULL;
  1187. }
  1188. CSGBrush *brush = memnew(CSGBrush);
  1189. int face_count;
  1190. switch (mode) {
  1191. case MODE_DEPTH: face_count = triangles.size() * 2 / 3 + (final_polygon.size()) * 2; break;
  1192. case MODE_SPIN: face_count = (spin_degrees < 360 ? triangles.size() * 2 / 3 : 0) + (final_polygon.size()) * 2 * spin_sides; break;
  1193. case MODE_PATH: {
  1194. float bl = curve->get_baked_length();
  1195. int splits = MAX(2, Math::ceil(bl / path_interval));
  1196. face_count = triangles.size() * 2 / 3 + splits * final_polygon.size() * 2;
  1197. } break;
  1198. }
  1199. bool invert_val = is_inverting_faces();
  1200. Ref<Material> material = get_material();
  1201. PoolVector<Vector3> faces;
  1202. PoolVector<Vector2> uvs;
  1203. PoolVector<bool> smooth;
  1204. PoolVector<Ref<Material> > materials;
  1205. PoolVector<bool> invert;
  1206. faces.resize(face_count * 3);
  1207. uvs.resize(face_count * 3);
  1208. smooth.resize(face_count);
  1209. materials.resize(face_count);
  1210. invert.resize(face_count);
  1211. AABB aabb; //must be computed
  1212. {
  1213. PoolVector<Vector3>::Write facesw = faces.write();
  1214. PoolVector<Vector2>::Write uvsw = uvs.write();
  1215. PoolVector<bool>::Write smoothw = smooth.write();
  1216. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  1217. PoolVector<bool>::Write invertw = invert.write();
  1218. int face = 0;
  1219. switch (mode) {
  1220. case MODE_DEPTH: {
  1221. //add triangles, front and back
  1222. for (int i = 0; i < 2; i++) {
  1223. for (int j = 0; j < triangles.size(); j += 3) {
  1224. for (int k = 0; k < 3; k++) {
  1225. int src[3] = { 0, i == 0 ? 1 : 2, i == 0 ? 2 : 1 };
  1226. Vector2 p = final_polygon[triangles[j + src[k]]];
  1227. Vector3 v = Vector3(p.x, p.y, 0);
  1228. if (i == 0) {
  1229. v.z -= depth;
  1230. }
  1231. facesw[face * 3 + k] = v;
  1232. }
  1233. smoothw[face] = false;
  1234. materialsw[face] = material;
  1235. invertw[face] = invert_val;
  1236. face++;
  1237. }
  1238. }
  1239. //add triangles for depth
  1240. for (int i = 0; i < final_polygon.size(); i++) {
  1241. int i_n = (i + 1) % final_polygon.size();
  1242. Vector3 v[4] = {
  1243. Vector3(final_polygon[i].x, final_polygon[i].y, -depth),
  1244. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, -depth),
  1245. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, 0),
  1246. Vector3(final_polygon[i].x, final_polygon[i].y, 0),
  1247. };
  1248. Vector2 u[4] = {
  1249. Vector2(0, 0),
  1250. Vector2(0, 1),
  1251. Vector2(1, 1),
  1252. Vector2(1, 0)
  1253. };
  1254. // face 1
  1255. facesw[face * 3 + 0] = v[0];
  1256. facesw[face * 3 + 1] = v[1];
  1257. facesw[face * 3 + 2] = v[2];
  1258. uvsw[face * 3 + 0] = u[0];
  1259. uvsw[face * 3 + 1] = u[1];
  1260. uvsw[face * 3 + 2] = u[2];
  1261. smoothw[face] = smooth_faces;
  1262. invertw[face] = invert_val;
  1263. materialsw[face] = material;
  1264. face++;
  1265. // face 2
  1266. facesw[face * 3 + 0] = v[2];
  1267. facesw[face * 3 + 1] = v[3];
  1268. facesw[face * 3 + 2] = v[0];
  1269. uvsw[face * 3 + 0] = u[2];
  1270. uvsw[face * 3 + 1] = u[3];
  1271. uvsw[face * 3 + 2] = u[0];
  1272. smoothw[face] = smooth_faces;
  1273. invertw[face] = invert_val;
  1274. materialsw[face] = material;
  1275. face++;
  1276. }
  1277. } break;
  1278. case MODE_SPIN: {
  1279. for (int i = 0; i < spin_sides; i++) {
  1280. float inci = float(i) / spin_sides;
  1281. float inci_n = float((i + 1)) / spin_sides;
  1282. float angi = -(inci * spin_degrees / 360.0) * Math_PI * 2.0;
  1283. float angi_n = -(inci_n * spin_degrees / 360.0) * Math_PI * 2.0;
  1284. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1285. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1286. //add triangles for depth
  1287. for (int j = 0; j < final_polygon.size(); j++) {
  1288. int j_n = (j + 1) % final_polygon.size();
  1289. Vector3 v[4] = {
  1290. Vector3(normali.x * final_polygon[j].x, final_polygon[j].y, normali.z * final_polygon[j].x),
  1291. Vector3(normali.x * final_polygon[j_n].x, final_polygon[j_n].y, normali.z * final_polygon[j_n].x),
  1292. Vector3(normali_n.x * final_polygon[j_n].x, final_polygon[j_n].y, normali_n.z * final_polygon[j_n].x),
  1293. Vector3(normali_n.x * final_polygon[j].x, final_polygon[j].y, normali_n.z * final_polygon[j].x),
  1294. };
  1295. Vector2 u[4] = {
  1296. Vector2(0, 0),
  1297. Vector2(0, 1),
  1298. Vector2(1, 1),
  1299. Vector2(1, 0)
  1300. };
  1301. // face 1
  1302. facesw[face * 3 + 0] = v[0];
  1303. facesw[face * 3 + 1] = v[2];
  1304. facesw[face * 3 + 2] = v[1];
  1305. uvsw[face * 3 + 0] = u[0];
  1306. uvsw[face * 3 + 1] = u[2];
  1307. uvsw[face * 3 + 2] = u[1];
  1308. smoothw[face] = smooth_faces;
  1309. invertw[face] = invert_val;
  1310. materialsw[face] = material;
  1311. face++;
  1312. // face 2
  1313. facesw[face * 3 + 0] = v[2];
  1314. facesw[face * 3 + 1] = v[0];
  1315. facesw[face * 3 + 2] = v[3];
  1316. uvsw[face * 3 + 0] = u[2];
  1317. uvsw[face * 3 + 1] = u[0];
  1318. uvsw[face * 3 + 2] = u[3];
  1319. smoothw[face] = smooth_faces;
  1320. invertw[face] = invert_val;
  1321. materialsw[face] = material;
  1322. face++;
  1323. }
  1324. if (i == 0 && spin_degrees < 360) {
  1325. for (int j = 0; j < triangles.size(); j += 3) {
  1326. for (int k = 0; k < 3; k++) {
  1327. int src[3] = { 0, 2, 1 };
  1328. Vector2 p = final_polygon[triangles[j + src[k]]];
  1329. Vector3 v = Vector3(p.x, p.y, 0);
  1330. facesw[face * 3 + k] = v;
  1331. }
  1332. smoothw[face] = false;
  1333. materialsw[face] = material;
  1334. invertw[face] = invert_val;
  1335. face++;
  1336. }
  1337. }
  1338. if (i == spin_sides - 1 && spin_degrees < 360) {
  1339. for (int j = 0; j < triangles.size(); j += 3) {
  1340. for (int k = 0; k < 3; k++) {
  1341. int src[3] = { 0, 1, 2 };
  1342. Vector2 p = final_polygon[triangles[j + src[k]]];
  1343. Vector3 v = Vector3(normali_n.x * p.x, p.y, normali_n.z * p.x);
  1344. facesw[face * 3 + k] = v;
  1345. }
  1346. smoothw[face] = false;
  1347. materialsw[face] = material;
  1348. invertw[face] = invert_val;
  1349. face++;
  1350. }
  1351. }
  1352. }
  1353. } break;
  1354. case MODE_PATH: {
  1355. float bl = curve->get_baked_length();
  1356. int splits = MAX(2, Math::ceil(bl / path_interval));
  1357. Transform path_to_this = get_global_transform().affine_inverse() * path->get_global_transform();
  1358. Transform prev_xf;
  1359. Vector3 lookat_dir;
  1360. if (path_rotation == PATH_ROTATION_POLYGON) {
  1361. lookat_dir = (path->get_global_transform().affine_inverse() * get_global_transform()).xform(Vector3(0, 0, -1));
  1362. } else {
  1363. Vector3 p1, p2;
  1364. p1 = curve->interpolate_baked(0);
  1365. p2 = curve->interpolate_baked(0.1);
  1366. lookat_dir = (p2 - p1).normalized();
  1367. }
  1368. for (int i = 0; i <= splits; i++) {
  1369. float ofs = i * path_interval;
  1370. Transform xf;
  1371. xf.origin = curve->interpolate_baked(ofs);
  1372. Vector3 local_dir;
  1373. if (path_rotation == PATH_ROTATION_PATH_FOLLOW && ofs > 0) {
  1374. //before end
  1375. Vector3 p1 = curve->interpolate_baked(ofs - 0.1);
  1376. Vector3 p2 = curve->interpolate_baked(ofs);
  1377. local_dir = (p2 - p1).normalized();
  1378. } else {
  1379. local_dir = lookat_dir;
  1380. }
  1381. xf = xf.looking_at(xf.origin + local_dir, Vector3(0, 1, 0));
  1382. Basis rot(Vector3(0, 0, 1), curve->interpolate_baked_tilt(ofs));
  1383. xf = xf * rot; //post mult
  1384. xf = path_to_this * xf;
  1385. if (i > 0) {
  1386. //put triangles where they belong
  1387. //add triangles for depth
  1388. for (int j = 0; j < final_polygon.size(); j++) {
  1389. int j_n = (j + 1) % final_polygon.size();
  1390. Vector3 v[4] = {
  1391. prev_xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1392. prev_xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1393. xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1394. xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1395. };
  1396. Vector2 u[4] = {
  1397. Vector2(0, 0),
  1398. Vector2(0, 1),
  1399. Vector2(1, 1),
  1400. Vector2(1, 0)
  1401. };
  1402. // face 1
  1403. facesw[face * 3 + 0] = v[0];
  1404. facesw[face * 3 + 1] = v[1];
  1405. facesw[face * 3 + 2] = v[2];
  1406. uvsw[face * 3 + 0] = u[0];
  1407. uvsw[face * 3 + 1] = u[1];
  1408. uvsw[face * 3 + 2] = u[2];
  1409. smoothw[face] = smooth_faces;
  1410. invertw[face] = invert_val;
  1411. materialsw[face] = material;
  1412. face++;
  1413. // face 2
  1414. facesw[face * 3 + 0] = v[2];
  1415. facesw[face * 3 + 1] = v[3];
  1416. facesw[face * 3 + 2] = v[0];
  1417. uvsw[face * 3 + 0] = u[2];
  1418. uvsw[face * 3 + 1] = u[3];
  1419. uvsw[face * 3 + 2] = u[0];
  1420. smoothw[face] = smooth_faces;
  1421. invertw[face] = invert_val;
  1422. materialsw[face] = material;
  1423. face++;
  1424. }
  1425. }
  1426. if (i == 0) {
  1427. for (int j = 0; j < triangles.size(); j += 3) {
  1428. for (int k = 0; k < 3; k++) {
  1429. int src[3] = { 0, 1, 2 };
  1430. Vector2 p = final_polygon[triangles[j + src[k]]];
  1431. Vector3 v = Vector3(p.x, p.y, 0);
  1432. facesw[face * 3 + k] = xf.xform(v);
  1433. }
  1434. smoothw[face] = false;
  1435. materialsw[face] = material;
  1436. invertw[face] = invert_val;
  1437. face++;
  1438. }
  1439. }
  1440. if (i == splits) {
  1441. for (int j = 0; j < triangles.size(); j += 3) {
  1442. for (int k = 0; k < 3; k++) {
  1443. int src[3] = { 0, 2, 1 };
  1444. Vector2 p = final_polygon[triangles[j + src[k]]];
  1445. Vector3 v = Vector3(p.x, p.y, 0);
  1446. facesw[face * 3 + k] = xf.xform(v);
  1447. }
  1448. smoothw[face] = false;
  1449. materialsw[face] = material;
  1450. invertw[face] = invert_val;
  1451. face++;
  1452. }
  1453. }
  1454. prev_xf = xf;
  1455. }
  1456. } break;
  1457. }
  1458. if (face != face_count) {
  1459. ERR_PRINT("Face mismatch bug! fix code");
  1460. }
  1461. for (int i = 0; i < face_count * 3; i++) {
  1462. if (i == 0) {
  1463. aabb.position = facesw[i];
  1464. } else {
  1465. aabb.expand_to(facesw[i]);
  1466. }
  1467. }
  1468. }
  1469. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1470. return brush;
  1471. }
  1472. void CSGPolygon::_notification(int p_what) {
  1473. if (p_what == NOTIFICATION_EXIT_TREE) {
  1474. if (path_cache) {
  1475. path_cache->disconnect("tree_exited", this, "_path_exited");
  1476. path_cache->disconnect("curve_changed", this, "_path_changed");
  1477. path_cache = NULL;
  1478. }
  1479. }
  1480. }
  1481. void CSGPolygon::_validate_property(PropertyInfo &property) const {
  1482. if (property.name.begins_with("spin") && mode != MODE_SPIN) {
  1483. property.usage = 0;
  1484. }
  1485. if (property.name.begins_with("path") && mode != MODE_PATH) {
  1486. property.usage = 0;
  1487. }
  1488. if (property.name == "depth" && mode != MODE_DEPTH) {
  1489. property.usage = 0;
  1490. }
  1491. CSGShape::_validate_property(property);
  1492. }
  1493. void CSGPolygon::_path_changed() {
  1494. _make_dirty();
  1495. update_gizmo();
  1496. }
  1497. void CSGPolygon::_path_exited() {
  1498. path_cache = NULL;
  1499. }
  1500. void CSGPolygon::_bind_methods() {
  1501. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon::set_polygon);
  1502. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon::get_polygon);
  1503. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon::set_mode);
  1504. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon::get_mode);
  1505. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon::set_depth);
  1506. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon::get_depth);
  1507. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon::set_spin_degrees);
  1508. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon::get_spin_degrees);
  1509. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon::set_spin_sides);
  1510. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon::get_spin_sides);
  1511. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon::set_path_node);
  1512. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon::get_path_node);
  1513. ClassDB::bind_method(D_METHOD("set_path_interval", "distance"), &CSGPolygon::set_path_interval);
  1514. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon::get_path_interval);
  1515. ClassDB::bind_method(D_METHOD("set_path_rotation", "mode"), &CSGPolygon::set_path_rotation);
  1516. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon::get_path_rotation);
  1517. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon::set_material);
  1518. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon::get_material);
  1519. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon::set_smooth_faces);
  1520. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon::get_smooth_faces);
  1521. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon::_is_editable_3d_polygon);
  1522. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon::_has_editable_3d_polygon_no_depth);
  1523. ClassDB::bind_method(D_METHOD("_path_exited"), &CSGPolygon::_path_exited);
  1524. ClassDB::bind_method(D_METHOD("_path_changed"), &CSGPolygon::_path_changed);
  1525. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  1526. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  1527. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  1528. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  1529. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  1530. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node"), "set_path_node", "get_path_node");
  1531. ADD_PROPERTY(PropertyInfo(Variant::REAL, "path_interval", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_path_interval", "get_path_interval");
  1532. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  1533. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1534. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  1535. BIND_ENUM_CONSTANT(MODE_DEPTH);
  1536. BIND_ENUM_CONSTANT(MODE_SPIN);
  1537. BIND_ENUM_CONSTANT(MODE_PATH);
  1538. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  1539. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  1540. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  1541. }
  1542. void CSGPolygon::set_polygon(const Vector<Vector2> &p_polygon) {
  1543. polygon = p_polygon;
  1544. _make_dirty();
  1545. update_gizmo();
  1546. }
  1547. Vector<Vector2> CSGPolygon::get_polygon() const {
  1548. return polygon;
  1549. }
  1550. void CSGPolygon::set_mode(Mode p_mode) {
  1551. mode = p_mode;
  1552. _make_dirty();
  1553. update_gizmo();
  1554. _change_notify();
  1555. }
  1556. CSGPolygon::Mode CSGPolygon::get_mode() const {
  1557. return mode;
  1558. }
  1559. void CSGPolygon::set_depth(const float p_depth) {
  1560. ERR_FAIL_COND(p_depth < 0.001);
  1561. depth = p_depth;
  1562. _make_dirty();
  1563. update_gizmo();
  1564. }
  1565. float CSGPolygon::get_depth() const {
  1566. return depth;
  1567. }
  1568. void CSGPolygon::set_spin_degrees(const float p_spin_degrees) {
  1569. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  1570. spin_degrees = p_spin_degrees;
  1571. _make_dirty();
  1572. update_gizmo();
  1573. }
  1574. float CSGPolygon::get_spin_degrees() const {
  1575. return spin_degrees;
  1576. }
  1577. void CSGPolygon::set_spin_sides(const int p_spin_sides) {
  1578. ERR_FAIL_COND(p_spin_sides < 3);
  1579. spin_sides = p_spin_sides;
  1580. _make_dirty();
  1581. update_gizmo();
  1582. }
  1583. int CSGPolygon::get_spin_sides() const {
  1584. return spin_sides;
  1585. }
  1586. void CSGPolygon::set_path_node(const NodePath &p_path) {
  1587. path_node = p_path;
  1588. _make_dirty();
  1589. update_gizmo();
  1590. }
  1591. NodePath CSGPolygon::get_path_node() const {
  1592. return path_node;
  1593. }
  1594. void CSGPolygon::set_path_interval(float p_interval) {
  1595. ERR_FAIL_COND(p_interval < 0.001);
  1596. path_interval = p_interval;
  1597. _make_dirty();
  1598. update_gizmo();
  1599. }
  1600. float CSGPolygon::get_path_interval() const {
  1601. return path_interval;
  1602. }
  1603. void CSGPolygon::set_path_rotation(PathRotation p_rotation) {
  1604. path_rotation = p_rotation;
  1605. _make_dirty();
  1606. update_gizmo();
  1607. }
  1608. CSGPolygon::PathRotation CSGPolygon::get_path_rotation() const {
  1609. return path_rotation;
  1610. }
  1611. void CSGPolygon::set_smooth_faces(const bool p_smooth_faces) {
  1612. smooth_faces = p_smooth_faces;
  1613. _make_dirty();
  1614. }
  1615. bool CSGPolygon::get_smooth_faces() const {
  1616. return smooth_faces;
  1617. }
  1618. void CSGPolygon::set_material(const Ref<Material> &p_material) {
  1619. material = p_material;
  1620. _make_dirty();
  1621. }
  1622. Ref<Material> CSGPolygon::get_material() const {
  1623. return material;
  1624. }
  1625. bool CSGPolygon::_is_editable_3d_polygon() const {
  1626. return true;
  1627. }
  1628. bool CSGPolygon::_has_editable_3d_polygon_no_depth() const {
  1629. return true;
  1630. }
  1631. CSGPolygon::CSGPolygon() {
  1632. // defaults
  1633. mode = MODE_DEPTH;
  1634. polygon.push_back(Vector2(0, 0));
  1635. polygon.push_back(Vector2(0, 1));
  1636. polygon.push_back(Vector2(1, 1));
  1637. polygon.push_back(Vector2(1, 0));
  1638. depth = 1.0;
  1639. spin_degrees = 360;
  1640. spin_sides = 8;
  1641. smooth_faces = false;
  1642. path_interval = 1;
  1643. path_rotation = PATH_ROTATION_PATH;
  1644. path_cache = NULL;
  1645. }