csg_shape.cpp 64 KB

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