csg_shape.cpp 67 KB

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