csg_shape.cpp 68 KB

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