csg_shape.cpp 67 KB

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