csg_shape.cpp 69 KB

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