csg_shape.cpp 54 KB

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