guiConvexShapeEditorCtrl.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/worldEditor/guiConvexShapeEditorCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "T3D/convexShape.h"
  27. #include "renderInstance/renderPassManager.h"
  28. #include "collision/collision.h"
  29. #include "math/util/frustum.h"
  30. #include "math/mathUtils.h"
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #include "gfx/gfxTextureHandle.h"
  33. #include "gfx/gfxTransformSaver.h"
  34. #include "gfx/primBuilder.h"
  35. #include "gfx/gfxDrawUtil.h"
  36. #include "scene/sceneRenderState.h"
  37. #include "scene/sceneManager.h"
  38. #include "gui/core/guiCanvas.h"
  39. #include "gui/buttons/guiButtonCtrl.h"
  40. #include "gui/worldEditor/undoActions.h"
  41. #include "T3D/gameBase/gameConnection.h"
  42. #include "gfx/sim/debugDraw.h"
  43. #include "collision/optimizedPolyList.h"
  44. #include "core/volume.h"
  45. #include "gui/worldEditor/worldEditor.h"
  46. #include "T3D/prefab.h"
  47. #include "T3D/trigger.h"
  48. #include "T3D/zone.h"
  49. #include "T3D/portal.h"
  50. #include "math/mPolyhedron.impl.h"
  51. IMPLEMENT_CONOBJECT( GuiConvexEditorCtrl );
  52. ConsoleDocClass( GuiConvexEditorCtrl,
  53. "@brief The base class for the sketch tool\n\n"
  54. "Editor use only.\n\n"
  55. "@internal"
  56. );
  57. GuiConvexEditorCtrl::GuiConvexEditorCtrl()
  58. : mIsDirty( false ),
  59. mFaceSEL( -1 ),
  60. mFaceHL( -1 ),
  61. mFaceSavedXfm( true ),
  62. mSavedUndo( false ),
  63. mDragging( false ),
  64. mGizmoMatOffset( Point3F::Zero ),
  65. mPivotPos( Point3F::Zero ),
  66. mUsingPivot( false ),
  67. mSettingPivot( false ),
  68. mActiveTool( NULL ),
  69. mMouseDown( false ),
  70. mCreateTool( NULL ),
  71. mSavedGizmoFlags( -1 ),
  72. mHasCopied( false ),
  73. mLastUndo( NULL ),
  74. mUndoManager( NULL ),
  75. mCtrlDown( false )
  76. {
  77. mMaterialName = StringTable->insert("Grid512_OrangeLines_Mat");
  78. }
  79. GuiConvexEditorCtrl::~GuiConvexEditorCtrl()
  80. {
  81. }
  82. bool GuiConvexEditorCtrl::onAdd()
  83. {
  84. if ( !Parent::onAdd() )
  85. return false;
  86. SceneManager::getPreRenderSignal().notify( this, &GuiConvexEditorCtrl::_prepRenderImage );
  87. mCreateTool = new ConvexEditorCreateTool( this );
  88. return true;
  89. }
  90. void GuiConvexEditorCtrl::onRemove()
  91. {
  92. SceneManager::getPreRenderSignal().remove( this, &GuiConvexEditorCtrl::_prepRenderImage );
  93. SAFE_DELETE( mCreateTool );
  94. Parent::onRemove();
  95. }
  96. void GuiConvexEditorCtrl::initPersistFields()
  97. {
  98. addField( "isDirty", TypeBool, Offset( mIsDirty, GuiConvexEditorCtrl ) );
  99. addField( "materialName", TypeString, Offset(mMaterialName, GuiConvexEditorCtrl) );
  100. Parent::initPersistFields();
  101. }
  102. bool GuiConvexEditorCtrl::onWake()
  103. {
  104. if ( !Parent::onWake() )
  105. return false;
  106. SimGroup *missionGroup;
  107. if ( !Sim::findObject( "MissionGroup", missionGroup ) )
  108. return true;
  109. SimGroup::iterator itr = missionGroup->begin();
  110. for ( ; itr != missionGroup->end(); itr++ )
  111. {
  112. if ( dStrcmp( (*itr)->getClassName(), "ConvexShape" ) == 0 )
  113. {
  114. mConvexSEL = static_cast<ConvexShape*>( *itr );
  115. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  116. return true;
  117. }
  118. }
  119. return true;
  120. }
  121. void GuiConvexEditorCtrl::onSleep()
  122. {
  123. Parent::onSleep();
  124. mConvexSEL = NULL;
  125. mConvexHL = NULL;
  126. }
  127. void GuiConvexEditorCtrl::setVisible( bool val )
  128. {
  129. //ConvexShape::smRenderEdges = value;
  130. if ( isProperlyAdded() )
  131. {
  132. if ( !val )
  133. {
  134. mFaceHL = -1;
  135. mConvexHL = NULL;
  136. setSelection( NULL, -1 );
  137. if ( mSavedGizmoFlags != -1 )
  138. {
  139. mGizmoProfile->flags = mSavedGizmoFlags;
  140. mSavedGizmoFlags = -1;
  141. }
  142. SimGroup* misGroup;
  143. if (Sim::findObject("MissionGroup", misGroup))
  144. {
  145. //Make our proxy objects "real" again
  146. for (U32 i = 0; i < mProxyObjects.size(); ++i)
  147. {
  148. if (!mProxyObjects[i].shapeProxy || !mProxyObjects[i].targetObject)
  149. continue;
  150. AbstractClassRep* classRep = AbstractClassRep::findClassRep(mProxyObjects[i].targetObjectClass);
  151. if (!classRep)
  152. {
  153. Con::errorf("WorldEditor::createPolyhedralObject - No such class: %s", mProxyObjects[i].targetObjectClass);
  154. continue;
  155. }
  156. SceneObject* polyObj = createPolyhedralObject(mProxyObjects[i].targetObjectClass.c_str(), mProxyObjects[i].shapeProxy);
  157. misGroup->addObject(polyObj);
  158. //Now, remove the convex proxy
  159. mProxyObjects[i].shapeProxy->deleteObject();
  160. mProxyObjects[i].targetObject->deleteObject();
  161. mProxyObjects.erase(i);
  162. --i;
  163. }
  164. }
  165. }
  166. else
  167. {
  168. mConvexHL = NULL;
  169. mFaceHL = -1;
  170. setSelection( NULL, -1 );
  171. WorldEditor *wedit;
  172. if ( Sim::findObject( "EWorldEditor", wedit ) )
  173. {
  174. S32 count = wedit->getSelectionSize();
  175. for ( S32 i = 0; i < count; i++ )
  176. {
  177. S32 objId = wedit->getSelectObject(i);
  178. ConvexShape *pShape;
  179. if ( Sim::findObject( objId, pShape ) )
  180. {
  181. mConvexSEL = pShape;
  182. wedit->clearSelection();
  183. wedit->selectObject( String::ToString("%i",objId) );
  184. break;
  185. }
  186. }
  187. }
  188. updateGizmoPos();
  189. mSavedGizmoFlags = mGizmoProfile->flags;
  190. SimGroup* misGroup;
  191. if (Sim::findObject("MissionGroup", misGroup))
  192. {
  193. for (U32 c = 0; c < misGroup->size(); ++c)
  194. {
  195. bool isTrigger = (misGroup->at(c)->getClassName() == StringTable->insert("Trigger"));
  196. bool isZone = (misGroup->at(c)->getClassName() == StringTable->insert("Zone"));
  197. bool isPortal = (misGroup->at(c)->getClassName() == StringTable->insert("Portal"));
  198. bool isOccluder = (misGroup->at(c)->getClassName() == StringTable->insert("OcclusionVolume"));
  199. if (isZone || isPortal || isOccluder)
  200. {
  201. SceneObject* sceneObj = static_cast<SceneObject*>(misGroup->at(c));
  202. if (!sceneObj)
  203. {
  204. Con::errorf("WorldEditor::createConvexShapeFrom - Invalid object");
  205. continue;
  206. }
  207. ConvexShape* proxyShape = createConvexShapeFrom(sceneObj);
  208. //Set the texture to a representatory one so we know what's what
  209. if (isTrigger)
  210. proxyShape->mMaterialName = "TriggerProxyMaterial";
  211. else if (isPortal)
  212. proxyShape->mMaterialName = "PortalProxyMaterial";
  213. else if (isZone)
  214. proxyShape->mMaterialName = "ZoneProxyMaterial";
  215. else if (isOccluder)
  216. proxyShape->mMaterialName = "OccluderProxyMaterial";
  217. proxyShape->_updateMaterial();
  218. sceneObj->setHidden(true);
  219. //set up the proxy object
  220. ConvexShapeProxy newProxy;
  221. newProxy.shapeProxy = proxyShape;
  222. newProxy.targetObject = sceneObj;
  223. if (isTrigger)
  224. newProxy.targetObjectClass = "Trigger";
  225. else if (isPortal)
  226. newProxy.targetObjectClass = "Portal";
  227. else if (isZone)
  228. newProxy.targetObjectClass = "Zone";
  229. else
  230. newProxy.targetObjectClass = "OcclusionVolume";
  231. mProxyObjects.push_back(newProxy);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. Parent::setVisible( val );
  238. }
  239. void GuiConvexEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  240. {
  241. mouseLock();
  242. mMouseDown = true;
  243. if ( event.modifier & SI_ALT )
  244. {
  245. setActiveTool( mCreateTool );
  246. mActiveTool->on3DMouseDown( event );
  247. return;
  248. }
  249. if ( mConvexSEL && isShapeValid( mConvexSEL ) )
  250. mLastValidShape = mConvexSEL->mSurfaces;
  251. if ( mConvexSEL &&
  252. mFaceSEL != -1 &&
  253. mGizmo->getMode() == RotateMode &&
  254. mGizmo->getSelection() == Gizmo::Centroid )
  255. {
  256. mSettingPivot = true;
  257. mSavedPivotPos = mGizmo->getPosition();
  258. setPivotPos( mConvexSEL, mFaceSEL, event );
  259. updateGizmoPos();
  260. return;
  261. }
  262. mGizmo->on3DMouseDown( event );
  263. }
  264. void GuiConvexEditorCtrl::on3DRightMouseDown(const Gui3DMouseEvent & event)
  265. {
  266. return;
  267. /*
  268. if ( mConvexSEL && mFaceSEL != -1 && mFaceSEL == mFaceHL )
  269. {
  270. _submitUndo( "Split ConvexShape face." );
  271. const MatrixF &surf = mConvexSEL->mSurfaces[mFaceSEL];
  272. MatrixF newSurf( surf );
  273. MatrixF rotMat( EulerF( 0.0f, mDegToRad( 2.0f ), 0.0f ) );
  274. newSurf *= rotMat;
  275. mConvexSEL->mSurfaces.insert( mFaceSEL+1, newSurf );
  276. }
  277. */
  278. }
  279. void GuiConvexEditorCtrl::on3DRightMouseUp(const Gui3DMouseEvent & event)
  280. {
  281. //ConvexShape *hitShape;
  282. //S32 hitFace;
  283. //bool hit = _cursorCast( event, &hitShape, &hitFace );
  284. //Con::printf( hit ? "HIT" : "MISS" );
  285. }
  286. void GuiConvexEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  287. {
  288. mouseUnlock();
  289. mMouseDown = false;
  290. mHasCopied = false;
  291. mHasGeometry = false;
  292. if ( mActiveTool )
  293. {
  294. ConvexEditorTool::EventResult result = mActiveTool->on3DMouseUp( event );
  295. if ( result == ConvexEditorTool::Done )
  296. setActiveTool( NULL );
  297. return;
  298. }
  299. if ( !mSettingPivot && !mDragging && ( mGizmo->getSelection() == Gizmo::None || !mConvexSEL ) )
  300. {
  301. if ( mConvexSEL != mConvexHL )
  302. {
  303. setSelection( mConvexHL, -1 );
  304. }
  305. else
  306. {
  307. if ( mFaceSEL != mFaceHL )
  308. setSelection( mConvexSEL, mFaceHL );
  309. else
  310. setSelection( mConvexSEL, -1 );
  311. }
  312. mUsingPivot = false;
  313. }
  314. mSettingPivot = false;
  315. mSavedPivotPos = mGizmo->getPosition();
  316. mSavedUndo = false;
  317. mGizmo->on3DMouseUp( event );
  318. if ( mDragging )
  319. {
  320. mDragging = false;
  321. if ( mConvexSEL )
  322. {
  323. Vector< U32 > removedPlanes;
  324. mConvexSEL->cullEmptyPlanes( &removedPlanes );
  325. // If a face has been removed we need to validate / remap
  326. // our selected and highlighted faces.
  327. if ( !removedPlanes.empty() )
  328. {
  329. S32 prevFaceHL = mFaceHL;
  330. S32 prevFaceSEL = mFaceSEL;
  331. if ( removedPlanes.contains( mFaceHL ) )
  332. prevFaceHL = mFaceHL = -1;
  333. if ( removedPlanes.contains( mFaceSEL ) )
  334. prevFaceSEL = mFaceSEL = -1;
  335. for ( S32 i = 0; i < removedPlanes.size(); i++ )
  336. {
  337. if ( (S32)removedPlanes[i] < prevFaceSEL )
  338. mFaceSEL--;
  339. if ( (S32)removedPlanes[i] < prevFaceHL )
  340. mFaceHL--;
  341. }
  342. setSelection( mConvexSEL, mFaceSEL );
  343. // We need to reindex faces.
  344. updateShape( mConvexSEL );
  345. }
  346. }
  347. }
  348. updateGizmoPos();
  349. }
  350. void GuiConvexEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  351. {
  352. if ( mActiveTool )
  353. {
  354. // If we have an active tool pass this event to it.
  355. // If it handled it, consume the event.
  356. if ( mActiveTool->on3DMouseMove( event ) )
  357. return;
  358. }
  359. ConvexShape *hitShape = NULL;
  360. S32 hitFace = -1;
  361. _cursorCast( event, &hitShape, &hitFace );
  362. if ( !mConvexSEL )
  363. {
  364. mConvexHL = hitShape;
  365. mFaceHL = -1;
  366. }
  367. else
  368. {
  369. if ( mConvexSEL == hitShape )
  370. {
  371. mConvexHL = hitShape;
  372. mFaceHL = hitFace;
  373. }
  374. else
  375. {
  376. // Mousing over a shape that is not the one currently selected.
  377. if ( mFaceSEL != -1 )
  378. {
  379. mFaceHL = -1;
  380. }
  381. else
  382. {
  383. mConvexHL = hitShape;
  384. mFaceHL = -1;
  385. }
  386. }
  387. }
  388. if ( mConvexSEL )
  389. mGizmo->on3DMouseMove( event );
  390. }
  391. void GuiConvexEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  392. {
  393. if ( mActiveTool )
  394. {
  395. // If we have an active tool pass this event to it.
  396. // If it handled it, consume the event.
  397. if ( mActiveTool->on3DMouseDragged( event ) )
  398. return;
  399. }
  400. //mGizmoProfile->rotateScalar = 0.55f;
  401. //mGizmoProfile->scaleScalar = 0.55f;
  402. if ( !mConvexSEL )
  403. return;
  404. if ( mGizmo->getMode() == RotateMode &&
  405. mGizmo->getSelection() == Gizmo::Centroid )
  406. {
  407. setPivotPos( mConvexSEL, mFaceSEL, event );
  408. mDragging = true;
  409. return;
  410. }
  411. mGizmo->on3DMouseDragged( event );
  412. if ( event.modifier & SI_SHIFT &&
  413. ( mGizmo->getMode() == MoveMode || mGizmo->getMode() == RotateMode ) &&
  414. !mHasCopied )
  415. {
  416. if ( mFaceSEL != -1 )
  417. {
  418. ConvexShape *newShape = mCreateTool->extrudeShapeFromFace( mConvexSEL, mFaceSEL );
  419. //newShape->_updateGeometry();
  420. submitUndo( CreateShape, newShape );
  421. setSelection( newShape, 0 );
  422. updateGizmoPos();
  423. mGizmo->on3DMouseDown( event );
  424. mHasCopied = true;
  425. mSavedUndo = true;
  426. }
  427. else
  428. {
  429. ConvexShape *newShape = new ConvexShape();
  430. newShape->setTransform( mConvexSEL->getTransform() );
  431. newShape->setScale( mConvexSEL->getScale() );
  432. newShape->mSurfaces.clear();
  433. newShape->mSurfaces.merge( mConvexSEL->mSurfaces );
  434. setupShape( newShape );
  435. newShape->setField("material", mConvexSEL->getMaterialName());
  436. submitUndo( CreateShape, newShape );
  437. setSelection( newShape, -1 );
  438. updateGizmoPos();
  439. mHasCopied = true;
  440. mSavedUndo = true;
  441. }
  442. return;
  443. }
  444. if ( mGizmo->getMode() == RotateMode &&
  445. event.modifier & SI_CTRL &&
  446. !mHasCopied &&
  447. mFaceSEL != -1 )
  448. {
  449. // Can must verify that splitting the face at the current angle
  450. // ( of the gizmo ) will generate a valid shape. If not enough rotation
  451. // has occurred we will have two faces that are coplanar and must wait
  452. // until later in the drag to perform the split.
  453. //AssertFatal( isShapeValid( mConvexSEL ), "Shape was already invalid at beginning of split operation." );
  454. if ( !isShapeValid( mConvexSEL ) )
  455. return;
  456. mLastValidShape = mConvexSEL->mSurfaces;
  457. Point3F rot = mGizmo->getDeltaTotalRot();
  458. rot.normalize();
  459. rot *= mDegToRad( 10.0f );
  460. MatrixF rotMat( (EulerF)rot );
  461. MatrixF worldToObj( mConvexSEL->getTransform() );
  462. worldToObj.scale( mConvexSEL->getScale() );
  463. worldToObj.inverse();
  464. mConvexSEL->mSurfaces.increment();
  465. MatrixF &newSurf = mConvexSEL->mSurfaces.last();
  466. newSurf = mConvexSEL->mSurfaces[mFaceSEL] * rotMat;
  467. //worldToObj.mul( mGizmo->getTransform() );
  468. //Point3F pos( mPivotPos );
  469. //worldToObj.mulP( pos );
  470. //newSurf.setPosition( pos );
  471. updateShape( mConvexSEL );
  472. if ( !isShapeValid( mConvexSEL ) )
  473. {
  474. mConvexSEL->mSurfaces = mLastValidShape;
  475. updateShape( mConvexSEL );
  476. }
  477. else
  478. {
  479. mHasCopied = true;
  480. mSavedUndo = true;
  481. mLastValidShape = mConvexSEL->mSurfaces;
  482. submitUndo( ModifyShape, mConvexSEL );
  483. setSelection( mConvexSEL, mConvexSEL->mSurfaces.size() - 1 );
  484. updateGizmoPos();
  485. }
  486. return;
  487. }
  488. // If we are dragging, but no gizmo selection...
  489. // Then treat this like a regular mouse move, update the highlighted
  490. // convex/face under the cursor and handle onMouseUp as we normally would
  491. // to change the selection.
  492. if ( mGizmo->getSelection() == Gizmo::None )
  493. {
  494. ConvexShape *hitShape = NULL;
  495. S32 hitFace = -1;
  496. _cursorCast( event, &hitShape, &hitFace );
  497. mFaceHL = hitFace;
  498. mConvexHL = hitShape;
  499. return;
  500. }
  501. mDragging = true;
  502. // Manipulating a face.
  503. if ( mFaceSEL != -1 )
  504. {
  505. if ( !mSavedUndo )
  506. {
  507. mSavedUndo = true;
  508. submitUndo( ModifyShape, mConvexSEL );
  509. }
  510. if ( mGizmo->getMode() == ScaleMode )
  511. {
  512. scaleFace( mConvexSEL, mFaceSEL, mGizmo->getScale() );
  513. }
  514. else
  515. {
  516. // Why does this have to be so ugly.
  517. if ( mGizmo->getMode() == RotateMode ||
  518. ( mGizmo->getMode() == MoveMode &&
  519. ( event.modifier & SI_CTRL ||
  520. ( mGizmo->getSelection() == Gizmo::Axis_Z && mHasCopied )
  521. )
  522. )
  523. )
  524. {
  525. const MatrixF &gMat = mGizmo->getTransform();
  526. MatrixF surfMat;
  527. surfMat.mul( mConvexSEL->mWorldToObj, gMat );
  528. MatrixF worldToObj ( mConvexSEL->getTransform() );
  529. worldToObj.scale( mConvexSEL->getScale() );
  530. worldToObj.inverse();
  531. Point3F newPos;
  532. newPos = gMat.getPosition();
  533. worldToObj.mulP( newPos );
  534. surfMat.setPosition( newPos );
  535. // Clear out floating point errors.
  536. cleanMatrix( surfMat );
  537. mConvexSEL->mSurfaces[mFaceSEL] = surfMat;
  538. updateShape( mConvexSEL, mFaceSEL );
  539. }
  540. else
  541. {
  542. // Translating a face in x/y/z
  543. translateFace( mConvexSEL, mFaceSEL, mGizmo->getTotalOffset() );
  544. }
  545. }
  546. if ( isShapeValid( mConvexSEL ) )
  547. {
  548. AssertFatal( mConvexSEL->mSurfaces.size() > mFaceSEL, "mFaceSEL out of range." );
  549. mLastValidShape = mConvexSEL->mSurfaces;
  550. }
  551. else
  552. {
  553. AssertFatal( mLastValidShape.size() > mFaceSEL, "mFaceSEL out of range." );
  554. mConvexSEL->mSurfaces = mLastValidShape;
  555. updateShape( mConvexSEL );
  556. }
  557. return;
  558. }
  559. // Manipulating a whole Convex.
  560. if ( !mSavedUndo )
  561. {
  562. mSavedUndo = true;
  563. submitUndo( ModifyShape, mConvexSEL );
  564. }
  565. if ( mGizmo->getMode() == MoveMode )
  566. {
  567. mConvexSEL->setPosition( mGizmo->getPosition() );
  568. }
  569. else if ( mGizmo->getMode() == RotateMode )
  570. {
  571. mConvexSEL->setTransform( mGizmo->getTransform() );
  572. }
  573. else
  574. {
  575. mConvexSEL->setScale( mGizmo->getScale() );
  576. }
  577. if ( mConvexSEL->getClientObject() )
  578. {
  579. ConvexShape *clientObj = static_cast< ConvexShape* >( mConvexSEL->getClientObject() );
  580. clientObj->setTransform( mConvexSEL->getTransform() );
  581. clientObj->setScale( mConvexSEL->getScale() );
  582. }
  583. }
  584. void GuiConvexEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  585. {
  586. }
  587. void GuiConvexEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  588. {
  589. }
  590. bool GuiConvexEditorCtrl::onKeyDown( const GuiEvent &evt )
  591. {
  592. bool handled = false;
  593. switch ( evt.keyCode )
  594. {
  595. case KEY_ESCAPE:
  596. handled = handleEscape();
  597. break;
  598. case KEY_A:
  599. if ( evt.modifier & SI_ALT )
  600. {
  601. GizmoAlignment align = mGizmo->getProfile()->alignment;
  602. if ( align == World )
  603. mGizmo->getProfile()->alignment = Object;
  604. else
  605. mGizmo->getProfile()->alignment = World;
  606. handled = true;
  607. }
  608. break;
  609. case KEY_LCONTROL:
  610. //mCtrlDown = true;
  611. break;
  612. default:
  613. break;
  614. }
  615. return handled;
  616. }
  617. bool GuiConvexEditorCtrl::onKeyUp( const GuiEvent &evt )
  618. {
  619. bool handled = false;
  620. switch ( evt.keyCode )
  621. {
  622. case KEY_LCONTROL:
  623. //mCtrlDown = false;
  624. break;
  625. default:
  626. break;
  627. }
  628. return handled;
  629. }
  630. void GuiConvexEditorCtrl::get3DCursor( GuiCursor *&cursor,
  631. bool &visible,
  632. const Gui3DMouseEvent &event_ )
  633. {
  634. //cursor = mAddNodeCursor;
  635. //visible = false;
  636. cursor = NULL;
  637. visible = false;
  638. GuiCanvas *root = getRoot();
  639. if ( !root )
  640. return;
  641. S32 currCursor = PlatformCursorController::curArrow;
  642. if ( root->mCursorChanged == currCursor )
  643. return;
  644. PlatformWindow *window = root->getPlatformWindow();
  645. PlatformCursorController *controller = window->getCursorController();
  646. // We've already changed the cursor,
  647. // so set it back before we change it again.
  648. if( root->mCursorChanged != -1)
  649. controller->popCursor();
  650. // Now change the cursor shape
  651. controller->pushCursor(currCursor);
  652. root->mCursorChanged = currCursor;
  653. }
  654. void GuiConvexEditorCtrl::updateGizmo()
  655. {
  656. mGizmoProfile->restoreDefaultState();
  657. const GizmoMode &mode = mGizmoProfile->mode;
  658. S32 &flags = mGizmoProfile->flags;
  659. GizmoAlignment &align = mGizmoProfile->alignment;
  660. U8 keys = Input::getModifierKeys();
  661. mCtrlDown = keys & ( SI_LCTRL | SI_LSHIFT );
  662. bool altDown = keys & ( SI_LALT );
  663. if ( altDown )
  664. {
  665. flags = 0;
  666. return;
  667. }
  668. if ( mFaceSEL != -1 )
  669. {
  670. align = Object;
  671. flags |= GizmoProfile::CanRotateUniform;
  672. flags &= ~GizmoProfile::CanRotateScreen;
  673. }
  674. else
  675. {
  676. flags &= ~GizmoProfile::CanRotateUniform;
  677. flags |= GizmoProfile::CanRotateScreen;
  678. }
  679. if ( mFaceSEL != -1 && mode == ScaleMode )
  680. flags &= ~GizmoProfile::CanScaleZ;
  681. else
  682. flags |= GizmoProfile::CanScaleZ;
  683. if ( mFaceSEL != -1 && mode == MoveMode )
  684. {
  685. if ( mCtrlDown )
  686. flags &= ~( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  687. else
  688. flags |= ( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  689. }
  690. }
  691. void GuiConvexEditorCtrl::renderScene(const RectI & updateRect)
  692. {
  693. // Synch selected ConvexShape with the WorldEditor.
  694. WorldEditor *wedit;
  695. if ( Sim::findObject( "EWorldEditor", wedit) )
  696. {
  697. S32 count = wedit->getSelectionSize();
  698. if ( !mConvexSEL && count != 0 )
  699. wedit->clearSelection();
  700. else if ( mConvexSEL && count != 1 )
  701. {
  702. wedit->clearSelection();
  703. wedit->selectObject( mConvexSEL->getIdString() );
  704. }
  705. else if ( mConvexSEL && count == 1 )
  706. {
  707. if ( wedit->getSelectObject(0) != mConvexSEL->getId() )
  708. {
  709. wedit->clearSelection();
  710. wedit->selectObject( mConvexSEL->getIdString() );
  711. }
  712. }
  713. }
  714. // Update status bar text.
  715. SimObject *statusbar;
  716. if ( Sim::findObject( "EditorGuiStatusBar", statusbar ) )
  717. {
  718. String text( "Sketch Tool." );
  719. GizmoMode mode = mGizmo->getMode();
  720. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  721. {
  722. Point3F delta;
  723. String qualifier;
  724. if ( mode == RotateMode )
  725. {
  726. if ( mSettingPivot )
  727. delta = mGizmo->getPosition() - mSavedPivotPos;
  728. else
  729. delta = mGizmo->getDeltaTotalRot();
  730. }
  731. else if ( mode == MoveMode )
  732. delta = mGizmo->getTotalOffset();
  733. else if ( mode == ScaleMode )
  734. delta = mGizmo->getDeltaTotalScale();
  735. if ( mGizmo->getAlignment() == Object &&
  736. mode != ScaleMode )
  737. {
  738. mConvexSEL->mWorldToObj.mulV( delta );
  739. if ( mFaceSEL != -1 && mode != RotateMode )
  740. {
  741. MatrixF objToSurf( mConvexSEL->mSurfaces[ mFaceSEL ] );
  742. objToSurf.scale( mConvexSEL->getScale() );
  743. objToSurf.inverse();
  744. objToSurf.mulV( delta );
  745. }
  746. }
  747. if ( mIsZero( delta.x, 0.0001f ) )
  748. delta.x = 0.0f;
  749. if ( mIsZero( delta.y, 0.0001f ) )
  750. delta.y = 0.0f;
  751. if ( mIsZero( delta.z, 0.0001f ) )
  752. delta.z = 0.0f;
  753. if ( mode == RotateMode )
  754. {
  755. if ( mSettingPivot )
  756. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  757. else
  758. {
  759. delta.x = mRadToDeg( delta.x );
  760. delta.y = mRadToDeg( delta.y );
  761. delta.z = mRadToDeg( delta.z );
  762. text = String::ToString( "Delta angle ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  763. }
  764. }
  765. else if ( mode == MoveMode )
  766. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  767. else if ( mode == ScaleMode )
  768. text = String::ToString( "Delta scale ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  769. }
  770. else
  771. {
  772. if ( !mConvexSEL )
  773. text = "Sketch Tool. ALT + Click-Drag to create a new ConvexShape.";
  774. else if ( mFaceSEL == -1 )
  775. {
  776. if ( mode == MoveMode )
  777. text = "Move selection. SHIFT while dragging duplicates objects.";
  778. else if ( mode == RotateMode )
  779. text = "Rotate selection.";
  780. else if ( mode == ScaleMode )
  781. text = "Scale selection.";
  782. }
  783. else
  784. {
  785. if ( mode == MoveMode )
  786. text = "Move face. SHIFT while beginning a drag EXTRUDES a new convex. Press CTRL for alternate translation mode.";
  787. else if ( mode == RotateMode )
  788. text = "Rotate face. Gizmo/Pivot is draggable. CTRL while dragging splits/folds a new face. SHIFT while dragging extrudes a new convex.";
  789. else if ( mode == ScaleMode )
  790. text = "Scale face.";
  791. }
  792. }
  793. // Issue a warning in the status bar
  794. // if this convex has an excessive number of surfaces...
  795. if ( mConvexSEL && mConvexSEL->getSurfaces().size() > ConvexShape::smMaxSurfaces )
  796. {
  797. text = "WARNING: Reduce the number of surfaces on the selected ConvexShape, only the first 100 will be saved!";
  798. }
  799. Con::executef( statusbar, "setInfo", text.c_str() );
  800. Con::executef( statusbar, "setSelectionObjectsByCount", Con::getIntArg( mConvexSEL == NULL ? 0 : 1 ) );
  801. }
  802. if ( mActiveTool )
  803. mActiveTool->renderScene( updateRect );
  804. ColorI colorHL( 255, 50, 255, 255 );
  805. ColorI colorSEL( 255, 50, 255, 255 );
  806. ColorI colorNA( 255, 255, 255, 100 );
  807. GFXDrawUtil *drawer = GFX->getDrawUtil();
  808. if ( mConvexSEL && !mDragging )
  809. {
  810. if ( mFaceSEL == -1 )
  811. {
  812. GFXStateBlockDesc desc;
  813. desc.setBlend( true );
  814. desc.setZReadWrite( true, true );
  815. Box3F objBox = mConvexSEL->getObjBox();
  816. objBox.scale( mConvexSEL->getScale() );
  817. const MatrixF &objMat = mConvexSEL->getTransform();
  818. Point3F boxPos = objBox.getCenter();
  819. objMat.mulP( boxPos );
  820. drawer->drawObjectBox( desc, objBox.getExtents(), boxPos, objMat, ColorI::WHITE );
  821. }
  822. else
  823. {
  824. mConvexSEL->renderFaceEdges( -1, colorNA );
  825. drawFacePlane( mConvexSEL, mFaceSEL );
  826. }
  827. if ( mConvexHL == mConvexSEL &&
  828. mFaceHL != -1 &&
  829. mFaceHL != mFaceSEL &&
  830. mGizmo->getSelection() == Gizmo::None )
  831. {
  832. mConvexSEL->renderFaceEdges( mFaceHL, colorHL );
  833. }
  834. }
  835. if ( mConvexHL && mConvexHL != mConvexSEL )
  836. {
  837. mConvexHL->renderFaceEdges( -1 );
  838. }
  839. if ( mGizmo->getMode() != RotateMode && mUsingPivot )
  840. {
  841. mUsingPivot = false;
  842. updateGizmoPos();
  843. }
  844. F32 gizmoAlpha = 1.0f;
  845. if ( !mConvexSEL )
  846. gizmoAlpha = 0.0f;
  847. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  848. {
  849. if ( mSettingPivot )
  850. gizmoAlpha = 1.0f;
  851. else
  852. gizmoAlpha = 0.0f;
  853. }
  854. DebugDrawer::get()->render();
  855. {
  856. GFXTransformSaver saver;
  857. // Now draw all the 2d stuff!
  858. GFX->setClipRect(updateRect);
  859. if ( mConvexSEL && mFaceSEL != -1 )
  860. {
  861. Vector< Point3F > lineList;
  862. mConvexSEL->getSurfaceLineList( mFaceSEL, lineList );
  863. MatrixF objToWorld( mConvexSEL->getTransform() );
  864. objToWorld.scale( mConvexSEL->getScale() );
  865. for ( S32 i = 0; i < lineList.size(); i++ )
  866. objToWorld.mulP( lineList[i] );
  867. for ( S32 i = 0; i < lineList.size() - 1; i++ )
  868. {
  869. Point3F p0( lineList[i] );
  870. Point3F p1( lineList[i+1] );
  871. drawLine( p0, p1, colorSEL, 3.0f );
  872. }
  873. }
  874. if ( gizmoAlpha == 1.0f )
  875. {
  876. if ( mGizmoProfile->mode != NoneMode )
  877. mGizmo->renderText( mSaveViewport, mSaveModelview, mSaveProjection );
  878. }
  879. if ( mActiveTool )
  880. mActiveTool->render2D();
  881. }
  882. if ( gizmoAlpha == 1.0f )
  883. mGizmo->renderGizmo( mLastCameraQuery.cameraMatrix, mLastCameraQuery.fov );
  884. }
  885. void GuiConvexEditorCtrl::drawFacePlane( ConvexShape *shape, S32 faceId )
  886. {
  887. // Build a vb of the face points ( in world space ) scaled outward in
  888. // the surface space in x/y with uv coords.
  889. /*
  890. Vector< Point3F > points;
  891. Vector< Point2F > coords;
  892. shape->getSurfaceTriangles( faceId, &points, &coords, false );
  893. if ( points.empty() )
  894. return;
  895. GFXVertexBufferHandle< GFXVertexPCT > vb;
  896. vb.set( GFX, points.size(), GFXBufferTypeVolatile );
  897. GFXVertexPCT *vert = vb.lock();
  898. for ( S32 i = 0; i < points.size(); i++ )
  899. {
  900. vert->point = points[i];
  901. vert->color.set( 255, 255, 255, 200 );
  902. vert->texCoord = coords[i];
  903. vert++;
  904. }
  905. vb.unlock();
  906. GFXTransformSaver saver;
  907. MatrixF renderMat( shape->getTransform() );
  908. renderMat.scale( shape->getScale() );
  909. GFX->multWorld( renderMat );
  910. GFXStateBlockDesc desc;
  911. desc.setBlend( true );
  912. desc.setCullMode( GFXCullNone );
  913. desc.setZReadWrite( true, false );
  914. desc.samplersDefined = true;
  915. desc.samplers[0] = GFXSamplerStateDesc::getWrapLinear();
  916. GFX->setStateBlockByDesc( desc );
  917. GFX->setVertexBuffer( vb );
  918. GFXTexHandle tex( "core/art/grids/512_transp", &GFXStaticTextureSRGBProfile, "ConvexEditor_grid" );
  919. GFX->setTexture( 0, tex );
  920. GFX->setupGenericShaders();
  921. GFX->drawPrimitive( GFXTriangleList, 0, points.size() / 3 );
  922. */
  923. }
  924. void GuiConvexEditorCtrl::scaleFace( ConvexShape *shape, S32 faceId, Point3F scale )
  925. {
  926. if ( !mHasGeometry )
  927. {
  928. mHasGeometry = true;
  929. mSavedGeometry = shape->mGeometry;
  930. mSavedSurfaces = shape->mSurfaces;
  931. }
  932. else
  933. {
  934. shape->mGeometry = mSavedGeometry;
  935. shape->mSurfaces = mSavedSurfaces;
  936. }
  937. if ( shape->mGeometry.faces.size() <= faceId )
  938. return;
  939. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  940. Vector< Point3F > &pointList = shape->mGeometry.points;
  941. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  942. Point3F projScale;
  943. scale.z = 1.0f;
  944. const MatrixF &surfToObj = shape->mSurfaces[ face.id ];
  945. MatrixF objToSurf( surfToObj );
  946. objToSurf.inverse();
  947. for ( S32 i = 0; i < face.points.size(); i++ )
  948. {
  949. Point3F &pnt = pointList[ face.points[i] ];
  950. objToSurf.mulP( pnt );
  951. pnt *= scale;
  952. surfToObj.mulP( pnt );
  953. }
  954. updateModifiedFace( shape, faceId );
  955. }
  956. void GuiConvexEditorCtrl::translateFace( ConvexShape *shape, S32 faceId, const Point3F &displace )
  957. {
  958. if ( !mHasGeometry )
  959. {
  960. mHasGeometry = true;
  961. mSavedGeometry = shape->mGeometry;
  962. mSavedSurfaces = shape->mSurfaces;
  963. }
  964. else
  965. {
  966. shape->mGeometry = mSavedGeometry;
  967. shape->mSurfaces = mSavedSurfaces;
  968. }
  969. if ( shape->mGeometry.faces.size() <= faceId )
  970. return;
  971. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  972. Vector< Point3F > &pointList = shape->mGeometry.points;
  973. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  974. // Transform displacement into object space.
  975. MatrixF worldToObj( shape->getTransform() );
  976. worldToObj.scale( shape->getScale() );
  977. worldToObj.inverse();
  978. Point3F displaceOS;
  979. worldToObj.mulV( displace, &displaceOS );
  980. for ( S32 i = 0; i < face.points.size(); i++ )
  981. {
  982. Point3F &pnt = pointList[ face.points[i] ];
  983. pnt += displaceOS;
  984. }
  985. updateModifiedFace( shape, faceId );
  986. }
  987. void GuiConvexEditorCtrl::updateModifiedFace( ConvexShape *shape, S32 faceId )
  988. {
  989. if ( shape->mGeometry.faces.size() <= faceId )
  990. return;
  991. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  992. Vector< Point3F > &pointList = shape->mGeometry.points;
  993. Vector< ConvexShape::Face > &faceList = shape->mGeometry.faces;
  994. for ( S32 i = 0; i < faceList.size(); i++ )
  995. {
  996. ConvexShape::Face &curFace = faceList[i];
  997. MatrixF &curSurface = shape->mSurfaces[ curFace.id ];
  998. U32 curPntCount = curFace.points.size();
  999. if ( curPntCount < 3 )
  1000. continue;
  1001. // Does this face use any of the points which we have modified?
  1002. // Collect them in correct winding order.
  1003. S32 pId0 = -1;
  1004. for ( S32 j = 0; j < curFace.winding.size(); j++ )
  1005. {
  1006. if ( face.points.contains( curFace.points[ curFace.winding[ j ] ] ) )
  1007. {
  1008. pId0 = j;
  1009. break;
  1010. }
  1011. }
  1012. if ( pId0 == -1 )
  1013. continue;
  1014. S32 pId1 = -1, pId2 = -1;
  1015. pId1 = ( pId0 + 1 ) % curFace.winding.size();
  1016. pId2 = ( pId0 + 2 ) % curFace.winding.size();
  1017. const Point3F &p0 = pointList[ curFace.points[ curFace.winding[ pId0 ] ] ];
  1018. const Point3F &p1 = pointList[ curFace.points[ curFace.winding[ pId1 ] ] ];
  1019. const Point3F &p2 = pointList[ curFace.points[ curFace.winding[ pId2 ] ] ];
  1020. PlaneF newPlane( p0, p1, p2 );
  1021. Point3F uvec = newPlane.getNormal();
  1022. Point3F fvec = curSurface.getForwardVector();
  1023. Point3F rvec = curSurface.getRightVector();
  1024. F32 dt0 = mDot( uvec, fvec );
  1025. F32 dt1 = mDot( uvec, rvec );
  1026. if ( mFabs( dt0 ) < mFabs( dt1 ) )
  1027. {
  1028. rvec = mCross( fvec, uvec );
  1029. rvec.normalizeSafe();
  1030. fvec = mCross( uvec, rvec );
  1031. fvec.normalizeSafe();
  1032. }
  1033. else
  1034. {
  1035. fvec = mCross( uvec, rvec );
  1036. fvec.normalizeSafe();
  1037. rvec = mCross( fvec, uvec );
  1038. rvec.normalizeSafe();
  1039. }
  1040. curSurface.setColumn( 0, rvec );
  1041. curSurface.setColumn( 1, fvec );
  1042. curSurface.setColumn( 2, uvec );
  1043. curSurface.setPosition( newPlane.getPosition() );
  1044. }
  1045. updateShape( shape );
  1046. }
  1047. bool GuiConvexEditorCtrl::isShapeValid( ConvexShape *shape )
  1048. {
  1049. // Test for no-geometry.
  1050. if ( shape->mGeometry.points.empty() )
  1051. return false;
  1052. const Vector<Point3F> &pointList = shape->mGeometry.points;
  1053. const Vector<ConvexShape::Face> &faceList = shape->mGeometry.faces;
  1054. // Test that all points are shared by at least 3 faces.
  1055. for ( S32 i = 0; i < pointList.size(); i++ )
  1056. {
  1057. U32 counter = 0;
  1058. for ( S32 j = 0; j < faceList.size(); j++ )
  1059. {
  1060. if ( faceList[j].points.contains( i ) )
  1061. counter++;
  1062. }
  1063. if ( counter < 3 )
  1064. return false;
  1065. }
  1066. // Test for co-planar faces.
  1067. for ( S32 i = 0; i < shape->mPlanes.size(); i++ )
  1068. {
  1069. for ( S32 j = i + 1; j < shape->mPlanes.size(); j++ )
  1070. {
  1071. F32 d = mDot( shape->mPlanes[i], shape->mPlanes[j] );
  1072. if ( d > 0.999f )
  1073. return false;
  1074. }
  1075. }
  1076. // Test for faces with zero or negative area.
  1077. for ( S32 i = 0; i < shape->mGeometry.faces.size(); i++ )
  1078. {
  1079. if ( shape->mGeometry.faces[i].area < 0.0f )
  1080. return false;
  1081. if ( shape->mGeometry.faces[i].triangles.empty() )
  1082. return false;
  1083. }
  1084. return true;
  1085. }
  1086. void GuiConvexEditorCtrl::setupShape( ConvexShape *shape )
  1087. {
  1088. shape->setField( "material", mMaterialName );
  1089. shape->registerObject();
  1090. updateShape( shape );
  1091. SimGroup *group;
  1092. if ( Sim::findObject( "missionGroup", group ) )
  1093. group->addObject( shape );
  1094. }
  1095. void GuiConvexEditorCtrl::updateShape( ConvexShape *shape, S32 offsetFace )
  1096. {
  1097. shape->_updateGeometry( true );
  1098. /*
  1099. if ( offsetFace != -1 )
  1100. {
  1101. shape->mSurfaces[ offsetFace ].setPosition( mPivotPos );
  1102. }*/
  1103. synchClientObject( shape );
  1104. }
  1105. void GuiConvexEditorCtrl::synchClientObject( const ConvexShape *serverConvex )
  1106. {
  1107. if ( serverConvex->getClientObject() )
  1108. {
  1109. ConvexShape *clientConvex = static_cast< ConvexShape* >( serverConvex->getClientObject() );
  1110. clientConvex->setScale( serverConvex->getScale() );
  1111. clientConvex->setTransform( serverConvex->getTransform() );
  1112. clientConvex->mSurfaces.clear();
  1113. clientConvex->mSurfaces.merge( serverConvex->mSurfaces );
  1114. clientConvex->_updateGeometry(true);
  1115. }
  1116. }
  1117. void GuiConvexEditorCtrl::updateGizmoPos()
  1118. {
  1119. if ( mConvexSEL )
  1120. {
  1121. if ( mFaceSEL != -1 )
  1122. {
  1123. MatrixF surfMat = mConvexSEL->getSurfaceWorldMat( mFaceSEL );
  1124. MatrixF objToWorld( mConvexSEL->getTransform() );
  1125. objToWorld.scale( mConvexSEL->getScale() );
  1126. Point3F gizmoPos(0,0,0);
  1127. if ( mUsingPivot )
  1128. {
  1129. gizmoPos = mPivotPos;
  1130. }
  1131. else
  1132. {
  1133. Point3F faceCenterPnt = mConvexSEL->mSurfaces[ mFaceSEL ].getPosition();
  1134. objToWorld.mulP( faceCenterPnt );
  1135. mGizmoMatOffset = surfMat.getPosition() - faceCenterPnt;
  1136. gizmoPos = faceCenterPnt;
  1137. }
  1138. mGizmo->set( surfMat, gizmoPos, Point3F::One );
  1139. }
  1140. else
  1141. {
  1142. mGizmoMatOffset = Point3F::Zero;
  1143. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  1144. }
  1145. }
  1146. }
  1147. bool GuiConvexEditorCtrl::setActiveTool( ConvexEditorTool *tool )
  1148. {
  1149. if ( mActiveTool == tool )
  1150. return false;
  1151. ConvexEditorTool *prevTool = mActiveTool;
  1152. ConvexEditorTool *newTool = tool;
  1153. if ( prevTool )
  1154. prevTool->onDeactivated( newTool );
  1155. mActiveTool = newTool;
  1156. if ( newTool )
  1157. newTool->onActivated( prevTool );
  1158. return true;
  1159. }
  1160. bool GuiConvexEditorCtrl::handleEscape()
  1161. {
  1162. if ( mActiveTool )
  1163. {
  1164. mActiveTool->onDeactivated( NULL );
  1165. mActiveTool = NULL;
  1166. return true;
  1167. }
  1168. if ( mFaceSEL != -1 )
  1169. {
  1170. setSelection( mConvexSEL, -1 );
  1171. return true;
  1172. }
  1173. if ( mConvexSEL )
  1174. {
  1175. setSelection( NULL, -1 );
  1176. return true;
  1177. }
  1178. return false;
  1179. }
  1180. bool GuiConvexEditorCtrl::handleDelete()
  1181. {
  1182. if ( mActiveTool )
  1183. {
  1184. mActiveTool->onDeactivated( NULL );
  1185. mActiveTool = NULL;
  1186. }
  1187. if ( mConvexSEL )
  1188. {
  1189. if ( mFaceSEL != -1 )
  1190. {
  1191. submitUndo( ModifyShape, mConvexSEL );
  1192. mConvexSEL->mSurfaces.erase_fast( mFaceSEL );
  1193. updateShape( mConvexSEL );
  1194. if ( !isShapeValid( mConvexSEL ) )
  1195. {
  1196. S32 selFace = mFaceSEL;
  1197. mLastUndo->undo();
  1198. mFaceSEL = selFace;
  1199. updateShape( mConvexSEL );
  1200. updateGizmoPos();
  1201. }
  1202. else
  1203. {
  1204. setSelection( mConvexSEL, -1 );
  1205. }
  1206. }
  1207. else
  1208. {
  1209. // Grab the mission editor undo manager.
  1210. UndoManager *undoMan = NULL;
  1211. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1212. {
  1213. Con::errorf( "GuiConvexEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  1214. }
  1215. else
  1216. {
  1217. // Create the UndoAction.
  1218. MEDeleteUndoAction *action = new MEDeleteUndoAction("Deleted ConvexShape");
  1219. action->deleteObject( mConvexSEL );
  1220. mIsDirty = true;
  1221. mFaceHL = -1;
  1222. setSelection( NULL, -1 );
  1223. // Submit it.
  1224. undoMan->addAction( action );
  1225. }
  1226. }
  1227. }
  1228. return true;
  1229. }
  1230. bool GuiConvexEditorCtrl::hasSelection() const
  1231. {
  1232. return mConvexSEL != NULL;
  1233. }
  1234. void GuiConvexEditorCtrl::clearSelection()
  1235. {
  1236. mFaceHL = -1;
  1237. mConvexHL = NULL;
  1238. setSelection( NULL, -1 );
  1239. }
  1240. void GuiConvexEditorCtrl::handleDeselect()
  1241. {
  1242. if ( mActiveTool )
  1243. {
  1244. mActiveTool->onDeactivated( NULL );
  1245. mActiveTool = NULL;
  1246. }
  1247. mFaceHL = -1;
  1248. mConvexHL = NULL;
  1249. setSelection( NULL, -1 );
  1250. }
  1251. void GuiConvexEditorCtrl::setSelection( ConvexShape *shape, S32 faceId )
  1252. {
  1253. mFaceSEL = faceId;
  1254. mConvexSEL = shape;
  1255. updateGizmoPos();
  1256. Con::executef( this, "onSelectionChanged", shape ? shape->getIdString() : "", Con::getIntArg(faceId) );
  1257. }
  1258. void GuiConvexEditorCtrl::_prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* state )
  1259. {
  1260. if ( !isAwake() )
  1261. return;
  1262. /*
  1263. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1264. ri->type = RenderPassManager::RIT_Editor;
  1265. ri->renderDelegate.bind( this, &GuiConvexEditorCtrl::_renderObject );
  1266. ri->defaultKey = 100;
  1267. state->getRenderPass()->addInst( ri );
  1268. */
  1269. }
  1270. void GuiConvexEditorCtrl::_renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst )
  1271. {
  1272. }
  1273. void GuiConvexEditorCtrl::submitUndo( UndoType type, ConvexShape *shape )
  1274. {
  1275. Vector< ConvexShape* > shapes;
  1276. shapes.push_back( shape );
  1277. submitUndo( type, shapes );
  1278. }
  1279. void GuiConvexEditorCtrl::submitUndo( UndoType type, const Vector<ConvexShape*> &shapes )
  1280. {
  1281. // Grab the mission editor undo manager.
  1282. Sim::findObject( "EUndoManager", mUndoManager );
  1283. if ( !mUndoManager )
  1284. {
  1285. Con::errorf( "GuiConvexEditorCtrl::submitUndo() - EUndoManager not found!" );
  1286. return;
  1287. }
  1288. if ( type == ModifyShape )
  1289. {
  1290. // Setup the action.
  1291. GuiConvexEditorUndoAction *action = new GuiConvexEditorUndoAction( "Modified a ConvexShape" );
  1292. ConvexShape *shape = shapes.first();
  1293. action->mObjId = shape->getId();
  1294. action->mEditor = this;
  1295. action->mSavedObjToWorld = shape->getTransform();
  1296. action->mSavedScale = shape->getScale();
  1297. action->mSavedSurfaces.merge( shape->mSurfaces );
  1298. action->mUndoManager = mUndoManager;
  1299. mUndoManager->addAction( action );
  1300. mLastUndo = action;
  1301. }
  1302. else if ( type == CreateShape )
  1303. {
  1304. MECreateUndoAction *action = new MECreateUndoAction( "Create ConvexShape" );
  1305. for ( S32 i = 0; i < shapes.size(); i++ )
  1306. action->addObject( shapes[i] );
  1307. mUndoManager->addAction( action );
  1308. mLastUndo = action;
  1309. }
  1310. else if ( type == DeleteShape )
  1311. {
  1312. MEDeleteUndoAction *action = new MEDeleteUndoAction( "Deleted ConvexShape" );
  1313. for ( S32 i = 0; i < shapes.size(); i++ )
  1314. action->deleteObject( shapes[i] );
  1315. mUndoManager->addAction( action );
  1316. mLastUndo = action;
  1317. }
  1318. else if ( type == HollowShape )
  1319. {
  1320. CompoundUndoAction *action = new CompoundUndoAction( "Hollow ConvexShape" );
  1321. MECreateUndoAction *createAction = new MECreateUndoAction();
  1322. MEDeleteUndoAction *deleteAction = new MEDeleteUndoAction();
  1323. deleteAction->deleteObject( shapes.first() );
  1324. for ( S32 i = 1; i < shapes.size(); i++ )
  1325. createAction->addObject( shapes[i] );
  1326. action->addAction( deleteAction );
  1327. action->addAction( createAction );
  1328. mUndoManager->addAction( action );
  1329. mLastUndo = action;
  1330. }
  1331. mIsDirty = true;
  1332. }
  1333. bool GuiConvexEditorCtrl::_cursorCastCallback( RayInfo* ri )
  1334. {
  1335. // Reject anything that's not a ConvexShape.
  1336. return dynamic_cast< ConvexShape* >( ri->object );
  1337. }
  1338. bool GuiConvexEditorCtrl::_cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace )
  1339. {
  1340. RayInfo ri;
  1341. if ( gServerContainer.castRay( event.pos, event.pos + event.vec * 10000.0f, StaticShapeObjectType, &ri, &GuiConvexEditorCtrl::_cursorCastCallback ) &&
  1342. dynamic_cast< ConvexShape* >( ri.object ) )
  1343. {
  1344. // Do not select or edit ConvexShapes that are within a Prefab.
  1345. if ( Prefab::getPrefabByChild( ri.object ) )
  1346. return false;
  1347. *hitShape = static_cast< ConvexShape* >( ri.object );
  1348. *hitFace = ri.face;
  1349. mLastRayInfo = ri;
  1350. return true;
  1351. }
  1352. return false;
  1353. }
  1354. void GuiConvexEditorCtrl::setPivotPos( ConvexShape *shape, S32 faceId, const Gui3DMouseEvent &event )
  1355. {
  1356. PlaneF plane;
  1357. mTransformPlane( shape->getTransform(), shape->getScale(), shape->mPlanes[ faceId ], &plane );
  1358. Point3F start( event.pos );
  1359. Point3F end( start + event.vec * 10000.0f );
  1360. F32 t = plane.intersect( start, end );
  1361. if ( t >= 0.0f && t <= 1.0f )
  1362. {
  1363. Point3F hitPos;
  1364. hitPos.interpolate( start, end, t );
  1365. mPivotPos = hitPos;
  1366. mUsingPivot = true;
  1367. MatrixF worldToObj( shape->getTransform() );
  1368. worldToObj.scale( shape->getScale() );
  1369. worldToObj.inverse();
  1370. Point3F objPivotPos( mPivotPos );
  1371. worldToObj.mulP( objPivotPos );
  1372. updateGizmoPos();
  1373. }
  1374. }
  1375. void GuiConvexEditorCtrl::cleanMatrix( MatrixF &mat )
  1376. {
  1377. if ( mat.isAffine() )
  1378. return;
  1379. VectorF col0 = mat.getColumn3F(0);
  1380. VectorF col1 = mat.getColumn3F(1);
  1381. VectorF col2 = mat.getColumn3F(2);
  1382. col0.normalize();
  1383. col1.normalize();
  1384. col2.normalize();
  1385. col2 = mCross( col0, col1 );
  1386. col2.normalize();
  1387. col1 = mCross( col2, col0 );
  1388. col1.normalize();
  1389. col0 = mCross( col1, col2 );
  1390. col0.normalize();
  1391. mat.setColumn(0,col0);
  1392. mat.setColumn(1,col1);
  1393. mat.setColumn(2,col2);
  1394. AssertFatal( mat.isAffine(), "GuiConvexEditorCtrl::cleanMatrix, non-affine matrix" );
  1395. }
  1396. S32 GuiConvexEditorCtrl::getEdgeByPoints( ConvexShape *shape, S32 faceId, S32 p0, S32 p1 )
  1397. {
  1398. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1399. for ( S32 i = 0; i < face.edges.size(); i++ )
  1400. {
  1401. const ConvexShape::Edge &edge = face.edges[i];
  1402. if ( edge.p0 != p0 && edge.p0 != p1 )
  1403. continue;
  1404. if ( edge.p1 != p0 && edge.p1 != p1 )
  1405. continue;
  1406. return i;
  1407. }
  1408. return -1;
  1409. }
  1410. bool GuiConvexEditorCtrl::getEdgesTouchingPoint( ConvexShape *shape, S32 faceId, S32 pId, Vector< U32 > &edgeIdxList, S32 excludeEdge )
  1411. {
  1412. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1413. const Vector< ConvexShape::Edge > &edgeList = face.edges;
  1414. for ( S32 i = 0; i < edgeList.size(); i++ )
  1415. {
  1416. if ( i == excludeEdge )
  1417. continue;
  1418. const ConvexShape::Edge &curEdge = edgeList[i];
  1419. if ( curEdge.p0 == pId || curEdge.p1 == pId )
  1420. edgeIdxList.push_back(i);
  1421. }
  1422. return !edgeIdxList.empty();
  1423. }
  1424. void GuiConvexEditorUndoAction::undo()
  1425. {
  1426. ConvexShape *object = NULL;
  1427. if ( !Sim::findObject( mObjId, object ) )
  1428. return;
  1429. // Temporarily save the ConvexShape current data.
  1430. Vector< MatrixF > tempSurfaces;
  1431. tempSurfaces.merge( object->mSurfaces );
  1432. MatrixF tempObjToWorld( object->getTransform() );
  1433. Point3F tempScale( object->getScale() );
  1434. // Restore the Object to the UndoAction state.
  1435. object->mSurfaces.clear();
  1436. object->mSurfaces.merge( mSavedSurfaces );
  1437. object->setScale( mSavedScale );
  1438. object->setTransform( mSavedObjToWorld );
  1439. // Regenerate the ConvexShape and synch the client object.
  1440. object->_updateGeometry();
  1441. GuiConvexEditorCtrl::synchClientObject( object );
  1442. // If applicable set the selected ConvexShape and face
  1443. // on the editor.
  1444. mEditor->setSelection( object, -1 );
  1445. mEditor->updateGizmoPos();
  1446. // Now save the previous ConvexShape data in this UndoAction
  1447. // since an undo action must become a redo action and vice-versa
  1448. mSavedObjToWorld = tempObjToWorld;
  1449. mSavedScale = tempScale;
  1450. mSavedSurfaces.clear();
  1451. mSavedSurfaces.merge( tempSurfaces );
  1452. }
  1453. ConvexEditorCreateTool::ConvexEditorCreateTool( GuiConvexEditorCtrl *editor )
  1454. : Parent( editor ),
  1455. mStage( -1 ),
  1456. mNewConvex( NULL )
  1457. {
  1458. }
  1459. void ConvexEditorCreateTool::onActivated( ConvexEditorTool *prevTool )
  1460. {
  1461. mEditor->clearSelection();
  1462. mStage = -1;
  1463. mNewConvex = NULL;
  1464. }
  1465. void ConvexEditorCreateTool::onDeactivated( ConvexEditorTool *newTool )
  1466. {
  1467. if ( mNewConvex )
  1468. mNewConvex->deleteObject();
  1469. mStage = -1;
  1470. mNewConvex = NULL;
  1471. mEditor->mouseUnlock();
  1472. }
  1473. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseDown( const Gui3DMouseEvent &event )
  1474. {
  1475. if ( mStage == -1 )
  1476. {
  1477. mEditor->setFirstResponder();
  1478. mEditor->mouseLock();
  1479. Point3F start( event.pos );
  1480. Point3F end( event.pos + event.vec * 10000.0f );
  1481. RayInfo ri;
  1482. bool hit = gServerContainer.castRay( event.pos, end, STATIC_COLLISION_TYPEMASK, &ri );
  1483. MatrixF objMat( true );
  1484. // Calculate the orientation matrix of the new ConvexShape
  1485. // based on what has been clicked.
  1486. if ( !hit )
  1487. {
  1488. objMat.setPosition( event.pos + event.vec * 100.0f );
  1489. }
  1490. else
  1491. {
  1492. if ( dynamic_cast< ConvexShape* >( ri.object ) )
  1493. {
  1494. ConvexShape *hitShape = static_cast< ConvexShape* >( ri.object );
  1495. objMat = hitShape->getSurfaceWorldMat( ri.face );
  1496. objMat.setPosition( ri.point );
  1497. }
  1498. else
  1499. {
  1500. Point3F rvec;
  1501. Point3F fvec( mEditor->getCameraMat().getForwardVector() );
  1502. Point3F uvec( ri.normal );
  1503. rvec = mCross( fvec, uvec );
  1504. if ( rvec.isZero() )
  1505. {
  1506. fvec = mEditor->getCameraMat().getRightVector();
  1507. rvec = mCross( fvec, uvec );
  1508. }
  1509. rvec.normalizeSafe();
  1510. fvec = mCross( uvec, rvec );
  1511. fvec.normalizeSafe();
  1512. uvec = mCross( rvec, fvec );
  1513. uvec.normalizeSafe();
  1514. objMat.setColumn( 0, rvec );
  1515. objMat.setColumn( 1, fvec );
  1516. objMat.setColumn( 2, uvec );
  1517. objMat.setPosition( ri.point );
  1518. }
  1519. }
  1520. mNewConvex = new ConvexShape();
  1521. mNewConvex->setTransform( objMat );
  1522. mNewConvex->setField( "material", Parent::mEditor->mMaterialName );
  1523. mNewConvex->registerObject();
  1524. mPlaneSizes.set( 0.1f, 0.1f, 0.1f );
  1525. mNewConvex->resizePlanes( mPlaneSizes );
  1526. mEditor->updateShape( mNewConvex );
  1527. mTransform = objMat;
  1528. mCreatePlane.set( objMat.getPosition(), objMat.getUpVector() );
  1529. }
  1530. else if ( mStage == 0 )
  1531. {
  1532. // Handle this on mouseUp
  1533. }
  1534. return Handled;
  1535. }
  1536. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseUp( const Gui3DMouseEvent &event )
  1537. {
  1538. if ( mNewConvex && mStage == -1 )
  1539. {
  1540. mStage = 0;
  1541. mCreatePlane = PlaneF( mNewConvex->getPosition(), mNewConvex->getTransform().getForwardVector() );
  1542. mTransform.setPosition( mNewConvex->getPosition() );
  1543. return Handled;
  1544. }
  1545. else if ( mStage == 0 )
  1546. {
  1547. SimGroup *mg;
  1548. Sim::findObject( "MissionGroup", mg );
  1549. mg->addObject( mNewConvex );
  1550. mStage = -1;
  1551. // Grab the mission editor undo manager.
  1552. UndoManager *undoMan = NULL;
  1553. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1554. {
  1555. Con::errorf( "ConvexEditorCreateTool::on3DMouseDown() - EUndoManager not found!" );
  1556. mNewConvex = NULL;
  1557. return Failed;
  1558. }
  1559. // Create the UndoAction.
  1560. MECreateUndoAction *action = new MECreateUndoAction("Create ConvexShape");
  1561. action->addObject( mNewConvex );
  1562. // Submit it.
  1563. undoMan->addAction( action );
  1564. mEditor->setField( "isDirty", "1" );
  1565. mEditor->setSelection( mNewConvex, -1 );
  1566. mNewConvex = NULL;
  1567. mEditor->mouseUnlock();
  1568. return Done;
  1569. }
  1570. return Done;
  1571. }
  1572. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseMove( const Gui3DMouseEvent &event )
  1573. {
  1574. if ( mStage == 0 )
  1575. {
  1576. Point3F start( event.pos );
  1577. Point3F end( start + event.vec * 10000.0f );
  1578. F32 t = mCreatePlane.intersect( start, end );
  1579. Point3F hitPos;
  1580. if ( t < 0.0f || t > 1.0f )
  1581. return Handled;
  1582. hitPos.interpolate( start, end, t );
  1583. MatrixF worldToObj( mTransform );
  1584. worldToObj.inverse();
  1585. worldToObj.mulP( hitPos );
  1586. F32 delta = ( hitPos.z );
  1587. mPlaneSizes.z = getMax( 0.1f, delta );
  1588. mNewConvex->resizePlanes( mPlaneSizes );
  1589. mEditor->updateShape( mNewConvex );
  1590. Point3F pos( mTransform.getPosition() );
  1591. pos += mPlaneSizes.z * 0.5f * mTransform.getUpVector();
  1592. mNewConvex->setPosition( pos );
  1593. }
  1594. return Handled;
  1595. }
  1596. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseDragged( const Gui3DMouseEvent &event )
  1597. {
  1598. if ( !mNewConvex || mStage != -1 )
  1599. return Handled;
  1600. Point3F start( event.pos );
  1601. Point3F end( event.pos + event.vec * 10000.0f );
  1602. F32 t = mCreatePlane.intersect( start, end );
  1603. if ( t < 0.0f || t > 1.0f )
  1604. return Handled;
  1605. Point3F hitPos;
  1606. hitPos.interpolate( start, end, t );
  1607. MatrixF xfm( mTransform );
  1608. xfm.inverse();
  1609. xfm.mulP( hitPos);
  1610. Point3F scale;
  1611. scale.x = getMax( mFabs( hitPos.x ), 0.1f );
  1612. scale.y = getMax( mFabs( hitPos.y ), 0.1f );
  1613. scale.z = 0.1f;
  1614. mNewConvex->resizePlanes( scale );
  1615. mPlaneSizes = scale;
  1616. mEditor->updateShape( mNewConvex );
  1617. Point3F pos( mTransform.getPosition() );
  1618. pos += mTransform.getRightVector() * hitPos.x * 0.5f;
  1619. pos += mTransform.getForwardVector() * hitPos.y * 0.5f;
  1620. mNewConvex->setPosition( pos );
  1621. return Handled;
  1622. }
  1623. void ConvexEditorCreateTool::renderScene( const RectI &updateRect )
  1624. {
  1625. }
  1626. ConvexShape* ConvexEditorCreateTool::extrudeShapeFromFace( ConvexShape *inShape, S32 inFaceId )
  1627. {
  1628. ConvexShape::Geometry &inShapeGeometry = inShape->getGeometry();
  1629. ConvexShape::Face &inFace = inShapeGeometry.faces[inFaceId];
  1630. Vector< Point3F > &inShapePointList = inShapeGeometry.points;
  1631. Vector< MatrixF > &inShapeSurfaces = inShape->getSurfaces();
  1632. S32 shapeFaceCount = inFace.edges.size() + 2;
  1633. MatrixF inShapeToWorld( inShape->getTransform() );
  1634. inShapeToWorld.scale( inShape->getScale() );
  1635. //MatrixF inWorldToShape( inShapeToWorld );
  1636. //inWorldToShape.inverse();
  1637. MatrixF shapeToWorld;
  1638. shapeToWorld.mul( inShape->getTransform(), inShapeSurfaces[inFaceId] );
  1639. Point3F tmp( inShapeSurfaces[inFaceId].getPosition() );
  1640. inShapeToWorld.mulP( tmp );
  1641. shapeToWorld.setPosition( tmp );
  1642. MatrixF worldToShape( shapeToWorld );
  1643. worldToShape.inverse();
  1644. MatrixF inShapeToNewShape;
  1645. inShapeToNewShape.mul( inShapeToWorld, worldToShape );
  1646. ConvexShape *newShape = new ConvexShape;
  1647. newShape->setTransform( shapeToWorld );
  1648. Vector< MatrixF > &shapeSurfaces = newShape->getSurfaces();
  1649. shapeSurfaces.setSize( shapeFaceCount );
  1650. //shapeSurfaces.setSize( 2 );
  1651. const Point3F &shapePos = shapeToWorld.getPosition();
  1652. shapeSurfaces[0].identity();
  1653. shapeSurfaces[1].identity();
  1654. shapeSurfaces[1].setColumn( 0, -shapeSurfaces[1].getColumn3F(0) );
  1655. shapeSurfaces[1].setColumn( 2, -shapeSurfaces[1].getColumn3F(2) );
  1656. for ( S32 i = 0; i < inFace.winding.size(); i++ )
  1657. {
  1658. Point3F p0 = inShapePointList[ inFace.points[ inFace.winding[ i ] ] ];
  1659. Point3F p1;
  1660. if ( i+1 < inFace.winding.size() )
  1661. p1 = inShapePointList[ inFace.points[ inFace.winding[ i+1 ] ] ];
  1662. else
  1663. p1 = inShapePointList[ inFace.points[ inFace.winding[ 0 ] ] ];
  1664. inShapeToWorld.mulP( p0 );
  1665. inShapeToWorld.mulP( p1 );
  1666. Point3F newPos = MathUtils::mClosestPointOnSegment( p0, p1, shapePos );
  1667. Point3F rvec = p0 - p1;
  1668. rvec.normalizeSafe();
  1669. Point3F fvec = shapeToWorld.getUpVector();
  1670. Point3F uvec = mCross( rvec, fvec );
  1671. if ( i + 2 >= shapeSurfaces.size() )
  1672. continue;
  1673. //F32 dt = mDot( shapeToWorld.getUpVector(), rvec );
  1674. //AssertFatal( mIsZero( dt ), "bad" );
  1675. MatrixF &surf = shapeSurfaces[i+2];
  1676. surf.identity();
  1677. surf.setColumn( 0, rvec );
  1678. surf.setColumn( 1, fvec );
  1679. surf.setColumn( 2, uvec );
  1680. surf.setPosition( newPos );
  1681. surf.mulL( worldToShape );
  1682. }
  1683. //newShape->setField( "material", Parent::mEditor->mMaterialName );
  1684. newShape->setField("material", inShape->getMaterialName());
  1685. newShape->registerObject();
  1686. mEditor->updateShape( newShape );
  1687. SimGroup *group;
  1688. if ( Sim::findObject( "missionGroup", group ) )
  1689. group->addObject( newShape );
  1690. return newShape;
  1691. }
  1692. void GuiConvexEditorCtrl::hollowShape( ConvexShape *shape, F32 thickness )
  1693. {
  1694. // Create a new Convex for each face of the original shape.
  1695. // This is the same as an extrude from face operation going inward by the thickness
  1696. // for every face.
  1697. Vector< ConvexShape* > convexList;
  1698. for ( S32 i = 0; i < shape->mGeometry.faces.size(); i++ )
  1699. {
  1700. ConvexShape *faceShape = mCreateTool->extrudeShapeFromFace( shape, i );
  1701. MatrixF &inwardFace = faceShape->mSurfaces[1];
  1702. //MatrixF &outwardFace = faceShape->mSurfaces[0];
  1703. Point3F invec = inwardFace.getUpVector();
  1704. inwardFace.setPosition( inwardFace.getPosition() + invec * thickness );
  1705. updateShape( faceShape );
  1706. convexList.push_back( faceShape );
  1707. }
  1708. convexList.push_front( shape );
  1709. submitUndo( HollowShape, convexList );
  1710. }
  1711. void GuiConvexEditorCtrl::hollowSelection()
  1712. {
  1713. if ( mConvexSEL )
  1714. {
  1715. hollowShape( mConvexSEL, 0.15f );
  1716. setSelection( NULL, -1 );
  1717. }
  1718. }
  1719. void GuiConvexEditorCtrl::recenterSelection()
  1720. {
  1721. if ( mConvexSEL )
  1722. {
  1723. recenterShape( mConvexSEL );
  1724. updateGizmoPos();
  1725. }
  1726. }
  1727. void GuiConvexEditorCtrl::recenterShape( ConvexShape *shape )
  1728. {
  1729. submitUndo( ModifyShape, shape );
  1730. shape->recenter();
  1731. synchClientObject( shape );
  1732. }
  1733. void GuiConvexEditorCtrl::dropSelectionAtScreenCenter()
  1734. {
  1735. // This code copied from WorldEditor.
  1736. // All the dropping code would be moved to somewhere common, but its not.
  1737. if ( !mConvexSEL )
  1738. return;
  1739. // Calculate the center of the screen (in global screen coordinates)
  1740. Point2I offset = localToGlobalCoord(Point2I(0,0));
  1741. Point3F sp(F32(offset.x + F32(getExtent().x / 2)), F32(offset.y + (getExtent().y / 2)), 1.0f);
  1742. // Calculate the view distance to fit the selection
  1743. // within the camera's view.
  1744. const Box3F bounds = mConvexSEL->getWorldBox();
  1745. F32 radius = bounds.len()*0.5f;
  1746. F32 viewdist = calculateViewDistance(radius);
  1747. // Be careful of infinite sized objects, or just large ones in general.
  1748. if(viewdist > 100.0f )
  1749. viewdist = 100.0f;
  1750. // Position the selection
  1751. mConvexSEL->setPosition( smCamPos + smCamMatrix.getForwardVector() * viewdist );
  1752. synchClientObject( mConvexSEL );
  1753. updateGizmoPos();
  1754. }
  1755. void GuiConvexEditorCtrl::splitSelectedFace()
  1756. {
  1757. if ( !mConvexSEL || mFaceSEL == -1 )
  1758. return;
  1759. if ( !isShapeValid( mConvexSEL ) )
  1760. return;
  1761. mLastValidShape = mConvexSEL->mSurfaces;
  1762. const F32 radians = mDegToRad( 15.0f );
  1763. Point3F rot( 0, 0, 0 );
  1764. MatrixF rotMat( true );
  1765. mConvexSEL->mSurfaces.increment();
  1766. MatrixF &dstMat = mConvexSEL->mSurfaces.last();
  1767. const MatrixF &srcMat = mConvexSEL->mSurfaces[mFaceSEL];
  1768. for ( S32 i = 0; i < 6; i++ )
  1769. {
  1770. F32 sign = i > 2 ? -1.0f : 1.0f;
  1771. U32 idx = i % 3;
  1772. rot.zero();
  1773. rot[idx] = sign * radians;
  1774. rotMat.set( (EulerF)rot );
  1775. dstMat = srcMat * rotMat;
  1776. updateShape( mConvexSEL );
  1777. if ( isShapeValid( mConvexSEL ) )
  1778. {
  1779. mSavedSurfaces = mConvexSEL->mSurfaces;
  1780. mConvexSEL->mSurfaces = mLastValidShape;
  1781. submitUndo( ModifyShape, mConvexSEL );
  1782. mConvexSEL->mSurfaces = mSavedSurfaces;
  1783. mLastValidShape = mSavedSurfaces;
  1784. setSelection( mConvexSEL, mConvexSEL->mSurfaces.size() - 1 );
  1785. return;
  1786. }
  1787. }
  1788. mConvexSEL->mSurfaces = mLastValidShape;
  1789. updateShape( mConvexSEL );
  1790. updateGizmoPos();
  1791. }
  1792. SceneObject* GuiConvexEditorCtrl::createPolyhedralObject(const char* className, SceneObject* geometryProvider)
  1793. {
  1794. if (!geometryProvider)
  1795. {
  1796. Con::errorf("WorldEditor::createPolyhedralObject - Invalid geometry provider!");
  1797. return NULL;
  1798. }
  1799. if (!className || !className[0])
  1800. {
  1801. Con::errorf("WorldEditor::createPolyhedralObject - Invalid class name");
  1802. return NULL;
  1803. }
  1804. AbstractClassRep* classRep = AbstractClassRep::findClassRep(className);
  1805. if (!classRep)
  1806. {
  1807. Con::errorf("WorldEditor::createPolyhedralObject - No such class: %s", className);
  1808. return NULL;
  1809. }
  1810. // We don't want the extracted poly list to be affected by the object's
  1811. // current transform and scale so temporarily reset them.
  1812. MatrixF savedTransform = geometryProvider->getTransform();
  1813. Point3F savedScale = geometryProvider->getScale();
  1814. geometryProvider->setTransform(MatrixF::Identity);
  1815. geometryProvider->setScale(Point3F(1.f, 1.f, 1.f));
  1816. // Extract the geometry. Use the object-space bounding volumes
  1817. // as we have moved the object to the origin for the moment.
  1818. OptimizedPolyList polyList;
  1819. if (!geometryProvider->buildPolyList(PLC_Export, &polyList, geometryProvider->getObjBox(), geometryProvider->getObjBox().getBoundingSphere()))
  1820. {
  1821. Con::errorf("WorldEditor::createPolyhedralObject - Failed to extract geometry!");
  1822. return NULL;
  1823. }
  1824. // Restore the object's original transform.
  1825. geometryProvider->setTransform(savedTransform);
  1826. geometryProvider->setScale(savedScale);
  1827. // Create the object.
  1828. SceneObject* object = dynamic_cast< SceneObject* >(classRep->create());
  1829. if (!Object)
  1830. {
  1831. Con::errorf("WorldEditor::createPolyhedralObject - Could not create SceneObject with class '%s'", className);
  1832. return NULL;
  1833. }
  1834. // Convert the polylist to a polyhedron.
  1835. Polyhedron polyhedron = polyList.toPolyhedron();
  1836. // Add the vertex data.
  1837. const U32 numPoints = polyhedron.getNumPoints();
  1838. const Point3F* points = polyhedron.getPoints();
  1839. for (U32 i = 0; i < numPoints; ++i)
  1840. {
  1841. static StringTableEntry sPoint = StringTable->insert("point");
  1842. object->setDataField(sPoint, NULL, EngineMarshallData(points[i]));
  1843. }
  1844. // Add the plane data.
  1845. const U32 numPlanes = polyhedron.getNumPlanes();
  1846. const PlaneF* planes = polyhedron.getPlanes();
  1847. for (U32 i = 0; i < numPlanes; ++i)
  1848. {
  1849. static StringTableEntry sPlane = StringTable->insert("plane");
  1850. const PlaneF& plane = planes[i];
  1851. char buffer[1024];
  1852. dSprintf(buffer, sizeof(buffer), "%g %g %g %g", plane.x, plane.y, plane.z, plane.d);
  1853. object->setDataField(sPlane, NULL, buffer);
  1854. }
  1855. // Add the edge data.
  1856. const U32 numEdges = polyhedron.getNumEdges();
  1857. const Polyhedron::Edge* edges = polyhedron.getEdges();
  1858. for (U32 i = 0; i < numEdges; ++i)
  1859. {
  1860. static StringTableEntry sEdge = StringTable->insert("edge");
  1861. const Polyhedron::Edge& edge = edges[i];
  1862. char buffer[1024];
  1863. dSprintf(buffer, sizeof(buffer), "%i %i %i %i ",
  1864. edge.face[0], edge.face[1],
  1865. edge.vertex[0], edge.vertex[1]
  1866. );
  1867. object->setDataField(sEdge, NULL, buffer);
  1868. }
  1869. // Set the transform.
  1870. object->setTransform(savedTransform);
  1871. object->setScale(savedScale);
  1872. // Register and return the object.
  1873. if (!object->registerObject())
  1874. {
  1875. Con::errorf("WorldEditor::createPolyhedralObject - Failed to register object!");
  1876. delete object;
  1877. return NULL;
  1878. }
  1879. return object;
  1880. }
  1881. ConvexShape* GuiConvexEditorCtrl::createConvexShapeFrom(SceneObject* polyObject)
  1882. {
  1883. if (!polyObject)
  1884. {
  1885. Con::errorf("WorldEditor::createConvexShapeFrom - Invalid object");
  1886. return NULL;
  1887. }
  1888. IScenePolyhedralObject* iPoly = dynamic_cast< IScenePolyhedralObject* >(polyObject);
  1889. if (!iPoly)
  1890. {
  1891. Con::errorf("WorldEditor::createConvexShapeFrom - Not a polyhedral object!");
  1892. return NULL;
  1893. }
  1894. // Get polyhedron.
  1895. AnyPolyhedron polyhedron = iPoly->ToAnyPolyhedron();
  1896. const U32 numPlanes = polyhedron.getNumPlanes();
  1897. if (!numPlanes)
  1898. {
  1899. Con::errorf("WorldEditor::createConvexShapeFrom - Object returned no valid polyhedron");
  1900. return NULL;
  1901. }
  1902. // Create a ConvexShape.
  1903. ConvexShape* shape = new ConvexShape();
  1904. // Add all planes.
  1905. for (U32 i = 0; i < numPlanes; ++i)
  1906. {
  1907. const PlaneF& plane = polyhedron.getPlanes()[i];
  1908. // Polyhedron planes are facing inwards so we need to
  1909. // invert the normal here.
  1910. Point3F normal = plane.getNormal();
  1911. normal.neg();
  1912. // Turn the orientation of the plane into a quaternion.
  1913. // The normal is our up vector (that's what's expected
  1914. // by ConvexShape for the surface orientation).
  1915. MatrixF orientation(true);
  1916. MathUtils::getMatrixFromUpVector(normal, &orientation);
  1917. const QuatF quat(orientation);
  1918. // Get the plane position.
  1919. const Point3F position = plane.getPosition();
  1920. // Turn everything into a "surface" property for the ConvexShape.
  1921. char buffer[1024];
  1922. dSprintf(buffer, sizeof(buffer), "%g %g %g %g %g %g %g",
  1923. quat.x, quat.y, quat.z, quat.w,
  1924. position.x, position.y, position.z
  1925. );
  1926. // Add the surface.
  1927. static StringTableEntry sSurface = StringTable->insert("surface");
  1928. shape->setDataField(sSurface, NULL, buffer);
  1929. }
  1930. // Copy the transform.
  1931. shape->setTransform(polyObject->getTransform());
  1932. shape->setScale(polyObject->getScale());
  1933. // Register the shape.
  1934. if (!shape->registerObject())
  1935. {
  1936. Con::errorf("WorldEditor::createConvexShapeFrom - Could not register ConvexShape!");
  1937. delete shape;
  1938. return NULL;
  1939. }
  1940. return shape;
  1941. }
  1942. DefineConsoleMethod( GuiConvexEditorCtrl, hollowSelection, void, (), , "" )
  1943. {
  1944. object->hollowSelection();
  1945. }
  1946. DefineConsoleMethod( GuiConvexEditorCtrl, recenterSelection, void, (), , "" )
  1947. {
  1948. object->recenterSelection();
  1949. }
  1950. DefineConsoleMethod( GuiConvexEditorCtrl, hasSelection, S32, (), , "" )
  1951. {
  1952. return object->hasSelection();
  1953. }
  1954. DefineConsoleMethod( GuiConvexEditorCtrl, handleDelete, void, (), , "" )
  1955. {
  1956. object->handleDelete();
  1957. }
  1958. DefineConsoleMethod( GuiConvexEditorCtrl, handleDeselect, void, (), , "" )
  1959. {
  1960. object->handleDeselect();
  1961. }
  1962. DefineConsoleMethod( GuiConvexEditorCtrl, dropSelectionAtScreenCenter, void, (), , "" )
  1963. {
  1964. object->dropSelectionAtScreenCenter();
  1965. }
  1966. DefineConsoleMethod( GuiConvexEditorCtrl, selectConvex, void, (ConvexShape *convex), , "( ConvexShape )" )
  1967. {
  1968. if (convex)
  1969. object->setSelection( convex, -1 );
  1970. }
  1971. DefineConsoleMethod( GuiConvexEditorCtrl, splitSelectedFace, void, (), , "" )
  1972. {
  1973. object->splitSelectedFace();
  1974. }