guiConvexShapeEditorCtrl.cpp 84 KB

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