csg_shape.cpp 72 KB

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