csg_shape.cpp 64 KB

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