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