csg_shape.cpp 63 KB

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