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. #include "T3D/Scene.h"
  52. IMPLEMENT_CONOBJECT( GuiConvexEditorCtrl );
  53. ConsoleDocClass( GuiConvexEditorCtrl,
  54. "@brief The base class for the sketch tool\n\n"
  55. "Editor use only.\n\n"
  56. "@internal"
  57. );
  58. GuiConvexEditorCtrl::GuiConvexEditorCtrl()
  59. : mIsDirty( false ),
  60. mFaceSEL( -1 ),
  61. mFaceHL( -1 ),
  62. mFaceSavedXfm( true ),
  63. mSavedUndo( false ),
  64. mDragging( false ),
  65. mGizmoMatOffset( Point3F::Zero ),
  66. mPivotPos( Point3F::Zero ),
  67. mUsingPivot( false ),
  68. mSettingPivot( false ),
  69. mActiveTool( NULL ),
  70. mMouseDown( false ),
  71. mCreateTool( NULL ),
  72. mSavedGizmoFlags( -1 ),
  73. mHasCopied( false ),
  74. mLastUndo( NULL ),
  75. mUndoManager( NULL ),
  76. mCtrlDown( false )
  77. {
  78. mMaterialName = StringTable->insert("Grid512_OrangeLines_Mat");
  79. }
  80. GuiConvexEditorCtrl::~GuiConvexEditorCtrl()
  81. {
  82. }
  83. bool GuiConvexEditorCtrl::onAdd()
  84. {
  85. if ( !Parent::onAdd() )
  86. return false;
  87. SceneManager::getPreRenderSignal().notify( this, &GuiConvexEditorCtrl::_prepRenderImage );
  88. mCreateTool = new ConvexEditorCreateTool( this );
  89. return true;
  90. }
  91. void GuiConvexEditorCtrl::onRemove()
  92. {
  93. SceneManager::getPreRenderSignal().remove( this, &GuiConvexEditorCtrl::_prepRenderImage );
  94. SAFE_DELETE( mCreateTool );
  95. Parent::onRemove();
  96. }
  97. void GuiConvexEditorCtrl::initPersistFields()
  98. {
  99. addField( "isDirty", TypeBool, Offset( mIsDirty, GuiConvexEditorCtrl ) );
  100. addField( "materialName", TypeString, Offset(mMaterialName, GuiConvexEditorCtrl) );
  101. Parent::initPersistFields();
  102. }
  103. bool GuiConvexEditorCtrl::onWake()
  104. {
  105. if ( !Parent::onWake() )
  106. return false;
  107. Scene* scene = Scene::getRootScene();
  108. if ( !scene )
  109. return true;
  110. SimGroup::iterator itr = scene->begin();
  111. for ( ; itr != scene->end(); itr++ )
  112. {
  113. if ( dStrcmp( (*itr)->getClassName(), "ConvexShape" ) == 0 )
  114. {
  115. mConvexSEL = static_cast<ConvexShape*>( *itr );
  116. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  117. return true;
  118. }
  119. }
  120. return true;
  121. }
  122. void GuiConvexEditorCtrl::onSleep()
  123. {
  124. Parent::onSleep();
  125. mConvexSEL = NULL;
  126. mConvexHL = NULL;
  127. }
  128. void GuiConvexEditorCtrl::setVisible( bool val )
  129. {
  130. //ConvexShape::smRenderEdges = value;
  131. if ( isProperlyAdded() )
  132. {
  133. if ( !val )
  134. {
  135. mFaceHL = -1;
  136. mConvexHL = NULL;
  137. setSelection( NULL, -1 );
  138. if ( mSavedGizmoFlags != -1 )
  139. {
  140. mGizmoProfile->flags = mSavedGizmoFlags;
  141. mSavedGizmoFlags = -1;
  142. }
  143. Scene* scene = Scene::getRootScene();
  144. if (scene != nullptr)
  145. {
  146. //Make our proxy objects "real" again
  147. for (U32 i = 0; i < mProxyObjects.size(); ++i)
  148. {
  149. if (!mProxyObjects[i].shapeProxy || !mProxyObjects[i].targetObject)
  150. continue;
  151. AbstractClassRep* classRep = AbstractClassRep::findClassRep(mProxyObjects[i].targetObjectClass);
  152. if (!classRep)
  153. {
  154. Con::errorf("WorldEditor::createPolyhedralObject - No such class: %s", mProxyObjects[i].targetObjectClass.c_str());
  155. continue;
  156. }
  157. SceneObject* polyObj = createPolyhedralObject(mProxyObjects[i].targetObjectClass.c_str(), mProxyObjects[i].shapeProxy);
  158. scene->addObject(polyObj);
  159. //Now, remove the convex proxy
  160. mProxyObjects[i].shapeProxy->deleteObject();
  161. mProxyObjects[i].targetObject->deleteObject();
  162. mProxyObjects.erase(i);
  163. --i;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. mConvexHL = NULL;
  170. mFaceHL = -1;
  171. setSelection( NULL, -1 );
  172. WorldEditor *wedit;
  173. if ( Sim::findObject( "EWorldEditor", wedit ) )
  174. {
  175. S32 count = wedit->getSelectionSize();
  176. for ( S32 i = 0; i < count; i++ )
  177. {
  178. S32 objId = wedit->getSelectObject(i);
  179. ConvexShape *pShape;
  180. if ( Sim::findObject( objId, pShape ) )
  181. {
  182. mConvexSEL = pShape;
  183. wedit->clearSelection();
  184. wedit->selectObject( String::ToString("%i",objId) );
  185. break;
  186. }
  187. }
  188. }
  189. updateGizmoPos();
  190. mSavedGizmoFlags = mGizmoProfile->flags;
  191. Scene* scene = Scene::getRootScene();
  192. if (scene != nullptr)
  193. {
  194. for (U32 c = 0; c < scene->size(); ++c)
  195. {
  196. bool isTrigger = (scene->at(c)->getClassName() == StringTable->insert("Trigger"));
  197. bool isZone = (scene->at(c)->getClassName() == StringTable->insert("Zone"));
  198. bool isPortal = (scene->at(c)->getClassName() == StringTable->insert("Portal"));
  199. bool isOccluder = (scene->at(c)->getClassName() == StringTable->insert("OcclusionVolume"));
  200. if (isZone || isPortal || isOccluder)
  201. {
  202. SceneObject* sceneObj = static_cast<SceneObject*>(scene->at(c));
  203. if (!sceneObj)
  204. {
  205. Con::errorf("WorldEditor::createConvexShapeFrom - Invalid object");
  206. continue;
  207. }
  208. ConvexShape* proxyShape = createConvexShapeFrom(sceneObj);
  209. //Set the texture to a representatory one so we know what's what
  210. if (isTrigger)
  211. proxyShape->mMaterialName = "TriggerProxyMaterial";
  212. else if (isPortal)
  213. proxyShape->mMaterialName = "PortalProxyMaterial";
  214. else if (isZone)
  215. proxyShape->mMaterialName = "ZoneProxyMaterial";
  216. else if (isOccluder)
  217. proxyShape->mMaterialName = "OccluderProxyMaterial";
  218. proxyShape->_updateMaterial();
  219. sceneObj->setHidden(true);
  220. //set up the proxy object
  221. ConvexShapeProxy newProxy;
  222. newProxy.shapeProxy = proxyShape;
  223. newProxy.targetObject = sceneObj;
  224. if (isTrigger)
  225. newProxy.targetObjectClass = "Trigger";
  226. else if (isPortal)
  227. newProxy.targetObjectClass = "Portal";
  228. else if (isZone)
  229. newProxy.targetObjectClass = "Zone";
  230. else
  231. newProxy.targetObjectClass = "OcclusionVolume";
  232. mProxyObjects.push_back(newProxy);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. Parent::setVisible( val );
  239. }
  240. void GuiConvexEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  241. {
  242. mouseLock();
  243. mMouseDown = true;
  244. if ( event.modifier & SI_ALT )
  245. {
  246. setActiveTool( mCreateTool );
  247. mActiveTool->on3DMouseDown( event );
  248. return;
  249. }
  250. if ( mConvexSEL && isShapeValid( mConvexSEL ) )
  251. mLastValidShape = mConvexSEL->mSurfaces;
  252. if ( mConvexSEL &&
  253. mFaceSEL != -1 &&
  254. mGizmo->getMode() == RotateMode &&
  255. mGizmo->getSelection() == Gizmo::Centroid )
  256. {
  257. mSettingPivot = true;
  258. mSavedPivotPos = mGizmo->getPosition();
  259. setPivotPos( mConvexSEL, mFaceSEL, event );
  260. updateGizmoPos();
  261. return;
  262. }
  263. mGizmo->on3DMouseDown( event );
  264. }
  265. void GuiConvexEditorCtrl::on3DRightMouseDown(const Gui3DMouseEvent & event)
  266. {
  267. return;
  268. /*
  269. if ( mConvexSEL && mFaceSEL != -1 && mFaceSEL == mFaceHL )
  270. {
  271. _submitUndo( "Split ConvexShape face." );
  272. const MatrixF &surf = mConvexSEL->mSurfaces[mFaceSEL];
  273. MatrixF newSurf( surf );
  274. MatrixF rotMat( EulerF( 0.0f, mDegToRad( 2.0f ), 0.0f ) );
  275. newSurf *= rotMat;
  276. mConvexSEL->mSurfaces.insert( mFaceSEL+1, newSurf );
  277. }
  278. */
  279. }
  280. void GuiConvexEditorCtrl::on3DRightMouseUp(const Gui3DMouseEvent & event)
  281. {
  282. //ConvexShape *hitShape;
  283. //S32 hitFace;
  284. //bool hit = _cursorCast( event, &hitShape, &hitFace );
  285. //Con::printf( hit ? "HIT" : "MISS" );
  286. }
  287. void GuiConvexEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  288. {
  289. mouseUnlock();
  290. mMouseDown = false;
  291. mHasCopied = false;
  292. mHasGeometry = false;
  293. if ( mActiveTool )
  294. {
  295. ConvexEditorTool::EventResult result = mActiveTool->on3DMouseUp( event );
  296. if ( result == ConvexEditorTool::Done )
  297. setActiveTool( NULL );
  298. return;
  299. }
  300. if ( !mSettingPivot && !mDragging && ( mGizmo->getSelection() == Gizmo::None || !mConvexSEL ) )
  301. {
  302. if ( mConvexSEL != mConvexHL )
  303. {
  304. setSelection( mConvexHL, -1 );
  305. }
  306. else
  307. {
  308. if ( mFaceSEL != mFaceHL )
  309. setSelection( mConvexSEL, mFaceHL );
  310. else
  311. setSelection( mConvexSEL, -1 );
  312. }
  313. mUsingPivot = false;
  314. }
  315. mSettingPivot = false;
  316. mSavedPivotPos = mGizmo->getPosition();
  317. mSavedUndo = false;
  318. mGizmo->on3DMouseUp( event );
  319. if ( mDragging )
  320. {
  321. mDragging = false;
  322. if ( mConvexSEL )
  323. {
  324. Vector< U32 > removedPlanes;
  325. mConvexSEL->cullEmptyPlanes( &removedPlanes );
  326. // If a face has been removed we need to validate / remap
  327. // our selected and highlighted faces.
  328. if ( !removedPlanes.empty() )
  329. {
  330. S32 prevFaceHL = mFaceHL;
  331. S32 prevFaceSEL = mFaceSEL;
  332. if ( removedPlanes.contains( mFaceHL ) )
  333. prevFaceHL = mFaceHL = -1;
  334. if ( removedPlanes.contains( mFaceSEL ) )
  335. prevFaceSEL = mFaceSEL = -1;
  336. for ( S32 i = 0; i < removedPlanes.size(); i++ )
  337. {
  338. if ( (S32)removedPlanes[i] < prevFaceSEL )
  339. mFaceSEL--;
  340. if ( (S32)removedPlanes[i] < prevFaceHL )
  341. mFaceHL--;
  342. }
  343. setSelection( mConvexSEL, mFaceSEL );
  344. // We need to reindex faces.
  345. updateShape( mConvexSEL );
  346. }
  347. }
  348. }
  349. updateGizmoPos();
  350. }
  351. void GuiConvexEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  352. {
  353. if ( mActiveTool )
  354. {
  355. // If we have an active tool pass this event to it.
  356. // If it handled it, consume the event.
  357. if ( mActiveTool->on3DMouseMove( event ) )
  358. return;
  359. }
  360. ConvexShape *hitShape = NULL;
  361. S32 hitFace = -1;
  362. _cursorCast( event, &hitShape, &hitFace );
  363. if ( !mConvexSEL )
  364. {
  365. mConvexHL = hitShape;
  366. mFaceHL = -1;
  367. }
  368. else
  369. {
  370. if ( mConvexSEL == hitShape )
  371. {
  372. mConvexHL = hitShape;
  373. mFaceHL = hitFace;
  374. }
  375. else
  376. {
  377. // Mousing over a shape that is not the one currently selected.
  378. if ( mFaceSEL != -1 )
  379. {
  380. mFaceHL = -1;
  381. }
  382. else
  383. {
  384. mConvexHL = hitShape;
  385. mFaceHL = -1;
  386. }
  387. }
  388. }
  389. if ( mConvexSEL )
  390. mGizmo->on3DMouseMove( event );
  391. }
  392. void GuiConvexEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  393. {
  394. if ( mActiveTool )
  395. {
  396. // If we have an active tool pass this event to it.
  397. // If it handled it, consume the event.
  398. if ( mActiveTool->on3DMouseDragged( event ) )
  399. return;
  400. }
  401. //mGizmoProfile->rotateScalar = 0.55f;
  402. //mGizmoProfile->scaleScalar = 0.55f;
  403. if ( !mConvexSEL )
  404. return;
  405. if ( mGizmo->getMode() == RotateMode &&
  406. mGizmo->getSelection() == Gizmo::Centroid )
  407. {
  408. setPivotPos( mConvexSEL, mFaceSEL, event );
  409. mDragging = true;
  410. return;
  411. }
  412. mGizmo->on3DMouseDragged( event );
  413. if ( event.modifier & SI_SHIFT &&
  414. ( mGizmo->getMode() == MoveMode || mGizmo->getMode() == RotateMode ) &&
  415. !mHasCopied )
  416. {
  417. if ( mFaceSEL != -1 )
  418. {
  419. ConvexShape *newShape = mCreateTool->extrudeShapeFromFace( mConvexSEL, mFaceSEL );
  420. //newShape->_updateGeometry();
  421. submitUndo( CreateShape, newShape );
  422. setSelection( newShape, 0 );
  423. updateGizmoPos();
  424. mGizmo->on3DMouseDown( event );
  425. mHasCopied = true;
  426. mSavedUndo = true;
  427. }
  428. else
  429. {
  430. ConvexShape *newShape = new ConvexShape();
  431. newShape->setTransform( mConvexSEL->getTransform() );
  432. newShape->setScale( mConvexSEL->getScale() );
  433. newShape->mSurfaces.clear();
  434. newShape->mSurfaces.merge( mConvexSEL->mSurfaces );
  435. setupShape( newShape );
  436. newShape->setField("material", mConvexSEL->getMaterialName());
  437. submitUndo( CreateShape, newShape );
  438. setSelection( newShape, -1 );
  439. updateGizmoPos();
  440. mHasCopied = true;
  441. mSavedUndo = true;
  442. }
  443. return;
  444. }
  445. if ( mGizmo->getMode() == RotateMode &&
  446. event.modifier & SI_CTRL &&
  447. !mHasCopied &&
  448. mFaceSEL != -1 )
  449. {
  450. // Can must verify that splitting the face at the current angle
  451. // ( of the gizmo ) will generate a valid shape. If not enough rotation
  452. // has occurred we will have two faces that are coplanar and must wait
  453. // until later in the drag to perform the split.
  454. //AssertFatal( isShapeValid( mConvexSEL ), "Shape was already invalid at beginning of split operation." );
  455. if ( !isShapeValid( mConvexSEL ) )
  456. return;
  457. mLastValidShape = mConvexSEL->mSurfaces;
  458. Point3F rot = mGizmo->getDeltaTotalRot();
  459. rot.normalize();
  460. rot *= mDegToRad( 10.0f );
  461. MatrixF rotMat( (EulerF)rot );
  462. MatrixF worldToObj( mConvexSEL->getTransform() );
  463. worldToObj.scale( mConvexSEL->getScale() );
  464. worldToObj.inverse();
  465. mConvexSEL->mSurfaces.increment();
  466. MatrixF &newSurf = mConvexSEL->mSurfaces.last();
  467. newSurf = mConvexSEL->mSurfaces[mFaceSEL] * rotMat;
  468. //worldToObj.mul( mGizmo->getTransform() );
  469. //Point3F pos( mPivotPos );
  470. //worldToObj.mulP( pos );
  471. //newSurf.setPosition( pos );
  472. updateShape( mConvexSEL );
  473. if ( !isShapeValid( mConvexSEL ) )
  474. {
  475. mConvexSEL->mSurfaces = mLastValidShape;
  476. updateShape( mConvexSEL );
  477. }
  478. else
  479. {
  480. mHasCopied = true;
  481. mSavedUndo = true;
  482. mLastValidShape = mConvexSEL->mSurfaces;
  483. submitUndo( ModifyShape, mConvexSEL );
  484. setSelection( mConvexSEL, mConvexSEL->mSurfaces.size() - 1 );
  485. updateGizmoPos();
  486. }
  487. return;
  488. }
  489. // If we are dragging, but no gizmo selection...
  490. // Then treat this like a regular mouse move, update the highlighted
  491. // convex/face under the cursor and handle onMouseUp as we normally would
  492. // to change the selection.
  493. if ( mGizmo->getSelection() == Gizmo::None )
  494. {
  495. ConvexShape *hitShape = NULL;
  496. S32 hitFace = -1;
  497. _cursorCast( event, &hitShape, &hitFace );
  498. mFaceHL = hitFace;
  499. mConvexHL = hitShape;
  500. return;
  501. }
  502. mDragging = true;
  503. // Manipulating a face.
  504. if ( mFaceSEL != -1 )
  505. {
  506. if ( !mSavedUndo )
  507. {
  508. mSavedUndo = true;
  509. submitUndo( ModifyShape, mConvexSEL );
  510. }
  511. if ( mGizmo->getMode() == ScaleMode )
  512. {
  513. scaleFace( mConvexSEL, mFaceSEL, mGizmo->getScale() );
  514. }
  515. else
  516. {
  517. // Why does this have to be so ugly.
  518. if ( mGizmo->getMode() == RotateMode ||
  519. ( mGizmo->getMode() == MoveMode &&
  520. ( event.modifier & SI_CTRL ||
  521. ( mGizmo->getSelection() == Gizmo::Axis_Z && mHasCopied )
  522. )
  523. )
  524. )
  525. {
  526. const MatrixF &gMat = mGizmo->getTransform();
  527. MatrixF surfMat;
  528. surfMat.mul( mConvexSEL->mWorldToObj, gMat );
  529. MatrixF worldToObj ( mConvexSEL->getTransform() );
  530. worldToObj.scale( mConvexSEL->getScale() );
  531. worldToObj.inverse();
  532. Point3F newPos;
  533. newPos = gMat.getPosition();
  534. worldToObj.mulP( newPos );
  535. surfMat.setPosition( newPos );
  536. // Clear out floating point errors.
  537. cleanMatrix( surfMat );
  538. mConvexSEL->mSurfaces[mFaceSEL] = surfMat;
  539. updateShape( mConvexSEL, mFaceSEL );
  540. }
  541. else
  542. {
  543. // Translating a face in x/y/z
  544. translateFace( mConvexSEL, mFaceSEL, mGizmo->getTotalOffset() );
  545. }
  546. }
  547. if ( isShapeValid( mConvexSEL ) )
  548. {
  549. AssertFatal( mConvexSEL->mSurfaces.size() > mFaceSEL, "mFaceSEL out of range." );
  550. mLastValidShape = mConvexSEL->mSurfaces;
  551. }
  552. else
  553. {
  554. AssertFatal( mLastValidShape.size() > mFaceSEL, "mFaceSEL out of range." );
  555. mConvexSEL->mSurfaces = mLastValidShape;
  556. updateShape( mConvexSEL );
  557. }
  558. return;
  559. }
  560. // Manipulating a whole Convex.
  561. if ( !mSavedUndo )
  562. {
  563. mSavedUndo = true;
  564. submitUndo( ModifyShape, mConvexSEL );
  565. }
  566. if ( mGizmo->getMode() == MoveMode )
  567. {
  568. mConvexSEL->setPosition( mGizmo->getPosition() );
  569. }
  570. else if ( mGizmo->getMode() == RotateMode )
  571. {
  572. mConvexSEL->setTransform( mGizmo->getTransform() );
  573. }
  574. else
  575. {
  576. mConvexSEL->setScale( mGizmo->getScale() );
  577. }
  578. if ( mConvexSEL->getClientObject() )
  579. {
  580. ConvexShape *clientObj = static_cast< ConvexShape* >( mConvexSEL->getClientObject() );
  581. clientObj->setTransform( mConvexSEL->getTransform() );
  582. clientObj->setScale( mConvexSEL->getScale() );
  583. }
  584. }
  585. void GuiConvexEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  586. {
  587. }
  588. void GuiConvexEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  589. {
  590. }
  591. bool GuiConvexEditorCtrl::onKeyDown( const GuiEvent &evt )
  592. {
  593. bool handled = false;
  594. switch ( evt.keyCode )
  595. {
  596. case KEY_ESCAPE:
  597. handled = handleEscape();
  598. break;
  599. case KEY_A:
  600. if ( evt.modifier & SI_ALT )
  601. {
  602. GizmoAlignment align = mGizmo->getProfile()->alignment;
  603. if ( align == World )
  604. mGizmo->getProfile()->alignment = Object;
  605. else
  606. mGizmo->getProfile()->alignment = World;
  607. handled = true;
  608. }
  609. break;
  610. case KEY_LCONTROL:
  611. //mCtrlDown = true;
  612. break;
  613. default:
  614. break;
  615. }
  616. return handled;
  617. }
  618. bool GuiConvexEditorCtrl::onKeyUp( const GuiEvent &evt )
  619. {
  620. bool handled = false;
  621. switch ( evt.keyCode )
  622. {
  623. case KEY_LCONTROL:
  624. //mCtrlDown = false;
  625. break;
  626. default:
  627. break;
  628. }
  629. return handled;
  630. }
  631. void GuiConvexEditorCtrl::get3DCursor( GuiCursor *&cursor,
  632. bool &visible,
  633. const Gui3DMouseEvent &event_ )
  634. {
  635. //cursor = mAddNodeCursor;
  636. //visible = false;
  637. cursor = NULL;
  638. visible = false;
  639. GuiCanvas *root = getRoot();
  640. if ( !root )
  641. return;
  642. S32 currCursor = PlatformCursorController::curArrow;
  643. if ( root->mCursorChanged == currCursor )
  644. return;
  645. PlatformWindow *window = root->getPlatformWindow();
  646. PlatformCursorController *controller = window->getCursorController();
  647. // We've already changed the cursor,
  648. // so set it back before we change it again.
  649. if( root->mCursorChanged != -1)
  650. controller->popCursor();
  651. // Now change the cursor shape
  652. controller->pushCursor(currCursor);
  653. root->mCursorChanged = currCursor;
  654. }
  655. void GuiConvexEditorCtrl::updateGizmo()
  656. {
  657. mGizmoProfile->restoreDefaultState();
  658. const GizmoMode &mode = mGizmoProfile->mode;
  659. S32 &flags = mGizmoProfile->flags;
  660. GizmoAlignment &align = mGizmoProfile->alignment;
  661. U8 keys = Input::getModifierKeys();
  662. mCtrlDown = keys & ( SI_LCTRL | SI_LSHIFT );
  663. bool altDown = keys & ( SI_LALT );
  664. if ( altDown )
  665. {
  666. flags = 0;
  667. return;
  668. }
  669. if ( mFaceSEL != -1 )
  670. {
  671. align = Object;
  672. flags |= GizmoProfile::CanRotateUniform;
  673. flags &= ~GizmoProfile::CanRotateScreen;
  674. }
  675. else
  676. {
  677. flags &= ~GizmoProfile::CanRotateUniform;
  678. flags |= GizmoProfile::CanRotateScreen;
  679. }
  680. if ( mFaceSEL != -1 && mode == ScaleMode )
  681. flags &= ~GizmoProfile::CanScaleZ;
  682. else
  683. flags |= GizmoProfile::CanScaleZ;
  684. if ( mFaceSEL != -1 && mode == MoveMode )
  685. {
  686. if ( mCtrlDown )
  687. flags &= ~( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  688. else
  689. flags |= ( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  690. }
  691. }
  692. void GuiConvexEditorCtrl::renderScene(const RectI & updateRect)
  693. {
  694. // Synch selected ConvexShape with the WorldEditor.
  695. WorldEditor *wedit;
  696. if ( Sim::findObject( "EWorldEditor", wedit) )
  697. {
  698. S32 count = wedit->getSelectionSize();
  699. if ( !mConvexSEL && count != 0 )
  700. wedit->clearSelection();
  701. else if ( mConvexSEL && count != 1 )
  702. {
  703. wedit->clearSelection();
  704. wedit->selectObject( mConvexSEL->getIdString() );
  705. }
  706. else if ( mConvexSEL && count == 1 )
  707. {
  708. if ( wedit->getSelectObject(0) != mConvexSEL->getId() )
  709. {
  710. wedit->clearSelection();
  711. wedit->selectObject( mConvexSEL->getIdString() );
  712. }
  713. }
  714. }
  715. // Update status bar text.
  716. SimObject *statusbar;
  717. if ( Sim::findObject( "EditorGuiStatusBar", statusbar ) )
  718. {
  719. String text( "Sketch Tool." );
  720. GizmoMode mode = mGizmo->getMode();
  721. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  722. {
  723. Point3F delta;
  724. String qualifier;
  725. if ( mode == RotateMode )
  726. {
  727. if ( mSettingPivot )
  728. delta = mGizmo->getPosition() - mSavedPivotPos;
  729. else
  730. delta = mGizmo->getDeltaTotalRot();
  731. }
  732. else if ( mode == MoveMode )
  733. delta = mGizmo->getTotalOffset();
  734. else if ( mode == ScaleMode )
  735. delta = mGizmo->getDeltaTotalScale();
  736. if ( mGizmo->getAlignment() == Object &&
  737. mode != ScaleMode )
  738. {
  739. mConvexSEL->mWorldToObj.mulV( delta );
  740. if ( mFaceSEL != -1 && mode != RotateMode )
  741. {
  742. MatrixF objToSurf( mConvexSEL->mSurfaces[ mFaceSEL ] );
  743. objToSurf.scale( mConvexSEL->getScale() );
  744. objToSurf.inverse();
  745. objToSurf.mulV( delta );
  746. }
  747. }
  748. if ( mIsZero( delta.x, 0.0001f ) )
  749. delta.x = 0.0f;
  750. if ( mIsZero( delta.y, 0.0001f ) )
  751. delta.y = 0.0f;
  752. if ( mIsZero( delta.z, 0.0001f ) )
  753. delta.z = 0.0f;
  754. if ( mode == RotateMode )
  755. {
  756. if ( mSettingPivot )
  757. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  758. else
  759. {
  760. delta.x = mRadToDeg( delta.x );
  761. delta.y = mRadToDeg( delta.y );
  762. delta.z = mRadToDeg( delta.z );
  763. text = String::ToString( "Delta angle ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  764. }
  765. }
  766. else if ( mode == MoveMode )
  767. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  768. else if ( mode == ScaleMode )
  769. text = String::ToString( "Delta scale ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  770. }
  771. else
  772. {
  773. if ( !mConvexSEL )
  774. text = "Sketch Tool. ALT + Click-Drag to create a new ConvexShape.";
  775. else if ( mFaceSEL == -1 )
  776. {
  777. if ( mode == MoveMode )
  778. text = "Move selection. SHIFT while dragging duplicates objects.";
  779. else if ( mode == RotateMode )
  780. text = "Rotate selection.";
  781. else if ( mode == ScaleMode )
  782. text = "Scale selection.";
  783. }
  784. else
  785. {
  786. if ( mode == MoveMode )
  787. text = "Move face. SHIFT while beginning a drag EXTRUDES a new convex. Press CTRL for alternate translation mode.";
  788. else if ( mode == RotateMode )
  789. text = "Rotate face. Gizmo/Pivot is draggable. CTRL while dragging splits/folds a new face. SHIFT while dragging extrudes a new convex.";
  790. else if ( mode == ScaleMode )
  791. text = "Scale face.";
  792. }
  793. }
  794. // Issue a warning in the status bar
  795. // if this convex has an excessive number of surfaces...
  796. if ( mConvexSEL && mConvexSEL->getSurfaces().size() > ConvexShape::smMaxSurfaces )
  797. {
  798. text = "WARNING: Reduce the number of surfaces on the selected ConvexShape, only the first 100 will be saved!";
  799. }
  800. Con::executef( statusbar, "setInfo", text.c_str() );
  801. Con::executef( statusbar, "setSelectionObjectsByCount", Con::getIntArg( mConvexSEL == NULL ? 0 : 1 ) );
  802. }
  803. if ( mActiveTool )
  804. mActiveTool->renderScene( updateRect );
  805. ColorI colorHL( 255, 50, 255, 255 );
  806. ColorI colorSEL( 255, 50, 255, 255 );
  807. ColorI colorNA( 255, 255, 255, 100 );
  808. GFXDrawUtil *drawer = GFX->getDrawUtil();
  809. if ( mConvexSEL && !mDragging )
  810. {
  811. if ( mFaceSEL == -1 )
  812. {
  813. GFXStateBlockDesc desc;
  814. desc.setBlend( true );
  815. desc.setZReadWrite( true, true );
  816. Box3F objBox = mConvexSEL->getObjBox();
  817. objBox.scale( mConvexSEL->getScale() );
  818. const MatrixF &objMat = mConvexSEL->getTransform();
  819. Point3F boxPos = objBox.getCenter();
  820. objMat.mulP( boxPos );
  821. drawer->drawObjectBox( desc, objBox.getExtents(), boxPos, objMat, ColorI::WHITE );
  822. }
  823. else
  824. {
  825. mConvexSEL->renderFaceEdges( -1, colorNA );
  826. drawFacePlane( mConvexSEL, mFaceSEL );
  827. }
  828. if ( mConvexHL == mConvexSEL &&
  829. mFaceHL != -1 &&
  830. mFaceHL != mFaceSEL &&
  831. mGizmo->getSelection() == Gizmo::None )
  832. {
  833. mConvexSEL->renderFaceEdges( mFaceHL, colorHL );
  834. }
  835. }
  836. if ( mConvexHL && mConvexHL != mConvexSEL )
  837. {
  838. mConvexHL->renderFaceEdges( -1 );
  839. }
  840. if ( mGizmo->getMode() != RotateMode && mUsingPivot )
  841. {
  842. mUsingPivot = false;
  843. updateGizmoPos();
  844. }
  845. F32 gizmoAlpha = 1.0f;
  846. if ( !mConvexSEL )
  847. gizmoAlpha = 0.0f;
  848. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  849. {
  850. if ( mSettingPivot )
  851. gizmoAlpha = 1.0f;
  852. else
  853. gizmoAlpha = 0.0f;
  854. }
  855. DebugDrawer::get()->render();
  856. {
  857. GFXTransformSaver saver;
  858. // Now draw all the 2d stuff!
  859. GFX->setClipRect(updateRect);
  860. if ( mConvexSEL && mFaceSEL != -1 )
  861. {
  862. Vector< Point3F > lineList;
  863. mConvexSEL->getSurfaceLineList( mFaceSEL, lineList );
  864. MatrixF objToWorld( mConvexSEL->getTransform() );
  865. objToWorld.scale( mConvexSEL->getScale() );
  866. for ( S32 i = 0; i < lineList.size(); i++ )
  867. objToWorld.mulP( lineList[i] );
  868. for ( S32 i = 0; i < lineList.size() - 1; i++ )
  869. {
  870. Point3F p0( lineList[i] );
  871. Point3F p1( lineList[i+1] );
  872. drawLine( p0, p1, colorSEL, 3.0f );
  873. }
  874. }
  875. if ( gizmoAlpha == 1.0f )
  876. {
  877. if ( mGizmoProfile->mode != NoneMode )
  878. mGizmo->renderText( mSaveViewport, mSaveModelview, mSaveProjection );
  879. }
  880. if ( mActiveTool )
  881. mActiveTool->render2D();
  882. }
  883. if ( gizmoAlpha == 1.0f )
  884. mGizmo->renderGizmo( mLastCameraQuery.cameraMatrix, mLastCameraQuery.fov );
  885. }
  886. void GuiConvexEditorCtrl::drawFacePlane( ConvexShape *shape, S32 faceId )
  887. {
  888. // Build a vb of the face points ( in world space ) scaled outward in
  889. // the surface space in x/y with uv coords.
  890. /*
  891. Vector< Point3F > points;
  892. Vector< Point2F > coords;
  893. shape->getSurfaceTriangles( faceId, &points, &coords, false );
  894. if ( points.empty() )
  895. return;
  896. GFXVertexBufferHandle< GFXVertexPCT > vb;
  897. vb.set( GFX, points.size(), GFXBufferTypeVolatile );
  898. GFXVertexPCT *vert = vb.lock();
  899. for ( S32 i = 0; i < points.size(); i++ )
  900. {
  901. vert->point = points[i];
  902. vert->color.set( 255, 255, 255, 200 );
  903. vert->texCoord = coords[i];
  904. vert++;
  905. }
  906. vb.unlock();
  907. GFXTransformSaver saver;
  908. MatrixF renderMat( shape->getTransform() );
  909. renderMat.scale( shape->getScale() );
  910. GFX->multWorld( renderMat );
  911. GFXStateBlockDesc desc;
  912. desc.setBlend( true );
  913. desc.setCullMode( GFXCullNone );
  914. desc.setZReadWrite( true, false );
  915. desc.samplersDefined = true;
  916. desc.samplers[0] = GFXSamplerStateDesc::getWrapLinear();
  917. GFX->setStateBlockByDesc( desc );
  918. GFX->setVertexBuffer( vb );
  919. GFXTexHandle tex( "core/art/grids/512_transp", &GFXStaticTextureSRGBProfile, "ConvexEditor_grid" );
  920. GFX->setTexture( 0, tex );
  921. GFX->setupGenericShaders();
  922. GFX->drawPrimitive( GFXTriangleList, 0, points.size() / 3 );
  923. */
  924. }
  925. void GuiConvexEditorCtrl::scaleFace( ConvexShape *shape, S32 faceId, Point3F scale )
  926. {
  927. if ( !mHasGeometry )
  928. {
  929. mHasGeometry = true;
  930. mSavedGeometry = shape->mGeometry;
  931. mSavedSurfaces = shape->mSurfaces;
  932. }
  933. else
  934. {
  935. shape->mGeometry = mSavedGeometry;
  936. shape->mSurfaces = mSavedSurfaces;
  937. }
  938. if ( shape->mGeometry.faces.size() <= faceId )
  939. return;
  940. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  941. Vector< Point3F > &pointList = shape->mGeometry.points;
  942. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  943. Point3F projScale;
  944. scale.z = 1.0f;
  945. const MatrixF &surfToObj = shape->mSurfaces[ face.id ];
  946. MatrixF objToSurf( surfToObj );
  947. objToSurf.inverse();
  948. for ( S32 i = 0; i < face.points.size(); i++ )
  949. {
  950. Point3F &pnt = pointList[ face.points[i] ];
  951. objToSurf.mulP( pnt );
  952. pnt *= scale;
  953. surfToObj.mulP( pnt );
  954. }
  955. updateModifiedFace( shape, faceId );
  956. }
  957. void GuiConvexEditorCtrl::translateFace( ConvexShape *shape, S32 faceId, const Point3F &displace )
  958. {
  959. if ( !mHasGeometry )
  960. {
  961. mHasGeometry = true;
  962. mSavedGeometry = shape->mGeometry;
  963. mSavedSurfaces = shape->mSurfaces;
  964. }
  965. else
  966. {
  967. shape->mGeometry = mSavedGeometry;
  968. shape->mSurfaces = mSavedSurfaces;
  969. }
  970. if ( shape->mGeometry.faces.size() <= faceId )
  971. return;
  972. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  973. Vector< Point3F > &pointList = shape->mGeometry.points;
  974. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  975. // Transform displacement into object space.
  976. MatrixF worldToObj( shape->getTransform() );
  977. worldToObj.scale( shape->getScale() );
  978. worldToObj.inverse();
  979. Point3F displaceOS;
  980. worldToObj.mulV( displace, &displaceOS );
  981. for ( S32 i = 0; i < face.points.size(); i++ )
  982. {
  983. Point3F &pnt = pointList[ face.points[i] ];
  984. pnt += displaceOS;
  985. }
  986. updateModifiedFace( shape, faceId );
  987. }
  988. void GuiConvexEditorCtrl::updateModifiedFace( ConvexShape *shape, S32 faceId )
  989. {
  990. if ( shape->mGeometry.faces.size() <= faceId )
  991. return;
  992. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  993. Vector< Point3F > &pointList = shape->mGeometry.points;
  994. Vector< ConvexShape::Face > &faceList = shape->mGeometry.faces;
  995. for ( S32 i = 0; i < faceList.size(); i++ )
  996. {
  997. ConvexShape::Face &curFace = faceList[i];
  998. MatrixF &curSurface = shape->mSurfaces[ curFace.id ];
  999. U32 curPntCount = curFace.points.size();
  1000. if ( curPntCount < 3 )
  1001. continue;
  1002. // Does this face use any of the points which we have modified?
  1003. // Collect them in correct winding order.
  1004. S32 pId0 = -1;
  1005. for ( S32 j = 0; j < curFace.winding.size(); j++ )
  1006. {
  1007. if ( face.points.contains( curFace.points[ curFace.winding[ j ] ] ) )
  1008. {
  1009. pId0 = j;
  1010. break;
  1011. }
  1012. }
  1013. if ( pId0 == -1 )
  1014. continue;
  1015. S32 pId1 = -1, pId2 = -1;
  1016. pId1 = ( pId0 + 1 ) % curFace.winding.size();
  1017. pId2 = ( pId0 + 2 ) % curFace.winding.size();
  1018. const Point3F &p0 = pointList[ curFace.points[ curFace.winding[ pId0 ] ] ];
  1019. const Point3F &p1 = pointList[ curFace.points[ curFace.winding[ pId1 ] ] ];
  1020. const Point3F &p2 = pointList[ curFace.points[ curFace.winding[ pId2 ] ] ];
  1021. PlaneF newPlane( p0, p1, p2 );
  1022. Point3F uvec = newPlane.getNormal();
  1023. Point3F fvec = curSurface.getForwardVector();
  1024. Point3F rvec = curSurface.getRightVector();
  1025. F32 dt0 = mDot( uvec, fvec );
  1026. F32 dt1 = mDot( uvec, rvec );
  1027. if ( mFabs( dt0 ) < mFabs( dt1 ) )
  1028. {
  1029. rvec = mCross( fvec, uvec );
  1030. rvec.normalizeSafe();
  1031. fvec = mCross( uvec, rvec );
  1032. fvec.normalizeSafe();
  1033. }
  1034. else
  1035. {
  1036. fvec = mCross( uvec, rvec );
  1037. fvec.normalizeSafe();
  1038. rvec = mCross( fvec, uvec );
  1039. rvec.normalizeSafe();
  1040. }
  1041. curSurface.setColumn( 0, rvec );
  1042. curSurface.setColumn( 1, fvec );
  1043. curSurface.setColumn( 2, uvec );
  1044. curSurface.setPosition( newPlane.getPosition() );
  1045. }
  1046. updateShape( shape );
  1047. }
  1048. bool GuiConvexEditorCtrl::isShapeValid( ConvexShape *shape )
  1049. {
  1050. // Test for no-geometry.
  1051. if ( shape->mGeometry.points.empty() )
  1052. return false;
  1053. const Vector<Point3F> &pointList = shape->mGeometry.points;
  1054. const Vector<ConvexShape::Face> &faceList = shape->mGeometry.faces;
  1055. // Test that all points are shared by at least 3 faces.
  1056. for ( S32 i = 0; i < pointList.size(); i++ )
  1057. {
  1058. U32 counter = 0;
  1059. for ( S32 j = 0; j < faceList.size(); j++ )
  1060. {
  1061. if ( faceList[j].points.contains( i ) )
  1062. counter++;
  1063. }
  1064. if ( counter < 3 )
  1065. return false;
  1066. }
  1067. // Test for co-planar faces.
  1068. for ( S32 i = 0; i < shape->mPlanes.size(); i++ )
  1069. {
  1070. for ( S32 j = i + 1; j < shape->mPlanes.size(); j++ )
  1071. {
  1072. F32 d = mDot( shape->mPlanes[i], shape->mPlanes[j] );
  1073. if ( d > 0.999f )
  1074. return false;
  1075. }
  1076. }
  1077. // Test for faces with zero or negative area.
  1078. for ( S32 i = 0; i < shape->mGeometry.faces.size(); i++ )
  1079. {
  1080. if ( shape->mGeometry.faces[i].area < 0.0f )
  1081. return false;
  1082. if ( shape->mGeometry.faces[i].triangles.empty() )
  1083. return false;
  1084. }
  1085. return true;
  1086. }
  1087. void GuiConvexEditorCtrl::setupShape( ConvexShape *shape )
  1088. {
  1089. shape->setField( "material", mMaterialName );
  1090. shape->registerObject();
  1091. updateShape( shape );
  1092. Scene* scene = Scene::getRootScene();
  1093. if ( scene )
  1094. scene->addObject( shape );
  1095. }
  1096. void GuiConvexEditorCtrl::updateShape( ConvexShape *shape, S32 offsetFace )
  1097. {
  1098. shape->_updateGeometry( true );
  1099. /*
  1100. if ( offsetFace != -1 )
  1101. {
  1102. shape->mSurfaces[ offsetFace ].setPosition( mPivotPos );
  1103. }*/
  1104. synchClientObject( shape );
  1105. }
  1106. void GuiConvexEditorCtrl::synchClientObject( const ConvexShape *serverConvex )
  1107. {
  1108. if ( serverConvex->getClientObject() )
  1109. {
  1110. ConvexShape *clientConvex = static_cast< ConvexShape* >( serverConvex->getClientObject() );
  1111. clientConvex->setScale( serverConvex->getScale() );
  1112. clientConvex->setTransform( serverConvex->getTransform() );
  1113. clientConvex->mSurfaces.clear();
  1114. clientConvex->mSurfaces.merge( serverConvex->mSurfaces );
  1115. clientConvex->_updateGeometry(true);
  1116. }
  1117. }
  1118. void GuiConvexEditorCtrl::updateGizmoPos()
  1119. {
  1120. if ( mConvexSEL )
  1121. {
  1122. if ( mFaceSEL != -1 )
  1123. {
  1124. MatrixF surfMat = mConvexSEL->getSurfaceWorldMat( mFaceSEL );
  1125. MatrixF objToWorld( mConvexSEL->getTransform() );
  1126. objToWorld.scale( mConvexSEL->getScale() );
  1127. Point3F gizmoPos(0,0,0);
  1128. if ( mUsingPivot )
  1129. {
  1130. gizmoPos = mPivotPos;
  1131. }
  1132. else
  1133. {
  1134. Point3F faceCenterPnt = mConvexSEL->mSurfaces[ mFaceSEL ].getPosition();
  1135. objToWorld.mulP( faceCenterPnt );
  1136. mGizmoMatOffset = surfMat.getPosition() - faceCenterPnt;
  1137. gizmoPos = faceCenterPnt;
  1138. }
  1139. mGizmo->set( surfMat, gizmoPos, Point3F::One );
  1140. }
  1141. else
  1142. {
  1143. mGizmoMatOffset = Point3F::Zero;
  1144. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  1145. }
  1146. }
  1147. }
  1148. bool GuiConvexEditorCtrl::setActiveTool( ConvexEditorTool *tool )
  1149. {
  1150. if ( mActiveTool == tool )
  1151. return false;
  1152. ConvexEditorTool *prevTool = mActiveTool;
  1153. ConvexEditorTool *newTool = tool;
  1154. if ( prevTool )
  1155. prevTool->onDeactivated( newTool );
  1156. mActiveTool = newTool;
  1157. if ( newTool )
  1158. newTool->onActivated( prevTool );
  1159. return true;
  1160. }
  1161. bool GuiConvexEditorCtrl::handleEscape()
  1162. {
  1163. if ( mActiveTool )
  1164. {
  1165. mActiveTool->onDeactivated( NULL );
  1166. mActiveTool = NULL;
  1167. return true;
  1168. }
  1169. if ( mFaceSEL != -1 )
  1170. {
  1171. setSelection( mConvexSEL, -1 );
  1172. return true;
  1173. }
  1174. if ( mConvexSEL )
  1175. {
  1176. setSelection( NULL, -1 );
  1177. return true;
  1178. }
  1179. return false;
  1180. }
  1181. bool GuiConvexEditorCtrl::handleDelete()
  1182. {
  1183. if ( mActiveTool )
  1184. {
  1185. mActiveTool->onDeactivated( NULL );
  1186. mActiveTool = NULL;
  1187. }
  1188. if ( mConvexSEL )
  1189. {
  1190. if ( mFaceSEL != -1 )
  1191. {
  1192. submitUndo( ModifyShape, mConvexSEL );
  1193. mConvexSEL->mSurfaces.erase_fast( mFaceSEL );
  1194. updateShape( mConvexSEL );
  1195. if ( !isShapeValid( mConvexSEL ) )
  1196. {
  1197. S32 selFace = mFaceSEL;
  1198. mLastUndo->undo();
  1199. mFaceSEL = selFace;
  1200. updateShape( mConvexSEL );
  1201. updateGizmoPos();
  1202. }
  1203. else
  1204. {
  1205. setSelection( mConvexSEL, -1 );
  1206. }
  1207. }
  1208. else
  1209. {
  1210. // Grab the mission editor undo manager.
  1211. UndoManager *undoMan = NULL;
  1212. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1213. {
  1214. Con::errorf( "GuiConvexEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  1215. }
  1216. else
  1217. {
  1218. // Create the UndoAction.
  1219. MEDeleteUndoAction *action = new MEDeleteUndoAction("Deleted ConvexShape");
  1220. action->deleteObject( mConvexSEL );
  1221. mIsDirty = true;
  1222. mFaceHL = -1;
  1223. setSelection( NULL, -1 );
  1224. // Submit it.
  1225. undoMan->addAction( action );
  1226. }
  1227. }
  1228. }
  1229. return true;
  1230. }
  1231. bool GuiConvexEditorCtrl::hasSelection() const
  1232. {
  1233. return mConvexSEL != NULL;
  1234. }
  1235. void GuiConvexEditorCtrl::clearSelection()
  1236. {
  1237. mFaceHL = -1;
  1238. mConvexHL = NULL;
  1239. setSelection( NULL, -1 );
  1240. }
  1241. void GuiConvexEditorCtrl::handleDeselect()
  1242. {
  1243. if ( mActiveTool )
  1244. {
  1245. mActiveTool->onDeactivated( NULL );
  1246. mActiveTool = NULL;
  1247. }
  1248. mFaceHL = -1;
  1249. mConvexHL = NULL;
  1250. setSelection( NULL, -1 );
  1251. }
  1252. void GuiConvexEditorCtrl::setSelection( ConvexShape *shape, S32 faceId )
  1253. {
  1254. mFaceSEL = faceId;
  1255. mConvexSEL = shape;
  1256. updateGizmoPos();
  1257. Con::executef( this, "onSelectionChanged", shape ? shape->getIdString() : "", Con::getIntArg(faceId) );
  1258. }
  1259. void GuiConvexEditorCtrl::_prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* state )
  1260. {
  1261. if ( !isAwake() )
  1262. return;
  1263. /*
  1264. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1265. ri->type = RenderPassManager::RIT_Editor;
  1266. ri->renderDelegate.bind( this, &GuiConvexEditorCtrl::_renderObject );
  1267. ri->defaultKey = 100;
  1268. state->getRenderPass()->addInst( ri );
  1269. */
  1270. }
  1271. void GuiConvexEditorCtrl::_renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst )
  1272. {
  1273. }
  1274. void GuiConvexEditorCtrl::submitUndo( UndoType type, ConvexShape *shape )
  1275. {
  1276. Vector< ConvexShape* > shapes;
  1277. shapes.push_back( shape );
  1278. submitUndo( type, shapes );
  1279. }
  1280. void GuiConvexEditorCtrl::submitUndo( UndoType type, const Vector<ConvexShape*> &shapes )
  1281. {
  1282. // Grab the mission editor undo manager.
  1283. Sim::findObject( "EUndoManager", mUndoManager );
  1284. if ( !mUndoManager )
  1285. {
  1286. Con::errorf( "GuiConvexEditorCtrl::submitUndo() - EUndoManager not found!" );
  1287. return;
  1288. }
  1289. if ( type == ModifyShape )
  1290. {
  1291. // Setup the action.
  1292. GuiConvexEditorUndoAction *action = new GuiConvexEditorUndoAction( "Modified a ConvexShape" );
  1293. ConvexShape *shape = shapes.first();
  1294. action->mObjId = shape->getId();
  1295. action->mEditor = this;
  1296. action->mSavedObjToWorld = shape->getTransform();
  1297. action->mSavedScale = shape->getScale();
  1298. action->mSavedSurfaces.merge( shape->mSurfaces );
  1299. action->mUndoManager = mUndoManager;
  1300. mUndoManager->addAction( action );
  1301. mLastUndo = action;
  1302. }
  1303. else if ( type == CreateShape )
  1304. {
  1305. MECreateUndoAction *action = new MECreateUndoAction( "Create ConvexShape" );
  1306. for ( S32 i = 0; i < shapes.size(); i++ )
  1307. action->addObject( shapes[i] );
  1308. mUndoManager->addAction( action );
  1309. mLastUndo = action;
  1310. }
  1311. else if ( type == DeleteShape )
  1312. {
  1313. MEDeleteUndoAction *action = new MEDeleteUndoAction( "Deleted ConvexShape" );
  1314. for ( S32 i = 0; i < shapes.size(); i++ )
  1315. action->deleteObject( shapes[i] );
  1316. mUndoManager->addAction( action );
  1317. mLastUndo = action;
  1318. }
  1319. else if ( type == HollowShape )
  1320. {
  1321. CompoundUndoAction *action = new CompoundUndoAction( "Hollow ConvexShape" );
  1322. MECreateUndoAction *createAction = new MECreateUndoAction();
  1323. MEDeleteUndoAction *deleteAction = new MEDeleteUndoAction();
  1324. deleteAction->deleteObject( shapes.first() );
  1325. for ( S32 i = 1; i < shapes.size(); i++ )
  1326. createAction->addObject( shapes[i] );
  1327. action->addAction( deleteAction );
  1328. action->addAction( createAction );
  1329. mUndoManager->addAction( action );
  1330. mLastUndo = action;
  1331. }
  1332. mIsDirty = true;
  1333. }
  1334. bool GuiConvexEditorCtrl::_cursorCastCallback( RayInfo* ri )
  1335. {
  1336. // Reject anything that's not a ConvexShape.
  1337. return dynamic_cast< ConvexShape* >( ri->object );
  1338. }
  1339. bool GuiConvexEditorCtrl::_cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace )
  1340. {
  1341. RayInfo ri;
  1342. if ( gServerContainer.castRay( event.pos, event.pos + event.vec * 10000.0f, StaticShapeObjectType, &ri, &GuiConvexEditorCtrl::_cursorCastCallback ) &&
  1343. dynamic_cast< ConvexShape* >( ri.object ) )
  1344. {
  1345. // Do not select or edit ConvexShapes that are within a Prefab.
  1346. if ( Prefab::getPrefabByChild( ri.object ) )
  1347. return false;
  1348. *hitShape = static_cast< ConvexShape* >( ri.object );
  1349. *hitFace = ri.face;
  1350. mLastRayInfo = ri;
  1351. return true;
  1352. }
  1353. return false;
  1354. }
  1355. void GuiConvexEditorCtrl::setPivotPos( ConvexShape *shape, S32 faceId, const Gui3DMouseEvent &event )
  1356. {
  1357. PlaneF plane;
  1358. mTransformPlane( shape->getTransform(), shape->getScale(), shape->mPlanes[ faceId ], &plane );
  1359. Point3F start( event.pos );
  1360. Point3F end( start + event.vec * 10000.0f );
  1361. F32 t = plane.intersect( start, end );
  1362. if ( t >= 0.0f && t <= 1.0f )
  1363. {
  1364. Point3F hitPos;
  1365. hitPos.interpolate( start, end, t );
  1366. mPivotPos = hitPos;
  1367. mUsingPivot = true;
  1368. MatrixF worldToObj( shape->getTransform() );
  1369. worldToObj.scale( shape->getScale() );
  1370. worldToObj.inverse();
  1371. Point3F objPivotPos( mPivotPos );
  1372. worldToObj.mulP( objPivotPos );
  1373. updateGizmoPos();
  1374. }
  1375. }
  1376. void GuiConvexEditorCtrl::cleanMatrix( MatrixF &mat )
  1377. {
  1378. if ( mat.isAffine() )
  1379. return;
  1380. VectorF col0 = mat.getColumn3F(0);
  1381. VectorF col1 = mat.getColumn3F(1);
  1382. VectorF col2 = mat.getColumn3F(2);
  1383. col0.normalize();
  1384. col1.normalize();
  1385. col2.normalize();
  1386. col2 = mCross( col0, col1 );
  1387. col2.normalize();
  1388. col1 = mCross( col2, col0 );
  1389. col1.normalize();
  1390. col0 = mCross( col1, col2 );
  1391. col0.normalize();
  1392. mat.setColumn(0,col0);
  1393. mat.setColumn(1,col1);
  1394. mat.setColumn(2,col2);
  1395. AssertFatal( mat.isAffine(), "GuiConvexEditorCtrl::cleanMatrix, non-affine matrix" );
  1396. }
  1397. S32 GuiConvexEditorCtrl::getEdgeByPoints( ConvexShape *shape, S32 faceId, S32 p0, S32 p1 )
  1398. {
  1399. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1400. for ( S32 i = 0; i < face.edges.size(); i++ )
  1401. {
  1402. const ConvexShape::Edge &edge = face.edges[i];
  1403. if ( edge.p0 != p0 && edge.p0 != p1 )
  1404. continue;
  1405. if ( edge.p1 != p0 && edge.p1 != p1 )
  1406. continue;
  1407. return i;
  1408. }
  1409. return -1;
  1410. }
  1411. bool GuiConvexEditorCtrl::getEdgesTouchingPoint( ConvexShape *shape, S32 faceId, S32 pId, Vector< U32 > &edgeIdxList, S32 excludeEdge )
  1412. {
  1413. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1414. const Vector< ConvexShape::Edge > &edgeList = face.edges;
  1415. for ( S32 i = 0; i < edgeList.size(); i++ )
  1416. {
  1417. if ( i == excludeEdge )
  1418. continue;
  1419. const ConvexShape::Edge &curEdge = edgeList[i];
  1420. if ( curEdge.p0 == pId || curEdge.p1 == pId )
  1421. edgeIdxList.push_back(i);
  1422. }
  1423. return !edgeIdxList.empty();
  1424. }
  1425. void GuiConvexEditorUndoAction::undo()
  1426. {
  1427. ConvexShape *object = NULL;
  1428. if ( !Sim::findObject( mObjId, object ) )
  1429. return;
  1430. // Temporarily save the ConvexShape current data.
  1431. Vector< MatrixF > tempSurfaces;
  1432. tempSurfaces.merge( object->mSurfaces );
  1433. MatrixF tempObjToWorld( object->getTransform() );
  1434. Point3F tempScale( object->getScale() );
  1435. // Restore the Object to the UndoAction state.
  1436. object->mSurfaces.clear();
  1437. object->mSurfaces.merge( mSavedSurfaces );
  1438. object->setScale( mSavedScale );
  1439. object->setTransform( mSavedObjToWorld );
  1440. // Regenerate the ConvexShape and synch the client object.
  1441. object->_updateGeometry();
  1442. GuiConvexEditorCtrl::synchClientObject( object );
  1443. // If applicable set the selected ConvexShape and face
  1444. // on the editor.
  1445. mEditor->setSelection( object, -1 );
  1446. mEditor->updateGizmoPos();
  1447. // Now save the previous ConvexShape data in this UndoAction
  1448. // since an undo action must become a redo action and vice-versa
  1449. mSavedObjToWorld = tempObjToWorld;
  1450. mSavedScale = tempScale;
  1451. mSavedSurfaces.clear();
  1452. mSavedSurfaces.merge( tempSurfaces );
  1453. }
  1454. ConvexEditorCreateTool::ConvexEditorCreateTool( GuiConvexEditorCtrl *editor )
  1455. : Parent( editor ),
  1456. mStage( -1 ),
  1457. mNewConvex( NULL )
  1458. {
  1459. }
  1460. void ConvexEditorCreateTool::onActivated( ConvexEditorTool *prevTool )
  1461. {
  1462. mEditor->clearSelection();
  1463. mStage = -1;
  1464. mNewConvex = NULL;
  1465. }
  1466. void ConvexEditorCreateTool::onDeactivated( ConvexEditorTool *newTool )
  1467. {
  1468. if ( mNewConvex )
  1469. mNewConvex->deleteObject();
  1470. mStage = -1;
  1471. mNewConvex = NULL;
  1472. mEditor->mouseUnlock();
  1473. }
  1474. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseDown( const Gui3DMouseEvent &event )
  1475. {
  1476. if ( mStage == -1 )
  1477. {
  1478. mEditor->setFirstResponder();
  1479. mEditor->mouseLock();
  1480. Point3F start( event.pos );
  1481. Point3F end( event.pos + event.vec * 10000.0f );
  1482. RayInfo ri;
  1483. bool hit = gServerContainer.castRay( event.pos, end, STATIC_COLLISION_TYPEMASK, &ri );
  1484. MatrixF objMat( true );
  1485. // Calculate the orientation matrix of the new ConvexShape
  1486. // based on what has been clicked.
  1487. if ( !hit )
  1488. {
  1489. objMat.setPosition( event.pos + event.vec * 100.0f );
  1490. }
  1491. else
  1492. {
  1493. if ( dynamic_cast< ConvexShape* >( ri.object ) )
  1494. {
  1495. ConvexShape *hitShape = static_cast< ConvexShape* >( ri.object );
  1496. objMat = hitShape->getSurfaceWorldMat( ri.face );
  1497. objMat.setPosition( ri.point );
  1498. }
  1499. else
  1500. {
  1501. Point3F rvec;
  1502. Point3F fvec( mEditor->getCameraMat().getForwardVector() );
  1503. Point3F uvec( ri.normal );
  1504. rvec = mCross( fvec, uvec );
  1505. if ( rvec.isZero() )
  1506. {
  1507. fvec = mEditor->getCameraMat().getRightVector();
  1508. rvec = mCross( fvec, uvec );
  1509. }
  1510. rvec.normalizeSafe();
  1511. fvec = mCross( uvec, rvec );
  1512. fvec.normalizeSafe();
  1513. uvec = mCross( rvec, fvec );
  1514. uvec.normalizeSafe();
  1515. objMat.setColumn( 0, rvec );
  1516. objMat.setColumn( 1, fvec );
  1517. objMat.setColumn( 2, uvec );
  1518. objMat.setPosition( ri.point );
  1519. }
  1520. }
  1521. mNewConvex = new ConvexShape();
  1522. mNewConvex->setTransform( objMat );
  1523. mNewConvex->setField( "material", Parent::mEditor->mMaterialName );
  1524. mNewConvex->registerObject();
  1525. mPlaneSizes.set( 0.1f, 0.1f, 0.1f );
  1526. mNewConvex->resizePlanes( mPlaneSizes );
  1527. mEditor->updateShape( mNewConvex );
  1528. mTransform = objMat;
  1529. mCreatePlane.set( objMat.getPosition(), objMat.getUpVector() );
  1530. }
  1531. else if ( mStage == 0 )
  1532. {
  1533. // Handle this on mouseUp
  1534. }
  1535. return Handled;
  1536. }
  1537. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseUp( const Gui3DMouseEvent &event )
  1538. {
  1539. if ( mNewConvex && mStage == -1 )
  1540. {
  1541. mStage = 0;
  1542. mCreatePlane = PlaneF( mNewConvex->getPosition(), mNewConvex->getTransform().getForwardVector() );
  1543. mTransform.setPosition( mNewConvex->getPosition() );
  1544. return Handled;
  1545. }
  1546. else if ( mStage == 0 )
  1547. {
  1548. SimGroup *scene = Scene::getRootScene();
  1549. scene->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. Scene* scene = Scene::getRootScene();
  1688. if ( scene )
  1689. scene->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. DefineEngineMethod( GuiConvexEditorCtrl, hollowSelection, void, (), , "" )
  1943. {
  1944. object->hollowSelection();
  1945. }
  1946. DefineEngineMethod( GuiConvexEditorCtrl, recenterSelection, void, (), , "" )
  1947. {
  1948. object->recenterSelection();
  1949. }
  1950. DefineEngineMethod( GuiConvexEditorCtrl, hasSelection, S32, (), , "" )
  1951. {
  1952. return object->hasSelection();
  1953. }
  1954. DefineEngineMethod( GuiConvexEditorCtrl, handleDelete, void, (), , "" )
  1955. {
  1956. object->handleDelete();
  1957. }
  1958. DefineEngineMethod( GuiConvexEditorCtrl, handleDeselect, void, (), , "" )
  1959. {
  1960. object->handleDeselect();
  1961. }
  1962. DefineEngineMethod( GuiConvexEditorCtrl, dropSelectionAtScreenCenter, void, (), , "" )
  1963. {
  1964. object->dropSelectionAtScreenCenter();
  1965. }
  1966. DefineEngineMethod( GuiConvexEditorCtrl, selectConvex, void, (ConvexShape *convex), , "( ConvexShape )" )
  1967. {
  1968. if (convex)
  1969. object->setSelection( convex, -1 );
  1970. }
  1971. DefineEngineMethod( GuiConvexEditorCtrl, splitSelectedFace, void, (), , "" )
  1972. {
  1973. object->splitSelectedFace();
  1974. }