csg_shape.cpp 59 KB

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