csg_shape.cpp 67 KB

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