csg_shape.cpp 55 KB

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