csg_shape.cpp 67 KB

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