csg_shape.cpp 69 KB

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