csg_shape.cpp 68 KB

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