csg_shape.cpp 71 KB

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