guiConvexShapeEditorCtrl.cpp 84 KB

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