spatial_editor_gizmos.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. /*************************************************************************/
  2. /* spatial_editor_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "spatial_editor_gizmos.h"
  30. #include "geometry.h"
  31. #include "scene/3d/camera.h"
  32. #include "scene/resources/surface_tool.h"
  33. #include "scene/resources/sphere_shape.h"
  34. #include "scene/resources/box_shape.h"
  35. #include "scene/resources/capsule_shape.h"
  36. #include "scene/resources/ray_shape.h"
  37. #include "scene/resources/convex_polygon_shape.h"
  38. #include "scene/resources/plane_shape.h"
  39. #include "editor_shape_gizmos.h"
  40. // Keep small children away from this file.
  41. // It's so ugly it will eat them alive
  42. #define HANDLE_HALF_SIZE 0.05
  43. void SpatialGizmoTool::clear() {
  44. for(int i=0;i<instances.size();i++) {
  45. if (instances[i].instance.is_valid())
  46. VS::get_singleton()->free(instances[i].instance);
  47. }
  48. billboard_handle=false;
  49. collision_segments.clear();
  50. collision_mesh=Ref<TriangleMesh>();
  51. instances.clear();
  52. handles.clear();
  53. secondary_handles.clear();
  54. }
  55. void SpatialGizmoTool::Instance::create_instance(Spatial *p_base) {
  56. instance = VS::get_singleton()->instance_create2(mesh->get_rid(),p_base->get_world()->get_scenario());
  57. VS::get_singleton()->instance_attach_object_instance_ID(instance,p_base->get_instance_ID());
  58. if (billboard)
  59. VS::get_singleton()->instance_geometry_set_flag(instance,VS::INSTANCE_FLAG_BILLBOARD,true);
  60. if (unscaled)
  61. VS::get_singleton()->instance_geometry_set_flag(instance,VS::INSTANCE_FLAG_DEPH_SCALE,true);
  62. if (skeleton.is_valid())
  63. VS::get_singleton()->instance_attach_skeleton(instance,skeleton);
  64. if (extra_margin)
  65. VS::get_singleton()->instance_set_extra_visibility_margin(instance,1);
  66. VS::get_singleton()->instance_geometry_set_flag(instance,VS::INSTANCE_FLAG_CAST_SHADOW,false);
  67. VS::get_singleton()->instance_geometry_set_flag(instance,VS::INSTANCE_FLAG_RECEIVE_SHADOWS,false);
  68. }
  69. void SpatialGizmoTool::add_mesh(const Ref<Mesh>& p_mesh,bool p_billboard, const RID &p_skeleton) {
  70. ERR_FAIL_COND(!spatial_node);
  71. Instance ins;
  72. ins.billboard=p_billboard;
  73. ins.mesh=p_mesh;
  74. ins.skeleton=p_skeleton;
  75. if (valid) {
  76. ins.create_instance(spatial_node);
  77. VS::get_singleton()->instance_set_transform(ins.instance,spatial_node->get_global_transform());
  78. }
  79. instances.push_back(ins);
  80. }
  81. void SpatialGizmoTool::add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material,bool p_billboard){
  82. ERR_FAIL_COND(!spatial_node);
  83. Instance ins;
  84. Ref<Mesh> mesh = memnew( Mesh );
  85. Array a;
  86. a.resize(Mesh::ARRAY_MAX);
  87. a[Mesh::ARRAY_VERTEX]=p_lines;
  88. DVector<Color> color;
  89. color.resize(p_lines.size());
  90. {
  91. DVector<Color>::Write w = color.write();
  92. for(int i=0;i<p_lines.size();i++) {
  93. if (is_selected())
  94. w[i]=Color(1,1,1,0.6);
  95. else
  96. w[i]=Color(1,1,1,0.25);
  97. }
  98. }
  99. a[Mesh::ARRAY_COLOR]=color;
  100. mesh->add_surface(Mesh::PRIMITIVE_LINES,a);
  101. mesh->surface_set_material(0,p_material);
  102. if (p_billboard) {
  103. float md=0;
  104. for(int i=0;i<p_lines.size();i++) {
  105. md=MAX(0,p_lines[i].length());
  106. }
  107. if (md) {
  108. mesh->set_custom_aabb(AABB(Vector3(-md,-md,-md),Vector3(md,md,md)*2.0));
  109. }
  110. }
  111. ins.billboard=p_billboard;
  112. ins.mesh=mesh;
  113. if (valid) {
  114. ins.create_instance(spatial_node);
  115. VS::get_singleton()->instance_set_transform(ins.instance,spatial_node->get_global_transform());
  116. }
  117. instances.push_back(ins);
  118. }
  119. void SpatialGizmoTool::add_unscaled_billboard(const Ref<Material>& p_material,float p_scale) {
  120. ERR_FAIL_COND(!spatial_node);
  121. Instance ins;
  122. Vector<Vector3 > vs;
  123. Vector<Vector2 > uv;
  124. vs.push_back(Vector3(-p_scale,p_scale,0));
  125. vs.push_back(Vector3(p_scale,p_scale,0));
  126. vs.push_back(Vector3(p_scale,-p_scale,0));
  127. vs.push_back(Vector3(-p_scale,-p_scale,0));
  128. uv.push_back(Vector2(1,0));
  129. uv.push_back(Vector2(0,0));
  130. uv.push_back(Vector2(0,1));
  131. uv.push_back(Vector2(1,1));
  132. Ref<Mesh> mesh = memnew( Mesh );
  133. Array a;
  134. a.resize(Mesh::ARRAY_MAX);
  135. a[Mesh::ARRAY_VERTEX]=vs;
  136. a[Mesh::ARRAY_TEX_UV]=uv;
  137. mesh->add_surface(Mesh::PRIMITIVE_TRIANGLE_FAN,a);
  138. mesh->surface_set_material(0,p_material);
  139. if (true) {
  140. float md=0;
  141. for(int i=0;i<vs.size();i++) {
  142. md=MAX(0,vs[i].length());
  143. }
  144. if (md) {
  145. mesh->set_custom_aabb(AABB(Vector3(-md,-md,-md),Vector3(md,md,md)*2.0));
  146. }
  147. }
  148. ins.mesh=mesh;
  149. ins.unscaled=true;
  150. ins.billboard=true;
  151. if (valid) {
  152. ins.create_instance(spatial_node);
  153. VS::get_singleton()->instance_set_transform(ins.instance,spatial_node->get_global_transform());
  154. }
  155. instances.push_back(ins);
  156. }
  157. void SpatialGizmoTool::add_collision_triangles(const Ref<TriangleMesh>& p_tmesh) {
  158. collision_mesh=p_tmesh;
  159. }
  160. void SpatialGizmoTool::add_collision_segments(const Vector<Vector3> &p_lines) {
  161. int from=collision_segments.size();
  162. collision_segments.resize(from+p_lines.size());
  163. for(int i=0;i<p_lines.size();i++) {
  164. collision_segments[from+i]=p_lines[i];
  165. }
  166. }
  167. void SpatialGizmoTool::add_handles(const Vector<Vector3> &p_handles, bool p_billboard,bool p_secondary){
  168. billboard_handle=p_billboard;
  169. if (!is_selected())
  170. return;
  171. ERR_FAIL_COND(!spatial_node);
  172. ERR_FAIL_COND(!spatial_node);
  173. Instance ins;
  174. Ref<Mesh> mesh = memnew( Mesh );
  175. #if 1
  176. Array a;
  177. a.resize(VS::ARRAY_MAX);
  178. a[VS::ARRAY_VERTEX]=p_handles;
  179. DVector<Color> colors;
  180. {
  181. colors.resize(p_handles.size());
  182. DVector<Color>::Write w=colors.write();
  183. for(int i=0;i<p_handles.size();i++) {
  184. Color col(1,1,1,1);
  185. if (SpatialEditor::get_singleton()->get_over_gizmo_handle()!=i)
  186. col=Color(0.9,0.9,0.9,0.9);
  187. w[i]=col;
  188. }
  189. }
  190. a[VS::ARRAY_COLOR]=colors;
  191. mesh->add_surface(Mesh::PRIMITIVE_POINTS,a);
  192. mesh->surface_set_material(0,SpatialEditorGizmos::singleton->handle2_material);
  193. if (p_billboard) {
  194. float md=0;
  195. for(int i=0;i<p_handles.size();i++) {
  196. md=MAX(0,p_handles[i].length());
  197. }
  198. if (md) {
  199. mesh->set_custom_aabb(AABB(Vector3(-md,-md,-md),Vector3(md,md,md)*2.0));
  200. }
  201. }
  202. #else
  203. for(int ih=0;ih<p_handles.size();ih++) {
  204. Vector<Vector3> vertices;
  205. Vector<Vector3> normals;
  206. int vtx_idx=0;
  207. #define ADD_VTX(m_idx);\
  208. vertices.push_back( (face_points[m_idx]*HANDLE_HALF_SIZE+p_handles[ih]) );\
  209. normals.push_back( normal_points[m_idx] );\
  210. vtx_idx++;\
  211. for (int i=0;i<6;i++) {
  212. Vector3 face_points[4];
  213. Vector3 normal_points[4];
  214. float uv_points[8]={0,0,0,1,1,1,1,0};
  215. for (int j=0;j<4;j++) {
  216. float v[3];
  217. v[0]=1.0;
  218. v[1]=1-2*((j>>1)&1);
  219. v[2]=v[1]*(1-2*(j&1));
  220. for (int k=0;k<3;k++) {
  221. if (i<3)
  222. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  223. else
  224. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  225. }
  226. normal_points[j]=Vector3();
  227. normal_points[j][i%3]=(i>=3?-1:1);
  228. }
  229. //tri 1
  230. ADD_VTX(0);
  231. ADD_VTX(1);
  232. ADD_VTX(2);
  233. //tri 2
  234. ADD_VTX(2);
  235. ADD_VTX(3);
  236. ADD_VTX(0);
  237. }
  238. Array d;
  239. d.resize(VS::ARRAY_MAX);
  240. d[VisualServer::ARRAY_NORMAL]= normals ;
  241. d[VisualServer::ARRAY_VERTEX]= vertices ;
  242. mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES,d);
  243. mesh->surface_set_material(ih,SpatialEditorGizmos::singleton->handle_material);
  244. }
  245. #endif
  246. ins.mesh=mesh;
  247. ins.billboard=p_billboard;
  248. ins.extra_margin=true;
  249. if (valid) {
  250. ins.create_instance(spatial_node);
  251. VS::get_singleton()->instance_set_transform(ins.instance,spatial_node->get_global_transform());
  252. }
  253. instances.push_back(ins);
  254. if (!p_secondary) {
  255. int chs=handles.size();
  256. handles.resize(chs+p_handles.size());
  257. for(int i=0;i<p_handles.size();i++) {
  258. handles[i+chs]=p_handles[i];
  259. }
  260. } else {
  261. int chs=secondary_handles.size();
  262. secondary_handles.resize(chs+p_handles.size());
  263. for(int i=0;i<p_handles.size();i++) {
  264. secondary_handles[i+chs]=p_handles[i];
  265. }
  266. }
  267. }
  268. void SpatialGizmoTool::set_spatial_node(Spatial *p_node){
  269. spatial_node=p_node;
  270. }
  271. bool SpatialGizmoTool::intersect_frustum(const Camera *p_camera,const Vector<Plane> &p_frustum) {
  272. ERR_FAIL_COND_V(!spatial_node,false);
  273. ERR_FAIL_COND_V(!valid,false);
  274. if (collision_segments.size()) {
  275. const Plane *p=p_frustum.ptr();
  276. int fc=p_frustum.size();
  277. int vc=collision_segments.size();
  278. const Vector3* vptr=collision_segments.ptr();
  279. Transform t = spatial_node->get_global_transform();
  280. for(int i=0;i<vc/2;i++) {
  281. Vector3 a=t.xform(vptr[i*2+0]);
  282. Vector3 b=t.xform(vptr[i*2+1]);
  283. bool any_out=false;
  284. for(int j=0;j<fc;j++) {
  285. if (p[j].distance_to(a) > 0 && p[j].distance_to(b) >0) {
  286. any_out=true;
  287. break;
  288. }
  289. }
  290. if (!any_out)
  291. return true;
  292. }
  293. return false;
  294. }
  295. return false;
  296. }
  297. bool SpatialGizmoTool::intersect_ray(const Camera *p_camera,const Point2& p_point, Vector3& r_pos, Vector3& r_normal,int *r_gizmo_handle,bool p_sec_first) {
  298. ERR_FAIL_COND_V(!spatial_node,false);
  299. ERR_FAIL_COND_V(!valid,false);
  300. if (r_gizmo_handle) {
  301. Transform t = spatial_node->get_global_transform();
  302. t.orthonormalize();
  303. if (billboard_handle) {
  304. t.set_look_at(t.origin,t.origin+p_camera->get_transform().basis.get_axis(2),p_camera->get_transform().basis.get_axis(1));
  305. }
  306. Transform ti=t.affine_inverse();
  307. Vector3 ray_from=ti.xform(p_camera->project_ray_origin(p_point));
  308. Vector3 ray_dir=t.basis.xform_inv(p_camera->project_ray_normal(p_point)).normalized();
  309. Vector3 ray_to = ray_from+ray_dir*4096;
  310. float min_d=1e20;
  311. int idx=-1;
  312. for(int i=0;i<secondary_handles.size();i++) {
  313. #if 1
  314. Vector3 hpos = t.xform(secondary_handles[i]);
  315. Vector2 p = p_camera->unproject_position(hpos);
  316. if (p.distance_to(p_point)<SpatialEditorGizmos::singleton->handle_t->get_width()*0.6) {
  317. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  318. if (dp<min_d) {
  319. r_pos=t.xform(hpos);
  320. r_normal=p_camera->get_transform().basis.get_axis(2);
  321. min_d=dp;
  322. idx=i+handles.size();
  323. }
  324. }
  325. #else
  326. AABB aabb;
  327. aabb.pos=Vector3(-1,-1,-1)*HANDLE_HALF_SIZE;
  328. aabb.size=aabb.pos*-2;
  329. aabb.pos+=secondary_handles[i];
  330. Vector3 rpos,rnorm;
  331. if (aabb.intersects_segment(ray_from,ray_to,&rpos,&rnorm)) {
  332. real_t dp = ray_dir.dot(rpos);
  333. if (dp<min_d) {
  334. r_pos=t.xform(rpos);
  335. r_normal=ti.basis.xform_inv(rnorm).normalized();
  336. min_d=dp;
  337. idx=i+handles.size();
  338. }
  339. }
  340. #endif
  341. }
  342. if (p_sec_first && idx!=-1) {
  343. *r_gizmo_handle=idx;
  344. return true;
  345. }
  346. min_d=1e20;
  347. for(int i=0;i<handles.size();i++) {
  348. #if 1
  349. Vector3 hpos = t.xform(handles[i]);
  350. Vector2 p = p_camera->unproject_position(hpos);
  351. if (p.distance_to(p_point)<SpatialEditorGizmos::singleton->handle_t->get_width()*0.6) {
  352. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  353. if (dp<min_d) {
  354. r_pos=t.xform(hpos);
  355. r_normal=p_camera->get_transform().basis.get_axis(2);
  356. min_d=dp;
  357. idx=i;
  358. }
  359. }
  360. #else
  361. AABB aabb;
  362. aabb.pos=Vector3(-1,-1,-1)*HANDLE_HALF_SIZE;
  363. aabb.size=aabb.pos*-2;
  364. aabb.pos+=handles[i];
  365. Vector3 rpos,rnorm;
  366. if (aabb.intersects_segment(ray_from,ray_to,&rpos,&rnorm)) {
  367. real_t dp = ray_dir.dot(rpos);
  368. if (dp<min_d) {
  369. r_pos=t.xform(rpos);
  370. r_normal=ti.basis.xform_inv(rnorm).normalized();
  371. min_d=dp;
  372. idx=i;
  373. }
  374. }
  375. #endif
  376. }
  377. if (idx>=0) {
  378. *r_gizmo_handle=idx;
  379. return true;
  380. }
  381. }
  382. if (collision_segments.size()) {
  383. Plane camp(p_camera->get_transform().origin,(-p_camera->get_transform().basis.get_axis(2)).normalized());
  384. int vc=collision_segments.size();
  385. const Vector3* vptr=collision_segments.ptr();
  386. Transform t = spatial_node->get_global_transform();
  387. if (billboard_handle) {
  388. t.set_look_at(t.origin,t.origin+p_camera->get_transform().basis.get_axis(2),p_camera->get_transform().basis.get_axis(1));
  389. }
  390. Vector3 cp;
  391. float cpd=1e20;
  392. for(int i=0;i<vc/2;i++) {
  393. Vector3 a=t.xform(vptr[i*2+0]);
  394. Vector3 b=t.xform(vptr[i*2+1]);
  395. Vector2 s[2];
  396. s[0] = p_camera->unproject_position(a);
  397. s[1] = p_camera->unproject_position(b);
  398. Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point,s);
  399. float pd = p.distance_to(p_point);
  400. if (pd<cpd) {
  401. float d = s[0].distance_to(s[1]);
  402. Vector3 tcp;
  403. if (d>0) {
  404. float d2=s[0].distance_to(p)/d;
  405. tcp = a+(b-a)*d2;
  406. } else {
  407. tcp=a;
  408. }
  409. if (camp.distance_to(tcp)<p_camera->get_znear())
  410. continue;
  411. cp=tcp;
  412. cpd=pd;
  413. }
  414. }
  415. if (cpd<8) {
  416. r_pos=cp;
  417. r_normal=-p_camera->project_ray_normal(p_point);
  418. return true;
  419. }
  420. return false;
  421. }
  422. if (collision_mesh.is_valid()) {
  423. Transform gt = spatial_node->get_global_transform();
  424. if (billboard_handle) {
  425. gt.set_look_at(gt.origin,gt.origin+p_camera->get_transform().basis.get_axis(2),p_camera->get_transform().basis.get_axis(1));
  426. }
  427. Transform ai=gt.affine_inverse();
  428. Vector3 ray_from = ai.xform(p_camera->project_ray_origin(p_point));
  429. Vector3 ray_dir=ai.basis.xform(p_camera->project_ray_normal(p_point)).normalized();
  430. Vector3 rpos,rnorm;
  431. #if 1
  432. if (collision_mesh->intersect_ray(ray_from,ray_dir,rpos,rnorm)) {
  433. r_pos=gt.xform(rpos);
  434. r_normal=gt.basis.xform(rnorm).normalized();
  435. return true;
  436. }
  437. #else
  438. if (collision_mesh->intersect_segment(ray_from,ray_from+ray_dir*4906.0,rpos,rnorm)) {
  439. r_pos=gt.xform(rpos);
  440. r_normal=gt.basis.xform(rnorm).normalized();
  441. return true;
  442. }
  443. #endif
  444. }
  445. return false;
  446. }
  447. void SpatialGizmoTool::create() {
  448. ERR_FAIL_COND(!spatial_node);
  449. ERR_FAIL_COND(valid);
  450. valid=true;
  451. for(int i=0;i<instances.size();i++) {
  452. instances[i].create_instance(spatial_node);
  453. }
  454. transform();
  455. }
  456. void SpatialGizmoTool::transform(){
  457. ERR_FAIL_COND(!spatial_node);
  458. ERR_FAIL_COND(!valid);
  459. for(int i=0;i<instances.size();i++) {
  460. VS::get_singleton()->instance_set_transform(instances[i].instance,spatial_node->get_global_transform());
  461. }
  462. }
  463. void SpatialGizmoTool::free(){
  464. ERR_FAIL_COND(!spatial_node);
  465. ERR_FAIL_COND(!valid);
  466. for(int i=0;i<instances.size();i++) {
  467. if (instances[i].instance.is_valid())
  468. VS::get_singleton()->free(instances[i].instance);
  469. instances[i].instance=RID();
  470. }
  471. valid=false;
  472. }
  473. SpatialGizmoTool::SpatialGizmoTool() {
  474. valid=false;
  475. billboard_handle=false;
  476. }
  477. SpatialGizmoTool::~SpatialGizmoTool(){
  478. clear();
  479. }
  480. Vector3 SpatialGizmoTool::get_handle_pos(int p_idx) const {
  481. ERR_FAIL_INDEX_V(p_idx,handles.size(),Vector3());
  482. return handles[p_idx];
  483. }
  484. //// light gizmo
  485. String LightSpatialGizmo::get_handle_name(int p_idx) const {
  486. if (p_idx==0)
  487. return "Radius";
  488. else
  489. return "Aperture";
  490. }
  491. Variant LightSpatialGizmo::get_handle_value(int p_idx) const{
  492. if (p_idx==0)
  493. return light->get_parameter(Light::PARAM_RADIUS);
  494. if (p_idx==1)
  495. return light->get_parameter(Light::PARAM_SPOT_ANGLE);
  496. return Variant();
  497. }
  498. static float _find_closest_angle_to_half_pi_arc(const Vector3& p_from, const Vector3& p_to, float p_arc_radius,const Transform& p_arc_xform) {
  499. //bleh, discrete is simpler
  500. static const int arc_test_points=64;
  501. float min_d = 1e20;
  502. Vector3 min_p;
  503. for(int i=0;i<arc_test_points;i++) {
  504. float a = i*Math_PI*0.5/arc_test_points;
  505. float an = (i+1)*Math_PI*0.5/arc_test_points;
  506. Vector3 p=Vector3( Math::cos(a), 0, -Math::sin(a) )*p_arc_radius;
  507. Vector3 n=Vector3( Math::cos(an), 0,- Math::sin(an) )*p_arc_radius;
  508. Vector3 ra,rb;
  509. Geometry::get_closest_points_between_segments(p,n,p_from,p_to,ra,rb);
  510. float d = ra.distance_to(rb);
  511. if (d<min_d) {
  512. min_d=d;
  513. min_p=ra;
  514. }
  515. }
  516. //min_p = p_arc_xform.affine_inverse().xform(min_p);
  517. float a = Vector2(min_p.x,-min_p.z).atan2();
  518. return a*180.0/Math_PI;
  519. }
  520. void LightSpatialGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point) {
  521. Transform gt = light->get_global_transform();
  522. gt.orthonormalize();
  523. Transform gi = gt.affine_inverse();
  524. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  525. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  526. Vector3 s[2]={gi.xform(ray_from),gi.xform(ray_from+ray_dir*4096)};
  527. if (p_idx==0) {
  528. if (light->cast_to<SpotLight>()) {
  529. Vector3 ra,rb;
  530. Geometry::get_closest_points_between_segments(Vector3(),Vector3(0,0,-4096),s[0],s[1],ra,rb);
  531. float d = -ra.z;
  532. if (d<0)
  533. d=0;
  534. light->set_parameter(Light::PARAM_RADIUS,d);
  535. } else if (light->cast_to<OmniLight>()) {
  536. Plane cp=Plane( gt.origin, p_camera->get_transform().basis.get_axis(2));
  537. Vector3 inters;
  538. if (cp.intersects_ray(ray_from,ray_dir,&inters)) {
  539. float r = inters.distance_to(gt.origin);
  540. light->set_parameter(Light::PARAM_RADIUS,r);
  541. }
  542. }
  543. } else if (p_idx==1) {
  544. float a = _find_closest_angle_to_half_pi_arc(s[0],s[1],light->get_parameter(Light::PARAM_RADIUS),gt);
  545. light->set_parameter(Light::PARAM_SPOT_ANGLE,CLAMP(a,0.01,89.99));
  546. }
  547. }
  548. void LightSpatialGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  549. if (p_cancel) {
  550. light->set_parameter(p_idx==0?Light::PARAM_RADIUS:Light::PARAM_SPOT_ANGLE,p_restore);
  551. } else if (p_idx==0) {
  552. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  553. ur->create_action("Change Light Radius");
  554. ur->add_do_method(light,"set_parameter",Light::PARAM_RADIUS,light->get_parameter(Light::PARAM_RADIUS));
  555. ur->add_undo_method(light,"set_parameter",Light::PARAM_RADIUS,p_restore);
  556. ur->commit_action();
  557. } else if (p_idx==1) {
  558. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  559. ur->create_action("Change Light Radius");
  560. ur->add_do_method(light,"set_parameter",Light::PARAM_SPOT_ANGLE,light->get_parameter(Light::PARAM_SPOT_ANGLE));
  561. ur->add_undo_method(light,"set_parameter",Light::PARAM_SPOT_ANGLE,p_restore);
  562. ur->commit_action();
  563. }
  564. }
  565. void LightSpatialGizmo::redraw() {
  566. if (light->cast_to<DirectionalLight>()) {
  567. const int arrow_points=5;
  568. Vector3 arrow[arrow_points]={
  569. Vector3(0,0,2),
  570. Vector3(1,1,2),
  571. Vector3(1,1,-1),
  572. Vector3(2,2,-1),
  573. Vector3(0,0,-3)
  574. };
  575. int arrow_sides=4;
  576. Vector<Vector3> lines;
  577. for(int i = 0; i < arrow_sides ; i++) {
  578. Matrix3 ma(Vector3(0,0,1),Math_PI*2*float(i)/arrow_sides);
  579. Matrix3 mb(Vector3(0,0,1),Math_PI*2*float(i+1)/arrow_sides);
  580. for(int j=1;j<arrow_points-1;j++) {
  581. if (j!=2) {
  582. lines.push_back(ma.xform(arrow[j]));
  583. lines.push_back(ma.xform(arrow[j+1]));
  584. }
  585. if (j<arrow_points-1) {
  586. lines.push_back(ma.xform(arrow[j]));
  587. lines.push_back(mb.xform(arrow[j]));
  588. }
  589. }
  590. }
  591. add_lines(lines,SpatialEditorGizmos::singleton->light_material);
  592. add_collision_segments(lines);
  593. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_directional_icon,0.05);
  594. }
  595. if (light->cast_to<OmniLight>()) {
  596. clear();
  597. OmniLight *on = light->cast_to<OmniLight>();
  598. float r = on->get_parameter(Light::PARAM_RADIUS);
  599. Vector<Vector3> points;
  600. for(int i=0;i<=360;i++) {
  601. float ra=Math::deg2rad(i);
  602. float rb=Math::deg2rad(i+1);
  603. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  604. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  605. /*points.push_back(Vector3(a.x,0,a.y));
  606. points.push_back(Vector3(b.x,0,b.y));
  607. points.push_back(Vector3(0,a.x,a.y));
  608. points.push_back(Vector3(0,b.x,b.y));*/
  609. points.push_back(Vector3(a.x,a.y,0));
  610. points.push_back(Vector3(b.x,b.y,0));
  611. }
  612. add_lines(points,SpatialEditorGizmos::singleton->light_material,true);
  613. add_collision_segments(points);
  614. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_omni_icon,0.05);
  615. Vector<Vector3> handles;
  616. handles.push_back(Vector3(r,0,0));
  617. add_handles(handles,true);
  618. }
  619. if (light->cast_to<SpotLight>()) {
  620. clear();
  621. Vector<Vector3> points;
  622. SpotLight *on = light->cast_to<SpotLight>();
  623. float r = on->get_parameter(Light::PARAM_RADIUS);
  624. float w = r*Math::sin(Math::deg2rad(on->get_parameter(Light::PARAM_SPOT_ANGLE)));
  625. float d = r*Math::cos(Math::deg2rad(on->get_parameter(Light::PARAM_SPOT_ANGLE)));
  626. for(int i=0;i<360;i++) {
  627. float ra=Math::deg2rad(i);
  628. float rb=Math::deg2rad(i+1);
  629. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*w;
  630. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*w;
  631. /*points.push_back(Vector3(a.x,0,a.y));
  632. points.push_back(Vector3(b.x,0,b.y));
  633. points.push_back(Vector3(0,a.x,a.y));
  634. points.push_back(Vector3(0,b.x,b.y));*/
  635. points.push_back(Vector3(a.x,a.y,-d));
  636. points.push_back(Vector3(b.x,b.y,-d));
  637. if (i%90==0) {
  638. points.push_back(Vector3(a.x,a.y,-d));
  639. points.push_back(Vector3());
  640. }
  641. }
  642. points.push_back(Vector3(0,0,-r));
  643. points.push_back(Vector3());
  644. add_lines(points,SpatialEditorGizmos::singleton->light_material);
  645. Vector<Vector3> handles;
  646. handles.push_back(Vector3(0,0,-r));
  647. Vector<Vector3> collision_segments;
  648. for(int i=0;i<64;i++) {
  649. float ra=i*Math_PI*2.0/64.0;
  650. float rb=(i+1)*Math_PI*2.0/64.0;
  651. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*w;
  652. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*w;
  653. collision_segments.push_back(Vector3(a.x,a.y,-d));
  654. collision_segments.push_back(Vector3(b.x,b.y,-d));
  655. if (i%16==0) {
  656. collision_segments.push_back(Vector3(a.x,a.y,-d));
  657. collision_segments.push_back(Vector3());
  658. }
  659. if (i==16) {
  660. handles.push_back(Vector3(a.x,a.y,-d));
  661. }
  662. }
  663. collision_segments.push_back(Vector3(0,0,-r));
  664. collision_segments.push_back(Vector3());
  665. add_handles(handles);
  666. add_collision_segments(collision_segments);
  667. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_omni_icon,0.05);
  668. }
  669. }
  670. LightSpatialGizmo::LightSpatialGizmo(Light* p_light){
  671. light=p_light;
  672. set_spatial_node(p_light);
  673. }
  674. //////
  675. String CameraSpatialGizmo::get_handle_name(int p_idx) const {
  676. if (camera->get_projection()==Camera::PROJECTION_PERSPECTIVE) {
  677. return "FOV";
  678. } else {
  679. return "Size";
  680. }
  681. }
  682. Variant CameraSpatialGizmo::get_handle_value(int p_idx) const{
  683. if (camera->get_projection()==Camera::PROJECTION_PERSPECTIVE) {
  684. return camera->get_fov();
  685. } else {
  686. return camera->get_size();
  687. }
  688. }
  689. void CameraSpatialGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point){
  690. Transform gt = camera->get_global_transform();
  691. gt.orthonormalize();
  692. Transform gi = gt.affine_inverse();
  693. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  694. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  695. Vector3 s[2]={gi.xform(ray_from),gi.xform(ray_from+ray_dir*4096)};
  696. if (camera->get_projection()==Camera::PROJECTION_PERSPECTIVE) {
  697. Transform gt=camera->get_global_transform();
  698. float a = _find_closest_angle_to_half_pi_arc(s[0],s[1],1.0,gt);
  699. camera->set("fov",a);
  700. } else {
  701. Vector3 ra,rb;
  702. Geometry::get_closest_points_between_segments(Vector3(0,0,-1),Vector3(4096,0,-1),s[0],s[1],ra,rb);
  703. float d = ra.x * 2.0;
  704. if (d<0)
  705. d=0;
  706. camera->set("size",d);
  707. }
  708. }
  709. void CameraSpatialGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  710. if (camera->get_projection()==Camera::PROJECTION_PERSPECTIVE) {
  711. if (p_cancel) {
  712. camera->set("fov",p_restore);
  713. } else {
  714. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  715. ur->create_action("Change Camera FOV");
  716. ur->add_do_property(camera,"fov",camera->get_fov());
  717. ur->add_undo_property(camera,"fov",p_restore);
  718. ur->commit_action();
  719. }
  720. } else {
  721. if (p_cancel) {
  722. camera->set("size",p_restore);
  723. } else {
  724. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  725. ur->create_action("Change Camera Size");
  726. ur->add_do_property(camera,"size",camera->get_size());
  727. ur->add_undo_property(camera,"size",p_restore);
  728. ur->commit_action();
  729. }
  730. }
  731. }
  732. void CameraSpatialGizmo::redraw(){
  733. clear();
  734. Vector<Vector3> lines;
  735. Vector<Vector3> handles;
  736. switch(camera->get_projection()) {
  737. case Camera::PROJECTION_PERSPECTIVE: {
  738. float fov = camera->get_fov();
  739. Vector3 side=Vector3( Math::sin(Math::deg2rad(fov)), 0, -Math::cos(Math::deg2rad(fov)) );
  740. Vector3 nside=side;
  741. nside.x=-nside.x;
  742. Vector3 up=Vector3(0,side.x,0);
  743. #define ADD_TRIANGLE( m_a, m_b, m_c)\
  744. {\
  745. lines.push_back(m_a);\
  746. lines.push_back(m_b);\
  747. lines.push_back(m_b);\
  748. lines.push_back(m_c);\
  749. lines.push_back(m_c);\
  750. lines.push_back(m_a);\
  751. }
  752. ADD_TRIANGLE( Vector3(), side+up, side-up );
  753. ADD_TRIANGLE( Vector3(), nside+up, nside-up );
  754. ADD_TRIANGLE( Vector3(), side+up, nside+up );
  755. ADD_TRIANGLE( Vector3(), side-up, nside-up );
  756. handles.push_back(side);
  757. side.x*=0.25;
  758. nside.x*=0.25;
  759. Vector3 tup( 0, up.y*3/2,side.z);
  760. ADD_TRIANGLE( tup, side+up, nside+up );
  761. } break;
  762. case Camera::PROJECTION_ORTHOGONAL: {
  763. #define ADD_QUAD( m_a, m_b, m_c, m_d)\
  764. {\
  765. lines.push_back(m_a);\
  766. lines.push_back(m_b);\
  767. lines.push_back(m_b);\
  768. lines.push_back(m_c);\
  769. lines.push_back(m_c);\
  770. lines.push_back(m_d);\
  771. lines.push_back(m_d);\
  772. lines.push_back(m_a);\
  773. }
  774. float size = camera->get_size();
  775. float hsize=size*0.5;
  776. Vector3 right(hsize,0,0);
  777. Vector3 up(0,hsize,0);
  778. Vector3 back(0,0,-1.0);
  779. Vector3 front(0,0,0);
  780. ADD_QUAD( -up-right,-up+right,up+right,up-right);
  781. ADD_QUAD( -up-right+back,-up+right+back,up+right+back,up-right+back);
  782. ADD_QUAD( up+right,up+right+back,up-right+back,up-right);
  783. ADD_QUAD( -up+right,-up+right+back,-up-right+back,-up-right);
  784. handles.push_back(right+back);
  785. right.x*=0.25;
  786. Vector3 tup( 0, up.y*3/2,back.z );
  787. ADD_TRIANGLE( tup, right+up+back, -right+up+back );
  788. } break;
  789. }
  790. add_lines(lines,SpatialEditorGizmos::singleton->camera_material);
  791. add_collision_segments(lines);
  792. add_handles(handles);
  793. }
  794. CameraSpatialGizmo::CameraSpatialGizmo(Camera* p_camera){
  795. camera=p_camera;
  796. set_spatial_node(camera);
  797. }
  798. //////
  799. void MeshInstanceSpatialGizmo::redraw() {
  800. Ref<Mesh> m = mesh->get_mesh();
  801. if (!m.is_valid())
  802. return; //none
  803. Ref<TriangleMesh> tm = m->generate_triangle_mesh();
  804. if (tm.is_valid())
  805. add_collision_triangles(tm);
  806. }
  807. MeshInstanceSpatialGizmo::MeshInstanceSpatialGizmo(MeshInstance* p_mesh) {
  808. mesh=p_mesh;
  809. set_spatial_node(p_mesh);
  810. }
  811. /////
  812. void Position3DSpatialGizmo::redraw() {
  813. clear();
  814. add_mesh(SpatialEditorGizmos::singleton->pos3d_mesh);
  815. Vector<Vector3> cursor_points;
  816. float cs = 0.25;
  817. cursor_points.push_back(Vector3(+cs,0,0));
  818. cursor_points.push_back(Vector3(-cs,0,0));
  819. cursor_points.push_back(Vector3(0,+cs,0));
  820. cursor_points.push_back(Vector3(0,-cs,0));
  821. cursor_points.push_back(Vector3(0,0,+cs));
  822. cursor_points.push_back(Vector3(0,0,-cs));
  823. add_collision_segments(cursor_points);
  824. }
  825. Position3DSpatialGizmo::Position3DSpatialGizmo(Position3D* p_p3d) {
  826. p3d=p_p3d;
  827. set_spatial_node(p3d);
  828. }
  829. /////
  830. void SkeletonSpatialGizmo::redraw() {
  831. clear();
  832. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  833. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  834. surface_tool->set_material(SpatialEditorGizmos::singleton->skeleton_material);
  835. Vector<Transform> grests;
  836. grests.resize(skel->get_bone_count());
  837. Vector<int> bones;
  838. Vector<float> weights;
  839. bones.resize(4);
  840. weights.resize(4);
  841. for(int i=0;i<4;i++) {
  842. bones[i]=0;
  843. weights[i]=0;
  844. }
  845. weights[0]=1;
  846. for (int i=0;i<skel->get_bone_count();i++) {
  847. int parent = skel->get_bone_parent(i);
  848. if (parent>=0) {
  849. grests[i]=grests[parent] * skel->get_bone_rest(i);
  850. Vector3 v0 = grests[parent].origin;
  851. Vector3 v1 = grests[i].origin;
  852. bones[0]=parent;
  853. surface_tool->add_bones(bones);
  854. surface_tool->add_weights(weights);
  855. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  856. surface_tool->add_vertex(v0);
  857. bones[0]=i;
  858. surface_tool->add_bones(bones);
  859. surface_tool->add_weights(weights);
  860. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  861. surface_tool->add_vertex(v1);
  862. } else {
  863. grests[i]=skel->get_bone_rest(i);
  864. bones[0]=i;
  865. }
  866. Transform t = grests[i];
  867. t.orthonormalize();
  868. for (int i=0;i<6;i++) {
  869. Vector3 face_points[4];
  870. for (int j=0;j<4;j++) {
  871. float v[3];
  872. v[0]=1.0;
  873. v[1]=1-2*((j>>1)&1);
  874. v[2]=v[1]*(1-2*(j&1));
  875. for (int k=0;k<3;k++) {
  876. if (i<3)
  877. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  878. else
  879. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  880. }
  881. }
  882. for(int j=0;j<4;j++) {
  883. surface_tool->add_bones(bones);
  884. surface_tool->add_weights(weights);
  885. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  886. surface_tool->add_vertex(t.xform(face_points[j]*0.04));
  887. surface_tool->add_bones(bones);
  888. surface_tool->add_weights(weights);
  889. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  890. surface_tool->add_vertex(t.xform(face_points[(j+1)%4]*0.04));
  891. }
  892. }
  893. }
  894. Ref<Mesh> m = surface_tool->commit();
  895. add_mesh(m,false,skel->get_skeleton());
  896. }
  897. SkeletonSpatialGizmo::SkeletonSpatialGizmo(Skeleton* p_skel) {
  898. skel=p_skel;
  899. set_spatial_node(p_skel);
  900. }
  901. /////
  902. void SpatialPlayerSpatialGizmo::redraw() {
  903. clear();
  904. if (splayer->cast_to<SpatialStreamPlayer>()) {
  905. add_unscaled_billboard(SpatialEditorGizmos::singleton->stream_player_icon,0.05);
  906. } else if (splayer->cast_to<SpatialSamplePlayer>()) {
  907. add_unscaled_billboard(SpatialEditorGizmos::singleton->sample_player_icon,0.05);
  908. }
  909. }
  910. SpatialPlayerSpatialGizmo::SpatialPlayerSpatialGizmo(SpatialPlayer* p_splayer){
  911. set_spatial_node(p_splayer);
  912. splayer=p_splayer;
  913. }
  914. /////
  915. void RoomSpatialGizmo::redraw() {
  916. clear();
  917. Ref<RoomBounds> roomie = room->get_room();
  918. if (roomie.is_null())
  919. return;
  920. DVector<Face3> faces = roomie->get_geometry_hint();
  921. Vector<Vector3> lines;
  922. int fc=faces.size();
  923. DVector<Face3>::Read r =faces.read();
  924. Map<_EdgeKey,Vector3> edge_map;
  925. for(int i=0;i<fc;i++) {
  926. Vector3 fn = r[i].get_plane().normal;
  927. for(int j=0;j<3;j++) {
  928. _EdgeKey ek;
  929. ek.from=r[i].vertex[j].snapped(CMP_EPSILON);
  930. ek.to=r[i].vertex[(j+1)%3].snapped(CMP_EPSILON);
  931. if (ek.from<ek.to)
  932. SWAP(ek.from,ek.to);
  933. Map<_EdgeKey,Vector3>::Element *E=edge_map.find(ek);
  934. if (E) {
  935. if (E->get().dot(fn) >0.9) {
  936. E->get()=Vector3();
  937. }
  938. } else {
  939. edge_map[ek]=fn;
  940. }
  941. }
  942. }
  943. for(Map<_EdgeKey,Vector3>::Element *E=edge_map.front();E;E=E->next()) {
  944. if (E->get()!=Vector3()) {
  945. lines.push_back(E->key().from);
  946. lines.push_back(E->key().to);
  947. }
  948. }
  949. add_lines(lines,SpatialEditorGizmos::singleton->room_material);
  950. add_collision_segments(lines);
  951. }
  952. RoomSpatialGizmo::RoomSpatialGizmo(Room* p_room){
  953. set_spatial_node(p_room);
  954. room=p_room;
  955. }
  956. /////
  957. void PortalSpatialGizmo::redraw() {
  958. clear();
  959. Vector<Point2> points = portal->get_shape();
  960. if (points.size()==0) {
  961. return;
  962. }
  963. Vector<Vector3> lines;
  964. Vector3 center;
  965. for(int i=0;i<points.size();i++) {
  966. Vector3 f;
  967. f.x=points[i].x;
  968. f.y=points[i].y;
  969. Vector3 fn;
  970. fn.x=points[(i+1)%points.size()].x;
  971. fn.y=points[(i+1)%points.size()].y;
  972. center+=f;
  973. lines.push_back(f);
  974. lines.push_back(fn);
  975. }
  976. center/=points.size();
  977. lines.push_back(center);
  978. lines.push_back(center+Vector3(0,0,1));
  979. add_lines(lines,SpatialEditorGizmos::singleton->portal_material);
  980. add_collision_segments(lines);
  981. }
  982. PortalSpatialGizmo::PortalSpatialGizmo(Portal* p_portal){
  983. set_spatial_node(p_portal);
  984. portal=p_portal;
  985. }
  986. /////
  987. void RayCastSpatialGizmo::redraw() {
  988. clear();
  989. Vector<Vector3> lines;
  990. lines.push_back(Vector3());
  991. lines.push_back(raycast->get_cast_to());
  992. add_lines(lines,SpatialEditorGizmos::singleton->raycast_material);
  993. add_collision_segments(lines);
  994. }
  995. RayCastSpatialGizmo::RayCastSpatialGizmo(RayCast* p_raycast){
  996. set_spatial_node(p_raycast);
  997. raycast=p_raycast;
  998. }
  999. /////
  1000. void CarWheelSpatialGizmo::redraw() {
  1001. clear();
  1002. Vector<Vector3> points;
  1003. float r = car_wheel->get_radius();
  1004. const int skip=10;
  1005. for(int i=0;i<=360;i+=skip) {
  1006. float ra=Math::deg2rad(i);
  1007. float rb=Math::deg2rad(i+skip);
  1008. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  1009. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  1010. points.push_back(Vector3(0,a.x,a.y));
  1011. points.push_back(Vector3(0,b.x,b.y));
  1012. const int springsec=4;
  1013. for(int j=0;j<springsec;j++) {
  1014. float t = car_wheel->get_travel()*5;
  1015. points.push_back(Vector3(a.x,i/360.0*t/springsec+j*(t/springsec),a.y)*0.2);
  1016. points.push_back(Vector3(b.x,(i+skip)/360.0*t/springsec+j*(t/springsec),b.y)*0.2);
  1017. }
  1018. }
  1019. //travel
  1020. points.push_back(Vector3(0,0,0));
  1021. points.push_back(Vector3(0,car_wheel->get_travel(),0));
  1022. //axis
  1023. points.push_back(Vector3(r*0.2,car_wheel->get_travel(),0));
  1024. points.push_back(Vector3(-r*0.2,car_wheel->get_travel(),0));
  1025. //axis
  1026. points.push_back(Vector3(r*0.2,0,0));
  1027. points.push_back(Vector3(-r*0.2,0,0));
  1028. //forward line
  1029. points.push_back(Vector3(0,-r,0));
  1030. points.push_back(Vector3(0,-r,r*2));
  1031. points.push_back(Vector3(0,-r,r*2));
  1032. points.push_back(Vector3(r*2*0.2,-r,r*2*0.8));
  1033. points.push_back(Vector3(0,-r,r*2));
  1034. points.push_back(Vector3(-r*2*0.2,-r,r*2*0.8));
  1035. add_lines(points,SpatialEditorGizmos::singleton->car_wheel_material);
  1036. add_collision_segments(points);
  1037. }
  1038. CarWheelSpatialGizmo::CarWheelSpatialGizmo(CarWheel* p_car_wheel){
  1039. set_spatial_node(p_car_wheel);
  1040. car_wheel=p_car_wheel;
  1041. }
  1042. ///
  1043. void TestCubeSpatialGizmo::redraw() {
  1044. clear();
  1045. add_collision_triangles(SpatialEditorGizmos::singleton->test_cube_tm);
  1046. }
  1047. TestCubeSpatialGizmo::TestCubeSpatialGizmo(TestCube* p_tc) {
  1048. tc=p_tc;
  1049. set_spatial_node(p_tc);
  1050. }
  1051. ///////////
  1052. String CollisionShapeSpatialGizmo::get_handle_name(int p_idx) const {
  1053. Ref<Shape> s = cs->get_shape();
  1054. if (s.is_null())
  1055. return "";
  1056. if (s->cast_to<SphereShape>()) {
  1057. return "Radius";
  1058. }
  1059. if (s->cast_to<BoxShape>()) {
  1060. return "Extents";
  1061. }
  1062. if (s->cast_to<CapsuleShape>()) {
  1063. return p_idx==0?"Radius":"Height";
  1064. }
  1065. if (s->cast_to<RayShape>()) {
  1066. return "Length";
  1067. }
  1068. return "";
  1069. }
  1070. Variant CollisionShapeSpatialGizmo::get_handle_value(int p_idx) const{
  1071. Ref<Shape> s = cs->get_shape();
  1072. if (s.is_null())
  1073. return Variant();
  1074. if (s->cast_to<SphereShape>()) {
  1075. Ref<SphereShape> ss = s;
  1076. return ss->get_radius();
  1077. }
  1078. if (s->cast_to<BoxShape>()) {
  1079. Ref<BoxShape> bs = s;
  1080. return bs->get_extents();
  1081. }
  1082. if (s->cast_to<CapsuleShape>()) {
  1083. Ref<CapsuleShape> cs = s;
  1084. return p_idx==0?cs->get_radius():cs->get_height();
  1085. }
  1086. if (s->cast_to<RayShape>()) {
  1087. Ref<RayShape> cs = s;
  1088. return cs->get_length();
  1089. }
  1090. return Variant();
  1091. }
  1092. void CollisionShapeSpatialGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point){
  1093. Ref<Shape> s = cs->get_shape();
  1094. if (s.is_null())
  1095. return;
  1096. Transform gt = cs->get_global_transform();
  1097. gt.orthonormalize();
  1098. Transform gi = gt.affine_inverse();
  1099. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1100. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1101. Vector3 sg[2]={gi.xform(ray_from),gi.xform(ray_from+ray_dir*4096)};
  1102. if (s->cast_to<SphereShape>()) {
  1103. Ref<SphereShape> ss = s;
  1104. Vector3 ra,rb;
  1105. Geometry::get_closest_points_between_segments(Vector3(),Vector3(4096,0,0),sg[0],sg[1],ra,rb);
  1106. float d = ra.x;
  1107. if (d<0.001)
  1108. d=0.001;
  1109. ss->set_radius(d);
  1110. }
  1111. if (s->cast_to<RayShape>()) {
  1112. Ref<RayShape> rs = s;
  1113. Vector3 ra,rb;
  1114. Geometry::get_closest_points_between_segments(Vector3(),Vector3(0,0,4096),sg[0],sg[1],ra,rb);
  1115. float d = ra.z;
  1116. if (d<0.001)
  1117. d=0.001;
  1118. rs->set_length(d);
  1119. }
  1120. if (s->cast_to<BoxShape>()) {
  1121. Vector3 axis;
  1122. axis[p_idx]=1.0;
  1123. Ref<BoxShape> bs = s;
  1124. Vector3 ra,rb;
  1125. Geometry::get_closest_points_between_segments(Vector3(),axis*4096,sg[0],sg[1],ra,rb);
  1126. float d = ra[p_idx];
  1127. if (d<0.001)
  1128. d=0.001;
  1129. Vector3 he = bs->get_extents();
  1130. he[p_idx]=d;
  1131. bs->set_extents(he);
  1132. }
  1133. if (s->cast_to<CapsuleShape>()) {
  1134. Vector3 axis;
  1135. axis[p_idx==0?0:2]=1.0;
  1136. Ref<CapsuleShape> cs = s;
  1137. Vector3 ra,rb;
  1138. Geometry::get_closest_points_between_segments(Vector3(),axis*4096,sg[0],sg[1],ra,rb);
  1139. float d = ra[p_idx];
  1140. if (p_idx==1)
  1141. d-=cs->get_radius();
  1142. if (d<0.001)
  1143. d=0.001;
  1144. if (p_idx==0)
  1145. cs->set_radius(d);
  1146. else if (p_idx==1)
  1147. cs->set_height(d*2.0);
  1148. }
  1149. }
  1150. void CollisionShapeSpatialGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  1151. Ref<Shape> s = cs->get_shape();
  1152. if (s.is_null())
  1153. return;
  1154. if (s->cast_to<SphereShape>()) {
  1155. Ref<SphereShape> ss=s;
  1156. if (p_cancel) {
  1157. ss->set_radius(p_restore);
  1158. return;
  1159. }
  1160. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1161. ur->create_action("Change Sphere Shape Radius");
  1162. ur->add_do_method(ss.ptr(),"set_radius",ss->get_radius());
  1163. ur->add_undo_method(ss.ptr(),"set_radius",p_restore);
  1164. ur->commit_action();
  1165. }
  1166. if (s->cast_to<BoxShape>()) {
  1167. Ref<BoxShape> ss=s;
  1168. if (p_cancel) {
  1169. ss->set_extents(p_restore);
  1170. return;
  1171. }
  1172. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1173. ur->create_action("Change Box Shape Extents");
  1174. ur->add_do_method(ss.ptr(),"set_extents",ss->get_extents());
  1175. ur->add_undo_method(ss.ptr(),"set_extents",p_restore);
  1176. ur->commit_action();
  1177. }
  1178. if (s->cast_to<CapsuleShape>()) {
  1179. Ref<CapsuleShape> ss=s;
  1180. if (p_cancel) {
  1181. if (p_idx==0)
  1182. ss->set_radius(p_restore);
  1183. else
  1184. ss->set_height(p_restore);
  1185. return;
  1186. }
  1187. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1188. if (p_idx==0) {
  1189. ur->create_action("Change Capsule Shape Radius");
  1190. ur->add_do_method(ss.ptr(),"set_radius",ss->get_radius());
  1191. ur->add_undo_method(ss.ptr(),"set_radius",p_restore);
  1192. } else {
  1193. ur->create_action("Change Capsule Shape Height");
  1194. ur->add_do_method(ss.ptr(),"set_height",ss->get_height());
  1195. ur->add_undo_method(ss.ptr(),"set_height",p_restore);
  1196. }
  1197. ur->commit_action();
  1198. }
  1199. if (s->cast_to<RayShape>()) {
  1200. Ref<RayShape> ss=s;
  1201. if (p_cancel) {
  1202. ss->set_length(p_restore);
  1203. return;
  1204. }
  1205. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1206. ur->create_action("Change Ray Shape Length");
  1207. ur->add_do_method(ss.ptr(),"set_length",ss->get_length());
  1208. ur->add_undo_method(ss.ptr(),"set_length",p_restore);
  1209. ur->commit_action();
  1210. }
  1211. }
  1212. void CollisionShapeSpatialGizmo::redraw(){
  1213. clear();
  1214. Ref<Shape> s = cs->get_shape();
  1215. if (s.is_null())
  1216. return;
  1217. if (s->cast_to<SphereShape>()) {
  1218. Ref<SphereShape> sp= s;
  1219. float r=sp->get_radius();
  1220. Vector<Vector3> points;
  1221. for(int i=0;i<=360;i++) {
  1222. float ra=Math::deg2rad(i);
  1223. float rb=Math::deg2rad(i+1);
  1224. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  1225. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  1226. points.push_back(Vector3(a.x,0,a.y));
  1227. points.push_back(Vector3(b.x,0,b.y));
  1228. points.push_back(Vector3(0,a.x,a.y));
  1229. points.push_back(Vector3(0,b.x,b.y));
  1230. points.push_back(Vector3(a.x,a.y,0));
  1231. points.push_back(Vector3(b.x,b.y,0));
  1232. }
  1233. Vector<Vector3> collision_segments;
  1234. for(int i=0;i<64;i++) {
  1235. float ra=i*Math_PI*2.0/64.0;
  1236. float rb=(i+1)*Math_PI*2.0/64.0;
  1237. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  1238. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  1239. collision_segments.push_back(Vector3(a.x,0,a.y));
  1240. collision_segments.push_back(Vector3(b.x,0,b.y));
  1241. collision_segments.push_back(Vector3(0,a.x,a.y));
  1242. collision_segments.push_back(Vector3(0,b.x,b.y));
  1243. collision_segments.push_back(Vector3(a.x,a.y,0));
  1244. collision_segments.push_back(Vector3(b.x,b.y,0));
  1245. }
  1246. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  1247. add_collision_segments(collision_segments);
  1248. Vector<Vector3> handles;
  1249. handles.push_back(Vector3(r,0,0));
  1250. add_handles(handles);
  1251. }
  1252. if (s->cast_to<BoxShape>()) {
  1253. Ref<BoxShape> bs=s;
  1254. Vector<Vector3> lines;
  1255. AABB aabb;
  1256. aabb.pos=-bs->get_extents();
  1257. aabb.size=aabb.pos*-2;
  1258. for(int i=0;i<12;i++) {
  1259. Vector3 a,b;
  1260. aabb.get_edge(i,a,b);
  1261. lines.push_back(a);
  1262. lines.push_back(b);
  1263. }
  1264. Vector<Vector3> handles;
  1265. for(int i=0;i<3;i++) {
  1266. Vector3 ax;
  1267. ax[i]=bs->get_extents()[i];
  1268. handles.push_back(ax);
  1269. }
  1270. add_lines(lines,SpatialEditorGizmos::singleton->shape_material);
  1271. add_collision_segments(lines);
  1272. add_handles(handles);
  1273. }
  1274. if (s->cast_to<CapsuleShape>()) {
  1275. Ref<CapsuleShape> cs=s;
  1276. float radius = cs->get_radius();
  1277. float height = cs->get_height();
  1278. Vector<Vector3> points;
  1279. Vector3 d(0,0,height*0.5);
  1280. for(int i=0;i<360;i++) {
  1281. float ra=Math::deg2rad(i);
  1282. float rb=Math::deg2rad(i+1);
  1283. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*radius;
  1284. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*radius;
  1285. points.push_back(Vector3(a.x,a.y,0)+d);
  1286. points.push_back(Vector3(b.x,b.y,0)+d);
  1287. points.push_back(Vector3(a.x,a.y,0)-d);
  1288. points.push_back(Vector3(b.x,b.y,0)-d);
  1289. if (i%90==0) {
  1290. points.push_back(Vector3(a.x,a.y,0)+d);
  1291. points.push_back(Vector3(a.x,a.y,0)-d);
  1292. }
  1293. Vector3 dud = i<180?d:-d;
  1294. points.push_back(Vector3(0,a.y,a.x)+dud);
  1295. points.push_back(Vector3(0,b.y,b.x)+dud);
  1296. points.push_back(Vector3(a.y,0,a.x)+dud);
  1297. points.push_back(Vector3(b.y,0,b.x)+dud);
  1298. }
  1299. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  1300. Vector<Vector3> collision_segments;
  1301. for(int i=0;i<64;i++) {
  1302. float ra=i*Math_PI*2.0/64.0;
  1303. float rb=(i+1)*Math_PI*2.0/64.0;
  1304. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*radius;
  1305. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*radius;
  1306. collision_segments.push_back(Vector3(a.x,a.y,0)+d);
  1307. collision_segments.push_back(Vector3(b.x,b.y,0)+d);
  1308. collision_segments.push_back(Vector3(a.x,a.y,0)-d);
  1309. collision_segments.push_back(Vector3(b.x,b.y,0)-d);
  1310. if (i%16==0) {
  1311. collision_segments.push_back(Vector3(a.x,a.y,0)+d);
  1312. collision_segments.push_back(Vector3(a.x,a.y,0)-d);
  1313. }
  1314. Vector3 dud = i<32?d:-d;
  1315. collision_segments.push_back(Vector3(0,a.y,a.x)+dud);
  1316. collision_segments.push_back(Vector3(0,b.y,b.x)+dud);
  1317. collision_segments.push_back(Vector3(a.y,0,a.x)+dud);
  1318. collision_segments.push_back(Vector3(b.y,0,b.x)+dud);
  1319. }
  1320. add_collision_segments(collision_segments);
  1321. Vector<Vector3> handles;
  1322. handles.push_back(Vector3(cs->get_radius(),0,0));
  1323. handles.push_back(Vector3(0,0,cs->get_height()*0.5+cs->get_radius()));
  1324. add_handles(handles);
  1325. }
  1326. if (s->cast_to<PlaneShape>()) {
  1327. Ref<PlaneShape> ps=s;
  1328. Plane p = ps->get_plane();
  1329. Vector<Vector3> points;
  1330. Vector3 n1 = p.get_any_perpendicular_normal();
  1331. Vector3 n2 = p.normal.cross(n1).normalized();
  1332. Vector3 pface[4]={
  1333. p.normal*p.d+n1*10.0+n2*10.0,
  1334. p.normal*p.d+n1*10.0+n2*-10.0,
  1335. p.normal*p.d+n1*-10.0+n2*-10.0,
  1336. p.normal*p.d+n1*-10.0+n2*10.0,
  1337. };
  1338. points.push_back(pface[0]);
  1339. points.push_back(pface[1]);
  1340. points.push_back(pface[1]);
  1341. points.push_back(pface[2]);
  1342. points.push_back(pface[2]);
  1343. points.push_back(pface[3]);
  1344. points.push_back(pface[3]);
  1345. points.push_back(pface[0]);
  1346. points.push_back(p.normal*p.d);
  1347. points.push_back(p.normal*p.d+p.normal*3);
  1348. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  1349. add_collision_segments(points);
  1350. }
  1351. if (s->cast_to<RayShape>()) {
  1352. Ref<RayShape> rs=s;
  1353. Vector<Vector3> points;
  1354. points.push_back(Vector3());
  1355. points.push_back(Vector3(0,0,rs->get_length()));
  1356. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  1357. add_collision_segments(points);
  1358. Vector<Vector3> handles;
  1359. handles.push_back(Vector3(0,0,rs->get_length()));
  1360. add_handles(handles);
  1361. }
  1362. }
  1363. CollisionShapeSpatialGizmo::CollisionShapeSpatialGizmo(CollisionShape* p_cs) {
  1364. cs=p_cs;
  1365. set_spatial_node(p_cs);
  1366. }
  1367. String VisibilityNotifierGizmo::get_handle_name(int p_idx) const {
  1368. switch(p_idx) {
  1369. case 0: return "X";
  1370. case 1: return "Y";
  1371. case 2: return "Z";
  1372. }
  1373. return "";
  1374. }
  1375. Variant VisibilityNotifierGizmo::get_handle_value(int p_idx) const{
  1376. return notifier->get_aabb();
  1377. }
  1378. void VisibilityNotifierGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point){
  1379. Transform gt = notifier->get_global_transform();
  1380. //gt.orthonormalize();
  1381. Transform gi = gt.affine_inverse();
  1382. AABB aabb = notifier->get_aabb();
  1383. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1384. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1385. Vector3 sg[2]={gi.xform(ray_from),gi.xform(ray_from+ray_dir*4096)};
  1386. Vector3 ofs = aabb.pos+aabb.size*0.5;;
  1387. Vector3 axis;
  1388. axis[p_idx]=1.0;
  1389. Vector3 ra,rb;
  1390. Geometry::get_closest_points_between_segments(ofs,ofs+axis*4096,sg[0],sg[1],ra,rb);
  1391. float d = ra[p_idx];
  1392. if (d<0.001)
  1393. d=0.001;
  1394. Vector3 he = aabb.size;
  1395. aabb.pos[p_idx]=(aabb.pos[p_idx]+aabb.size[p_idx]*0.5)-d;
  1396. aabb.size[p_idx]=d*2;
  1397. notifier->set_aabb(aabb);
  1398. }
  1399. void VisibilityNotifierGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  1400. if (p_cancel) {
  1401. notifier->set_aabb(p_restore);
  1402. return;
  1403. }
  1404. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1405. ur->create_action("Change Notifier Extents");
  1406. ur->add_do_method(notifier,"set_aabb",notifier->get_aabb());
  1407. ur->add_undo_method(notifier,"set_aabb",p_restore);
  1408. ur->commit_action();
  1409. }
  1410. void VisibilityNotifierGizmo::redraw(){
  1411. clear();
  1412. Vector<Vector3> lines;
  1413. AABB aabb = notifier->get_aabb();
  1414. for(int i=0;i<12;i++) {
  1415. Vector3 a,b;
  1416. aabb.get_edge(i,a,b);
  1417. lines.push_back(a);
  1418. lines.push_back(b);
  1419. }
  1420. Vector<Vector3> handles;
  1421. for(int i=0;i<3;i++) {
  1422. Vector3 ax;
  1423. ax[i]=aabb.pos[i]+aabb.size[i];
  1424. handles.push_back(ax);
  1425. }
  1426. add_lines(lines,SpatialEditorGizmos::singleton->visibility_notifier_material);
  1427. //add_unscaled_billboard(SpatialEditorGizmos::singleton->visi,0.05);
  1428. add_collision_segments(lines);
  1429. add_handles(handles);
  1430. }
  1431. VisibilityNotifierGizmo::VisibilityNotifierGizmo(VisibilityNotifier* p_notifier){
  1432. notifier=p_notifier;
  1433. set_spatial_node(p_notifier);
  1434. }
  1435. ////////
  1436. void NavigationMeshSpatialGizmo::redraw() {
  1437. clear();
  1438. Ref<NavigationMesh> navmeshie = navmesh->get_navigation_mesh();
  1439. if (navmeshie.is_null())
  1440. return;
  1441. DVector<Vector3> vertices = navmeshie->get_vertices();
  1442. DVector<Vector3>::Read vr=vertices.read();
  1443. List<Face3> faces;
  1444. for(int i=0;i<navmeshie->get_polygon_count();i++) {
  1445. Vector<int> p = navmeshie->get_polygon(i);
  1446. for(int j=2;j<p.size();j++) {
  1447. Face3 f;
  1448. f.vertex[0]=vr[p[0]];
  1449. f.vertex[1]=vr[p[j-1]];
  1450. f.vertex[2]=vr[p[j]];
  1451. faces.push_back(f);
  1452. }
  1453. }
  1454. Map<_EdgeKey,bool> edge_map;
  1455. DVector<Vector3> tmeshfaces;
  1456. tmeshfaces.resize(faces.size()*3);
  1457. {
  1458. DVector<Vector3>::Write tw=tmeshfaces.write();
  1459. int tidx=0;
  1460. for(List<Face3>::Element *E=faces.front();E;E=E->next()) {
  1461. const Face3 &f = E->get();
  1462. for(int j=0;j<3;j++) {
  1463. tw[tidx++]=f.vertex[j];
  1464. _EdgeKey ek;
  1465. ek.from=f.vertex[j].snapped(CMP_EPSILON);
  1466. ek.to=f.vertex[(j+1)%3].snapped(CMP_EPSILON);
  1467. if (ek.from<ek.to)
  1468. SWAP(ek.from,ek.to);
  1469. Map<_EdgeKey,bool>::Element *E=edge_map.find(ek);
  1470. if (E) {
  1471. E->get()=false;
  1472. } else {
  1473. edge_map[ek]=true;
  1474. }
  1475. }
  1476. }
  1477. }
  1478. Vector<Vector3> lines;
  1479. for(Map<_EdgeKey,bool>::Element *E=edge_map.front();E;E=E->next()) {
  1480. if (E->get()) {
  1481. lines.push_back(E->key().from);
  1482. lines.push_back(E->key().to);
  1483. }
  1484. }
  1485. Ref<TriangleMesh> tmesh = memnew( TriangleMesh);
  1486. tmesh->create(tmeshfaces);
  1487. add_lines(lines,navmesh->is_enabled()?SpatialEditorGizmos::singleton->navmesh_edge_material:SpatialEditorGizmos::singleton->navmesh_edge_material_disabled);
  1488. add_collision_triangles(tmesh);
  1489. Ref<Mesh> m = memnew( Mesh );
  1490. Array a;
  1491. a.resize(Mesh::ARRAY_MAX);
  1492. a[0]=tmeshfaces;
  1493. m->add_surface(Mesh::PRIMITIVE_TRIANGLES,a);
  1494. m->surface_set_material(0,navmesh->is_enabled()?SpatialEditorGizmos::singleton->navmesh_solid_material:SpatialEditorGizmos::singleton->navmesh_solid_material_disabled);
  1495. add_mesh(m);
  1496. add_collision_segments(lines);
  1497. }
  1498. NavigationMeshSpatialGizmo::NavigationMeshSpatialGizmo(NavigationMeshInstance *p_navmesh){
  1499. set_spatial_node(p_navmesh);
  1500. navmesh=p_navmesh;
  1501. }
  1502. ////////
  1503. SpatialEditorGizmos *SpatialEditorGizmos::singleton=NULL;
  1504. Ref<SpatialEditorGizmo> SpatialEditorGizmos::get_gizmo(Spatial *p_spatial) {
  1505. if (p_spatial->cast_to<Light>()) {
  1506. Ref<LightSpatialGizmo> lsg = memnew( LightSpatialGizmo(p_spatial->cast_to<Light>()) );
  1507. return lsg;
  1508. }
  1509. if (p_spatial->cast_to<Camera>()) {
  1510. Ref<CameraSpatialGizmo> lsg = memnew( CameraSpatialGizmo(p_spatial->cast_to<Camera>()) );
  1511. return lsg;
  1512. }
  1513. if (p_spatial->cast_to<Skeleton>()) {
  1514. Ref<SkeletonSpatialGizmo> lsg = memnew( SkeletonSpatialGizmo(p_spatial->cast_to<Skeleton>()) );
  1515. return lsg;
  1516. }
  1517. if (p_spatial->cast_to<Position3D>()) {
  1518. Ref<Position3DSpatialGizmo> lsg = memnew( Position3DSpatialGizmo(p_spatial->cast_to<Position3D>()) );
  1519. return lsg;
  1520. }
  1521. if (p_spatial->cast_to<MeshInstance>()) {
  1522. Ref<MeshInstanceSpatialGizmo> misg = memnew( MeshInstanceSpatialGizmo(p_spatial->cast_to<MeshInstance>()) );
  1523. return misg;
  1524. }
  1525. if (p_spatial->cast_to<Room>()) {
  1526. Ref<RoomSpatialGizmo> misg = memnew( RoomSpatialGizmo(p_spatial->cast_to<Room>()) );
  1527. return misg;
  1528. }
  1529. if (p_spatial->cast_to<NavigationMeshInstance>()) {
  1530. Ref<NavigationMeshSpatialGizmo> misg = memnew( NavigationMeshSpatialGizmo(p_spatial->cast_to<NavigationMeshInstance>()) );
  1531. return misg;
  1532. }
  1533. if (p_spatial->cast_to<RayCast>()) {
  1534. Ref<RayCastSpatialGizmo> misg = memnew( RayCastSpatialGizmo(p_spatial->cast_to<RayCast>()) );
  1535. return misg;
  1536. }
  1537. if (p_spatial->cast_to<Portal>()) {
  1538. Ref<PortalSpatialGizmo> misg = memnew( PortalSpatialGizmo(p_spatial->cast_to<Portal>()) );
  1539. return misg;
  1540. }
  1541. if (p_spatial->cast_to<TestCube>()) {
  1542. Ref<TestCubeSpatialGizmo> misg = memnew( TestCubeSpatialGizmo(p_spatial->cast_to<TestCube>()) );
  1543. return misg;
  1544. }
  1545. if (p_spatial->cast_to<SpatialPlayer>()) {
  1546. Ref<SpatialPlayerSpatialGizmo> misg = memnew( SpatialPlayerSpatialGizmo(p_spatial->cast_to<SpatialPlayer>()) );
  1547. return misg;
  1548. }
  1549. if (p_spatial->cast_to<CollisionShape>()) {
  1550. Ref<CollisionShapeSpatialGizmo> misg = memnew( CollisionShapeSpatialGizmo(p_spatial->cast_to<CollisionShape>()) );
  1551. return misg;
  1552. }
  1553. if (p_spatial->cast_to<VisibilityNotifier>()) {
  1554. Ref<VisibilityNotifierGizmo> misg = memnew( VisibilityNotifierGizmo(p_spatial->cast_to<VisibilityNotifier>()) );
  1555. return misg;
  1556. }
  1557. if (p_spatial->cast_to<EditableShape>()) {
  1558. Ref<EditableShapeSpatialGizmo> misg = memnew( EditableShapeSpatialGizmo(p_spatial->cast_to<EditableShape>()) );
  1559. return misg;
  1560. }
  1561. if (p_spatial->cast_to<CarWheel>()) {
  1562. Ref<CarWheelSpatialGizmo> misg = memnew( CarWheelSpatialGizmo(p_spatial->cast_to<CarWheel>()) );
  1563. return misg;
  1564. }
  1565. return Ref<SpatialEditorGizmo>();
  1566. }
  1567. Ref<FixedMaterial> SpatialEditorGizmos::create_line_material(const Color& p_base_color) {
  1568. Ref<FixedMaterial> line_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1569. line_material->set_flag(Material::FLAG_UNSHADED, true);
  1570. line_material->set_line_width(3.0);
  1571. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1572. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  1573. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,p_base_color);
  1574. return line_material;
  1575. }
  1576. Ref<FixedMaterial> SpatialEditorGizmos::create_solid_material(const Color& p_base_color) {
  1577. Ref<FixedMaterial> line_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1578. line_material->set_flag(Material::FLAG_UNSHADED, true);
  1579. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1580. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,p_base_color);
  1581. return line_material;
  1582. }
  1583. SpatialEditorGizmos::SpatialEditorGizmos() {
  1584. singleton=this;
  1585. handle_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1586. handle_material->set_flag(Material::FLAG_UNSHADED, true);
  1587. handle_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(0.8,0.8,0.8));
  1588. handle2_material = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1589. handle2_material->set_flag(Material::FLAG_UNSHADED, true);
  1590. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_POINT_SIZE, true);
  1591. handle_t = SpatialEditor::get_singleton()->get_icon("Editor3DHandle","EditorIcons");
  1592. handle2_material->set_point_size(handle_t->get_width());
  1593. handle2_material->set_texture(FixedMaterial::PARAM_DIFFUSE,handle_t);
  1594. handle2_material->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1));
  1595. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1596. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  1597. light_material = create_line_material(Color(1,1,0.2));
  1598. light_material_omni_icon = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1599. light_material_omni_icon->set_flag(Material::FLAG_UNSHADED, true);
  1600. light_material_omni_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  1601. light_material_omni_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1602. light_material_omni_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1603. light_material_omni_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,0.9));
  1604. light_material_omni_icon->set_texture(FixedMaterial::PARAM_DIFFUSE,SpatialEditor::get_singleton()->get_icon("GizmoLight","EditorIcons"));
  1605. light_material_directional_icon = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1606. light_material_directional_icon->set_flag(Material::FLAG_UNSHADED, true);
  1607. light_material_directional_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  1608. light_material_directional_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1609. light_material_directional_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1610. light_material_directional_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,0.9));
  1611. light_material_directional_icon->set_texture(FixedMaterial::PARAM_DIFFUSE,SpatialEditor::get_singleton()->get_icon("GizmoDirectionalLight","EditorIcons"));
  1612. camera_material = create_line_material(Color(1.0,0.5,1.0));
  1613. navmesh_edge_material = create_line_material(Color(0.1,0.8,1.0));
  1614. navmesh_solid_material = create_solid_material(Color(0.1,0.8,1.0,0.4));
  1615. navmesh_edge_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, false);
  1616. navmesh_solid_material->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  1617. navmesh_edge_material_disabled = create_line_material(Color(1.0,0.8,0.1));
  1618. navmesh_solid_material_disabled = create_solid_material(Color(1.0,0.8,0.1,0.4));
  1619. navmesh_edge_material_disabled->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, false);
  1620. navmesh_solid_material_disabled->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  1621. skeleton_material = create_line_material(Color(0.6,1.0,0.3));
  1622. skeleton_material->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  1623. skeleton_material->set_flag(Material::FLAG_UNSHADED,true);
  1624. skeleton_material->set_flag(Material::FLAG_ONTOP,true);
  1625. skeleton_material->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1626. //position 3D Shared mesh
  1627. pos3d_mesh = Ref<Mesh>( memnew( Mesh ) );
  1628. {
  1629. DVector<Vector3> cursor_points;
  1630. DVector<Color> cursor_colors;
  1631. float cs = 0.25;
  1632. cursor_points.push_back(Vector3(+cs,0,0));
  1633. cursor_points.push_back(Vector3(-cs,0,0));
  1634. cursor_points.push_back(Vector3(0,+cs,0));
  1635. cursor_points.push_back(Vector3(0,-cs,0));
  1636. cursor_points.push_back(Vector3(0,0,+cs));
  1637. cursor_points.push_back(Vector3(0,0,-cs));
  1638. cursor_colors.push_back(Color(1,0.5,0.5,0.7));
  1639. cursor_colors.push_back(Color(1,0.5,0.5,0.7));
  1640. cursor_colors.push_back(Color(0.5,1,0.5,0.7));
  1641. cursor_colors.push_back(Color(0.5,1,0.5,0.7));
  1642. cursor_colors.push_back(Color(0.5,0.5,1,0.7));
  1643. cursor_colors.push_back(Color(0.5,0.5,1,0.7));
  1644. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  1645. mat->set_flag(Material::FLAG_UNSHADED,true);
  1646. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY,true);
  1647. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA,true);
  1648. mat->set_line_width(3);
  1649. Array d;
  1650. d.resize(VS::ARRAY_MAX);
  1651. d[Mesh::ARRAY_VERTEX]=cursor_points;
  1652. d[Mesh::ARRAY_COLOR]=cursor_colors;
  1653. pos3d_mesh->add_surface(Mesh::PRIMITIVE_LINES,d);
  1654. pos3d_mesh->surface_set_material(0,mat);
  1655. }
  1656. sample_player_icon = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1657. sample_player_icon->set_flag(Material::FLAG_UNSHADED, true);
  1658. sample_player_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  1659. sample_player_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1660. sample_player_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1661. sample_player_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,0.9));
  1662. sample_player_icon->set_texture(FixedMaterial::PARAM_DIFFUSE,SpatialEditor::get_singleton()->get_icon("GizmoSpatialSamplePlayer","EditorIcons"));
  1663. room_material = create_line_material(Color(1.0,0.6,0.9));
  1664. portal_material = create_line_material(Color(1.0,0.8,0.6));
  1665. raycast_material = create_line_material(Color(1.0,0.8,0.6));
  1666. car_wheel_material = create_line_material(Color(0.6,0.8,1.0));
  1667. visibility_notifier_material = create_line_material(Color(1.0,0.5,1.0));
  1668. stream_player_icon = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1669. stream_player_icon->set_flag(Material::FLAG_UNSHADED, true);
  1670. stream_player_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  1671. stream_player_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1672. stream_player_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1673. stream_player_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,0.9));
  1674. stream_player_icon->set_texture(FixedMaterial::PARAM_DIFFUSE,SpatialEditor::get_singleton()->get_icon("GizmoSpatialStreamPlayer","EditorIcons"));
  1675. visibility_notifier_icon = Ref<FixedMaterial>( memnew( FixedMaterial ));
  1676. visibility_notifier_icon->set_flag(Material::FLAG_UNSHADED, true);
  1677. visibility_notifier_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  1678. visibility_notifier_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  1679. visibility_notifier_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  1680. visibility_notifier_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,0.9));
  1681. visibility_notifier_icon->set_texture(FixedMaterial::PARAM_DIFFUSE,SpatialEditor::get_singleton()->get_icon("Visible","EditorIcons"));
  1682. {
  1683. DVector<Vector3> vertices;
  1684. #undef ADD_VTX
  1685. #define ADD_VTX(m_idx);\
  1686. vertices.push_back( face_points[m_idx] );
  1687. for (int i=0;i<6;i++) {
  1688. Vector3 face_points[4];
  1689. for (int j=0;j<4;j++) {
  1690. float v[3];
  1691. v[0]=1.0;
  1692. v[1]=1-2*((j>>1)&1);
  1693. v[2]=v[1]*(1-2*(j&1));
  1694. for (int k=0;k<3;k++) {
  1695. if (i<3)
  1696. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  1697. else
  1698. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  1699. }
  1700. }
  1701. //tri 1
  1702. ADD_VTX(0);
  1703. ADD_VTX(1);
  1704. ADD_VTX(2);
  1705. //tri 2
  1706. ADD_VTX(2);
  1707. ADD_VTX(3);
  1708. ADD_VTX(0);
  1709. }
  1710. test_cube_tm = Ref<TriangleMesh>( memnew( TriangleMesh ) );
  1711. test_cube_tm->create(vertices);
  1712. }
  1713. shape_material = create_line_material(Color(0.2,1,1.0));
  1714. }