csg_shape.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  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_VISIBILITY_CHANGED) {
  418. if (parent) {
  419. parent->_make_dirty();
  420. }
  421. }
  422. if (p_what == NOTIFICATION_EXIT_TREE) {
  423. if (parent)
  424. parent->_make_dirty();
  425. parent = NULL;
  426. if (use_collision && is_root_shape() && root_collision_instance.is_valid()) {
  427. PhysicsServer::get_singleton()->free(root_collision_instance);
  428. root_collision_instance = RID();
  429. root_collision_shape.unref();
  430. }
  431. _make_dirty();
  432. }
  433. }
  434. void CSGShape::set_operation(Operation p_operation) {
  435. operation = p_operation;
  436. _make_dirty();
  437. }
  438. CSGShape::Operation CSGShape::get_operation() const {
  439. return operation;
  440. }
  441. void CSGShape::set_calculate_tangents(bool p_calculate_tangents) {
  442. calculate_tangents = p_calculate_tangents;
  443. _make_dirty();
  444. }
  445. bool CSGShape::is_calculating_tangents() const {
  446. return calculate_tangents;
  447. }
  448. void CSGShape::_validate_property(PropertyInfo &property) const {
  449. bool is_collision_prefixed = property.name.begins_with("collision_");
  450. if ((is_collision_prefixed || property.name.begins_with("use_collision")) && is_inside_tree() && !is_root_shape()) {
  451. //hide collision if not root
  452. property.usage = PROPERTY_USAGE_NOEDITOR;
  453. } else if (is_collision_prefixed && !bool(get("use_collision"))) {
  454. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  455. }
  456. }
  457. Array CSGShape::get_meshes() const {
  458. if (root_mesh.is_valid()) {
  459. Array arr;
  460. arr.resize(2);
  461. arr[0] = Transform();
  462. arr[1] = root_mesh;
  463. return arr;
  464. }
  465. return Array();
  466. }
  467. void CSGShape::_bind_methods() {
  468. ClassDB::bind_method(D_METHOD("_update_shape"), &CSGShape::_update_shape);
  469. ClassDB::bind_method(D_METHOD("is_root_shape"), &CSGShape::is_root_shape);
  470. ClassDB::bind_method(D_METHOD("set_operation", "operation"), &CSGShape::set_operation);
  471. ClassDB::bind_method(D_METHOD("get_operation"), &CSGShape::get_operation);
  472. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &CSGShape::set_snap);
  473. ClassDB::bind_method(D_METHOD("get_snap"), &CSGShape::get_snap);
  474. ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape::set_use_collision);
  475. ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape::is_using_collision);
  476. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape::set_collision_layer);
  477. ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape::get_collision_layer);
  478. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CSGShape::set_collision_mask);
  479. ClassDB::bind_method(D_METHOD("get_collision_mask"), &CSGShape::get_collision_mask);
  480. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &CSGShape::set_collision_mask_bit);
  481. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &CSGShape::get_collision_mask_bit);
  482. ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &CSGShape::set_collision_layer_bit);
  483. ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &CSGShape::get_collision_layer_bit);
  484. ClassDB::bind_method(D_METHOD("set_calculate_tangents", "enabled"), &CSGShape::set_calculate_tangents);
  485. ClassDB::bind_method(D_METHOD("is_calculating_tangents"), &CSGShape::is_calculating_tangents);
  486. ClassDB::bind_method(D_METHOD("get_meshes"), &CSGShape::get_meshes);
  487. ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "Union,Intersection,Subtraction"), "set_operation", "get_operation");
  488. ADD_PROPERTY(PropertyInfo(Variant::REAL, "snap", PROPERTY_HINT_RANGE, "0.0001,1,0.001"), "set_snap", "get_snap");
  489. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "calculate_tangents"), "set_calculate_tangents", "is_calculating_tangents");
  490. ADD_GROUP("Collision", "collision_");
  491. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_collision"), "set_use_collision", "is_using_collision");
  492. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  493. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  494. BIND_ENUM_CONSTANT(OPERATION_UNION);
  495. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  496. BIND_ENUM_CONSTANT(OPERATION_SUBTRACTION);
  497. }
  498. CSGShape::CSGShape() {
  499. operation = OPERATION_UNION;
  500. parent = NULL;
  501. brush = NULL;
  502. dirty = false;
  503. snap = 0.001;
  504. use_collision = false;
  505. collision_layer = 1;
  506. collision_mask = 1;
  507. calculate_tangents = true;
  508. set_notify_local_transform(true);
  509. }
  510. CSGShape::~CSGShape() {
  511. if (brush) {
  512. memdelete(brush);
  513. brush = NULL;
  514. }
  515. }
  516. //////////////////////////////////
  517. CSGBrush *CSGCombiner::_build_brush() {
  518. return NULL; //does not build anything
  519. }
  520. CSGCombiner::CSGCombiner() {
  521. }
  522. /////////////////////
  523. 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) {
  524. CSGBrush *brush = memnew(CSGBrush);
  525. PoolVector<bool> invert;
  526. invert.resize(p_vertices.size() / 3);
  527. {
  528. int ic = invert.size();
  529. PoolVector<bool>::Write w = invert.write();
  530. for (int i = 0; i < ic; i++) {
  531. w[i] = invert_faces;
  532. }
  533. }
  534. brush->build_from_faces(p_vertices, p_uv, p_smooth, p_materials, invert);
  535. return brush;
  536. }
  537. void CSGPrimitive::_bind_methods() {
  538. ClassDB::bind_method(D_METHOD("set_invert_faces", "invert_faces"), &CSGPrimitive::set_invert_faces);
  539. ClassDB::bind_method(D_METHOD("is_inverting_faces"), &CSGPrimitive::is_inverting_faces);
  540. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_faces"), "set_invert_faces", "is_inverting_faces");
  541. }
  542. void CSGPrimitive::set_invert_faces(bool p_invert) {
  543. if (invert_faces == p_invert)
  544. return;
  545. invert_faces = p_invert;
  546. _make_dirty();
  547. }
  548. bool CSGPrimitive::is_inverting_faces() {
  549. return invert_faces;
  550. }
  551. CSGPrimitive::CSGPrimitive() {
  552. invert_faces = false;
  553. }
  554. /////////////////////
  555. CSGBrush *CSGMesh::_build_brush() {
  556. if (!mesh.is_valid())
  557. return NULL;
  558. PoolVector<Vector3> vertices;
  559. PoolVector<bool> smooth;
  560. PoolVector<Ref<Material> > materials;
  561. PoolVector<Vector2> uvs;
  562. Ref<Material> material = get_material();
  563. for (int i = 0; i < mesh->get_surface_count(); i++) {
  564. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  565. continue;
  566. }
  567. Array arrays = mesh->surface_get_arrays(i);
  568. if (arrays.size() == 0) {
  569. _make_dirty();
  570. ERR_FAIL_COND_V(arrays.size() == 0, NULL);
  571. }
  572. PoolVector<Vector3> avertices = arrays[Mesh::ARRAY_VERTEX];
  573. if (avertices.size() == 0)
  574. continue;
  575. PoolVector<Vector3>::Read vr = avertices.read();
  576. PoolVector<Vector3> anormals = arrays[Mesh::ARRAY_NORMAL];
  577. PoolVector<Vector3>::Read nr;
  578. bool nr_used = false;
  579. if (anormals.size()) {
  580. nr = anormals.read();
  581. nr_used = true;
  582. }
  583. PoolVector<Vector2> auvs = arrays[Mesh::ARRAY_TEX_UV];
  584. PoolVector<Vector2>::Read uvr;
  585. bool uvr_used = false;
  586. if (auvs.size()) {
  587. uvr = auvs.read();
  588. uvr_used = true;
  589. }
  590. Ref<Material> mat;
  591. if (material.is_valid()) {
  592. mat = material;
  593. } else {
  594. mat = mesh->surface_get_material(i);
  595. }
  596. PoolVector<int> aindices = arrays[Mesh::ARRAY_INDEX];
  597. if (aindices.size()) {
  598. int as = vertices.size();
  599. int is = aindices.size();
  600. vertices.resize(as + is);
  601. smooth.resize((as + is) / 3);
  602. materials.resize((as + is) / 3);
  603. uvs.resize(as + is);
  604. PoolVector<Vector3>::Write vw = vertices.write();
  605. PoolVector<bool>::Write sw = smooth.write();
  606. PoolVector<Vector2>::Write uvw = uvs.write();
  607. PoolVector<Ref<Material> >::Write mw = materials.write();
  608. PoolVector<int>::Read ir = aindices.read();
  609. for (int j = 0; j < is; j += 3) {
  610. Vector3 vertex[3];
  611. Vector3 normal[3];
  612. Vector2 uv[3];
  613. for (int k = 0; k < 3; k++) {
  614. int idx = ir[j + k];
  615. vertex[k] = vr[idx];
  616. if (nr_used) {
  617. normal[k] = nr[idx];
  618. }
  619. if (uvr_used) {
  620. uv[k] = uvr[idx];
  621. }
  622. }
  623. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  624. vw[as + j + 0] = vertex[0];
  625. vw[as + j + 1] = vertex[1];
  626. vw[as + j + 2] = vertex[2];
  627. uvw[as + j + 0] = uv[0];
  628. uvw[as + j + 1] = uv[1];
  629. uvw[as + j + 2] = uv[2];
  630. sw[(as + j) / 3] = !flat;
  631. mw[(as + j) / 3] = mat;
  632. }
  633. } else {
  634. int as = vertices.size();
  635. int is = avertices.size();
  636. vertices.resize(as + is);
  637. smooth.resize((as + is) / 3);
  638. uvs.resize(as + is);
  639. materials.resize((as + is) / 3);
  640. PoolVector<Vector3>::Write vw = vertices.write();
  641. PoolVector<bool>::Write sw = smooth.write();
  642. PoolVector<Vector2>::Write uvw = uvs.write();
  643. PoolVector<Ref<Material> >::Write mw = materials.write();
  644. for (int j = 0; j < is; j += 3) {
  645. Vector3 vertex[3];
  646. Vector3 normal[3];
  647. Vector2 uv[3];
  648. for (int k = 0; k < 3; k++) {
  649. vertex[k] = vr[j + k];
  650. if (nr_used) {
  651. normal[k] = nr[j + k];
  652. }
  653. if (uvr_used) {
  654. uv[k] = uvr[j + k];
  655. }
  656. }
  657. bool flat = normal[0].distance_to(normal[1]) < CMP_EPSILON && normal[0].distance_to(normal[2]) < CMP_EPSILON;
  658. vw[as + j + 0] = vertex[0];
  659. vw[as + j + 1] = vertex[1];
  660. vw[as + j + 2] = vertex[2];
  661. uvw[as + j + 0] = uv[0];
  662. uvw[as + j + 1] = uv[1];
  663. uvw[as + j + 2] = uv[2];
  664. sw[(as + j) / 3] = !flat;
  665. mw[(as + j) / 3] = mat;
  666. }
  667. }
  668. }
  669. if (vertices.size() == 0)
  670. return NULL;
  671. return _create_brush_from_arrays(vertices, uvs, smooth, materials);
  672. }
  673. void CSGMesh::_mesh_changed() {
  674. _make_dirty();
  675. update_gizmo();
  676. }
  677. void CSGMesh::set_material(const Ref<Material> &p_material) {
  678. if (material == p_material)
  679. return;
  680. material = p_material;
  681. _make_dirty();
  682. }
  683. Ref<Material> CSGMesh::get_material() const {
  684. return material;
  685. }
  686. void CSGMesh::_bind_methods() {
  687. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CSGMesh::set_mesh);
  688. ClassDB::bind_method(D_METHOD("get_mesh"), &CSGMesh::get_mesh);
  689. ClassDB::bind_method(D_METHOD("_mesh_changed"), &CSGMesh::_mesh_changed);
  690. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGMesh::set_material);
  691. ClassDB::bind_method(D_METHOD("get_material"), &CSGMesh::get_material);
  692. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  693. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  694. }
  695. void CSGMesh::set_mesh(const Ref<Mesh> &p_mesh) {
  696. if (mesh == p_mesh)
  697. return;
  698. if (mesh.is_valid()) {
  699. mesh->disconnect("changed", this, "_mesh_changed");
  700. }
  701. mesh = p_mesh;
  702. if (mesh.is_valid()) {
  703. mesh->connect("changed", this, "_mesh_changed");
  704. }
  705. _make_dirty();
  706. }
  707. Ref<Mesh> CSGMesh::get_mesh() {
  708. return mesh;
  709. }
  710. ////////////////////////////////
  711. CSGBrush *CSGSphere::_build_brush() {
  712. // set our bounding box
  713. CSGBrush *brush = memnew(CSGBrush);
  714. int face_count = rings * radial_segments * 2 - radial_segments * 2;
  715. bool invert_val = is_inverting_faces();
  716. Ref<Material> material = get_material();
  717. PoolVector<Vector3> faces;
  718. PoolVector<Vector2> uvs;
  719. PoolVector<bool> smooth;
  720. PoolVector<Ref<Material> > materials;
  721. PoolVector<bool> invert;
  722. faces.resize(face_count * 3);
  723. uvs.resize(face_count * 3);
  724. smooth.resize(face_count);
  725. materials.resize(face_count);
  726. invert.resize(face_count);
  727. {
  728. PoolVector<Vector3>::Write facesw = faces.write();
  729. PoolVector<Vector2>::Write uvsw = uvs.write();
  730. PoolVector<bool>::Write smoothw = smooth.write();
  731. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  732. PoolVector<bool>::Write invertw = invert.write();
  733. int face = 0;
  734. for (int i = 1; i <= rings; i++) {
  735. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / rings);
  736. double z0 = Math::sin(lat0);
  737. double zr0 = Math::cos(lat0);
  738. double u0 = double(i - 1) / rings;
  739. double lat1 = Math_PI * (-0.5 + (double)i / rings);
  740. double z1 = Math::sin(lat1);
  741. double zr1 = Math::cos(lat1);
  742. double u1 = double(i) / rings;
  743. for (int j = radial_segments; j >= 1; j--) {
  744. double lng0 = 2 * Math_PI * (double)(j - 1) / radial_segments;
  745. double x0 = Math::cos(lng0);
  746. double y0 = Math::sin(lng0);
  747. double v0 = double(i - 1) / radial_segments;
  748. double lng1 = 2 * Math_PI * (double)(j) / radial_segments;
  749. double x1 = Math::cos(lng1);
  750. double y1 = Math::sin(lng1);
  751. double v1 = double(i) / radial_segments;
  752. Vector3 v[4] = {
  753. Vector3(x1 * zr0, z0, y1 * zr0) * radius,
  754. Vector3(x1 * zr1, z1, y1 * zr1) * radius,
  755. Vector3(x0 * zr1, z1, y0 * zr1) * radius,
  756. Vector3(x0 * zr0, z0, y0 * zr0) * radius
  757. };
  758. Vector2 u[4] = {
  759. Vector2(v1, u0),
  760. Vector2(v1, u1),
  761. Vector2(v0, u1),
  762. Vector2(v0, u0),
  763. };
  764. if (i < rings) {
  765. //face 1
  766. facesw[face * 3 + 0] = v[0];
  767. facesw[face * 3 + 1] = v[1];
  768. facesw[face * 3 + 2] = v[2];
  769. uvsw[face * 3 + 0] = u[0];
  770. uvsw[face * 3 + 1] = u[1];
  771. uvsw[face * 3 + 2] = u[2];
  772. smoothw[face] = smooth_faces;
  773. invertw[face] = invert_val;
  774. materialsw[face] = material;
  775. face++;
  776. }
  777. if (i > 1) {
  778. //face 2
  779. facesw[face * 3 + 0] = v[2];
  780. facesw[face * 3 + 1] = v[3];
  781. facesw[face * 3 + 2] = v[0];
  782. uvsw[face * 3 + 0] = u[2];
  783. uvsw[face * 3 + 1] = u[3];
  784. uvsw[face * 3 + 2] = u[0];
  785. smoothw[face] = smooth_faces;
  786. invertw[face] = invert_val;
  787. materialsw[face] = material;
  788. face++;
  789. }
  790. }
  791. }
  792. if (face != face_count) {
  793. ERR_PRINT("Face mismatch bug! fix code");
  794. }
  795. }
  796. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  797. return brush;
  798. }
  799. void CSGSphere::_bind_methods() {
  800. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGSphere::set_radius);
  801. ClassDB::bind_method(D_METHOD("get_radius"), &CSGSphere::get_radius);
  802. ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGSphere::set_radial_segments);
  803. ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGSphere::get_radial_segments);
  804. ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CSGSphere::set_rings);
  805. ClassDB::bind_method(D_METHOD("get_rings"), &CSGSphere::get_rings);
  806. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGSphere::set_smooth_faces);
  807. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGSphere::get_smooth_faces);
  808. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGSphere::set_material);
  809. ClassDB::bind_method(D_METHOD("get_material"), &CSGSphere::get_material);
  810. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001"), "set_radius", "get_radius");
  811. ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments");
  812. ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1"), "set_rings", "get_rings");
  813. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  814. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  815. }
  816. void CSGSphere::set_radius(const float p_radius) {
  817. ERR_FAIL_COND(p_radius <= 0);
  818. radius = p_radius;
  819. _make_dirty();
  820. update_gizmo();
  821. }
  822. float CSGSphere::get_radius() const {
  823. return radius;
  824. }
  825. void CSGSphere::set_radial_segments(const int p_radial_segments) {
  826. radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
  827. _make_dirty();
  828. update_gizmo();
  829. }
  830. int CSGSphere::get_radial_segments() const {
  831. return radial_segments;
  832. }
  833. void CSGSphere::set_rings(const int p_rings) {
  834. rings = p_rings > 1 ? p_rings : 1;
  835. _make_dirty();
  836. update_gizmo();
  837. }
  838. int CSGSphere::get_rings() const {
  839. return rings;
  840. }
  841. void CSGSphere::set_smooth_faces(const bool p_smooth_faces) {
  842. smooth_faces = p_smooth_faces;
  843. _make_dirty();
  844. }
  845. bool CSGSphere::get_smooth_faces() const {
  846. return smooth_faces;
  847. }
  848. void CSGSphere::set_material(const Ref<Material> &p_material) {
  849. material = p_material;
  850. _make_dirty();
  851. }
  852. Ref<Material> CSGSphere::get_material() const {
  853. return material;
  854. }
  855. CSGSphere::CSGSphere() {
  856. // defaults
  857. radius = 1.0;
  858. radial_segments = 12;
  859. rings = 6;
  860. smooth_faces = true;
  861. }
  862. ///////////////
  863. CSGBrush *CSGBox::_build_brush() {
  864. // set our bounding box
  865. CSGBrush *brush = memnew(CSGBrush);
  866. int face_count = 12; //it's a cube..
  867. bool invert_val = is_inverting_faces();
  868. Ref<Material> material = get_material();
  869. PoolVector<Vector3> faces;
  870. PoolVector<Vector2> uvs;
  871. PoolVector<bool> smooth;
  872. PoolVector<Ref<Material> > materials;
  873. PoolVector<bool> invert;
  874. faces.resize(face_count * 3);
  875. uvs.resize(face_count * 3);
  876. smooth.resize(face_count);
  877. materials.resize(face_count);
  878. invert.resize(face_count);
  879. {
  880. PoolVector<Vector3>::Write facesw = faces.write();
  881. PoolVector<Vector2>::Write uvsw = uvs.write();
  882. PoolVector<bool>::Write smoothw = smooth.write();
  883. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  884. PoolVector<bool>::Write invertw = invert.write();
  885. int face = 0;
  886. Vector3 vertex_mul(width * 0.5, height * 0.5, depth * 0.5);
  887. {
  888. for (int i = 0; i < 6; i++) {
  889. Vector3 face_points[4];
  890. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  891. for (int j = 0; j < 4; j++) {
  892. float v[3];
  893. v[0] = 1.0;
  894. v[1] = 1 - 2 * ((j >> 1) & 1);
  895. v[2] = v[1] * (1 - 2 * (j & 1));
  896. for (int k = 0; k < 3; k++) {
  897. if (i < 3)
  898. face_points[j][(i + k) % 3] = v[k];
  899. else
  900. face_points[3 - j][(i + k) % 3] = -v[k];
  901. }
  902. }
  903. Vector2 u[4];
  904. for (int j = 0; j < 4; j++) {
  905. u[j] = Vector2(uv_points[j * 2 + 0], uv_points[j * 2 + 1]);
  906. }
  907. //face 1
  908. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  909. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  910. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  911. uvsw[face * 3 + 0] = u[0];
  912. uvsw[face * 3 + 1] = u[1];
  913. uvsw[face * 3 + 2] = u[2];
  914. smoothw[face] = false;
  915. invertw[face] = invert_val;
  916. materialsw[face] = material;
  917. face++;
  918. //face 1
  919. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  920. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  921. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  922. uvsw[face * 3 + 0] = u[2];
  923. uvsw[face * 3 + 1] = u[3];
  924. uvsw[face * 3 + 2] = u[0];
  925. smoothw[face] = false;
  926. invertw[face] = invert_val;
  927. materialsw[face] = material;
  928. face++;
  929. }
  930. }
  931. if (face != face_count) {
  932. ERR_PRINT("Face mismatch bug! fix code");
  933. }
  934. }
  935. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  936. return brush;
  937. }
  938. void CSGBox::_bind_methods() {
  939. ClassDB::bind_method(D_METHOD("set_width", "width"), &CSGBox::set_width);
  940. ClassDB::bind_method(D_METHOD("get_width"), &CSGBox::get_width);
  941. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGBox::set_height);
  942. ClassDB::bind_method(D_METHOD("get_height"), &CSGBox::get_height);
  943. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGBox::set_depth);
  944. ClassDB::bind_method(D_METHOD("get_depth"), &CSGBox::get_depth);
  945. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGBox::set_material);
  946. ClassDB::bind_method(D_METHOD("get_material"), &CSGBox::get_material);
  947. ADD_PROPERTY(PropertyInfo(Variant::REAL, "width", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_width", "get_width");
  948. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  949. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  950. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  951. }
  952. void CSGBox::set_width(const float p_width) {
  953. width = p_width;
  954. _make_dirty();
  955. update_gizmo();
  956. }
  957. float CSGBox::get_width() const {
  958. return width;
  959. }
  960. void CSGBox::set_height(const float p_height) {
  961. height = p_height;
  962. _make_dirty();
  963. update_gizmo();
  964. }
  965. float CSGBox::get_height() const {
  966. return height;
  967. }
  968. void CSGBox::set_depth(const float p_depth) {
  969. depth = p_depth;
  970. _make_dirty();
  971. update_gizmo();
  972. }
  973. float CSGBox::get_depth() const {
  974. return depth;
  975. }
  976. void CSGBox::set_material(const Ref<Material> &p_material) {
  977. material = p_material;
  978. _make_dirty();
  979. update_gizmo();
  980. }
  981. Ref<Material> CSGBox::get_material() const {
  982. return material;
  983. }
  984. CSGBox::CSGBox() {
  985. // defaults
  986. width = 2.0;
  987. height = 2.0;
  988. depth = 2.0;
  989. }
  990. ///////////////
  991. CSGBrush *CSGCylinder::_build_brush() {
  992. // set our bounding box
  993. CSGBrush *brush = memnew(CSGBrush);
  994. int face_count = sides * (cone ? 1 : 2) + sides + (cone ? 0 : sides);
  995. bool invert_val = is_inverting_faces();
  996. Ref<Material> material = get_material();
  997. PoolVector<Vector3> faces;
  998. PoolVector<Vector2> uvs;
  999. PoolVector<bool> smooth;
  1000. PoolVector<Ref<Material> > materials;
  1001. PoolVector<bool> invert;
  1002. faces.resize(face_count * 3);
  1003. uvs.resize(face_count * 3);
  1004. smooth.resize(face_count);
  1005. materials.resize(face_count);
  1006. invert.resize(face_count);
  1007. {
  1008. PoolVector<Vector3>::Write facesw = faces.write();
  1009. PoolVector<Vector2>::Write uvsw = uvs.write();
  1010. PoolVector<bool>::Write smoothw = smooth.write();
  1011. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  1012. PoolVector<bool>::Write invertw = invert.write();
  1013. int face = 0;
  1014. Vector3 vertex_mul(radius, height * 0.5, radius);
  1015. {
  1016. for (int i = 0; i < sides; i++) {
  1017. float inc = float(i) / sides;
  1018. float inc_n = float((i + 1)) / sides;
  1019. float ang = inc * Math_PI * 2.0;
  1020. float ang_n = inc_n * Math_PI * 2.0;
  1021. Vector3 base(Math::cos(ang), 0, Math::sin(ang));
  1022. Vector3 base_n(Math::cos(ang_n), 0, Math::sin(ang_n));
  1023. Vector3 face_points[4] = {
  1024. base + Vector3(0, -1, 0),
  1025. base_n + Vector3(0, -1, 0),
  1026. base_n * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1027. base * (cone ? 0.0 : 1.0) + Vector3(0, 1, 0),
  1028. };
  1029. Vector2 u[4] = {
  1030. Vector2(inc, 0),
  1031. Vector2(inc_n, 0),
  1032. Vector2(inc_n, 1),
  1033. Vector2(inc, 1),
  1034. };
  1035. //side face 1
  1036. facesw[face * 3 + 0] = face_points[0] * vertex_mul;
  1037. facesw[face * 3 + 1] = face_points[1] * vertex_mul;
  1038. facesw[face * 3 + 2] = face_points[2] * vertex_mul;
  1039. uvsw[face * 3 + 0] = u[0];
  1040. uvsw[face * 3 + 1] = u[1];
  1041. uvsw[face * 3 + 2] = u[2];
  1042. smoothw[face] = smooth_faces;
  1043. invertw[face] = invert_val;
  1044. materialsw[face] = material;
  1045. face++;
  1046. if (!cone) {
  1047. //side face 2
  1048. facesw[face * 3 + 0] = face_points[2] * vertex_mul;
  1049. facesw[face * 3 + 1] = face_points[3] * vertex_mul;
  1050. facesw[face * 3 + 2] = face_points[0] * vertex_mul;
  1051. uvsw[face * 3 + 0] = u[2];
  1052. uvsw[face * 3 + 1] = u[3];
  1053. uvsw[face * 3 + 2] = u[0];
  1054. smoothw[face] = smooth_faces;
  1055. invertw[face] = invert_val;
  1056. materialsw[face] = material;
  1057. face++;
  1058. }
  1059. //bottom face 1
  1060. facesw[face * 3 + 0] = face_points[1] * vertex_mul;
  1061. facesw[face * 3 + 1] = face_points[0] * 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. if (!cone) {
  1071. //top face 1
  1072. facesw[face * 3 + 0] = face_points[3] * vertex_mul;
  1073. facesw[face * 3 + 1] = face_points[2] * vertex_mul;
  1074. facesw[face * 3 + 2] = Vector3(0, 1, 0) * vertex_mul;
  1075. uvsw[face * 3 + 0] = Vector2(face_points[1].x, face_points[1].y) * 0.5 + Vector2(0.5, 0.5);
  1076. uvsw[face * 3 + 1] = Vector2(face_points[0].x, face_points[0].y) * 0.5 + Vector2(0.5, 0.5);
  1077. uvsw[face * 3 + 2] = Vector2(0.5, 0.5);
  1078. smoothw[face] = false;
  1079. invertw[face] = invert_val;
  1080. materialsw[face] = material;
  1081. face++;
  1082. }
  1083. }
  1084. }
  1085. if (face != face_count) {
  1086. ERR_PRINT("Face mismatch bug! fix code");
  1087. }
  1088. }
  1089. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1090. return brush;
  1091. }
  1092. void CSGCylinder::_bind_methods() {
  1093. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCylinder::set_radius);
  1094. ClassDB::bind_method(D_METHOD("get_radius"), &CSGCylinder::get_radius);
  1095. ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCylinder::set_height);
  1096. ClassDB::bind_method(D_METHOD("get_height"), &CSGCylinder::get_height);
  1097. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGCylinder::set_sides);
  1098. ClassDB::bind_method(D_METHOD("get_sides"), &CSGCylinder::get_sides);
  1099. ClassDB::bind_method(D_METHOD("set_cone", "cone"), &CSGCylinder::set_cone);
  1100. ClassDB::bind_method(D_METHOD("is_cone"), &CSGCylinder::is_cone);
  1101. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGCylinder::set_material);
  1102. ClassDB::bind_method(D_METHOD("get_material"), &CSGCylinder::get_material);
  1103. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGCylinder::set_smooth_faces);
  1104. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGCylinder::get_smooth_faces);
  1105. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_radius", "get_radius");
  1106. ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_height", "get_height");
  1107. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1108. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cone"), "set_cone", "is_cone");
  1109. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1110. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  1111. }
  1112. void CSGCylinder::set_radius(const float p_radius) {
  1113. radius = p_radius;
  1114. _make_dirty();
  1115. update_gizmo();
  1116. }
  1117. float CSGCylinder::get_radius() const {
  1118. return radius;
  1119. }
  1120. void CSGCylinder::set_height(const float p_height) {
  1121. height = p_height;
  1122. _make_dirty();
  1123. update_gizmo();
  1124. }
  1125. float CSGCylinder::get_height() const {
  1126. return height;
  1127. }
  1128. void CSGCylinder::set_sides(const int p_sides) {
  1129. ERR_FAIL_COND(p_sides < 3);
  1130. sides = p_sides;
  1131. _make_dirty();
  1132. update_gizmo();
  1133. }
  1134. int CSGCylinder::get_sides() const {
  1135. return sides;
  1136. }
  1137. void CSGCylinder::set_cone(const bool p_cone) {
  1138. cone = p_cone;
  1139. _make_dirty();
  1140. update_gizmo();
  1141. }
  1142. bool CSGCylinder::is_cone() const {
  1143. return cone;
  1144. }
  1145. void CSGCylinder::set_smooth_faces(const bool p_smooth_faces) {
  1146. smooth_faces = p_smooth_faces;
  1147. _make_dirty();
  1148. }
  1149. bool CSGCylinder::get_smooth_faces() const {
  1150. return smooth_faces;
  1151. }
  1152. void CSGCylinder::set_material(const Ref<Material> &p_material) {
  1153. material = p_material;
  1154. _make_dirty();
  1155. }
  1156. Ref<Material> CSGCylinder::get_material() const {
  1157. return material;
  1158. }
  1159. CSGCylinder::CSGCylinder() {
  1160. // defaults
  1161. radius = 1.0;
  1162. height = 1.0;
  1163. sides = 8;
  1164. cone = false;
  1165. smooth_faces = true;
  1166. }
  1167. ///////////////
  1168. CSGBrush *CSGTorus::_build_brush() {
  1169. // set our bounding box
  1170. float min_radius = inner_radius;
  1171. float max_radius = outer_radius;
  1172. if (min_radius == max_radius)
  1173. return NULL; //sorry, can't
  1174. if (min_radius > max_radius) {
  1175. SWAP(min_radius, max_radius);
  1176. }
  1177. float radius = (max_radius - min_radius) * 0.5;
  1178. CSGBrush *brush = memnew(CSGBrush);
  1179. int face_count = ring_sides * sides * 2;
  1180. bool invert_val = is_inverting_faces();
  1181. Ref<Material> material = get_material();
  1182. PoolVector<Vector3> faces;
  1183. PoolVector<Vector2> uvs;
  1184. PoolVector<bool> smooth;
  1185. PoolVector<Ref<Material> > materials;
  1186. PoolVector<bool> invert;
  1187. faces.resize(face_count * 3);
  1188. uvs.resize(face_count * 3);
  1189. smooth.resize(face_count);
  1190. materials.resize(face_count);
  1191. invert.resize(face_count);
  1192. {
  1193. PoolVector<Vector3>::Write facesw = faces.write();
  1194. PoolVector<Vector2>::Write uvsw = uvs.write();
  1195. PoolVector<bool>::Write smoothw = smooth.write();
  1196. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  1197. PoolVector<bool>::Write invertw = invert.write();
  1198. int face = 0;
  1199. {
  1200. for (int i = 0; i < sides; i++) {
  1201. float inci = float(i) / sides;
  1202. float inci_n = float((i + 1)) / sides;
  1203. float angi = inci * Math_PI * 2.0;
  1204. float angi_n = inci_n * Math_PI * 2.0;
  1205. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1206. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1207. for (int j = 0; j < ring_sides; j++) {
  1208. float incj = float(j) / ring_sides;
  1209. float incj_n = float((j + 1)) / ring_sides;
  1210. float angj = incj * Math_PI * 2.0;
  1211. float angj_n = incj_n * Math_PI * 2.0;
  1212. Vector2 normalj = Vector2(Math::cos(angj), Math::sin(angj)) * radius + Vector2(min_radius + radius, 0);
  1213. Vector2 normalj_n = Vector2(Math::cos(angj_n), Math::sin(angj_n)) * radius + Vector2(min_radius + radius, 0);
  1214. Vector3 face_points[4] = {
  1215. Vector3(normali.x * normalj.x, normalj.y, normali.z * normalj.x),
  1216. Vector3(normali.x * normalj_n.x, normalj_n.y, normali.z * normalj_n.x),
  1217. Vector3(normali_n.x * normalj_n.x, normalj_n.y, normali_n.z * normalj_n.x),
  1218. Vector3(normali_n.x * normalj.x, normalj.y, normali_n.z * normalj.x)
  1219. };
  1220. Vector2 u[4] = {
  1221. Vector2(inci, incj),
  1222. Vector2(inci, incj_n),
  1223. Vector2(inci_n, incj_n),
  1224. Vector2(inci_n, incj),
  1225. };
  1226. // face 1
  1227. facesw[face * 3 + 0] = face_points[0];
  1228. facesw[face * 3 + 1] = face_points[2];
  1229. facesw[face * 3 + 2] = face_points[1];
  1230. uvsw[face * 3 + 0] = u[0];
  1231. uvsw[face * 3 + 1] = u[2];
  1232. uvsw[face * 3 + 2] = u[1];
  1233. smoothw[face] = smooth_faces;
  1234. invertw[face] = invert_val;
  1235. materialsw[face] = material;
  1236. face++;
  1237. //face 2
  1238. facesw[face * 3 + 0] = face_points[3];
  1239. facesw[face * 3 + 1] = face_points[2];
  1240. facesw[face * 3 + 2] = face_points[0];
  1241. uvsw[face * 3 + 0] = u[3];
  1242. uvsw[face * 3 + 1] = u[2];
  1243. uvsw[face * 3 + 2] = u[0];
  1244. smoothw[face] = smooth_faces;
  1245. invertw[face] = invert_val;
  1246. materialsw[face] = material;
  1247. face++;
  1248. }
  1249. }
  1250. }
  1251. if (face != face_count) {
  1252. ERR_PRINT("Face mismatch bug! fix code");
  1253. }
  1254. }
  1255. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1256. return brush;
  1257. }
  1258. void CSGTorus::_bind_methods() {
  1259. ClassDB::bind_method(D_METHOD("set_inner_radius", "radius"), &CSGTorus::set_inner_radius);
  1260. ClassDB::bind_method(D_METHOD("get_inner_radius"), &CSGTorus::get_inner_radius);
  1261. ClassDB::bind_method(D_METHOD("set_outer_radius", "radius"), &CSGTorus::set_outer_radius);
  1262. ClassDB::bind_method(D_METHOD("get_outer_radius"), &CSGTorus::get_outer_radius);
  1263. ClassDB::bind_method(D_METHOD("set_sides", "sides"), &CSGTorus::set_sides);
  1264. ClassDB::bind_method(D_METHOD("get_sides"), &CSGTorus::get_sides);
  1265. ClassDB::bind_method(D_METHOD("set_ring_sides", "sides"), &CSGTorus::set_ring_sides);
  1266. ClassDB::bind_method(D_METHOD("get_ring_sides"), &CSGTorus::get_ring_sides);
  1267. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGTorus::set_material);
  1268. ClassDB::bind_method(D_METHOD("get_material"), &CSGTorus::get_material);
  1269. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGTorus::set_smooth_faces);
  1270. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGTorus::get_smooth_faces);
  1271. 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");
  1272. 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");
  1273. ADD_PROPERTY(PropertyInfo(Variant::INT, "sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_sides", "get_sides");
  1274. ADD_PROPERTY(PropertyInfo(Variant::INT, "ring_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_ring_sides", "get_ring_sides");
  1275. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1276. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  1277. }
  1278. void CSGTorus::set_inner_radius(const float p_inner_radius) {
  1279. inner_radius = p_inner_radius;
  1280. _make_dirty();
  1281. update_gizmo();
  1282. }
  1283. float CSGTorus::get_inner_radius() const {
  1284. return inner_radius;
  1285. }
  1286. void CSGTorus::set_outer_radius(const float p_outer_radius) {
  1287. outer_radius = p_outer_radius;
  1288. _make_dirty();
  1289. update_gizmo();
  1290. }
  1291. float CSGTorus::get_outer_radius() const {
  1292. return outer_radius;
  1293. }
  1294. void CSGTorus::set_sides(const int p_sides) {
  1295. ERR_FAIL_COND(p_sides < 3);
  1296. sides = p_sides;
  1297. _make_dirty();
  1298. update_gizmo();
  1299. }
  1300. int CSGTorus::get_sides() const {
  1301. return sides;
  1302. }
  1303. void CSGTorus::set_ring_sides(const int p_ring_sides) {
  1304. ERR_FAIL_COND(p_ring_sides < 3);
  1305. ring_sides = p_ring_sides;
  1306. _make_dirty();
  1307. update_gizmo();
  1308. }
  1309. int CSGTorus::get_ring_sides() const {
  1310. return ring_sides;
  1311. }
  1312. void CSGTorus::set_smooth_faces(const bool p_smooth_faces) {
  1313. smooth_faces = p_smooth_faces;
  1314. _make_dirty();
  1315. }
  1316. bool CSGTorus::get_smooth_faces() const {
  1317. return smooth_faces;
  1318. }
  1319. void CSGTorus::set_material(const Ref<Material> &p_material) {
  1320. material = p_material;
  1321. _make_dirty();
  1322. }
  1323. Ref<Material> CSGTorus::get_material() const {
  1324. return material;
  1325. }
  1326. CSGTorus::CSGTorus() {
  1327. // defaults
  1328. inner_radius = 2.0;
  1329. outer_radius = 3.0;
  1330. sides = 8;
  1331. ring_sides = 6;
  1332. smooth_faces = true;
  1333. }
  1334. ///////////////
  1335. CSGBrush *CSGPolygon::_build_brush() {
  1336. // set our bounding box
  1337. if (polygon.size() < 3)
  1338. return NULL;
  1339. Vector<Point2> final_polygon = polygon;
  1340. if (Triangulate::get_area(final_polygon) > 0) {
  1341. final_polygon.invert();
  1342. }
  1343. Vector<int> triangles = Geometry::triangulate_polygon(final_polygon);
  1344. if (triangles.size() < 3)
  1345. return NULL;
  1346. Path *path = NULL;
  1347. Ref<Curve3D> curve;
  1348. // get bounds for our polygon
  1349. Vector2 final_polygon_min;
  1350. Vector2 final_polygon_max;
  1351. for (int i = 0; i < final_polygon.size(); i++) {
  1352. Vector2 p = final_polygon[i];
  1353. if (i == 0) {
  1354. final_polygon_min = p;
  1355. final_polygon_max = final_polygon_min;
  1356. } else {
  1357. if (p.x < final_polygon_min.x) final_polygon_min.x = p.x;
  1358. if (p.y < final_polygon_min.y) final_polygon_min.y = p.y;
  1359. if (p.x > final_polygon_max.x) final_polygon_max.x = p.x;
  1360. if (p.y > final_polygon_max.y) final_polygon_max.y = p.y;
  1361. }
  1362. }
  1363. Vector2 final_polygon_size = final_polygon_max - final_polygon_min;
  1364. if (mode == MODE_PATH) {
  1365. if (!has_node(path_node))
  1366. return NULL;
  1367. Node *n = get_node(path_node);
  1368. if (!n)
  1369. return NULL;
  1370. path = Object::cast_to<Path>(n);
  1371. if (!path)
  1372. return NULL;
  1373. if (path != path_cache) {
  1374. if (path_cache) {
  1375. path_cache->disconnect("tree_exited", this, "_path_exited");
  1376. path_cache->disconnect("curve_changed", this, "_path_changed");
  1377. path_cache = NULL;
  1378. }
  1379. path_cache = path;
  1380. if (path_cache) {
  1381. path_cache->connect("tree_exited", this, "_path_exited");
  1382. path_cache->connect("curve_changed", this, "_path_changed");
  1383. path_cache = NULL;
  1384. }
  1385. }
  1386. curve = path->get_curve();
  1387. if (curve.is_null())
  1388. return NULL;
  1389. if (curve->get_baked_length() <= 0)
  1390. return NULL;
  1391. }
  1392. CSGBrush *brush = memnew(CSGBrush);
  1393. int face_count = 0;
  1394. switch (mode) {
  1395. case MODE_DEPTH: face_count = triangles.size() * 2 / 3 + (final_polygon.size()) * 2; break;
  1396. case MODE_SPIN: face_count = (spin_degrees < 360 ? triangles.size() * 2 / 3 : 0) + (final_polygon.size()) * 2 * spin_sides; break;
  1397. case MODE_PATH: {
  1398. float bl = curve->get_baked_length();
  1399. int splits = MAX(2, Math::ceil(bl / path_interval));
  1400. if (path_joined) {
  1401. face_count = splits * final_polygon.size() * 2;
  1402. } else {
  1403. face_count = triangles.size() * 2 / 3 + splits * final_polygon.size() * 2;
  1404. }
  1405. } break;
  1406. }
  1407. bool invert_val = is_inverting_faces();
  1408. Ref<Material> material = get_material();
  1409. PoolVector<Vector3> faces;
  1410. PoolVector<Vector2> uvs;
  1411. PoolVector<bool> smooth;
  1412. PoolVector<Ref<Material> > materials;
  1413. PoolVector<bool> invert;
  1414. faces.resize(face_count * 3);
  1415. uvs.resize(face_count * 3);
  1416. smooth.resize(face_count);
  1417. materials.resize(face_count);
  1418. invert.resize(face_count);
  1419. AABB aabb; //must be computed
  1420. {
  1421. PoolVector<Vector3>::Write facesw = faces.write();
  1422. PoolVector<Vector2>::Write uvsw = uvs.write();
  1423. PoolVector<bool>::Write smoothw = smooth.write();
  1424. PoolVector<Ref<Material> >::Write materialsw = materials.write();
  1425. PoolVector<bool>::Write invertw = invert.write();
  1426. int face = 0;
  1427. switch (mode) {
  1428. case MODE_DEPTH: {
  1429. //add triangles, front and back
  1430. for (int i = 0; i < 2; i++) {
  1431. for (int j = 0; j < triangles.size(); j += 3) {
  1432. for (int k = 0; k < 3; k++) {
  1433. int src[3] = { 0, i == 0 ? 1 : 2, i == 0 ? 2 : 1 };
  1434. Vector2 p = final_polygon[triangles[j + src[k]]];
  1435. Vector3 v = Vector3(p.x, p.y, 0);
  1436. if (i == 0) {
  1437. v.z -= depth;
  1438. }
  1439. facesw[face * 3 + k] = v;
  1440. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1441. if (i == 0) {
  1442. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1443. }
  1444. }
  1445. smoothw[face] = false;
  1446. materialsw[face] = material;
  1447. invertw[face] = invert_val;
  1448. face++;
  1449. }
  1450. }
  1451. //add triangles for depth
  1452. for (int i = 0; i < final_polygon.size(); i++) {
  1453. int i_n = (i + 1) % final_polygon.size();
  1454. Vector3 v[4] = {
  1455. Vector3(final_polygon[i].x, final_polygon[i].y, -depth),
  1456. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, -depth),
  1457. Vector3(final_polygon[i_n].x, final_polygon[i_n].y, 0),
  1458. Vector3(final_polygon[i].x, final_polygon[i].y, 0),
  1459. };
  1460. Vector2 u[4] = {
  1461. Vector2(0, 0),
  1462. Vector2(0, 1),
  1463. Vector2(1, 1),
  1464. Vector2(1, 0)
  1465. };
  1466. // face 1
  1467. facesw[face * 3 + 0] = v[0];
  1468. facesw[face * 3 + 1] = v[1];
  1469. facesw[face * 3 + 2] = v[2];
  1470. uvsw[face * 3 + 0] = u[0];
  1471. uvsw[face * 3 + 1] = u[1];
  1472. uvsw[face * 3 + 2] = u[2];
  1473. smoothw[face] = smooth_faces;
  1474. invertw[face] = invert_val;
  1475. materialsw[face] = material;
  1476. face++;
  1477. // face 2
  1478. facesw[face * 3 + 0] = v[2];
  1479. facesw[face * 3 + 1] = v[3];
  1480. facesw[face * 3 + 2] = v[0];
  1481. uvsw[face * 3 + 0] = u[2];
  1482. uvsw[face * 3 + 1] = u[3];
  1483. uvsw[face * 3 + 2] = u[0];
  1484. smoothw[face] = smooth_faces;
  1485. invertw[face] = invert_val;
  1486. materialsw[face] = material;
  1487. face++;
  1488. }
  1489. } break;
  1490. case MODE_SPIN: {
  1491. for (int i = 0; i < spin_sides; i++) {
  1492. float inci = float(i) / spin_sides;
  1493. float inci_n = float((i + 1)) / spin_sides;
  1494. float angi = -(inci * spin_degrees / 360.0) * Math_PI * 2.0;
  1495. float angi_n = -(inci_n * spin_degrees / 360.0) * Math_PI * 2.0;
  1496. Vector3 normali = Vector3(Math::cos(angi), 0, Math::sin(angi));
  1497. Vector3 normali_n = Vector3(Math::cos(angi_n), 0, Math::sin(angi_n));
  1498. //add triangles for depth
  1499. for (int j = 0; j < final_polygon.size(); j++) {
  1500. int j_n = (j + 1) % final_polygon.size();
  1501. Vector3 v[4] = {
  1502. Vector3(normali.x * final_polygon[j].x, final_polygon[j].y, normali.z * final_polygon[j].x),
  1503. Vector3(normali.x * final_polygon[j_n].x, final_polygon[j_n].y, normali.z * final_polygon[j_n].x),
  1504. Vector3(normali_n.x * final_polygon[j_n].x, final_polygon[j_n].y, normali_n.z * final_polygon[j_n].x),
  1505. Vector3(normali_n.x * final_polygon[j].x, final_polygon[j].y, normali_n.z * final_polygon[j].x),
  1506. };
  1507. Vector2 u[4] = {
  1508. Vector2(0, 0),
  1509. Vector2(0, 1),
  1510. Vector2(1, 1),
  1511. Vector2(1, 0)
  1512. };
  1513. // face 1
  1514. facesw[face * 3 + 0] = v[0];
  1515. facesw[face * 3 + 1] = v[2];
  1516. facesw[face * 3 + 2] = v[1];
  1517. uvsw[face * 3 + 0] = u[0];
  1518. uvsw[face * 3 + 1] = u[2];
  1519. uvsw[face * 3 + 2] = u[1];
  1520. smoothw[face] = smooth_faces;
  1521. invertw[face] = invert_val;
  1522. materialsw[face] = material;
  1523. face++;
  1524. // face 2
  1525. facesw[face * 3 + 0] = v[2];
  1526. facesw[face * 3 + 1] = v[0];
  1527. facesw[face * 3 + 2] = v[3];
  1528. uvsw[face * 3 + 0] = u[2];
  1529. uvsw[face * 3 + 1] = u[0];
  1530. uvsw[face * 3 + 2] = u[3];
  1531. smoothw[face] = smooth_faces;
  1532. invertw[face] = invert_val;
  1533. materialsw[face] = material;
  1534. face++;
  1535. }
  1536. if (i == 0 && spin_degrees < 360) {
  1537. for (int j = 0; j < triangles.size(); j += 3) {
  1538. for (int k = 0; k < 3; k++) {
  1539. int src[3] = { 0, 2, 1 };
  1540. Vector2 p = final_polygon[triangles[j + src[k]]];
  1541. Vector3 v = Vector3(p.x, p.y, 0);
  1542. facesw[face * 3 + k] = v;
  1543. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1544. }
  1545. smoothw[face] = false;
  1546. materialsw[face] = material;
  1547. invertw[face] = invert_val;
  1548. face++;
  1549. }
  1550. }
  1551. if (i == spin_sides - 1 && spin_degrees < 360) {
  1552. for (int j = 0; j < triangles.size(); j += 3) {
  1553. for (int k = 0; k < 3; k++) {
  1554. int src[3] = { 0, 1, 2 };
  1555. Vector2 p = final_polygon[triangles[j + src[k]]];
  1556. Vector3 v = Vector3(normali_n.x * p.x, p.y, normali_n.z * p.x);
  1557. facesw[face * 3 + k] = v;
  1558. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1559. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1560. }
  1561. smoothw[face] = false;
  1562. materialsw[face] = material;
  1563. invertw[face] = invert_val;
  1564. face++;
  1565. }
  1566. }
  1567. }
  1568. } break;
  1569. case MODE_PATH: {
  1570. float bl = curve->get_baked_length();
  1571. int splits = MAX(2, Math::ceil(bl / path_interval));
  1572. float u1 = 0.0;
  1573. float u2 = path_continuous_u ? 0.0 : 1.0;
  1574. Transform path_to_this;
  1575. if (!path_local) {
  1576. // center on paths origin
  1577. path_to_this = get_global_transform().affine_inverse() * path->get_global_transform();
  1578. }
  1579. Transform prev_xf;
  1580. Vector3 lookat_dir;
  1581. if (path_rotation == PATH_ROTATION_POLYGON) {
  1582. lookat_dir = (path->get_global_transform().affine_inverse() * get_global_transform()).xform(Vector3(0, 0, -1));
  1583. } else {
  1584. Vector3 p1, p2;
  1585. p1 = curve->interpolate_baked(0);
  1586. p2 = curve->interpolate_baked(0.1);
  1587. lookat_dir = (p2 - p1).normalized();
  1588. }
  1589. for (int i = 0; i <= splits; i++) {
  1590. float ofs = i * path_interval;
  1591. if (i == splits && path_joined) {
  1592. ofs = 0.0;
  1593. }
  1594. Transform xf;
  1595. xf.origin = curve->interpolate_baked(ofs);
  1596. Vector3 local_dir;
  1597. if (path_rotation == PATH_ROTATION_PATH_FOLLOW && ofs > 0) {
  1598. //before end
  1599. Vector3 p1 = curve->interpolate_baked(ofs - 0.1);
  1600. Vector3 p2 = curve->interpolate_baked(ofs);
  1601. local_dir = (p2 - p1).normalized();
  1602. } else {
  1603. local_dir = lookat_dir;
  1604. }
  1605. xf = xf.looking_at(xf.origin + local_dir, Vector3(0, 1, 0));
  1606. Basis rot(Vector3(0, 0, 1), curve->interpolate_baked_tilt(ofs));
  1607. xf = xf * rot; //post mult
  1608. xf = path_to_this * xf;
  1609. if (i > 0) {
  1610. if (path_continuous_u) {
  1611. u1 = u2;
  1612. u2 += (prev_xf.origin - xf.origin).length();
  1613. };
  1614. //put triangles where they belong
  1615. //add triangles for depth
  1616. for (int j = 0; j < final_polygon.size(); j++) {
  1617. int j_n = (j + 1) % final_polygon.size();
  1618. Vector3 v[4] = {
  1619. prev_xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1620. prev_xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1621. xf.xform(Vector3(final_polygon[j_n].x, final_polygon[j_n].y, 0)),
  1622. xf.xform(Vector3(final_polygon[j].x, final_polygon[j].y, 0)),
  1623. };
  1624. Vector2 u[4] = {
  1625. Vector2(u1, 1),
  1626. Vector2(u1, 0),
  1627. Vector2(u2, 0),
  1628. Vector2(u2, 1)
  1629. };
  1630. // face 1
  1631. facesw[face * 3 + 0] = v[0];
  1632. facesw[face * 3 + 1] = v[1];
  1633. facesw[face * 3 + 2] = v[2];
  1634. uvsw[face * 3 + 0] = u[0];
  1635. uvsw[face * 3 + 1] = u[1];
  1636. uvsw[face * 3 + 2] = u[2];
  1637. smoothw[face] = smooth_faces;
  1638. invertw[face] = invert_val;
  1639. materialsw[face] = material;
  1640. face++;
  1641. // face 2
  1642. facesw[face * 3 + 0] = v[2];
  1643. facesw[face * 3 + 1] = v[3];
  1644. facesw[face * 3 + 2] = v[0];
  1645. uvsw[face * 3 + 0] = u[2];
  1646. uvsw[face * 3 + 1] = u[3];
  1647. uvsw[face * 3 + 2] = u[0];
  1648. smoothw[face] = smooth_faces;
  1649. invertw[face] = invert_val;
  1650. materialsw[face] = material;
  1651. face++;
  1652. }
  1653. }
  1654. if (i == 0 && !path_joined) {
  1655. for (int j = 0; j < triangles.size(); j += 3) {
  1656. for (int k = 0; k < 3; k++) {
  1657. int src[3] = { 0, 1, 2 };
  1658. Vector2 p = final_polygon[triangles[j + src[k]]];
  1659. Vector3 v = Vector3(p.x, p.y, 0);
  1660. facesw[face * 3 + k] = xf.xform(v);
  1661. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1662. }
  1663. smoothw[face] = false;
  1664. materialsw[face] = material;
  1665. invertw[face] = invert_val;
  1666. face++;
  1667. }
  1668. }
  1669. if (i == splits && !path_joined) {
  1670. for (int j = 0; j < triangles.size(); j += 3) {
  1671. for (int k = 0; k < 3; k++) {
  1672. int src[3] = { 0, 2, 1 };
  1673. Vector2 p = final_polygon[triangles[j + src[k]]];
  1674. Vector3 v = Vector3(p.x, p.y, 0);
  1675. facesw[face * 3 + k] = xf.xform(v);
  1676. uvsw[face * 3 + k] = (p - final_polygon_min) / final_polygon_size;
  1677. uvsw[face * 3 + k].x = 1.0 - uvsw[face * 3 + k].x; /* flip x */
  1678. }
  1679. smoothw[face] = false;
  1680. materialsw[face] = material;
  1681. invertw[face] = invert_val;
  1682. face++;
  1683. }
  1684. }
  1685. prev_xf = xf;
  1686. }
  1687. } break;
  1688. }
  1689. if (face != face_count) {
  1690. ERR_PRINT("Face mismatch bug! fix code");
  1691. }
  1692. for (int i = 0; i < face_count * 3; i++) {
  1693. if (i == 0) {
  1694. aabb.position = facesw[i];
  1695. } else {
  1696. aabb.expand_to(facesw[i]);
  1697. }
  1698. // invert UVs on the Y-axis OpenGL = upside down
  1699. uvsw[i].y = 1.0 - uvsw[i].y;
  1700. }
  1701. }
  1702. brush->build_from_faces(faces, uvs, smooth, materials, invert);
  1703. return brush;
  1704. }
  1705. void CSGPolygon::_notification(int p_what) {
  1706. if (p_what == NOTIFICATION_EXIT_TREE) {
  1707. if (path_cache) {
  1708. path_cache->disconnect("tree_exited", this, "_path_exited");
  1709. path_cache->disconnect("curve_changed", this, "_path_changed");
  1710. path_cache = NULL;
  1711. }
  1712. }
  1713. }
  1714. void CSGPolygon::_validate_property(PropertyInfo &property) const {
  1715. if (property.name.begins_with("spin") && mode != MODE_SPIN) {
  1716. property.usage = 0;
  1717. }
  1718. if (property.name.begins_with("path") && mode != MODE_PATH) {
  1719. property.usage = 0;
  1720. }
  1721. if (property.name == "depth" && mode != MODE_DEPTH) {
  1722. property.usage = 0;
  1723. }
  1724. CSGShape::_validate_property(property);
  1725. }
  1726. void CSGPolygon::_path_changed() {
  1727. _make_dirty();
  1728. update_gizmo();
  1729. }
  1730. void CSGPolygon::_path_exited() {
  1731. path_cache = NULL;
  1732. }
  1733. void CSGPolygon::_bind_methods() {
  1734. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CSGPolygon::set_polygon);
  1735. ClassDB::bind_method(D_METHOD("get_polygon"), &CSGPolygon::get_polygon);
  1736. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &CSGPolygon::set_mode);
  1737. ClassDB::bind_method(D_METHOD("get_mode"), &CSGPolygon::get_mode);
  1738. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CSGPolygon::set_depth);
  1739. ClassDB::bind_method(D_METHOD("get_depth"), &CSGPolygon::get_depth);
  1740. ClassDB::bind_method(D_METHOD("set_spin_degrees", "degrees"), &CSGPolygon::set_spin_degrees);
  1741. ClassDB::bind_method(D_METHOD("get_spin_degrees"), &CSGPolygon::get_spin_degrees);
  1742. ClassDB::bind_method(D_METHOD("set_spin_sides", "spin_sides"), &CSGPolygon::set_spin_sides);
  1743. ClassDB::bind_method(D_METHOD("get_spin_sides"), &CSGPolygon::get_spin_sides);
  1744. ClassDB::bind_method(D_METHOD("set_path_node", "path"), &CSGPolygon::set_path_node);
  1745. ClassDB::bind_method(D_METHOD("get_path_node"), &CSGPolygon::get_path_node);
  1746. ClassDB::bind_method(D_METHOD("set_path_interval", "distance"), &CSGPolygon::set_path_interval);
  1747. ClassDB::bind_method(D_METHOD("get_path_interval"), &CSGPolygon::get_path_interval);
  1748. ClassDB::bind_method(D_METHOD("set_path_rotation", "mode"), &CSGPolygon::set_path_rotation);
  1749. ClassDB::bind_method(D_METHOD("get_path_rotation"), &CSGPolygon::get_path_rotation);
  1750. ClassDB::bind_method(D_METHOD("set_path_local", "enable"), &CSGPolygon::set_path_local);
  1751. ClassDB::bind_method(D_METHOD("is_path_local"), &CSGPolygon::is_path_local);
  1752. ClassDB::bind_method(D_METHOD("set_path_continuous_u", "enable"), &CSGPolygon::set_path_continuous_u);
  1753. ClassDB::bind_method(D_METHOD("is_path_continuous_u"), &CSGPolygon::is_path_continuous_u);
  1754. ClassDB::bind_method(D_METHOD("set_path_joined", "enable"), &CSGPolygon::set_path_joined);
  1755. ClassDB::bind_method(D_METHOD("is_path_joined"), &CSGPolygon::is_path_joined);
  1756. ClassDB::bind_method(D_METHOD("set_material", "material"), &CSGPolygon::set_material);
  1757. ClassDB::bind_method(D_METHOD("get_material"), &CSGPolygon::get_material);
  1758. ClassDB::bind_method(D_METHOD("set_smooth_faces", "smooth_faces"), &CSGPolygon::set_smooth_faces);
  1759. ClassDB::bind_method(D_METHOD("get_smooth_faces"), &CSGPolygon::get_smooth_faces);
  1760. ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CSGPolygon::_is_editable_3d_polygon);
  1761. ClassDB::bind_method(D_METHOD("_has_editable_3d_polygon_no_depth"), &CSGPolygon::_has_editable_3d_polygon_no_depth);
  1762. ClassDB::bind_method(D_METHOD("_path_exited"), &CSGPolygon::_path_exited);
  1763. ClassDB::bind_method(D_METHOD("_path_changed"), &CSGPolygon::_path_changed);
  1764. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  1765. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Depth,Spin,Path"), "set_mode", "get_mode");
  1766. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth", PROPERTY_HINT_EXP_RANGE, "0.001,1000.0,0.001,or_greater"), "set_depth", "get_depth");
  1767. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spin_degrees", PROPERTY_HINT_RANGE, "1,360,0.1"), "set_spin_degrees", "get_spin_degrees");
  1768. ADD_PROPERTY(PropertyInfo(Variant::INT, "spin_sides", PROPERTY_HINT_RANGE, "3,64,1"), "set_spin_sides", "get_spin_sides");
  1769. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "path_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path"), "set_path_node", "get_path_node");
  1770. 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");
  1771. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_rotation", PROPERTY_HINT_ENUM, "Polygon,Path,PathFollow"), "set_path_rotation", "get_path_rotation");
  1772. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_local"), "set_path_local", "is_path_local");
  1773. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_continuous_u"), "set_path_continuous_u", "is_path_continuous_u");
  1774. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "path_joined"), "set_path_joined", "is_path_joined");
  1775. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_faces"), "set_smooth_faces", "get_smooth_faces");
  1776. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
  1777. BIND_ENUM_CONSTANT(MODE_DEPTH);
  1778. BIND_ENUM_CONSTANT(MODE_SPIN);
  1779. BIND_ENUM_CONSTANT(MODE_PATH);
  1780. BIND_ENUM_CONSTANT(PATH_ROTATION_POLYGON);
  1781. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH);
  1782. BIND_ENUM_CONSTANT(PATH_ROTATION_PATH_FOLLOW);
  1783. }
  1784. void CSGPolygon::set_polygon(const Vector<Vector2> &p_polygon) {
  1785. polygon = p_polygon;
  1786. _make_dirty();
  1787. update_gizmo();
  1788. }
  1789. Vector<Vector2> CSGPolygon::get_polygon() const {
  1790. return polygon;
  1791. }
  1792. void CSGPolygon::set_mode(Mode p_mode) {
  1793. mode = p_mode;
  1794. _make_dirty();
  1795. update_gizmo();
  1796. _change_notify();
  1797. }
  1798. CSGPolygon::Mode CSGPolygon::get_mode() const {
  1799. return mode;
  1800. }
  1801. void CSGPolygon::set_depth(const float p_depth) {
  1802. ERR_FAIL_COND(p_depth < 0.001);
  1803. depth = p_depth;
  1804. _make_dirty();
  1805. update_gizmo();
  1806. }
  1807. float CSGPolygon::get_depth() const {
  1808. return depth;
  1809. }
  1810. void CSGPolygon::set_path_continuous_u(bool p_enable) {
  1811. path_continuous_u = p_enable;
  1812. _make_dirty();
  1813. }
  1814. bool CSGPolygon::is_path_continuous_u() const {
  1815. return path_continuous_u;
  1816. }
  1817. void CSGPolygon::set_spin_degrees(const float p_spin_degrees) {
  1818. ERR_FAIL_COND(p_spin_degrees < 0.01 || p_spin_degrees > 360);
  1819. spin_degrees = p_spin_degrees;
  1820. _make_dirty();
  1821. update_gizmo();
  1822. }
  1823. float CSGPolygon::get_spin_degrees() const {
  1824. return spin_degrees;
  1825. }
  1826. void CSGPolygon::set_spin_sides(const int p_spin_sides) {
  1827. ERR_FAIL_COND(p_spin_sides < 3);
  1828. spin_sides = p_spin_sides;
  1829. _make_dirty();
  1830. update_gizmo();
  1831. }
  1832. int CSGPolygon::get_spin_sides() const {
  1833. return spin_sides;
  1834. }
  1835. void CSGPolygon::set_path_node(const NodePath &p_path) {
  1836. path_node = p_path;
  1837. _make_dirty();
  1838. update_gizmo();
  1839. }
  1840. NodePath CSGPolygon::get_path_node() const {
  1841. return path_node;
  1842. }
  1843. void CSGPolygon::set_path_interval(float p_interval) {
  1844. ERR_FAIL_COND(p_interval < 0.001);
  1845. path_interval = p_interval;
  1846. _make_dirty();
  1847. update_gizmo();
  1848. }
  1849. float CSGPolygon::get_path_interval() const {
  1850. return path_interval;
  1851. }
  1852. void CSGPolygon::set_path_rotation(PathRotation p_rotation) {
  1853. path_rotation = p_rotation;
  1854. _make_dirty();
  1855. update_gizmo();
  1856. }
  1857. CSGPolygon::PathRotation CSGPolygon::get_path_rotation() const {
  1858. return path_rotation;
  1859. }
  1860. void CSGPolygon::set_path_local(bool p_enable) {
  1861. path_local = p_enable;
  1862. _make_dirty();
  1863. update_gizmo();
  1864. }
  1865. bool CSGPolygon::is_path_local() const {
  1866. return path_local;
  1867. }
  1868. void CSGPolygon::set_path_joined(bool p_enable) {
  1869. path_joined = p_enable;
  1870. _make_dirty();
  1871. update_gizmo();
  1872. }
  1873. bool CSGPolygon::is_path_joined() const {
  1874. return path_joined;
  1875. }
  1876. void CSGPolygon::set_smooth_faces(const bool p_smooth_faces) {
  1877. smooth_faces = p_smooth_faces;
  1878. _make_dirty();
  1879. }
  1880. bool CSGPolygon::get_smooth_faces() const {
  1881. return smooth_faces;
  1882. }
  1883. void CSGPolygon::set_material(const Ref<Material> &p_material) {
  1884. material = p_material;
  1885. _make_dirty();
  1886. }
  1887. Ref<Material> CSGPolygon::get_material() const {
  1888. return material;
  1889. }
  1890. bool CSGPolygon::_is_editable_3d_polygon() const {
  1891. return true;
  1892. }
  1893. bool CSGPolygon::_has_editable_3d_polygon_no_depth() const {
  1894. return true;
  1895. }
  1896. CSGPolygon::CSGPolygon() {
  1897. // defaults
  1898. mode = MODE_DEPTH;
  1899. polygon.push_back(Vector2(0, 0));
  1900. polygon.push_back(Vector2(0, 1));
  1901. polygon.push_back(Vector2(1, 1));
  1902. polygon.push_back(Vector2(1, 0));
  1903. depth = 1.0;
  1904. spin_degrees = 360;
  1905. spin_sides = 8;
  1906. smooth_faces = false;
  1907. path_interval = 1;
  1908. path_rotation = PATH_ROTATION_PATH;
  1909. path_local = false;
  1910. path_continuous_u = false;
  1911. path_joined = false;
  1912. path_cache = NULL;
  1913. }