guiConvexShapeEditorCtrl.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  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. mHasGeometry(false),
  65. mDragging( false ),
  66. mGizmoMatOffset( Point3F::Zero ),
  67. mPivotPos( Point3F::Zero ),
  68. mUsingPivot( false ),
  69. mSettingPivot( false ),
  70. mActiveTool( NULL ),
  71. mMouseDown( false ),
  72. mCreateTool( NULL ),
  73. mSavedGizmoFlags( -1 ),
  74. mHasCopied( false ),
  75. mLastUndo( NULL ),
  76. mUndoManager( NULL ),
  77. mCtrlDown( false ),
  78. mGridSnap(false)
  79. {
  80. mMaterialName = StringTable->insert("Prototyping:WallOrange");
  81. }
  82. GuiConvexEditorCtrl::~GuiConvexEditorCtrl()
  83. {
  84. }
  85. bool GuiConvexEditorCtrl::onAdd()
  86. {
  87. if ( !Parent::onAdd() )
  88. return false;
  89. SceneManager::getPreRenderSignal().notify( this, &GuiConvexEditorCtrl::_prepRenderImage );
  90. mCreateTool = new ConvexEditorCreateTool( this );
  91. return true;
  92. }
  93. void GuiConvexEditorCtrl::onRemove()
  94. {
  95. SceneManager::getPreRenderSignal().remove( this, &GuiConvexEditorCtrl::_prepRenderImage );
  96. SAFE_DELETE( mCreateTool );
  97. Parent::onRemove();
  98. }
  99. void GuiConvexEditorCtrl::initPersistFields()
  100. {
  101. addField( "isDirty", TypeBool, Offset( mIsDirty, GuiConvexEditorCtrl ) );
  102. addField( "materialName", TypeMaterialAssetId, Offset(mMaterialName, GuiConvexEditorCtrl) );
  103. Parent::initPersistFields();
  104. }
  105. bool GuiConvexEditorCtrl::onWake()
  106. {
  107. if ( !Parent::onWake() )
  108. return false;
  109. Scene* scene = Scene::getRootScene();
  110. if ( !scene )
  111. return true;
  112. SimGroup::iterator itr = scene->begin();
  113. for ( ; itr != scene->end(); itr++ )
  114. {
  115. if ( String::compare( (*itr)->getClassName(), "ConvexShape" ) == 0 )
  116. {
  117. mConvexSEL = static_cast<ConvexShape*>( *itr );
  118. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  119. return true;
  120. }
  121. }
  122. return true;
  123. }
  124. void GuiConvexEditorCtrl::onSleep()
  125. {
  126. Parent::onSleep();
  127. mConvexSEL = NULL;
  128. mConvexHL = NULL;
  129. }
  130. void GuiConvexEditorCtrl::setVisible( bool val )
  131. {
  132. //ConvexShape::smRenderEdges = value;
  133. if ( isProperlyAdded() )
  134. {
  135. if ( !val )
  136. {
  137. mFaceHL = -1;
  138. mConvexHL = NULL;
  139. setSelection( NULL, -1 );
  140. if ( mSavedGizmoFlags != -1 )
  141. {
  142. mGizmoProfile->flags = mSavedGizmoFlags;
  143. mSavedGizmoFlags = -1;
  144. }
  145. Scene* scene = Scene::getRootScene();
  146. if (scene != nullptr)
  147. {
  148. //Make our proxy objects "real" again
  149. for (U32 i = 0; i < mProxyObjects.size(); ++i)
  150. {
  151. if (!mProxyObjects[i].shapeProxy || !mProxyObjects[i].targetObject)
  152. continue;
  153. AbstractClassRep* classRep = AbstractClassRep::findClassRep(mProxyObjects[i].targetObjectClass);
  154. if (!classRep)
  155. {
  156. Con::errorf("WorldEditor::createPolyhedralObject - No such class: %s", mProxyObjects[i].targetObjectClass.c_str());
  157. continue;
  158. }
  159. SceneObject* polyObj = createPolyhedralObject(mProxyObjects[i].targetObjectClass.c_str(), mProxyObjects[i].shapeProxy);
  160. scene->addObject(polyObj);
  161. //Now, remove the convex proxy
  162. mProxyObjects[i].shapeProxy->deleteObject();
  163. mProxyObjects[i].targetObject->deleteObject();
  164. mProxyObjects.erase(i);
  165. --i;
  166. }
  167. }
  168. }
  169. else
  170. {
  171. mConvexHL = NULL;
  172. mFaceHL = -1;
  173. setSelection( NULL, -1 );
  174. WorldEditor *wedit;
  175. if ( Sim::findObject( "EWorldEditor", wedit ) )
  176. {
  177. S32 count = wedit->getSelectionSize();
  178. for ( S32 i = 0; i < count; i++ )
  179. {
  180. S32 objId = wedit->getSelectObject(i);
  181. ConvexShape *pShape;
  182. if ( Sim::findObject( objId, pShape ) )
  183. {
  184. mConvexSEL = pShape;
  185. wedit->clearSelection();
  186. wedit->selectObject( String::ToString("%i",objId) );
  187. break;
  188. }
  189. }
  190. }
  191. updateGizmoPos();
  192. mSavedGizmoFlags = mGizmoProfile->flags;
  193. Scene* scene = Scene::getRootScene();
  194. if (scene != nullptr)
  195. {
  196. for (U32 c = 0; c < scene->size(); ++c)
  197. {
  198. bool isTrigger = (scene->at(c)->getClassName() == StringTable->insert("Trigger"));
  199. bool isZone = (scene->at(c)->getClassName() == StringTable->insert("Zone"));
  200. bool isPortal = (scene->at(c)->getClassName() == StringTable->insert("Portal"));
  201. bool isOccluder = (scene->at(c)->getClassName() == StringTable->insert("OcclusionVolume"));
  202. if (isZone || isPortal || isOccluder)
  203. {
  204. SceneObject* sceneObj = static_cast<SceneObject*>(scene->at(c));
  205. if (!sceneObj)
  206. {
  207. Con::errorf("WorldEditor::createConvexShapeFrom - Invalid object");
  208. continue;
  209. }
  210. ConvexShape* proxyShape = createConvexShapeFrom(sceneObj);
  211. //Set the texture to a representatory one so we know what's what
  212. if (isTrigger)
  213. proxyShape->mMaterialName = "ToolsModule:TriggerProxyMaterial";
  214. else if (isPortal)
  215. proxyShape->mMaterialName = "ToolsModule:PortalProxyMaterial";
  216. else if (isZone)
  217. proxyShape->mMaterialName = "ToolsModule:ZoneProxyMaterial";
  218. else if (isOccluder)
  219. proxyShape->mMaterialName = "ToolsModule:OccluderProxyMaterial";
  220. proxyShape->_updateMaterial();
  221. sceneObj->setHidden(true);
  222. //set up the proxy object
  223. ConvexShapeProxy newProxy;
  224. newProxy.shapeProxy = proxyShape;
  225. newProxy.targetObject = sceneObj;
  226. if (isTrigger)
  227. newProxy.targetObjectClass = "Trigger";
  228. else if (isPortal)
  229. newProxy.targetObjectClass = "Portal";
  230. else if (isZone)
  231. newProxy.targetObjectClass = "Zone";
  232. else
  233. newProxy.targetObjectClass = "OcclusionVolume";
  234. mProxyObjects.push_back(newProxy);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. Parent::setVisible( val );
  241. }
  242. void GuiConvexEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  243. {
  244. mouseLock();
  245. mMouseDown = true;
  246. if ( event.modifier & SI_ALT )
  247. {
  248. setActiveTool( mCreateTool );
  249. mActiveTool->on3DMouseDown( event );
  250. return;
  251. }
  252. if ( mConvexSEL && isShapeValid( mConvexSEL ) )
  253. mLastValidShape = mConvexSEL->mSurfaces;
  254. if ( mConvexSEL &&
  255. mFaceSEL != -1 &&
  256. mGizmo->getMode() == RotateMode &&
  257. mGizmo->getSelection() == Gizmo::Centroid )
  258. {
  259. mSettingPivot = true;
  260. mSavedPivotPos = mGizmo->getPosition();
  261. setPivotPos( mConvexSEL, mFaceSEL, event );
  262. updateGizmoPos();
  263. return;
  264. }
  265. mGizmo->on3DMouseDown( event );
  266. }
  267. void GuiConvexEditorCtrl::on3DRightMouseDown(const Gui3DMouseEvent & event)
  268. {
  269. return;
  270. /*
  271. if ( mConvexSEL && mFaceSEL != -1 && mFaceSEL == mFaceHL )
  272. {
  273. _submitUndo( "Split ConvexShape face." );
  274. const MatrixF &surf = mConvexSEL->mSurfaces[mFaceSEL];
  275. MatrixF newSurf( surf );
  276. MatrixF rotMat( EulerF( 0.0f, mDegToRad( 2.0f ), 0.0f ) );
  277. newSurf *= rotMat;
  278. mConvexSEL->mSurfaces.insert( mFaceSEL+1, newSurf );
  279. }
  280. */
  281. }
  282. void GuiConvexEditorCtrl::on3DRightMouseUp(const Gui3DMouseEvent & event)
  283. {
  284. //ConvexShape *hitShape;
  285. //S32 hitFace;
  286. //bool hit = _cursorCast( event, &hitShape, &hitFace );
  287. //Con::printf( hit ? "HIT" : "MISS" );
  288. }
  289. void GuiConvexEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  290. {
  291. mouseUnlock();
  292. mMouseDown = false;
  293. mHasCopied = false;
  294. mHasGeometry = false;
  295. if ( mActiveTool )
  296. {
  297. ConvexEditorTool::EventResult result = mActiveTool->on3DMouseUp( event );
  298. if ( result == ConvexEditorTool::Done )
  299. setActiveTool( NULL );
  300. return;
  301. }
  302. if ( !mSettingPivot && !mDragging && ( mGizmo->getSelection() == Gizmo::None || !mConvexSEL ) )
  303. {
  304. if ( mConvexSEL != mConvexHL )
  305. {
  306. setSelection( mConvexHL, -1 );
  307. }
  308. else
  309. {
  310. if ( mFaceSEL != mFaceHL )
  311. setSelection( mConvexSEL, mFaceHL );
  312. else
  313. setSelection( mConvexSEL, -1 );
  314. }
  315. mUsingPivot = false;
  316. }
  317. mSettingPivot = false;
  318. mSavedPivotPos = mGizmo->getPosition();
  319. mSavedUndo = false;
  320. mGizmo->on3DMouseUp( event );
  321. if ( mDragging )
  322. {
  323. mDragging = false;
  324. if ( mConvexSEL )
  325. {
  326. Vector< U32 > removedPlanes;
  327. mConvexSEL->cullEmptyPlanes( &removedPlanes );
  328. // If a face has been removed we need to validate / remap
  329. // our selected and highlighted faces.
  330. if ( !removedPlanes.empty() )
  331. {
  332. S32 prevFaceHL = mFaceHL;
  333. S32 prevFaceSEL = mFaceSEL;
  334. if ( removedPlanes.contains( mFaceHL ) )
  335. prevFaceHL = mFaceHL = -1;
  336. if ( removedPlanes.contains( mFaceSEL ) )
  337. prevFaceSEL = mFaceSEL = -1;
  338. for ( S32 i = 0; i < removedPlanes.size(); i++ )
  339. {
  340. if ( (S32)removedPlanes[i] < prevFaceSEL )
  341. mFaceSEL--;
  342. if ( (S32)removedPlanes[i] < prevFaceHL )
  343. mFaceHL--;
  344. }
  345. setSelection( mConvexSEL, mFaceSEL );
  346. // We need to reindex faces.
  347. updateShape( mConvexSEL );
  348. }
  349. }
  350. }
  351. updateGizmoPos();
  352. }
  353. void GuiConvexEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  354. {
  355. if ( mActiveTool )
  356. {
  357. // If we have an active tool pass this event to it.
  358. // If it handled it, consume the event.
  359. if ( mActiveTool->on3DMouseMove( event ) )
  360. return;
  361. }
  362. ConvexShape *hitShape = NULL;
  363. S32 hitFace = -1;
  364. _cursorCast( event, &hitShape, &hitFace );
  365. if ( !mConvexSEL )
  366. {
  367. mConvexHL = hitShape;
  368. mFaceHL = -1;
  369. }
  370. else
  371. {
  372. if ( mConvexSEL == hitShape )
  373. {
  374. mConvexHL = hitShape;
  375. mFaceHL = hitFace;
  376. }
  377. else
  378. {
  379. // Mousing over a shape that is not the one currently selected.
  380. if ( mFaceSEL != -1 )
  381. {
  382. mFaceHL = -1;
  383. }
  384. else
  385. {
  386. mConvexHL = hitShape;
  387. mFaceHL = -1;
  388. }
  389. }
  390. }
  391. if ( mConvexSEL )
  392. mGizmo->on3DMouseMove( event );
  393. }
  394. void GuiConvexEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  395. {
  396. if ( mActiveTool )
  397. {
  398. // If we have an active tool pass this event to it.
  399. // If it handled it, consume the event.
  400. if ( mActiveTool->on3DMouseDragged( event ) )
  401. return;
  402. }
  403. //mGizmoProfile->rotateScalar = 0.55f;
  404. //mGizmoProfile->scaleScalar = 0.55f;
  405. if ( !mConvexSEL )
  406. return;
  407. if ( mGizmo->getMode() == RotateMode &&
  408. mGizmo->getSelection() == Gizmo::Centroid )
  409. {
  410. setPivotPos( mConvexSEL, mFaceSEL, event );
  411. mDragging = true;
  412. return;
  413. }
  414. mGizmo->on3DMouseDragged( event );
  415. if ( event.modifier & SI_SHIFT &&
  416. ( mGizmo->getMode() == MoveMode || mGizmo->getMode() == RotateMode ) &&
  417. !mHasCopied )
  418. {
  419. if ( mFaceSEL != -1 )
  420. {
  421. ConvexShape *newShape = mCreateTool->extrudeShapeFromFace( mConvexSEL, mFaceSEL );
  422. //newShape->_updateGeometry();
  423. submitUndo( CreateShape, newShape );
  424. setSelection( newShape, 0 );
  425. updateGizmoPos();
  426. mGizmo->on3DMouseDown( event );
  427. mHasCopied = true;
  428. mSavedUndo = true;
  429. }
  430. else
  431. {
  432. ConvexShape *newShape = new ConvexShape();
  433. newShape->setTransform( mConvexSEL->getTransform() );
  434. newShape->setScale( mConvexSEL->getScale() );
  435. newShape->mSurfaces.clear();
  436. newShape->mSurfaces.merge( mConvexSEL->mSurfaces );
  437. setupShape( newShape );
  438. newShape->setField("material", mConvexSEL->getMaterialName());
  439. submitUndo( CreateShape, newShape );
  440. setSelection( newShape, -1 );
  441. updateGizmoPos();
  442. mHasCopied = true;
  443. mSavedUndo = true;
  444. }
  445. return;
  446. }
  447. if ( mGizmo->getMode() == RotateMode &&
  448. event.modifier & SI_CTRL &&
  449. !mHasCopied &&
  450. mFaceSEL != -1 )
  451. {
  452. // Can must verify that splitting the face at the current angle
  453. // ( of the gizmo ) will generate a valid shape. If not enough rotation
  454. // has occurred we will have two faces that are coplanar and must wait
  455. // until later in the drag to perform the split.
  456. //AssertFatal( isShapeValid( mConvexSEL ), "Shape was already invalid at beginning of split operation." );
  457. if ( !isShapeValid( mConvexSEL ) )
  458. return;
  459. mLastValidShape = mConvexSEL->mSurfaces;
  460. Point3F rot = mGizmo->getDeltaTotalRot();
  461. rot.normalize();
  462. rot *= mDegToRad( 10.0f );
  463. MatrixF rotMat( (EulerF)rot );
  464. MatrixF worldToObj( mConvexSEL->getTransform() );
  465. worldToObj.scale( mConvexSEL->getScale() );
  466. worldToObj.inverse();
  467. mConvexSEL->mSurfaces.increment();
  468. MatrixF &newSurf = mConvexSEL->mSurfaces.last();
  469. newSurf = mConvexSEL->mSurfaces[mFaceSEL] * rotMat;
  470. //worldToObj.mul( mGizmo->getTransform() );
  471. //Point3F pos( mPivotPos );
  472. //worldToObj.mulP( pos );
  473. //newSurf.setPosition( pos );
  474. updateShape( mConvexSEL );
  475. if ( !isShapeValid( mConvexSEL ) )
  476. {
  477. mConvexSEL->mSurfaces = mLastValidShape;
  478. updateShape( mConvexSEL );
  479. }
  480. else
  481. {
  482. mHasCopied = true;
  483. mSavedUndo = true;
  484. mLastValidShape = mConvexSEL->mSurfaces;
  485. submitUndo( ModifyShape, mConvexSEL );
  486. setSelection( mConvexSEL, mConvexSEL->mSurfaces.size() - 1 );
  487. updateGizmoPos();
  488. }
  489. return;
  490. }
  491. // If we are dragging, but no gizmo selection...
  492. // Then treat this like a regular mouse move, update the highlighted
  493. // convex/face under the cursor and handle onMouseUp as we normally would
  494. // to change the selection.
  495. if ( mGizmo->getSelection() == Gizmo::None )
  496. {
  497. ConvexShape *hitShape = NULL;
  498. S32 hitFace = -1;
  499. _cursorCast( event, &hitShape, &hitFace );
  500. mFaceHL = hitFace;
  501. mConvexHL = hitShape;
  502. return;
  503. }
  504. mDragging = true;
  505. // Manipulating a face.
  506. if ( mFaceSEL != -1 )
  507. {
  508. if ( !mSavedUndo )
  509. {
  510. mSavedUndo = true;
  511. submitUndo( ModifyShape, mConvexSEL );
  512. }
  513. if ( mGizmo->getMode() == ScaleMode && !(event.modifier & SI_CTRL))
  514. {
  515. scaleFace( mConvexSEL, mFaceSEL, mGizmo->getScale() );
  516. }
  517. else if ( mGizmo->getMode() == ScaleMode && (event.modifier & SI_CTRL) )
  518. {
  519. Point3F scale = mGizmo->getDeltaScale();
  520. F32 scalar = 1;
  521. mConvexSEL->mSurfaceUVs[mFaceSEL].scale += (Point2F(scale.x, scale.y) * scalar);
  522. if (mConvexSEL->mSurfaceUVs[mFaceSEL].scale.x < 0.01)
  523. mConvexSEL->mSurfaceUVs[mFaceSEL].scale.x = 0.01;
  524. if (mConvexSEL->mSurfaceUVs[mFaceSEL].scale.y < 0.01)
  525. mConvexSEL->mSurfaceUVs[mFaceSEL].scale.y = 0.01;
  526. if (mConvexSEL->mSurfaceUVs[mFaceSEL].scale.x > 100)
  527. mConvexSEL->mSurfaceUVs[mFaceSEL].scale.x = 100;
  528. if (mConvexSEL->mSurfaceUVs[mFaceSEL].scale.y > 100)
  529. mConvexSEL->mSurfaceUVs[mFaceSEL].scale.y = 100;
  530. Point2F test = mConvexSEL->mSurfaceUVs[mFaceSEL].scale;
  531. mConvexSEL->setMaskBits( ConvexShape::UpdateMask );
  532. updateShape( mConvexSEL, mFaceSEL );
  533. }
  534. /*else if ( mGizmo->getMode() == MoveMode && event.modifier & SI_CTRL ) {
  535. Point3F scale = mGizmo->getOffset();
  536. F32 scalar = 0.8;
  537. mConvexSEL->mSurfaceTextures[mFaceSEL].offset += (Point2F(-scale.x, scale.z) * scalar);
  538. mConvexSEL->setMaskBits( ConvexShape::UpdateMask );
  539. updateShape( mConvexSEL, mFaceSEL );
  540. }*/
  541. else
  542. {
  543. // Why does this have to be so ugly.
  544. if ( mGizmo->getMode() == RotateMode ||
  545. ( mGizmo->getMode() == MoveMode &&
  546. ( event.modifier & SI_CTRL ||
  547. ( mGizmo->getSelection() == Gizmo::Axis_Z && mHasCopied )
  548. )
  549. )
  550. )
  551. {
  552. const MatrixF &gMat = mGizmo->getTransform();
  553. MatrixF surfMat;
  554. surfMat.mul( mConvexSEL->mWorldToObj, gMat );
  555. MatrixF worldToObj ( mConvexSEL->getTransform() );
  556. worldToObj.scale( mConvexSEL->getScale() );
  557. worldToObj.inverse();
  558. Point3F newPos;
  559. newPos = gMat.getPosition();
  560. worldToObj.mulP( newPos );
  561. surfMat.setPosition( newPos );
  562. // Clear out floating point errors.
  563. cleanMatrix( surfMat );
  564. if (mGizmo->getSelection() == Gizmo::Axis_Z)
  565. {
  566. MatrixF curSurfMat = mConvexSEL->mSurfaces[mFaceSEL];
  567. EulerF curSufRot = curSurfMat.toEuler();
  568. EulerF newSufRot = surfMat.toEuler();
  569. float zRot = mRadToDeg(newSufRot.z - curSufRot.z);
  570. float curZRot = mConvexSEL->mSurfaceUVs[mFaceSEL].zRot;
  571. mConvexSEL->mSurfaceUVs[mFaceSEL].zRot += zRot;
  572. }
  573. mConvexSEL->mSurfaces[mFaceSEL] = surfMat;
  574. updateShape( mConvexSEL, mFaceSEL );
  575. }
  576. else
  577. {
  578. // Translating a face in x/y/z
  579. translateFace( mConvexSEL, mFaceSEL, mGizmo->getTotalOffset() );
  580. }
  581. }
  582. if ( isShapeValid( mConvexSEL ) )
  583. {
  584. AssertFatal( mConvexSEL->mSurfaces.size() > mFaceSEL, "mFaceSEL out of range." );
  585. mLastValidShape = mConvexSEL->mSurfaces;
  586. }
  587. else
  588. {
  589. AssertFatal( mLastValidShape.size() > mFaceSEL, "mFaceSEL out of range." );
  590. mConvexSEL->mSurfaces = mLastValidShape;
  591. updateShape( mConvexSEL );
  592. }
  593. return;
  594. }
  595. // Manipulating a whole Convex.
  596. if ( !mSavedUndo )
  597. {
  598. mSavedUndo = true;
  599. submitUndo( ModifyShape, mConvexSEL );
  600. }
  601. if ( mGizmo->getMode() == MoveMode )
  602. {
  603. //mConvexSEL->setPosition( mGizmo->getPosition() );
  604. //MatrixF mat = mGizmo->getTransform();
  605. Point3F wPos = mGizmo->getPosition();
  606. //mat.getColumn(3, &wPos);
  607. // adjust
  608. //wPos += offset;
  609. if (mGridSnap && mGridPlaneSize != 0.f)
  610. {
  611. if (mGizmo->getSelection() == Gizmo::Selection::Axis_X || mGizmo->getSelection() == Gizmo::Selection::Plane_XY || mGizmo->getSelection() == Gizmo::Selection::Plane_XZ)
  612. wPos.x -= mFmod(wPos.x, mGridPlaneSize);
  613. if (mGizmo->getSelection() == Gizmo::Selection::Axis_Y || mGizmo->getSelection() == Gizmo::Selection::Plane_XY || mGizmo->getSelection() == Gizmo::Selection::Plane_YZ)
  614. wPos.y -= mFmod(wPos.y, mGridPlaneSize);
  615. if (mGizmo->getSelection() == Gizmo::Selection::Axis_Z || mGizmo->getSelection() == Gizmo::Selection::Plane_XZ || mGizmo->getSelection() == Gizmo::Selection::Plane_YZ)
  616. wPos.z -= mFmod(wPos.z, mGridPlaneSize);
  617. }
  618. mConvexSEL->setPosition(wPos);
  619. }
  620. else if ( mGizmo->getMode() == RotateMode )
  621. {
  622. mConvexSEL->setTransform( mGizmo->getTransform() );
  623. }
  624. else
  625. {
  626. mConvexSEL->setScale( mGizmo->getScale() );
  627. }
  628. if ( mConvexSEL->getClientObject() )
  629. {
  630. ConvexShape *clientObj = static_cast< ConvexShape* >( mConvexSEL->getClientObject() );
  631. clientObj->setTransform( mConvexSEL->getTransform() );
  632. clientObj->setScale( mConvexSEL->getScale() );
  633. }
  634. }
  635. void GuiConvexEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  636. {
  637. }
  638. void GuiConvexEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  639. {
  640. }
  641. bool GuiConvexEditorCtrl::onKeyDown( const GuiEvent &evt )
  642. {
  643. bool handled = false;
  644. switch ( evt.keyCode )
  645. {
  646. case KEY_ESCAPE:
  647. handled = handleEscape();
  648. break;
  649. case KEY_A:
  650. if ( evt.modifier & SI_ALT )
  651. {
  652. GizmoAlignment align = mGizmo->getProfile()->alignment;
  653. if ( align == World )
  654. mGizmo->getProfile()->alignment = Object;
  655. else
  656. mGizmo->getProfile()->alignment = World;
  657. handled = true;
  658. }
  659. break;
  660. case KEY_LCONTROL:
  661. //mCtrlDown = true;
  662. break;
  663. default:
  664. break;
  665. }
  666. return handled;
  667. }
  668. bool GuiConvexEditorCtrl::onKeyUp( const GuiEvent &evt )
  669. {
  670. bool handled = false;
  671. switch ( evt.keyCode )
  672. {
  673. case KEY_LCONTROL:
  674. //mCtrlDown = false;
  675. break;
  676. default:
  677. break;
  678. }
  679. return handled;
  680. }
  681. void GuiConvexEditorCtrl::get3DCursor( GuiCursor *&cursor,
  682. bool &visible,
  683. const Gui3DMouseEvent &event_ )
  684. {
  685. //cursor = mAddNodeCursor;
  686. //visible = false;
  687. cursor = NULL;
  688. visible = false;
  689. GuiCanvas *root = getRoot();
  690. if ( !root )
  691. return;
  692. S32 currCursor = PlatformCursorController::curArrow;
  693. if ( root->mCursorChanged == currCursor )
  694. return;
  695. PlatformWindow *window = root->getPlatformWindow();
  696. PlatformCursorController *controller = window->getCursorController();
  697. // We've already changed the cursor,
  698. // so set it back before we change it again.
  699. if( root->mCursorChanged != -1)
  700. controller->popCursor();
  701. // Now change the cursor shape
  702. controller->pushCursor(currCursor);
  703. root->mCursorChanged = currCursor;
  704. }
  705. void GuiConvexEditorCtrl::updateGizmo()
  706. {
  707. mGizmoProfile->restoreDefaultState();
  708. const GizmoMode &mode = mGizmoProfile->mode;
  709. S32 &flags = mGizmoProfile->flags;
  710. GizmoAlignment &align = mGizmoProfile->alignment;
  711. U8 keys = Input::getModifierKeys();
  712. mCtrlDown = keys & ( SI_LCTRL | SI_LSHIFT );
  713. bool altDown = keys & ( SI_LALT );
  714. if ( altDown )
  715. {
  716. flags = 0;
  717. return;
  718. }
  719. if ( mFaceSEL != -1 )
  720. {
  721. align = Object;
  722. flags |= GizmoProfile::CanRotateUniform;
  723. flags &= ~GizmoProfile::CanRotateScreen;
  724. }
  725. else
  726. {
  727. flags &= ~GizmoProfile::CanRotateUniform;
  728. flags |= GizmoProfile::CanRotateScreen;
  729. }
  730. if ( mFaceSEL != -1 && mode == ScaleMode )
  731. flags &= ~GizmoProfile::CanScaleZ;
  732. else
  733. flags |= GizmoProfile::CanScaleZ;
  734. if ( mFaceSEL != -1 && mode == MoveMode )
  735. {
  736. if ( mCtrlDown )
  737. //hijacking the CTRL modifier for texture offsetting control
  738. //flags &= ~( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  739. flags &= ~GizmoProfile::CanScaleZ;
  740. else
  741. flags |= ( GizmoProfile::CanTranslateX | GizmoProfile::CanTranslateY | GizmoProfile::PlanarHandlesOn );
  742. }
  743. }
  744. void GuiConvexEditorCtrl::renderScene(const RectI & updateRect)
  745. {
  746. // Synch selected ConvexShape with the WorldEditor.
  747. WorldEditor *wedit;
  748. if ( Sim::findObject( "EWorldEditor", wedit) )
  749. {
  750. S32 count = wedit->getSelectionSize();
  751. if ( !mConvexSEL && count != 0 )
  752. wedit->clearSelection();
  753. else if ( mConvexSEL && count != 1 )
  754. {
  755. wedit->clearSelection();
  756. wedit->selectObject( mConvexSEL->getIdString() );
  757. }
  758. else if ( mConvexSEL && count == 1 )
  759. {
  760. if ( wedit->getSelectObject(0) != mConvexSEL->getId() )
  761. {
  762. wedit->clearSelection();
  763. wedit->selectObject( mConvexSEL->getIdString() );
  764. }
  765. }
  766. }
  767. // Update status bar text.
  768. SimObject *statusbar;
  769. if ( Sim::findObject( "EditorGuiStatusBar", statusbar ) )
  770. {
  771. String text( "Sketch Tool." );
  772. GizmoMode mode = mGizmo->getMode();
  773. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  774. {
  775. Point3F delta;
  776. String qualifier;
  777. if ( mode == RotateMode )
  778. {
  779. if ( mSettingPivot )
  780. delta = mGizmo->getPosition() - mSavedPivotPos;
  781. else
  782. delta = mGizmo->getDeltaTotalRot();
  783. }
  784. else if ( mode == MoveMode )
  785. delta = mGizmo->getTotalOffset();
  786. else if ( mode == ScaleMode )
  787. delta = mGizmo->getDeltaTotalScale();
  788. if ( mGizmo->getAlignment() == Object &&
  789. mode != ScaleMode )
  790. {
  791. mConvexSEL->mWorldToObj.mulV( delta );
  792. if ( mFaceSEL != -1 && mode != RotateMode )
  793. {
  794. MatrixF objToSurf( mConvexSEL->mSurfaces[ mFaceSEL ] );
  795. objToSurf.scale( mConvexSEL->getScale() );
  796. objToSurf.inverse();
  797. objToSurf.mulV( delta );
  798. }
  799. }
  800. if ( mIsZero( delta.x, 0.0001f ) )
  801. delta.x = 0.0f;
  802. if ( mIsZero( delta.y, 0.0001f ) )
  803. delta.y = 0.0f;
  804. if ( mIsZero( delta.z, 0.0001f ) )
  805. delta.z = 0.0f;
  806. if ( mode == RotateMode )
  807. {
  808. if ( mSettingPivot )
  809. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  810. else
  811. {
  812. delta.x = mRadToDeg( delta.x );
  813. delta.y = mRadToDeg( delta.y );
  814. delta.z = mRadToDeg( delta.z );
  815. text = String::ToString( "Delta angle ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  816. }
  817. }
  818. else if ( mode == MoveMode )
  819. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  820. else if ( mode == ScaleMode )
  821. text = String::ToString( "Delta scale ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  822. }
  823. else
  824. {
  825. if ( !mConvexSEL )
  826. text = "Sketch Tool. ALT + Click-Drag to create a new ConvexShape.";
  827. else if ( mFaceSEL == -1 )
  828. {
  829. if ( mode == MoveMode )
  830. text = "Move selection. SHIFT while dragging duplicates objects.";
  831. else if ( mode == RotateMode )
  832. text = "Rotate selection.";
  833. else if ( mode == ScaleMode )
  834. text = "Scale selection.";
  835. }
  836. else
  837. {
  838. if ( mode == MoveMode )
  839. text = "Move face. SHIFT while beginning a drag EXTRUDES a new convex. Press CTRL for alternate translation mode.";
  840. else if ( mode == RotateMode )
  841. text = "Rotate face. Gizmo/Pivot is draggable. CTRL while dragging splits/folds a new face. SHIFT while dragging extrudes a new convex.";
  842. else if ( mode == ScaleMode )
  843. text = "Scale face.";
  844. }
  845. }
  846. // Issue a warning in the status bar
  847. // if this convex has an excessive number of surfaces...
  848. if ( mConvexSEL && mConvexSEL->getSurfaces().size() > ConvexShape::smMaxSurfaces )
  849. {
  850. text = "WARNING: Reduce the number of surfaces on the selected ConvexShape, only the first 100 will be saved!";
  851. }
  852. Con::executef( statusbar, "setInfo", text.c_str() );
  853. Con::executef( statusbar, "setSelectionObjectsByCount", Con::getIntArg( mConvexSEL == NULL ? 0 : 1 ) );
  854. }
  855. if ( mActiveTool )
  856. mActiveTool->renderScene( updateRect );
  857. ColorI colorHL( 255, 50, 255, 255 );
  858. ColorI colorSEL( 255, 50, 255, 255 );
  859. ColorI colorNA( 255, 255, 255, 100 );
  860. GFXDrawUtil *drawer = GFX->getDrawUtil();
  861. if ( mConvexSEL && !mDragging )
  862. {
  863. if ( mFaceSEL == -1 )
  864. {
  865. GFXStateBlockDesc desc;
  866. desc.setBlend( true );
  867. desc.setZReadWrite( true, true );
  868. Box3F objBox = mConvexSEL->getObjBox();
  869. objBox.scale( mConvexSEL->getScale() );
  870. const MatrixF &objMat = mConvexSEL->getTransform();
  871. Point3F boxPos = objBox.getCenter();
  872. objMat.mulP( boxPos );
  873. drawer->drawObjectBox( desc, objBox.getExtents(), boxPos, objMat, ColorI::WHITE );
  874. }
  875. else
  876. {
  877. mConvexSEL->renderFaceEdges( -1, colorNA );
  878. drawFacePlane( mConvexSEL, mFaceSEL );
  879. }
  880. if ( mConvexHL == mConvexSEL &&
  881. mFaceHL != -1 &&
  882. mFaceHL != mFaceSEL &&
  883. mGizmo->getSelection() == Gizmo::None )
  884. {
  885. mConvexSEL->renderFaceEdges( mFaceHL, colorHL );
  886. }
  887. }
  888. if ( mConvexHL && mConvexHL != mConvexSEL )
  889. {
  890. mConvexHL->renderFaceEdges( -1 );
  891. }
  892. if ( mGizmo->getMode() != RotateMode && mUsingPivot )
  893. {
  894. mUsingPivot = false;
  895. updateGizmoPos();
  896. }
  897. F32 gizmoAlpha = 1.0f;
  898. if ( !mConvexSEL )
  899. gizmoAlpha = 0.0f;
  900. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None && mConvexSEL )
  901. {
  902. if ( mSettingPivot )
  903. gizmoAlpha = 1.0f;
  904. else
  905. gizmoAlpha = 0.0f;
  906. }
  907. DebugDrawer::get()->render();
  908. {
  909. GFXTransformSaver saver;
  910. // Now draw all the 2d stuff!
  911. GFX->setClipRect(updateRect);
  912. if ( mConvexSEL && mFaceSEL != -1 )
  913. {
  914. Vector< Point3F > lineList;
  915. mConvexSEL->getSurfaceLineList( mFaceSEL, lineList );
  916. MatrixF objToWorld( mConvexSEL->getTransform() );
  917. objToWorld.scale( mConvexSEL->getScale() );
  918. for ( S32 i = 0; i < lineList.size(); i++ )
  919. objToWorld.mulP( lineList[i] );
  920. for ( S32 i = 0; i < lineList.size() - 1; i++ )
  921. {
  922. Point3F p0( lineList[i] );
  923. Point3F p1( lineList[i+1] );
  924. drawLine( p0, p1, colorSEL, 3.0f );
  925. }
  926. }
  927. if ( gizmoAlpha == 1.0f )
  928. {
  929. if ( mGizmoProfile->mode != NoneMode )
  930. mGizmo->renderText( mSaveViewport, mSaveModelview, mSaveProjection );
  931. }
  932. if ( mActiveTool )
  933. mActiveTool->render2D();
  934. }
  935. if ( gizmoAlpha == 1.0f )
  936. mGizmo->renderGizmo( mLastCameraQuery.cameraMatrix, mLastCameraQuery.fov );
  937. }
  938. void GuiConvexEditorCtrl::drawFacePlane( ConvexShape *shape, S32 faceId )
  939. {
  940. // Build a vb of the face points ( in world space ) scaled outward in
  941. // the surface space in x/y with uv coords.
  942. /*
  943. Vector< Point3F > points;
  944. Vector< Point2F > coords;
  945. shape->getSurfaceTriangles( faceId, &points, &coords, false );
  946. if ( points.empty() )
  947. return;
  948. GFXVertexBufferHandle< GFXVertexPCT > vb;
  949. vb.set( GFX, points.size(), GFXBufferTypeVolatile );
  950. GFXVertexPCT *vert = vb.lock();
  951. for ( S32 i = 0; i < points.size(); i++ )
  952. {
  953. vert->point = points[i];
  954. vert->color.set( 255, 255, 255, 200 );
  955. vert->texCoord = coords[i];
  956. vert++;
  957. }
  958. vb.unlock();
  959. GFXTransformSaver saver;
  960. MatrixF renderMat( shape->getTransform() );
  961. renderMat.scale( shape->getScale() );
  962. GFX->multWorld( renderMat );
  963. GFXStateBlockDesc desc;
  964. desc.setBlend( true );
  965. desc.setCullMode( GFXCullNone );
  966. desc.setZReadWrite( true, false );
  967. desc.samplersDefined = true;
  968. desc.samplers[0] = GFXSamplerStateDesc::getWrapLinear();
  969. GFX->setStateBlockByDesc( desc );
  970. GFX->setVertexBuffer( vb );
  971. GFXTexHandle tex( "core/art/grids/512_transp", &GFXStaticTextureSRGBProfile, "ConvexEditor_grid" );
  972. GFX->setTexture( 0, tex );
  973. GFX->setupGenericShaders();
  974. GFX->drawPrimitive( GFXTriangleList, 0, points.size() / 3 );
  975. */
  976. }
  977. void GuiConvexEditorCtrl::scaleFace( ConvexShape *shape, S32 faceId, Point3F scale )
  978. {
  979. if ( !mHasGeometry )
  980. {
  981. mHasGeometry = true;
  982. mSavedGeometry = shape->mGeometry;
  983. mSavedSurfaces = shape->mSurfaces;
  984. }
  985. else
  986. {
  987. shape->mGeometry = mSavedGeometry;
  988. shape->mSurfaces = mSavedSurfaces;
  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. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  995. Point3F projScale;
  996. scale.z = 1.0f;
  997. const MatrixF &surfToObj = shape->mSurfaces[ face.id ];
  998. MatrixF objToSurf( surfToObj );
  999. objToSurf.inverse();
  1000. for ( S32 i = 0; i < face.points.size(); i++ )
  1001. {
  1002. Point3F &pnt = pointList[ face.points[i] ];
  1003. objToSurf.mulP( pnt );
  1004. pnt *= scale;
  1005. surfToObj.mulP( pnt );
  1006. }
  1007. updateModifiedFace( shape, faceId );
  1008. }
  1009. void GuiConvexEditorCtrl::translateFace( ConvexShape *shape, S32 faceId, const Point3F &displace )
  1010. {
  1011. if ( !mHasGeometry )
  1012. {
  1013. mHasGeometry = true;
  1014. mSavedGeometry = shape->mGeometry;
  1015. mSavedSurfaces = shape->mSurfaces;
  1016. }
  1017. else
  1018. {
  1019. shape->mGeometry = mSavedGeometry;
  1020. shape->mSurfaces = mSavedSurfaces;
  1021. }
  1022. if ( shape->mGeometry.faces.size() <= faceId )
  1023. return;
  1024. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1025. Vector< Point3F > &pointList = shape->mGeometry.points;
  1026. AssertFatal( shape->mSurfaces[ face.id ].isAffine(), "ConvexShapeEditor - surface not affine." );
  1027. Point3F modDisplace = Point3F(displace.x, displace.y, displace.z);
  1028. //snapping
  1029. if (mGridSnap && mGridPlaneSize != 0)
  1030. {
  1031. Point3F faceCenter = Point3F::Zero;
  1032. for (S32 i = 0; i < face.points.size(); i++)
  1033. {
  1034. Point3F &pnt = pointList[face.points[i]];
  1035. faceCenter += pnt;
  1036. }
  1037. faceCenter /= face.points.size();
  1038. // Transform displacement into object space.
  1039. MatrixF objToWorld(shape->getWorldTransform());
  1040. objToWorld.scale(shape->getScale());
  1041. objToWorld.inverse();
  1042. objToWorld.mulP(faceCenter);
  1043. modDisplace = faceCenter + displace;
  1044. Point3F fMod = Point3F::Zero;
  1045. if (!mIsZero(displace.x))
  1046. fMod.x = mFmod(modDisplace.x - (displace.x > 0 ? mGridPlaneSize : -mGridPlaneSize), mGridPlaneSize);
  1047. if (!mIsZero(displace.y))
  1048. fMod.y = mFmod(modDisplace.y - (displace.y > 0 ? mGridPlaneSize : -mGridPlaneSize), mGridPlaneSize);
  1049. if (!mIsZero(displace.z))
  1050. fMod.z = mFmod(modDisplace.z - (displace.z > 0 ? mGridPlaneSize : -mGridPlaneSize), mGridPlaneSize);
  1051. modDisplace -= fMod;
  1052. modDisplace -= faceCenter;
  1053. }
  1054. // Transform displacement into object space.
  1055. MatrixF worldToObj( shape->getTransform() );
  1056. worldToObj.scale( shape->getScale() );
  1057. worldToObj.inverse();
  1058. Point3F displaceOS;
  1059. worldToObj.mulV(modDisplace, &displaceOS);
  1060. for ( S32 i = 0; i < face.points.size(); i++ )
  1061. {
  1062. Point3F &pnt = pointList[ face.points[i] ];
  1063. pnt += displaceOS;
  1064. }
  1065. updateModifiedFace( shape, faceId );
  1066. }
  1067. void GuiConvexEditorCtrl::updateModifiedFace( ConvexShape *shape, S32 faceId )
  1068. {
  1069. if ( shape->mGeometry.faces.size() <= faceId )
  1070. return;
  1071. ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1072. Vector< Point3F > &pointList = shape->mGeometry.points;
  1073. Vector< ConvexShape::Face > &faceList = shape->mGeometry.faces;
  1074. for ( S32 i = 0; i < faceList.size(); i++ )
  1075. {
  1076. ConvexShape::Face &curFace = faceList[i];
  1077. MatrixF &curSurface = shape->mSurfaces[ curFace.id ];
  1078. U32 curPntCount = curFace.points.size();
  1079. if ( curPntCount < 3 )
  1080. continue;
  1081. // Does this face use any of the points which we have modified?
  1082. // Collect them in correct winding order.
  1083. S32 pId0 = -1;
  1084. for ( S32 j = 0; j < curFace.winding.size(); j++ )
  1085. {
  1086. if ( face.points.contains( curFace.points[ curFace.winding[ j ] ] ) )
  1087. {
  1088. pId0 = j;
  1089. break;
  1090. }
  1091. }
  1092. if ( pId0 == -1 )
  1093. continue;
  1094. S32 pId1 = -1, pId2 = -1;
  1095. pId1 = ( pId0 + 1 ) % curFace.winding.size();
  1096. pId2 = ( pId0 + 2 ) % curFace.winding.size();
  1097. const Point3F &p0 = pointList[ curFace.points[ curFace.winding[ pId0 ] ] ];
  1098. const Point3F &p1 = pointList[ curFace.points[ curFace.winding[ pId1 ] ] ];
  1099. const Point3F &p2 = pointList[ curFace.points[ curFace.winding[ pId2 ] ] ];
  1100. PlaneF newPlane( p0, p1, p2 );
  1101. Point3F uvec = newPlane.getNormal();
  1102. Point3F fvec = curSurface.getForwardVector();
  1103. Point3F rvec = curSurface.getRightVector();
  1104. F32 dt0 = mDot( uvec, fvec );
  1105. F32 dt1 = mDot( uvec, rvec );
  1106. if ( mFabs( dt0 ) < mFabs( dt1 ) )
  1107. {
  1108. rvec = mCross( fvec, uvec );
  1109. rvec.normalizeSafe();
  1110. fvec = mCross( uvec, rvec );
  1111. fvec.normalizeSafe();
  1112. }
  1113. else
  1114. {
  1115. fvec = mCross( uvec, rvec );
  1116. fvec.normalizeSafe();
  1117. rvec = mCross( fvec, uvec );
  1118. rvec.normalizeSafe();
  1119. }
  1120. curSurface.setColumn( 0, rvec );
  1121. curSurface.setColumn( 1, fvec );
  1122. curSurface.setColumn( 2, uvec );
  1123. curSurface.setPosition( newPlane.getPosition() );
  1124. }
  1125. updateShape( shape );
  1126. }
  1127. bool GuiConvexEditorCtrl::isShapeValid( ConvexShape *shape )
  1128. {
  1129. // Test for no-geometry.
  1130. if ( shape->mGeometry.points.empty() )
  1131. return false;
  1132. const Vector<Point3F> &pointList = shape->mGeometry.points;
  1133. const Vector<ConvexShape::Face> &faceList = shape->mGeometry.faces;
  1134. // Test that all points are shared by at least 3 faces.
  1135. for ( S32 i = 0; i < pointList.size(); i++ )
  1136. {
  1137. U32 counter = 0;
  1138. for ( S32 j = 0; j < faceList.size(); j++ )
  1139. {
  1140. if ( faceList[j].points.contains( i ) )
  1141. counter++;
  1142. }
  1143. if ( counter < 3 )
  1144. return false;
  1145. }
  1146. // Test for co-planar faces.
  1147. for ( S32 i = 0; i < shape->mPlanes.size(); i++ )
  1148. {
  1149. for ( S32 j = i + 1; j < shape->mPlanes.size(); j++ )
  1150. {
  1151. F32 d = mDot( shape->mPlanes[i], shape->mPlanes[j] );
  1152. if ( d > 0.999f )
  1153. return false;
  1154. }
  1155. }
  1156. // Test for faces with zero or negative area.
  1157. for ( S32 i = 0; i < shape->mGeometry.faces.size(); i++ )
  1158. {
  1159. if ( shape->mGeometry.faces[i].area < 0.0f )
  1160. return false;
  1161. if ( shape->mGeometry.faces[i].triangles.empty() )
  1162. return false;
  1163. }
  1164. return true;
  1165. }
  1166. void GuiConvexEditorCtrl::setupShape( ConvexShape *shape )
  1167. {
  1168. shape->setField( "materialAsset", mMaterialName );
  1169. shape->registerObject();
  1170. updateShape( shape );
  1171. Scene* scene = Scene::getRootScene();
  1172. if ( scene )
  1173. scene->addObject( shape );
  1174. }
  1175. void GuiConvexEditorCtrl::updateShape( ConvexShape *shape, S32 offsetFace )
  1176. {
  1177. shape->_updateGeometry( true );
  1178. /*
  1179. if ( offsetFace != -1 )
  1180. {
  1181. shape->mSurfaces[ offsetFace ].setPosition( mPivotPos );
  1182. }*/
  1183. synchClientObject( shape );
  1184. }
  1185. void GuiConvexEditorCtrl::synchClientObject( const ConvexShape *serverConvex )
  1186. {
  1187. if ( serverConvex->getClientObject() )
  1188. {
  1189. ConvexShape *clientConvex = static_cast< ConvexShape* >( serverConvex->getClientObject() );
  1190. clientConvex->setScale( serverConvex->getScale() );
  1191. clientConvex->setTransform( serverConvex->getTransform() );
  1192. clientConvex->mSurfaces.clear();
  1193. clientConvex->mSurfaces.merge( serverConvex->mSurfaces );
  1194. clientConvex->_updateGeometry(true);
  1195. }
  1196. }
  1197. void GuiConvexEditorCtrl::updateGizmoPos()
  1198. {
  1199. if ( mConvexSEL )
  1200. {
  1201. if ( mFaceSEL != -1 )
  1202. {
  1203. MatrixF surfMat = mConvexSEL->getSurfaceWorldMat( mFaceSEL );
  1204. MatrixF objToWorld( mConvexSEL->getTransform() );
  1205. objToWorld.scale( mConvexSEL->getScale() );
  1206. Point3F gizmoPos(0,0,0);
  1207. if ( mUsingPivot )
  1208. {
  1209. gizmoPos = mPivotPos;
  1210. }
  1211. else
  1212. {
  1213. Point3F faceCenterPnt = mConvexSEL->mSurfaces[ mFaceSEL ].getPosition();
  1214. objToWorld.mulP( faceCenterPnt );
  1215. mGizmoMatOffset = surfMat.getPosition() - faceCenterPnt;
  1216. gizmoPos = faceCenterPnt;
  1217. }
  1218. mGizmo->set( surfMat, gizmoPos, Point3F::One );
  1219. }
  1220. else
  1221. {
  1222. mGizmoMatOffset = Point3F::Zero;
  1223. mGizmo->set( mConvexSEL->getTransform(), mConvexSEL->getPosition(), mConvexSEL->getScale() );
  1224. }
  1225. }
  1226. }
  1227. bool GuiConvexEditorCtrl::setActiveTool( ConvexEditorTool *tool )
  1228. {
  1229. if ( mActiveTool == tool )
  1230. return false;
  1231. ConvexEditorTool *prevTool = mActiveTool;
  1232. ConvexEditorTool *newTool = tool;
  1233. if ( prevTool )
  1234. prevTool->onDeactivated( newTool );
  1235. mActiveTool = newTool;
  1236. if ( newTool )
  1237. newTool->onActivated( prevTool );
  1238. return true;
  1239. }
  1240. bool GuiConvexEditorCtrl::handleEscape()
  1241. {
  1242. if ( mActiveTool )
  1243. {
  1244. mActiveTool->onDeactivated( NULL );
  1245. mActiveTool = NULL;
  1246. return true;
  1247. }
  1248. if ( mFaceSEL != -1 )
  1249. {
  1250. setSelection( mConvexSEL, -1 );
  1251. return true;
  1252. }
  1253. if ( mConvexSEL )
  1254. {
  1255. setSelection( NULL, -1 );
  1256. return true;
  1257. }
  1258. return false;
  1259. }
  1260. bool GuiConvexEditorCtrl::handleDelete()
  1261. {
  1262. if ( mActiveTool )
  1263. {
  1264. mActiveTool->onDeactivated( NULL );
  1265. mActiveTool = NULL;
  1266. }
  1267. if ( mConvexSEL )
  1268. {
  1269. if ( mFaceSEL != -1 )
  1270. {
  1271. submitUndo( ModifyShape, mConvexSEL );
  1272. mConvexSEL->mSurfaces.erase_fast( mFaceSEL );
  1273. updateShape( mConvexSEL );
  1274. if ( !isShapeValid( mConvexSEL ) )
  1275. {
  1276. S32 selFace = mFaceSEL;
  1277. mLastUndo->undo();
  1278. mFaceSEL = selFace;
  1279. updateShape( mConvexSEL );
  1280. updateGizmoPos();
  1281. }
  1282. else
  1283. {
  1284. setSelection( mConvexSEL, -1 );
  1285. }
  1286. }
  1287. else
  1288. {
  1289. // Grab the mission editor undo manager.
  1290. UndoManager *undoMan = NULL;
  1291. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1292. {
  1293. Con::errorf( "GuiConvexEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  1294. }
  1295. else
  1296. {
  1297. // Create the UndoAction.
  1298. MEDeleteUndoAction *action = new MEDeleteUndoAction("Deleted ConvexShape");
  1299. action->deleteObject( mConvexSEL );
  1300. mIsDirty = true;
  1301. mFaceHL = -1;
  1302. setSelection( NULL, -1 );
  1303. // Submit it.
  1304. undoMan->addAction( action );
  1305. }
  1306. }
  1307. }
  1308. return true;
  1309. }
  1310. bool GuiConvexEditorCtrl::hasSelection() const
  1311. {
  1312. return mConvexSEL != NULL;
  1313. }
  1314. void GuiConvexEditorCtrl::clearSelection()
  1315. {
  1316. mFaceHL = -1;
  1317. mConvexHL = NULL;
  1318. setSelection( NULL, -1 );
  1319. }
  1320. void GuiConvexEditorCtrl::handleDeselect()
  1321. {
  1322. if ( mActiveTool )
  1323. {
  1324. mActiveTool->onDeactivated( NULL );
  1325. mActiveTool = NULL;
  1326. }
  1327. mFaceHL = -1;
  1328. mConvexHL = NULL;
  1329. setSelection( NULL, -1 );
  1330. }
  1331. void GuiConvexEditorCtrl::setSelection( ConvexShape *shape, S32 faceId )
  1332. {
  1333. mFaceSEL = faceId;
  1334. mConvexSEL = shape;
  1335. updateGizmoPos();
  1336. Con::executef( this, "onSelectionChanged", shape ? shape->getIdString() : "", Con::getIntArg(faceId) );
  1337. }
  1338. void GuiConvexEditorCtrl::_prepRenderImage( SceneManager* sceneGraph, const SceneRenderState* state )
  1339. {
  1340. if ( !isAwake() )
  1341. return;
  1342. /*
  1343. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1344. ri->type = RenderPassManager::RIT_Editor;
  1345. ri->renderDelegate.bind( this, &GuiConvexEditorCtrl::_renderObject );
  1346. ri->defaultKey = 100;
  1347. state->getRenderPass()->addInst( ri );
  1348. */
  1349. }
  1350. void GuiConvexEditorCtrl::_renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst )
  1351. {
  1352. }
  1353. void GuiConvexEditorCtrl::submitUndo( UndoType type, ConvexShape *shape )
  1354. {
  1355. Vector< ConvexShape* > shapes;
  1356. shapes.push_back( shape );
  1357. submitUndo( type, shapes );
  1358. }
  1359. void GuiConvexEditorCtrl::submitUndo( UndoType type, const Vector<ConvexShape*> &shapes )
  1360. {
  1361. // Grab the mission editor undo manager.
  1362. Sim::findObject( "EUndoManager", mUndoManager );
  1363. if ( !mUndoManager )
  1364. {
  1365. Con::errorf( "GuiConvexEditorCtrl::submitUndo() - EUndoManager not found!" );
  1366. return;
  1367. }
  1368. if ( type == ModifyShape )
  1369. {
  1370. // Setup the action.
  1371. GuiConvexEditorUndoAction *action = new GuiConvexEditorUndoAction( "Modified a ConvexShape" );
  1372. ConvexShape *shape = shapes.first();
  1373. action->mObjId = shape->getId();
  1374. action->mEditor = this;
  1375. action->mSavedObjToWorld = shape->getTransform();
  1376. action->mSavedScale = shape->getScale();
  1377. action->mSavedSurfaces.merge( shape->mSurfaces );
  1378. action->mUndoManager = mUndoManager;
  1379. mUndoManager->addAction( action );
  1380. mLastUndo = action;
  1381. }
  1382. else if ( type == CreateShape )
  1383. {
  1384. MECreateUndoAction *action = new MECreateUndoAction( "Create ConvexShape" );
  1385. for ( S32 i = 0; i < shapes.size(); i++ )
  1386. action->addObject( shapes[i] );
  1387. mUndoManager->addAction( action );
  1388. mLastUndo = action;
  1389. }
  1390. else if ( type == DeleteShape )
  1391. {
  1392. MEDeleteUndoAction *action = new MEDeleteUndoAction( "Deleted ConvexShape" );
  1393. for ( S32 i = 0; i < shapes.size(); i++ )
  1394. action->deleteObject( shapes[i] );
  1395. mUndoManager->addAction( action );
  1396. mLastUndo = action;
  1397. }
  1398. else if ( type == HollowShape )
  1399. {
  1400. CompoundUndoAction *action = new CompoundUndoAction( "Hollow ConvexShape" );
  1401. MECreateUndoAction *createAction = new MECreateUndoAction();
  1402. MEDeleteUndoAction *deleteAction = new MEDeleteUndoAction();
  1403. deleteAction->deleteObject( shapes.first() );
  1404. for ( S32 i = 1; i < shapes.size(); i++ )
  1405. createAction->addObject( shapes[i] );
  1406. action->addAction( deleteAction );
  1407. action->addAction( createAction );
  1408. mUndoManager->addAction( action );
  1409. mLastUndo = action;
  1410. }
  1411. mIsDirty = true;
  1412. }
  1413. bool GuiConvexEditorCtrl::_cursorCastCallback( RayInfo* ri )
  1414. {
  1415. // Reject anything that's not a ConvexShape.
  1416. return dynamic_cast< ConvexShape* >( ri->object );
  1417. }
  1418. bool GuiConvexEditorCtrl::_cursorCast( const Gui3DMouseEvent &event, ConvexShape **hitShape, S32 *hitFace )
  1419. {
  1420. RayInfo ri;
  1421. if ( gServerContainer.castRay( event.pos, event.pos + event.vec * 10000.0f, StaticShapeObjectType, &ri, &GuiConvexEditorCtrl::_cursorCastCallback ) &&
  1422. dynamic_cast< ConvexShape* >( ri.object ) )
  1423. {
  1424. // Do not select or edit ConvexShapes that are within a Prefab.
  1425. if ( Prefab::getPrefabByChild( ri.object ) )
  1426. return false;
  1427. *hitShape = static_cast< ConvexShape* >( ri.object );
  1428. *hitFace = ri.face;
  1429. mLastRayInfo = ri;
  1430. return true;
  1431. }
  1432. return false;
  1433. }
  1434. void GuiConvexEditorCtrl::setPivotPos( ConvexShape *shape, S32 faceId, const Gui3DMouseEvent &event )
  1435. {
  1436. PlaneF plane;
  1437. mTransformPlane( shape->getTransform(), shape->getScale(), shape->mPlanes[ faceId ], &plane );
  1438. Point3F start( event.pos );
  1439. Point3F end( start + event.vec * 10000.0f );
  1440. F32 t = plane.intersect( start, end );
  1441. if ( t >= 0.0f && t <= 1.0f )
  1442. {
  1443. Point3F hitPos;
  1444. hitPos.interpolate( start, end, t );
  1445. mPivotPos = hitPos;
  1446. mUsingPivot = true;
  1447. MatrixF worldToObj( shape->getTransform() );
  1448. worldToObj.scale( shape->getScale() );
  1449. worldToObj.inverse();
  1450. Point3F objPivotPos( mPivotPos );
  1451. worldToObj.mulP( objPivotPos );
  1452. updateGizmoPos();
  1453. }
  1454. }
  1455. void GuiConvexEditorCtrl::cleanMatrix( MatrixF &mat )
  1456. {
  1457. if ( mat.isAffine() )
  1458. return;
  1459. VectorF col0 = mat.getColumn3F(0);
  1460. VectorF col1 = mat.getColumn3F(1);
  1461. VectorF col2 = mat.getColumn3F(2);
  1462. col0.normalize();
  1463. col1.normalize();
  1464. col2.normalize();
  1465. col2 = mCross( col0, col1 );
  1466. col2.normalize();
  1467. col1 = mCross( col2, col0 );
  1468. col1.normalize();
  1469. col0 = mCross( col1, col2 );
  1470. col0.normalize();
  1471. mat.setColumn(0,col0);
  1472. mat.setColumn(1,col1);
  1473. mat.setColumn(2,col2);
  1474. AssertFatal( mat.isAffine(), "GuiConvexEditorCtrl::cleanMatrix, non-affine matrix" );
  1475. }
  1476. S32 GuiConvexEditorCtrl::getEdgeByPoints( ConvexShape *shape, S32 faceId, S32 p0, S32 p1 )
  1477. {
  1478. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1479. for ( S32 i = 0; i < face.edges.size(); i++ )
  1480. {
  1481. const ConvexShape::Edge &edge = face.edges[i];
  1482. if ( edge.p0 != p0 && edge.p0 != p1 )
  1483. continue;
  1484. if ( edge.p1 != p0 && edge.p1 != p1 )
  1485. continue;
  1486. return i;
  1487. }
  1488. return -1;
  1489. }
  1490. bool GuiConvexEditorCtrl::getEdgesTouchingPoint( ConvexShape *shape, S32 faceId, S32 pId, Vector< U32 > &edgeIdxList, S32 excludeEdge )
  1491. {
  1492. const ConvexShape::Face &face = shape->mGeometry.faces[faceId];
  1493. const Vector< ConvexShape::Edge > &edgeList = face.edges;
  1494. for ( S32 i = 0; i < edgeList.size(); i++ )
  1495. {
  1496. if ( i == excludeEdge )
  1497. continue;
  1498. const ConvexShape::Edge &curEdge = edgeList[i];
  1499. if ( curEdge.p0 == pId || curEdge.p1 == pId )
  1500. edgeIdxList.push_back(i);
  1501. }
  1502. return !edgeIdxList.empty();
  1503. }
  1504. Point2F GuiConvexEditorCtrl::getSelectedFaceUVOffset()
  1505. {
  1506. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1507. return Point2F(0, 0);
  1508. return mConvexSEL->mSurfaceUVs[mFaceSEL].offset;
  1509. }
  1510. Point2F GuiConvexEditorCtrl::getSelectedFaceUVScale()
  1511. {
  1512. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1513. return Point2F(0, 0);
  1514. return mConvexSEL->mSurfaceUVs[mFaceSEL].scale;
  1515. }
  1516. const char* GuiConvexEditorCtrl::getSelectedFaceMaterial()
  1517. {
  1518. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1519. return "";
  1520. if (mConvexSEL->mSurfaceUVs[mFaceSEL].matID == 0)
  1521. {
  1522. return mConvexSEL->getMaterial();
  1523. }
  1524. else
  1525. {
  1526. return mConvexSEL->mSurfaceTextures[mConvexSEL->mSurfaceUVs[mFaceSEL].matID - 1].getMaterial();
  1527. }
  1528. }
  1529. bool GuiConvexEditorCtrl::getSelectedFaceHorzFlip()
  1530. {
  1531. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1532. return false;
  1533. return mConvexSEL->mSurfaceUVs[mFaceSEL].horzFlip;
  1534. }
  1535. bool GuiConvexEditorCtrl::getSelectedFaceVertFlip()
  1536. {
  1537. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1538. return false;
  1539. return mConvexSEL->mSurfaceUVs[mFaceSEL].vertFlip;
  1540. }
  1541. float GuiConvexEditorCtrl::getSelectedFaceZRot()
  1542. {
  1543. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1544. return false;
  1545. return mConvexSEL->mSurfaceUVs[mFaceSEL].zRot;
  1546. }
  1547. void GuiConvexEditorCtrl::setSelectedFaceUVOffset(Point2F offset)
  1548. {
  1549. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1550. return;
  1551. mConvexSEL->mSurfaceUVs[mFaceSEL].offset = offset;
  1552. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1553. }
  1554. void GuiConvexEditorCtrl::setSelectedFaceUVScale(Point2F scale)
  1555. {
  1556. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1557. return;
  1558. mConvexSEL->mSurfaceUVs[mFaceSEL].scale = scale;
  1559. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1560. }
  1561. void GuiConvexEditorCtrl::setSelectedFaceMaterial(const char* materialName)
  1562. {
  1563. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1564. return;
  1565. if (mConvexSEL->mSurfaceUVs.size() < mFaceSEL)
  1566. return;
  1567. //first, see if the mat already exists in our list
  1568. bool found = false;
  1569. U32 oldmatID = mConvexSEL->mSurfaceUVs[mFaceSEL].matID;
  1570. if (String::compare(materialName, mConvexSEL->getMaterialName().c_str()))
  1571. {
  1572. for (U32 i = 0; i < mConvexSEL->mSurfaceTextures.size(); i++)
  1573. {
  1574. if (!String::compare(mConvexSEL->mSurfaceTextures[i].getMaterial(), materialName))
  1575. {
  1576. //found a match
  1577. mConvexSEL->mSurfaceUVs[mFaceSEL].matID = i + 1;
  1578. found = true;
  1579. }
  1580. }
  1581. if (!found)
  1582. {
  1583. //add a new one
  1584. ConvexShape::surfaceMaterial newMat;
  1585. newMat._setMaterial(materialName);
  1586. mConvexSEL->mSurfaceTextures.push_back(newMat);
  1587. mConvexSEL->mSurfaceUVs[mFaceSEL].matID = mConvexSEL->mSurfaceTextures.size();
  1588. }
  1589. }
  1590. else
  1591. {
  1592. mConvexSEL->mSurfaceUVs[mFaceSEL].matID = 0;
  1593. }
  1594. //run through and find out if there are any other faces still using the old mat texture
  1595. if (oldmatID != 0)
  1596. {
  1597. S32 curMatCount = mConvexSEL->mSurfaceTextures.size();
  1598. bool used = false;
  1599. for (U32 i = 0; i < mConvexSEL->mSurfaceUVs.size(); i++)
  1600. {
  1601. if (mConvexSEL->mSurfaceUVs[i].matID == oldmatID)
  1602. {
  1603. used = true;
  1604. break;
  1605. }
  1606. }
  1607. if (!used)
  1608. {
  1609. //that was the last reference, so let's update the listings on the shape
  1610. if (mConvexSEL->mSurfaceTextures[oldmatID - 1].materialInst)
  1611. SAFE_DELETE(mConvexSEL->mSurfaceTextures[oldmatID - 1].materialInst);
  1612. mConvexSEL->mSurfaceTextures.erase(oldmatID-1);
  1613. for (U32 i = 0; i < mConvexSEL->mSurfaceUVs.size(); i++)
  1614. {
  1615. if (mConvexSEL->mSurfaceUVs[i].matID > oldmatID)
  1616. mConvexSEL->mSurfaceUVs[i].matID--;
  1617. }
  1618. }
  1619. }
  1620. //mConvexSEL->mSurfaceUVs[mFaceSEL].materialName = materialName;
  1621. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1622. }
  1623. void GuiConvexEditorCtrl::setSelectedFaceHorzFlip(bool flipped)
  1624. {
  1625. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1626. return;
  1627. mConvexSEL->mSurfaceUVs[mFaceSEL].horzFlip = flipped;
  1628. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1629. }
  1630. void GuiConvexEditorCtrl::setSelectedFaceVertFlip(bool flipped)
  1631. {
  1632. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1633. return;
  1634. mConvexSEL->mSurfaceUVs[mFaceSEL].vertFlip = flipped;
  1635. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1636. }
  1637. void GuiConvexEditorCtrl::setSelectedFaceZRot(float degrees)
  1638. {
  1639. if (mFaceSEL == -1 || mConvexSEL == NULL)
  1640. return;
  1641. F32 oldRot = mDegToRad(mConvexSEL->mSurfaceUVs[mFaceSEL].zRot);
  1642. mConvexSEL->mSurfaceUVs[mFaceSEL].zRot = degrees;
  1643. EulerF curEul = mConvexSEL->mSurfaces[mFaceSEL].toEuler();
  1644. MatrixF oldRotMat = MatrixF(EulerF(0, 0, -oldRot));
  1645. mConvexSEL->mSurfaces[mFaceSEL].mul(oldRotMat);
  1646. MatrixF newRotMat = MatrixF(EulerF(0, 0, mDegToRad(mConvexSEL->mSurfaceUVs[mFaceSEL].zRot)));
  1647. mConvexSEL->mSurfaces[mFaceSEL].mul(newRotMat);
  1648. //Point3F curPos = mConvexSEL->mSurfaces[mFaceSEL].getPosition();
  1649. //we roll back our existing modified rotation before setting the new one, just to keep it consistent
  1650. //const MatrixF &gMat = MatrixF(EulerF(curEul.x, curEul.y, curEul.z - oldRot + mDegToRad(mConvexSEL->mSurfaceUVs[mFaceSEL].zRot)), curPos);
  1651. //mConvexSEL->mSurfaces[mFaceSEL] = MatrixF(EulerF(curEul.x, curEul.y, mDegToRad(mConvexSEL->mSurfaceUVs[mFaceSEL].zRot)), curPos);
  1652. /*MatrixF surfMat;
  1653. surfMat.mul(mConvexSEL->mWorldToObj, gMat);
  1654. MatrixF worldToObj(mConvexSEL->getTransform());
  1655. worldToObj.scale(mConvexSEL->getScale());
  1656. worldToObj.inverse();
  1657. Point3F newPos;
  1658. newPos = gMat.getPosition();
  1659. worldToObj.mulP(newPos);
  1660. surfMat.setPosition(newPos);
  1661. // Clear out floating point errors.
  1662. cleanMatrix(surfMat);
  1663. mConvexSEL->mSurfaces[mFaceSEL] = surfMat;*/
  1664. updateShape(mConvexSEL, mFaceSEL);
  1665. mConvexSEL->setMaskBits(ConvexShape::UpdateMask);
  1666. /*
  1667. const MatrixF &gMat = mGizmo->getTransform();
  1668. MatrixF surfMat;
  1669. surfMat.mul( mConvexSEL->mWorldToObj, gMat );
  1670. MatrixF worldToObj ( mConvexSEL->getTransform() );
  1671. worldToObj.scale( mConvexSEL->getScale() );
  1672. worldToObj.inverse();
  1673. Point3F newPos;
  1674. newPos = gMat.getPosition();
  1675. worldToObj.mulP( newPos );
  1676. surfMat.setPosition( newPos );
  1677. // Clear out floating point errors.
  1678. cleanMatrix( surfMat );
  1679. if (mGizmo->getSelection() == Gizmo::Axis_Z)
  1680. {
  1681. MatrixF curSurfMat = mConvexSEL->mSurfaces[mFaceSEL];
  1682. EulerF curSufRot = curSurfMat.toEuler();
  1683. EulerF newSufRot = surfMat.toEuler();
  1684. float zRot = mRadToDeg(newSufRot.z - curSufRot.z);
  1685. float curZRot = mConvexSEL->mSurfaceTextures[mFaceSEL].zRot;
  1686. mConvexSEL->mSurfaceTextures[mFaceSEL].zRot += zRot;
  1687. }
  1688. mConvexSEL->mSurfaces[mFaceSEL] = surfMat;
  1689. updateShape( mConvexSEL, mFaceSEL );
  1690. */
  1691. }
  1692. void GuiConvexEditorCtrl::toggleGridSnapping()
  1693. {
  1694. if (mGridSnap)
  1695. mGridSnap = false;
  1696. else
  1697. mGridSnap = true;
  1698. }
  1699. void GuiConvexEditorCtrl::updateShape()
  1700. {
  1701. if (mConvexSEL)
  1702. mConvexSEL->inspectPostApply();
  1703. }
  1704. void GuiConvexEditorCtrl::setGridSnapSize(float gridSize)
  1705. {
  1706. mGridPlaneSize = gridSize;
  1707. }
  1708. void GuiConvexEditorUndoAction::undo()
  1709. {
  1710. ConvexShape *object = NULL;
  1711. if ( !Sim::findObject( mObjId, object ) )
  1712. return;
  1713. // Temporarily save the ConvexShape current data.
  1714. Vector< MatrixF > tempSurfaces;
  1715. tempSurfaces.merge( object->mSurfaces );
  1716. MatrixF tempObjToWorld( object->getTransform() );
  1717. Point3F tempScale( object->getScale() );
  1718. // Restore the Object to the UndoAction state.
  1719. object->mSurfaces.clear();
  1720. object->mSurfaces.merge( mSavedSurfaces );
  1721. object->setScale( mSavedScale );
  1722. object->setTransform( mSavedObjToWorld );
  1723. // Regenerate the ConvexShape and synch the client object.
  1724. object->_updateGeometry();
  1725. GuiConvexEditorCtrl::synchClientObject( object );
  1726. // If applicable set the selected ConvexShape and face
  1727. // on the editor.
  1728. mEditor->setSelection( object, -1 );
  1729. mEditor->updateGizmoPos();
  1730. // Now save the previous ConvexShape data in this UndoAction
  1731. // since an undo action must become a redo action and vice-versa
  1732. mSavedObjToWorld = tempObjToWorld;
  1733. mSavedScale = tempScale;
  1734. mSavedSurfaces.clear();
  1735. mSavedSurfaces.merge( tempSurfaces );
  1736. }
  1737. ConvexEditorCreateTool::ConvexEditorCreateTool( GuiConvexEditorCtrl *editor )
  1738. : Parent( editor ),
  1739. mStage( -1 ),
  1740. mNewConvex( NULL )
  1741. {
  1742. }
  1743. void ConvexEditorCreateTool::onActivated( ConvexEditorTool *prevTool )
  1744. {
  1745. mEditor->clearSelection();
  1746. mStage = -1;
  1747. mNewConvex = NULL;
  1748. }
  1749. void ConvexEditorCreateTool::onDeactivated( ConvexEditorTool *newTool )
  1750. {
  1751. if ( mNewConvex )
  1752. mNewConvex->deleteObject();
  1753. mStage = -1;
  1754. mNewConvex = NULL;
  1755. mEditor->mouseUnlock();
  1756. }
  1757. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseDown( const Gui3DMouseEvent &event )
  1758. {
  1759. if ( mStage == -1 )
  1760. {
  1761. mEditor->setFirstResponder();
  1762. mEditor->mouseLock();
  1763. Point3F start( event.pos );
  1764. Point3F end( event.pos + event.vec * 10000.0f );
  1765. RayInfo ri;
  1766. bool hit = gServerContainer.castRay( event.pos, end, STATIC_COLLISION_TYPEMASK, &ri );
  1767. MatrixF objMat( true );
  1768. // Calculate the orientation matrix of the new ConvexShape
  1769. // based on what has been clicked.
  1770. if ( !hit )
  1771. {
  1772. objMat.setPosition( event.pos + event.vec * 100.0f );
  1773. }
  1774. else
  1775. {
  1776. if ( dynamic_cast< ConvexShape* >( ri.object ) )
  1777. {
  1778. ConvexShape *hitShape = static_cast< ConvexShape* >( ri.object );
  1779. objMat = hitShape->getSurfaceWorldMat( ri.face );
  1780. objMat.setPosition( ri.point );
  1781. }
  1782. else
  1783. {
  1784. Point3F rvec;
  1785. Point3F fvec( mEditor->getCameraMat().getForwardVector() );
  1786. Point3F uvec( ri.normal );
  1787. rvec = mCross( fvec, uvec );
  1788. if ( rvec.isZero() )
  1789. {
  1790. fvec = mEditor->getCameraMat().getRightVector();
  1791. rvec = mCross( fvec, uvec );
  1792. }
  1793. rvec.normalizeSafe();
  1794. fvec = mCross( uvec, rvec );
  1795. fvec.normalizeSafe();
  1796. uvec = mCross( rvec, fvec );
  1797. uvec.normalizeSafe();
  1798. objMat.setColumn( 0, rvec );
  1799. objMat.setColumn( 1, fvec );
  1800. objMat.setColumn( 2, uvec );
  1801. objMat.setPosition( ri.point );
  1802. }
  1803. }
  1804. mNewConvex = new ConvexShape();
  1805. mNewConvex->setTransform( objMat );
  1806. mNewConvex->setField( "materialAsset", Parent::mEditor->mMaterialName );
  1807. mNewConvex->registerObject();
  1808. mPlaneSizes.set( 0.1f, 0.1f, 0.1f );
  1809. mNewConvex->resizePlanes( mPlaneSizes );
  1810. mEditor->updateShape( mNewConvex );
  1811. mTransform = objMat;
  1812. mCreatePlane.set( objMat.getPosition(), objMat.getUpVector() );
  1813. }
  1814. else if ( mStage == 0 )
  1815. {
  1816. // Handle this on mouseUp
  1817. }
  1818. return Handled;
  1819. }
  1820. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseUp( const Gui3DMouseEvent &event )
  1821. {
  1822. if ( mNewConvex && mStage == -1 )
  1823. {
  1824. mStage = 0;
  1825. mCreatePlane = PlaneF( mNewConvex->getPosition(), mNewConvex->getTransform().getForwardVector() );
  1826. mTransform.setPosition( mNewConvex->getPosition() );
  1827. return Handled;
  1828. }
  1829. else if ( mStage == 0 )
  1830. {
  1831. SimGroup *scene = Scene::getRootScene();
  1832. scene->addObject( mNewConvex );
  1833. mStage = -1;
  1834. // Grab the mission editor undo manager.
  1835. UndoManager *undoMan = NULL;
  1836. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1837. {
  1838. Con::errorf( "ConvexEditorCreateTool::on3DMouseDown() - EUndoManager not found!" );
  1839. mNewConvex = NULL;
  1840. return Failed;
  1841. }
  1842. // Create the UndoAction.
  1843. MECreateUndoAction *action = new MECreateUndoAction("Create ConvexShape");
  1844. action->addObject( mNewConvex );
  1845. // Submit it.
  1846. undoMan->addAction( action );
  1847. mEditor->setField( "isDirty", "1" );
  1848. mEditor->setSelection( mNewConvex, -1 );
  1849. mNewConvex = NULL;
  1850. mEditor->mouseUnlock();
  1851. return Done;
  1852. }
  1853. return Done;
  1854. }
  1855. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseMove( const Gui3DMouseEvent &event )
  1856. {
  1857. if ( mStage == 0 )
  1858. {
  1859. Point3F start( event.pos );
  1860. Point3F end( start + event.vec * 10000.0f );
  1861. F32 t = mCreatePlane.intersect( start, end );
  1862. Point3F hitPos;
  1863. if ( t < 0.0f || t > 1.0f )
  1864. return Handled;
  1865. hitPos.interpolate( start, end, t );
  1866. MatrixF worldToObj( mTransform );
  1867. worldToObj.inverse();
  1868. worldToObj.mulP( hitPos );
  1869. F32 delta = ( hitPos.z );
  1870. mPlaneSizes.z = getMax( 0.1f, delta );
  1871. mNewConvex->resizePlanes( mPlaneSizes );
  1872. mEditor->updateShape( mNewConvex );
  1873. Point3F pos( mTransform.getPosition() );
  1874. pos += mPlaneSizes.z * 0.5f * mTransform.getUpVector();
  1875. mNewConvex->setPosition( pos );
  1876. }
  1877. return Handled;
  1878. }
  1879. ConvexEditorTool::EventResult ConvexEditorCreateTool::on3DMouseDragged( const Gui3DMouseEvent &event )
  1880. {
  1881. if ( !mNewConvex || mStage != -1 )
  1882. return Handled;
  1883. Point3F start( event.pos );
  1884. Point3F end( event.pos + event.vec * 10000.0f );
  1885. F32 t = mCreatePlane.intersect( start, end );
  1886. if ( t < 0.0f || t > 1.0f )
  1887. return Handled;
  1888. Point3F hitPos;
  1889. hitPos.interpolate( start, end, t );
  1890. MatrixF xfm( mTransform );
  1891. xfm.inverse();
  1892. xfm.mulP( hitPos);
  1893. Point3F scale;
  1894. scale.x = getMax( mFabs( hitPos.x ), 0.1f );
  1895. scale.y = getMax( mFabs( hitPos.y ), 0.1f );
  1896. scale.z = 0.1f;
  1897. mNewConvex->resizePlanes( scale );
  1898. mPlaneSizes = scale;
  1899. mEditor->updateShape( mNewConvex );
  1900. Point3F pos( mTransform.getPosition() );
  1901. pos += mTransform.getRightVector() * hitPos.x * 0.5f;
  1902. pos += mTransform.getForwardVector() * hitPos.y * 0.5f;
  1903. mNewConvex->setPosition( pos );
  1904. return Handled;
  1905. }
  1906. void ConvexEditorCreateTool::renderScene( const RectI &updateRect )
  1907. {
  1908. }
  1909. ConvexShape* ConvexEditorCreateTool::extrudeShapeFromFace( ConvexShape *inShape, S32 inFaceId )
  1910. {
  1911. ConvexShape::Geometry &inShapeGeometry = inShape->getGeometry();
  1912. ConvexShape::Face &inFace = inShapeGeometry.faces[inFaceId];
  1913. Vector< Point3F > &inShapePointList = inShapeGeometry.points;
  1914. Vector< MatrixF > &inShapeSurfaces = inShape->getSurfaces();
  1915. S32 shapeFaceCount = inFace.edges.size() + 2;
  1916. MatrixF inShapeToWorld( inShape->getTransform() );
  1917. inShapeToWorld.scale( inShape->getScale() );
  1918. //MatrixF inWorldToShape( inShapeToWorld );
  1919. //inWorldToShape.inverse();
  1920. MatrixF shapeToWorld;
  1921. shapeToWorld.mul( inShape->getTransform(), inShapeSurfaces[inFaceId] );
  1922. Point3F tmp( inShapeSurfaces[inFaceId].getPosition() );
  1923. inShapeToWorld.mulP( tmp );
  1924. shapeToWorld.setPosition( tmp );
  1925. MatrixF worldToShape( shapeToWorld );
  1926. worldToShape.inverse();
  1927. MatrixF inShapeToNewShape;
  1928. inShapeToNewShape.mul( inShapeToWorld, worldToShape );
  1929. ConvexShape *newShape = new ConvexShape;
  1930. newShape->setTransform( shapeToWorld );
  1931. Vector< MatrixF > &shapeSurfaces = newShape->getSurfaces();
  1932. shapeSurfaces.setSize( shapeFaceCount );
  1933. //shapeSurfaces.setSize( 2 );
  1934. const Point3F &shapePos = shapeToWorld.getPosition();
  1935. shapeSurfaces[0].identity();
  1936. shapeSurfaces[1].identity();
  1937. shapeSurfaces[1].setColumn( 0, -shapeSurfaces[1].getColumn3F(0) );
  1938. shapeSurfaces[1].setColumn( 2, -shapeSurfaces[1].getColumn3F(2) );
  1939. for ( S32 i = 0; i < inFace.winding.size(); i++ )
  1940. {
  1941. Point3F p0 = inShapePointList[ inFace.points[ inFace.winding[ i ] ] ];
  1942. Point3F p1;
  1943. if ( i+1 < inFace.winding.size() )
  1944. p1 = inShapePointList[ inFace.points[ inFace.winding[ i+1 ] ] ];
  1945. else
  1946. p1 = inShapePointList[ inFace.points[ inFace.winding[ 0 ] ] ];
  1947. inShapeToWorld.mulP( p0 );
  1948. inShapeToWorld.mulP( p1 );
  1949. Point3F newPos = MathUtils::mClosestPointOnSegment( p0, p1, shapePos );
  1950. Point3F rvec = p0 - p1;
  1951. rvec.normalizeSafe();
  1952. Point3F fvec = shapeToWorld.getUpVector();
  1953. Point3F uvec = mCross( rvec, fvec );
  1954. if ( i + 2 >= shapeSurfaces.size() )
  1955. continue;
  1956. //F32 dt = mDot( shapeToWorld.getUpVector(), rvec );
  1957. //AssertFatal( mIsZero( dt ), "bad" );
  1958. MatrixF &surf = shapeSurfaces[i+2];
  1959. surf.identity();
  1960. surf.setColumn( 0, rvec );
  1961. surf.setColumn( 1, fvec );
  1962. surf.setColumn( 2, uvec );
  1963. surf.setPosition( newPos );
  1964. surf.mulL( worldToShape );
  1965. }
  1966. //newShape->setField( "material", Parent::mEditor->mMaterialName );
  1967. newShape->setField("materialAsset", inShape->getMaterialName());
  1968. newShape->registerObject();
  1969. mEditor->updateShape( newShape );
  1970. Scene* scene = Scene::getRootScene();
  1971. if ( scene )
  1972. scene->addObject( newShape );
  1973. return newShape;
  1974. }
  1975. void GuiConvexEditorCtrl::hollowShape( ConvexShape *shape, F32 thickness )
  1976. {
  1977. // Create a new Convex for each face of the original shape.
  1978. // This is the same as an extrude from face operation going inward by the thickness
  1979. // for every face.
  1980. Vector< ConvexShape* > convexList;
  1981. for ( S32 i = 0; i < shape->mGeometry.faces.size(); i++ )
  1982. {
  1983. ConvexShape *faceShape = mCreateTool->extrudeShapeFromFace( shape, i );
  1984. MatrixF &inwardFace = faceShape->mSurfaces[1];
  1985. //MatrixF &outwardFace = faceShape->mSurfaces[0];
  1986. Point3F invec = inwardFace.getUpVector();
  1987. inwardFace.setPosition( inwardFace.getPosition() + invec * thickness );
  1988. updateShape( faceShape );
  1989. convexList.push_back( faceShape );
  1990. }
  1991. convexList.push_front( shape );
  1992. submitUndo( HollowShape, convexList );
  1993. }
  1994. void GuiConvexEditorCtrl::hollowSelection()
  1995. {
  1996. if ( mConvexSEL )
  1997. {
  1998. hollowShape( mConvexSEL, 0.15f );
  1999. setSelection( NULL, -1 );
  2000. }
  2001. }
  2002. void GuiConvexEditorCtrl::recenterSelection()
  2003. {
  2004. if ( mConvexSEL )
  2005. {
  2006. recenterShape( mConvexSEL );
  2007. updateGizmoPos();
  2008. }
  2009. }
  2010. void GuiConvexEditorCtrl::recenterShape( ConvexShape *shape )
  2011. {
  2012. submitUndo( ModifyShape, shape );
  2013. shape->recenter();
  2014. synchClientObject( shape );
  2015. }
  2016. void GuiConvexEditorCtrl::dropSelectionAtScreenCenter()
  2017. {
  2018. // This code copied from WorldEditor.
  2019. // All the dropping code would be moved to somewhere common, but its not.
  2020. if ( !mConvexSEL )
  2021. return;
  2022. // Calculate the center of the screen (in global screen coordinates)
  2023. Point2I offset = localToGlobalCoord(Point2I(0,0));
  2024. Point3F sp(F32(offset.x + F32(getExtent().x / 2)), F32(offset.y + (getExtent().y / 2)), 1.0f);
  2025. // Calculate the view distance to fit the selection
  2026. // within the camera's view.
  2027. const Box3F bounds = mConvexSEL->getWorldBox();
  2028. F32 radius = bounds.len()*0.5f;
  2029. F32 viewdist = calculateViewDistance(radius);
  2030. // Be careful of infinite sized objects, or just large ones in general.
  2031. if(viewdist > 100.0f )
  2032. viewdist = 100.0f;
  2033. // Position the selection
  2034. mConvexSEL->setPosition( smCamPos + smCamMatrix.getForwardVector() * viewdist );
  2035. synchClientObject( mConvexSEL );
  2036. updateGizmoPos();
  2037. }
  2038. void GuiConvexEditorCtrl::splitSelectedFace()
  2039. {
  2040. if ( !mConvexSEL || mFaceSEL == -1 )
  2041. return;
  2042. if ( !isShapeValid( mConvexSEL ) )
  2043. return;
  2044. mLastValidShape = mConvexSEL->mSurfaces;
  2045. const F32 radians = mDegToRad( 15.0f );
  2046. Point3F rot( 0, 0, 0 );
  2047. MatrixF rotMat( true );
  2048. mConvexSEL->mSurfaces.increment();
  2049. MatrixF &dstMat = mConvexSEL->mSurfaces.last();
  2050. const MatrixF &srcMat = mConvexSEL->mSurfaces[mFaceSEL];
  2051. for ( S32 i = 0; i < 6; i++ )
  2052. {
  2053. F32 sign = i > 2 ? -1.0f : 1.0f;
  2054. U32 idx = i % 3;
  2055. rot.zero();
  2056. rot[idx] = sign * radians;
  2057. rotMat.set( (EulerF)rot );
  2058. dstMat = srcMat * rotMat;
  2059. updateShape( mConvexSEL );
  2060. if ( isShapeValid( mConvexSEL ) )
  2061. {
  2062. mSavedSurfaces = mConvexSEL->mSurfaces;
  2063. mConvexSEL->mSurfaces = mLastValidShape;
  2064. submitUndo( ModifyShape, mConvexSEL );
  2065. mConvexSEL->mSurfaces = mSavedSurfaces;
  2066. mLastValidShape = mSavedSurfaces;
  2067. setSelection( mConvexSEL, mConvexSEL->mSurfaces.size() - 1 );
  2068. return;
  2069. }
  2070. }
  2071. mConvexSEL->mSurfaces = mLastValidShape;
  2072. updateShape( mConvexSEL );
  2073. updateGizmoPos();
  2074. }
  2075. SceneObject* GuiConvexEditorCtrl::createPolyhedralObject(const char* className, SceneObject* geometryProvider)
  2076. {
  2077. if (!geometryProvider)
  2078. {
  2079. Con::errorf("WorldEditor::createPolyhedralObject - Invalid geometry provider!");
  2080. return NULL;
  2081. }
  2082. if (!className || !className[0])
  2083. {
  2084. Con::errorf("WorldEditor::createPolyhedralObject - Invalid class name");
  2085. return NULL;
  2086. }
  2087. AbstractClassRep* classRep = AbstractClassRep::findClassRep(className);
  2088. if (!classRep)
  2089. {
  2090. Con::errorf("WorldEditor::createPolyhedralObject - No such class: %s", className);
  2091. return NULL;
  2092. }
  2093. // We don't want the extracted poly list to be affected by the object's
  2094. // current transform and scale so temporarily reset them.
  2095. MatrixF savedTransform = geometryProvider->getTransform();
  2096. Point3F savedScale = geometryProvider->getScale();
  2097. geometryProvider->setTransform(MatrixF::Identity);
  2098. geometryProvider->setScale(Point3F(1.f, 1.f, 1.f));
  2099. // Extract the geometry. Use the object-space bounding volumes
  2100. // as we have moved the object to the origin for the moment.
  2101. OptimizedPolyList polyList;
  2102. if (!geometryProvider->buildPolyList(PLC_Export, &polyList, geometryProvider->getObjBox(), geometryProvider->getObjBox().getBoundingSphere()))
  2103. {
  2104. Con::errorf("WorldEditor::createPolyhedralObject - Failed to extract geometry!");
  2105. return NULL;
  2106. }
  2107. // Restore the object's original transform.
  2108. geometryProvider->setTransform(savedTransform);
  2109. geometryProvider->setScale(savedScale);
  2110. // Create the object.
  2111. SceneObject* object = dynamic_cast< SceneObject* >(classRep->create());
  2112. if (!Object)
  2113. {
  2114. Con::errorf("WorldEditor::createPolyhedralObject - Could not create SceneObject with class '%s'", className);
  2115. return NULL;
  2116. }
  2117. // Convert the polylist to a polyhedron.
  2118. Polyhedron polyhedron = polyList.toPolyhedron();
  2119. // Add the vertex data.
  2120. const U32 numPoints = polyhedron.getNumPoints();
  2121. const Point3F* points = polyhedron.getPoints();
  2122. for (U32 i = 0; i < numPoints; ++i)
  2123. {
  2124. static StringTableEntry sPoint = StringTable->insert("point");
  2125. object->setDataField(sPoint, NULL, EngineMarshallData(points[i]));
  2126. }
  2127. // Add the plane data.
  2128. const U32 numPlanes = polyhedron.getNumPlanes();
  2129. const PlaneF* planes = polyhedron.getPlanes();
  2130. for (U32 i = 0; i < numPlanes; ++i)
  2131. {
  2132. static StringTableEntry sPlane = StringTable->insert("plane");
  2133. const PlaneF& plane = planes[i];
  2134. char buffer[1024];
  2135. dSprintf(buffer, sizeof(buffer), "%g %g %g %g", plane.x, plane.y, plane.z, plane.d);
  2136. object->setDataField(sPlane, NULL, buffer);
  2137. }
  2138. // Add the edge data.
  2139. const U32 numEdges = polyhedron.getNumEdges();
  2140. const Polyhedron::Edge* edges = polyhedron.getEdges();
  2141. for (U32 i = 0; i < numEdges; ++i)
  2142. {
  2143. static StringTableEntry sEdge = StringTable->insert("edge");
  2144. const Polyhedron::Edge& edge = edges[i];
  2145. char buffer[1024];
  2146. dSprintf(buffer, sizeof(buffer), "%i %i %i %i ",
  2147. edge.face[0], edge.face[1],
  2148. edge.vertex[0], edge.vertex[1]
  2149. );
  2150. object->setDataField(sEdge, NULL, buffer);
  2151. }
  2152. // Set the transform.
  2153. object->setTransform(savedTransform);
  2154. object->setScale(savedScale);
  2155. // Register and return the object.
  2156. if (!object->registerObject())
  2157. {
  2158. Con::errorf("WorldEditor::createPolyhedralObject - Failed to register object!");
  2159. delete object;
  2160. return NULL;
  2161. }
  2162. return object;
  2163. }
  2164. ConvexShape* GuiConvexEditorCtrl::createConvexShapeFrom(SceneObject* polyObject)
  2165. {
  2166. if (!polyObject)
  2167. {
  2168. Con::errorf("WorldEditor::createConvexShapeFrom - Invalid object");
  2169. return NULL;
  2170. }
  2171. IScenePolyhedralObject* iPoly = dynamic_cast< IScenePolyhedralObject* >(polyObject);
  2172. if (!iPoly)
  2173. {
  2174. Con::errorf("WorldEditor::createConvexShapeFrom - Not a polyhedral object!");
  2175. return NULL;
  2176. }
  2177. // Get polyhedron.
  2178. AnyPolyhedron polyhedron = iPoly->ToAnyPolyhedron();
  2179. const U32 numPlanes = polyhedron.getNumPlanes();
  2180. if (!numPlanes)
  2181. {
  2182. Con::errorf("WorldEditor::createConvexShapeFrom - Object returned no valid polyhedron");
  2183. return NULL;
  2184. }
  2185. // Create a ConvexShape.
  2186. ConvexShape* shape = new ConvexShape();
  2187. // Add all planes.
  2188. for (U32 i = 0; i < numPlanes; ++i)
  2189. {
  2190. const PlaneF& plane = polyhedron.getPlanes()[i];
  2191. // Polyhedron planes are facing inwards so we need to
  2192. // invert the normal here.
  2193. Point3F normal = plane.getNormal();
  2194. normal.neg();
  2195. // Turn the orientation of the plane into a quaternion.
  2196. // The normal is our up vector (that's what's expected
  2197. // by ConvexShape for the surface orientation).
  2198. MatrixF orientation(true);
  2199. MathUtils::getMatrixFromUpVector(normal, &orientation);
  2200. const QuatF quat(orientation);
  2201. // Get the plane position.
  2202. const Point3F position = plane.getPosition();
  2203. // Turn everything into a "surface" property for the ConvexShape.
  2204. char buffer[1024];
  2205. dSprintf(buffer, sizeof(buffer), "%g %g %g %g %g %g %g",
  2206. quat.x, quat.y, quat.z, quat.w,
  2207. position.x, position.y, position.z
  2208. );
  2209. // Add the surface.
  2210. static StringTableEntry sSurface = StringTable->insert("surface");
  2211. shape->setDataField(sSurface, NULL, buffer);
  2212. }
  2213. // Copy the transform.
  2214. shape->setTransform(polyObject->getTransform());
  2215. shape->setScale(polyObject->getScale());
  2216. // Register the shape.
  2217. if (!shape->registerObject())
  2218. {
  2219. Con::errorf("WorldEditor::createConvexShapeFrom - Could not register ConvexShape!");
  2220. delete shape;
  2221. return NULL;
  2222. }
  2223. return shape;
  2224. }
  2225. DefineEngineMethod( GuiConvexEditorCtrl, hollowSelection, void, (), , "" )
  2226. {
  2227. object->hollowSelection();
  2228. }
  2229. DefineEngineMethod( GuiConvexEditorCtrl, recenterSelection, void, (), , "" )
  2230. {
  2231. object->recenterSelection();
  2232. }
  2233. DefineEngineMethod( GuiConvexEditorCtrl, hasSelection, S32, (), , "" )
  2234. {
  2235. return object->hasSelection();
  2236. }
  2237. DefineEngineMethod( GuiConvexEditorCtrl, handleDelete, void, (), , "" )
  2238. {
  2239. object->handleDelete();
  2240. }
  2241. DefineEngineMethod( GuiConvexEditorCtrl, handleDeselect, void, (), , "" )
  2242. {
  2243. object->handleDeselect();
  2244. }
  2245. DefineEngineMethod( GuiConvexEditorCtrl, dropSelectionAtScreenCenter, void, (), , "" )
  2246. {
  2247. object->dropSelectionAtScreenCenter();
  2248. }
  2249. DefineEngineMethod( GuiConvexEditorCtrl, selectConvex, void, (ConvexShape *convex), , "( ConvexShape )" )
  2250. {
  2251. if (convex)
  2252. object->setSelection( convex, -1 );
  2253. }
  2254. DefineEngineMethod( GuiConvexEditorCtrl, splitSelectedFace, void, (), , "" )
  2255. {
  2256. object->splitSelectedFace();
  2257. }
  2258. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceUVOffset, Point2F, (), ,
  2259. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2260. "@param objB Object to mount onto us\n"
  2261. "@param slot Mount slot ID\n"
  2262. "@param txfm (optional) mount offset transform\n"
  2263. "@return true if successful, false if failed (objB is not valid)")
  2264. {
  2265. //return Point2F(0, 0);
  2266. return object->getSelectedFaceUVOffset();
  2267. }
  2268. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceUVScale, Point2F, (), ,
  2269. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2270. "@param objB Object to mount onto us\n"
  2271. "@param slot Mount slot ID\n"
  2272. "@param txfm (optional) mount offset transform\n"
  2273. "@return true if successful, false if failed (objB is not valid)")
  2274. {
  2275. //return Point2F(0, 0);
  2276. return object->getSelectedFaceUVScale();
  2277. }
  2278. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceUVOffset, void, ( Point2F offset ), ( Point2F(0,0) ),
  2279. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2280. "@param objB Object to mount onto us\n"
  2281. "@param slot Mount slot ID\n"
  2282. "@param txfm (optional) mount offset transform\n"
  2283. "@return true if successful, false if failed (objB is not valid)")
  2284. {
  2285. //return Point2F(0, 0);
  2286. return object->setSelectedFaceUVOffset(offset);
  2287. }
  2288. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceUVScale, void, (Point2F scale), (Point2F(0, 0)),
  2289. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2290. "@param objB Object to mount onto us\n"
  2291. "@param slot Mount slot ID\n"
  2292. "@param txfm (optional) mount offset transform\n"
  2293. "@return true if successful, false if failed (objB is not valid)")
  2294. {
  2295. //return Point2F(0, 0);
  2296. return object->setSelectedFaceUVScale(scale);
  2297. }
  2298. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceMaterial, void, (const char* materialName), (""),
  2299. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2300. "@param objB Object to mount onto us\n"
  2301. "@param slot Mount slot ID\n"
  2302. "@param txfm (optional) mount offset transform\n"
  2303. "@return true if successful, false if failed (objB is not valid)")
  2304. {
  2305. //return Point2F(0, 0);
  2306. if (!String::compare(materialName, ""))
  2307. return;
  2308. object->setSelectedFaceMaterial(materialName);
  2309. }
  2310. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceMaterial, const char*, (), ,
  2311. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2312. "@param objB Object to mount onto us\n"
  2313. "@param slot Mount slot ID\n"
  2314. "@param txfm (optional) mount offset transform\n"
  2315. "@return true if successful, false if failed (objB is not valid)")
  2316. {
  2317. return object->getSelectedFaceMaterial();
  2318. }
  2319. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceHorzFlip, void, (bool flipped), (false),
  2320. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2321. "@param objB Object to mount onto us\n"
  2322. "@param slot Mount slot ID\n"
  2323. "@param txfm (optional) mount offset transform\n"
  2324. "@return true if successful, false if failed (objB is not valid)")
  2325. {
  2326. object->setSelectedFaceHorzFlip(flipped);
  2327. }
  2328. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceVertFlip, void, (bool flipped), (false),
  2329. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2330. "@param objB Object to mount onto us\n"
  2331. "@param slot Mount slot ID\n"
  2332. "@param txfm (optional) mount offset transform\n"
  2333. "@return true if successful, false if failed (objB is not valid)")
  2334. {
  2335. object->setSelectedFaceVertFlip(flipped);
  2336. }
  2337. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceHorzFlip, bool, (), ,
  2338. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2339. "@param objB Object to mount onto us\n"
  2340. "@param slot Mount slot ID\n"
  2341. "@param txfm (optional) mount offset transform\n"
  2342. "@return true if successful, false if failed (objB is not valid)")
  2343. {
  2344. return object->getSelectedFaceHorzFlip();
  2345. }
  2346. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceVertFlip, bool, (), ,
  2347. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2348. "@param objB Object to mount onto us\n"
  2349. "@param slot Mount slot ID\n"
  2350. "@param txfm (optional) mount offset transform\n"
  2351. "@return true if successful, false if failed (objB is not valid)")
  2352. {
  2353. return object->getSelectedFaceVertFlip();
  2354. }
  2355. DefineEngineMethod(GuiConvexEditorCtrl, setSelectedFaceZRot, void, (float degrees), (0.0),
  2356. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2357. "@param objB Object to mount onto us\n"
  2358. "@param slot Mount slot ID\n"
  2359. "@param txfm (optional) mount offset transform\n"
  2360. "@return true if successful, false if failed (objB is not valid)")
  2361. {
  2362. object->setSelectedFaceZRot(degrees);
  2363. }
  2364. DefineEngineMethod(GuiConvexEditorCtrl, getSelectedFaceZRot, float, (), ,
  2365. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2366. "@param objB Object to mount onto us\n"
  2367. "@param slot Mount slot ID\n"
  2368. "@param txfm (optional) mount offset transform\n"
  2369. "@return true if successful, false if failed (objB is not valid)")
  2370. {
  2371. return object->getSelectedFaceZRot();
  2372. }
  2373. DefineEngineMethod(GuiConvexEditorCtrl, toggleGridSnapping, void, (),,
  2374. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2375. "@param objB Object to mount onto us\n"
  2376. "@param slot Mount slot ID\n"
  2377. "@param txfm (optional) mount offset transform\n"
  2378. "@return true if successful, false if failed (objB is not valid)")
  2379. {
  2380. object->toggleGridSnapping();
  2381. }
  2382. DefineEngineMethod(GuiConvexEditorCtrl, setGridSnapSize, void, (float gridSize), (1.0),
  2383. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2384. "@param objB Object to mount onto us\n"
  2385. "@param slot Mount slot ID\n"
  2386. "@param txfm (optional) mount offset transform\n"
  2387. "@return true if successful, false if failed (objB is not valid)")
  2388. {
  2389. object->setGridSnapSize(gridSize);
  2390. }
  2391. DefineEngineMethod(GuiConvexEditorCtrl, getGridSnapSize, float, (),,
  2392. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2393. "@param objB Object to mount onto us\n"
  2394. "@param slot Mount slot ID\n"
  2395. "@param txfm (optional) mount offset transform\n"
  2396. "@return true if successful, false if failed (objB is not valid)")
  2397. {
  2398. return object->getGridSnapSize();
  2399. }
  2400. DefineEngineMethod(GuiConvexEditorCtrl, updateShape, void, (),,
  2401. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  2402. "@param objB Object to mount onto us\n"
  2403. "@param slot Mount slot ID\n"
  2404. "@param txfm (optional) mount offset transform\n"
  2405. "@return true if successful, false if failed (objB is not valid)")
  2406. {
  2407. //return Point2F(0, 0);
  2408. return object->updateShape();
  2409. }