tsStatic.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "T3D/tsStatic.h"
  28. #include "core/resourceManager.h"
  29. #include "core/stream/bitStream.h"
  30. #include "scene/sceneRenderState.h"
  31. #include "scene/sceneManager.h"
  32. #include "scene/sceneObjectLightingPlugin.h"
  33. #include "lighting/lightManager.h"
  34. #include "math/mathIO.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "ts/tsMaterialList.h"
  37. #include "console/consoleTypes.h"
  38. #include "T3D/shapeBase.h"
  39. #include "sim/netConnection.h"
  40. #include "gfx/gfxDevice.h"
  41. #include "gfx/gfxTransformSaver.h"
  42. #include "ts/tsRenderState.h"
  43. #include "collision/boxConvex.h"
  44. #include "T3D/physics/physicsPlugin.h"
  45. #include "T3D/physics/physicsBody.h"
  46. #include "T3D/physics/physicsCollision.h"
  47. #include "materials/materialDefinition.h"
  48. #include "materials/materialManager.h"
  49. #include "materials/matInstance.h"
  50. #include "materials/materialFeatureData.h"
  51. #include "materials/materialFeatureTypes.h"
  52. #include "console/engineAPI.h"
  53. #include "T3D/accumulationVolume.h"
  54. #include "math/mTransform.h"
  55. #include "gui/editor/inspector/group.h"
  56. #include "console/typeValidators.h"
  57. using namespace Torque;
  58. extern bool gEditingMission;
  59. #ifdef TORQUE_AFX_ENABLED
  60. #include "afx/ce/afxZodiacMgr.h"
  61. #endif
  62. IMPLEMENT_CO_NETOBJECT_V1(TSStatic);
  63. ConsoleDocClass(TSStatic,
  64. "@brief A static object derived from a 3D model file and placed within the game world.\n\n"
  65. "TSStatic is the most basic 3D shape in Torque. Unlike StaticShape it doesn't make use of "
  66. "a datablock. It derrives directly from SceneObject. This makes TSStatic extremely light "
  67. "weight, which is why the Tools use this class when you want to drop in a DTS or DAE object.\n\n"
  68. "While a TSStatic doesn't provide any motion -- it stays were you initally put it -- it does allow for "
  69. "a single ambient animation sequence to play when the object is first added to the scene.\n\n"
  70. "@tsexample\n"
  71. "new TSStatic(Team1Base) {\n"
  72. " shapeName = \"art/shapes/desertStructures/station01.dts\";\n"
  73. " playAmbient = \"1\";\n"
  74. " receiveSunLight = \"1\";\n"
  75. " receiveLMLighting = \"1\";\n"
  76. " useCustomAmbientLighting = \"0\";\n"
  77. " customAmbientLighting = \"0 0 0 1\";\n"
  78. " collisionType = \"Visible Mesh\";\n"
  79. " decalType = \"Collision Mesh\";\n"
  80. " allowPlayerStep = \"1\";\n"
  81. " renderNormals = \"0\";\n"
  82. " forceDetail = \"-1\";\n"
  83. " position = \"315.18 -180.418 244.313\";\n"
  84. " rotation = \"0 0 1 195.952\";\n"
  85. " scale = \"1 1 1\";\n"
  86. " isRenderEnabled = \"true\";\n"
  87. " canSaveDynamicFields = \"1\";\n"
  88. "};\n"
  89. "@endtsexample\n"
  90. "@ingroup gameObjects\n"
  91. );
  92. bool TSStatic::smUseStaticObjectFade = false;
  93. F32 TSStatic::smStaticObjectFadeStart = 50;
  94. F32 TSStatic::smStaticObjectFadeEnd = 75;
  95. F32 TSStatic::smStaticObjectUnfadeableSize = 75;
  96. TSStatic::TSStatic()
  97. :
  98. cubeDescId(0),
  99. reflectorDesc(NULL)
  100. {
  101. mNetFlags.set(Ghostable | ScopeAlways);
  102. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  103. mShapeInstance = NULL;
  104. mPlayAmbient = true;
  105. mAmbientThread = NULL;
  106. mAllowPlayerStep = false;
  107. mConvexList = new Convex;
  108. mRenderNormalScalar = 0;
  109. mForceDetail = -1;
  110. mMeshCulling = false;
  111. mUseOriginSort = false;
  112. mUseAlphaFade = false;
  113. mAlphaFadeStart = 100.0f;
  114. mAlphaFadeEnd = 150.0f;
  115. mInvertAlphaFade = false;
  116. mAlphaFade = 1.0f;
  117. mPhysicsRep = NULL;
  118. mCollisionType = CollisionMesh;
  119. mDecalType = CollisionMesh;
  120. mIgnoreZodiacs = false;
  121. mHasGradients = false;
  122. mInvertGradientRange = false;
  123. mGradientRangeUser.set(0.0f, 180.0f);
  124. #ifdef TORQUE_AFX_ENABLED
  125. afxZodiacData::convertGradientRangeFromDegrees(mGradientRange, mGradientRangeUser);
  126. #endif
  127. mAnimOffset = 0.0f;
  128. mAnimSpeed = 1.0f;
  129. INIT_ASSET(Shape);
  130. }
  131. TSStatic::~TSStatic()
  132. {
  133. delete mConvexList;
  134. mConvexList = NULL;
  135. }
  136. ImplementEnumType(TSMeshType,
  137. "Type of mesh data available in a shape.\n"
  138. "@ingroup gameObjects")
  139. {
  140. TSStatic::None, "None", "No mesh data."
  141. },
  142. { TSStatic::Bounds, "Bounds", "Bounding box of the shape." },
  143. { TSStatic::CollisionMesh, "Collision Mesh", "Specifically desingated \"collision\" meshes." },
  144. { TSStatic::VisibleMesh, "Visible Mesh", "Rendered mesh polygons." },
  145. EndImplementEnumType;
  146. FRangeValidator percentValidator(0.0f, 1.0f);
  147. F32 AnimSpeedMax = 4.0f;
  148. FRangeValidator speedValidator(0.0f, AnimSpeedMax);
  149. void TSStatic::initPersistFields()
  150. {
  151. docsURL;
  152. addGroup("Shape");
  153. INITPERSISTFIELD_SHAPEASSET(Shape, TSStatic, "Model to use for this TSStatic");
  154. addProtectedField("shapeName", TypeShapeFilename, Offset(mShapeName, TSStatic),
  155. &TSStatic::_setShapeData, &defaultProtectedGetFn,
  156. "%Path and filename of the model file (.DTS, .DAE) to use for this TSStatic. Legacy field. Any loose files assigned here will attempt to be auto-imported in as an asset.", AbstractClassRep::FIELD_HideInInspectors);
  157. endGroup("Shape");
  158. addGroup("Materials");
  159. addProtectedField("skin", TypeRealString, Offset(mAppliedSkinName, TSStatic), &_setFieldSkin, &_getFieldSkin,
  160. "@brief The skin applied to the shape.\n\n"
  161. "'Skinning' the shape effectively renames the material targets, allowing "
  162. "different materials to be used on different instances of the same model.\n\n"
  163. "Any material targets that start with the old skin name have that part "
  164. "of the name replaced with the new skin name. The initial old skin name is "
  165. "\"base\". For example, if a new skin of \"blue\" was applied to a model "
  166. "that had material targets <i>base_body</i> and <i>face</i>, the new targets "
  167. "would be <i>blue_body</i> and <i>face</i>. Note that <i>face</i> was not "
  168. "renamed since it did not start with the old skin name of \"base\".\n\n"
  169. "To support models that do not use the default \"base\" naming convention, "
  170. "you can also specify the part of the name to replace in the skin field "
  171. "itself. For example, if a model had a material target called <i>shapemat</i>, "
  172. "we could apply a new skin \"shape=blue\", and the material target would be "
  173. "renamed to <i>bluemat</i> (note \"shape\" has been replaced with \"blue\").\n\n"
  174. "Multiple skin updates can also be applied at the same time by separating "
  175. "them with a semicolon. For example: \"base=blue;face=happy_face\".\n\n"
  176. "Material targets are only renamed if an existing Material maps to that "
  177. "name, or if there is a diffuse texture in the model folder with the same "
  178. "name as the new target.\n\n");
  179. endGroup("Materials");
  180. addGroup("Animation");
  181. addField("playAmbient", TypeBool, Offset(mPlayAmbient, TSStatic),
  182. "Enables automatic playing of the animation sequence named \"ambient\" (if it exists) when the TSStatic is loaded.");
  183. addFieldV("AnimOffset", TypeF32, Offset(mAnimOffset, TSStatic), &percentValidator,
  184. "Percent Animation Offset.");
  185. addFieldV("AnimSpeed", TypeF32, Offset(mAnimSpeed, TSStatic), &speedValidator,
  186. "Percent Animation Speed.");
  187. endGroup("Animation");
  188. addGroup("Rendering");
  189. addField("meshCulling", TypeBool, Offset(mMeshCulling, TSStatic),
  190. "Enables detailed culling of meshes within the TSStatic. Should only be used "
  191. "with large complex shapes like buildings which contain many submeshes.");
  192. addField("originSort", TypeBool, Offset(mUseOriginSort, TSStatic),
  193. "Enables translucent sorting of the TSStatic by its origin instead of the bounds.");
  194. endGroup("Rendering");
  195. addGroup("Reflection");
  196. addField("cubeReflectorDesc", TypeRealString, Offset(cubeDescName, TSStatic),
  197. "References a ReflectorDesc datablock that defines performance and quality properties for dynamic reflections.\n");
  198. endGroup("Reflection");
  199. addGroup("Collision");
  200. addField("collisionType", TypeTSMeshType, Offset(mCollisionType, TSStatic),
  201. "The type of mesh data to use for collision queries.");
  202. addField("decalType", TypeTSMeshType, Offset(mDecalType, TSStatic),
  203. "The type of mesh data used to clip decal polygons against.");
  204. addField("allowPlayerStep", TypeBool, Offset(mAllowPlayerStep, TSStatic),
  205. "@brief Allow a Player to walk up sloping polygons in the TSStatic (based on the collisionType).\n\n"
  206. "When set to false, the slightest bump will stop the player from walking on top of the object.\n");
  207. endGroup("Collision");
  208. addGroup("AlphaFade");
  209. addField("alphaFadeEnable", TypeBool, Offset(mUseAlphaFade, TSStatic), "Turn on/off Alpha Fade");
  210. addField("alphaFadeStart", TypeF32, Offset(mAlphaFadeStart, TSStatic), "Distance of start Alpha Fade");
  211. addField("alphaFadeEnd", TypeF32, Offset(mAlphaFadeEnd, TSStatic), "Distance of end Alpha Fade");
  212. addField("alphaFadeInverse", TypeBool, Offset(mInvertAlphaFade, TSStatic), "Invert Alpha Fade's Start & End Distance");
  213. endGroup("AlphaFade");
  214. addGroup("Debug");
  215. addField("renderNormals", TypeF32, Offset(mRenderNormalScalar, TSStatic),
  216. "Debug rendering mode shows the normals for each point in the TSStatic's mesh.");
  217. addField("forceDetail", TypeS32, Offset(mForceDetail, TSStatic),
  218. "Forces rendering to a particular detail level.");
  219. endGroup("Debug");
  220. addGroup("AFX");
  221. addField("ignoreZodiacs", TypeBool, Offset(mIgnoreZodiacs, TSStatic));
  222. addField("useGradientRange", TypeBool, Offset(mHasGradients, TSStatic));
  223. addField("gradientRange", TypePoint2F, Offset(mGradientRangeUser, TSStatic));
  224. addField("invertGradientRange", TypeBool, Offset(mInvertGradientRange, TSStatic));
  225. endGroup("AFX");
  226. Parent::initPersistFields();
  227. }
  228. void TSStatic::consoleInit()
  229. {
  230. Parent::consoleInit();
  231. // Vars for debug rendering while the RoadEditor is open, only used if smEditorOpen is true.
  232. Con::addVariable("$pref::useStaticObjectFade", TypeBool, &TSStatic::smUseStaticObjectFade, "Indicates if all statics should utilize the distance-based object fadeout logic.\n");
  233. Con::addVariable("$pref::staticObjectFadeStart", TypeF32, &TSStatic::smStaticObjectFadeStart, "Distance at which static object fading begins if $pref::useStaticObjectFade is on.\n");
  234. Con::addVariable("$pref::staticObjectFadeEnd", TypeF32, &TSStatic::smStaticObjectFadeEnd, "Distance at which static object fading should have fully faded if $pref::useStaticObjectFade is on.\n");
  235. Con::addVariable("$pref::staticObjectUnfadeableSize", TypeF32, &TSStatic::smStaticObjectUnfadeableSize, "Size of object where if the bounds is at or bigger than this, it will be ignored in the $pref::useStaticObjectFade logic. Useful for very large, distance-important objects.\n");
  236. }
  237. bool TSStatic::_setFieldSkin(void* object, const char* index, const char* data)
  238. {
  239. TSStatic* ts = static_cast<TSStatic*>(object);
  240. if (ts)
  241. ts->setSkinName(data);
  242. return false;
  243. }
  244. const char* TSStatic::_getFieldSkin(void* object, const char* data)
  245. {
  246. TSStatic* ts = static_cast<TSStatic*>(object);
  247. return ts ? ts->mSkinNameHandle.getString() : "";
  248. }
  249. void TSStatic::inspectPostApply()
  250. {
  251. // Apply any transformations set in the editor
  252. Parent::inspectPostApply();
  253. if (isServerObject())
  254. {
  255. setMaskBits(-1);
  256. prepCollision();
  257. }
  258. _updateShouldTick();
  259. }
  260. bool TSStatic::onAdd()
  261. {
  262. PROFILE_SCOPE(TSStatic_onAdd);
  263. if (isServerObject())
  264. {
  265. // Handle the old "usePolysoup" field
  266. SimFieldDictionary* fieldDict = getFieldDictionary();
  267. if (fieldDict)
  268. {
  269. StringTableEntry slotName = StringTable->insert("usePolysoup");
  270. SimFieldDictionary::Entry* entry = fieldDict->findDynamicField(slotName);
  271. if (entry)
  272. {
  273. // Was "usePolysoup" set?
  274. bool usePolysoup = dAtob(entry->value);
  275. // "usePolysoup" maps to the new VisibleMesh type
  276. if (usePolysoup)
  277. mCollisionType = VisibleMesh;
  278. // Remove the field in favor on the new "collisionType" field
  279. fieldDict->setFieldValue(slotName, "");
  280. }
  281. }
  282. }
  283. if (!Parent::onAdd())
  284. return false;
  285. // Setup the shape.
  286. if (!_createShape())
  287. {
  288. Con::errorf("TSStatic::onAdd() - Shape creation failed!");
  289. return false;
  290. }
  291. setRenderTransform(mObjToWorld);
  292. // Register for the resource change signal.
  293. //ResourceManager::get().getChangedSignal().notify(this, &TSStatic::_onResourceChanged);
  294. addToScene();
  295. if (isClientObject())
  296. {
  297. mCubeReflector.unregisterReflector();
  298. if (reflectorDesc)
  299. mCubeReflector.registerReflector(this, reflectorDesc);
  300. }
  301. _updateShouldTick();
  302. // Accumulation and environment mapping
  303. if (isClientObject() && mShapeInstance)
  304. {
  305. AccumulationVolume::addObject(this);
  306. }
  307. return true;
  308. }
  309. bool TSStatic::_createShape()
  310. {
  311. // Cleanup before we create.
  312. mCollisionDetails.clear();
  313. mDecalDetails.clear();
  314. mDecalDetailsPtr = 0;
  315. mLOSDetails.clear();
  316. SAFE_DELETE(mPhysicsRep);
  317. SAFE_DELETE(mShapeInstance);
  318. mAmbientThread = NULL;
  319. mShape = NULL;
  320. if(!mShapeAsset.isNull())
  321. {
  322. //Special-case handling, usually because we set noShape
  323. mShape = mShapeAsset->getShapeResource();
  324. }
  325. if (!mShape)
  326. {
  327. Con::errorf("TSStatic::_createShape() - Shape Asset %s had no valid shape!", mShapeAsset.getAssetId());
  328. return false;
  329. }
  330. if (isClientObject() &&
  331. !mShape->preloadMaterialList(mShape.getPath()) &&
  332. NetConnection::filesWereDownloaded())
  333. return false;
  334. mObjBox = mShape->mBounds;
  335. resetWorldBox();
  336. mShapeInstance = new TSShapeInstance(mShape, isClientObject());
  337. mShapeInstance->resetMaterialList();
  338. mShapeInstance->cloneMaterialList();
  339. if (isGhost())
  340. {
  341. // Reapply the current skin
  342. mAppliedSkinName = "";
  343. reSkin();
  344. updateMaterials();
  345. }
  346. prepCollision();
  347. // Find the "ambient" animation if it exists
  348. S32 ambientSeq = mShape->findSequence("ambient");
  349. if (ambientSeq > -1 && !mAmbientThread)
  350. mAmbientThread = mShapeInstance->addThread();
  351. if ( mAmbientThread )
  352. mShapeInstance->setSequence(mAmbientThread, ambientSeq, mAnimOffset);
  353. // Resolve CubeReflectorDesc.
  354. if (cubeDescName.isNotEmpty())
  355. {
  356. Sim::findObject(cubeDescName, reflectorDesc);
  357. }
  358. else if (cubeDescId > 0)
  359. {
  360. Sim::findObject(cubeDescId, reflectorDesc);
  361. }
  362. //Set up the material slot vars for easy manipulation
  363. /*S32 materialCount = mShape->materialList->getMaterialNameList().size(); //mMeshAsset->getMaterialCount();
  364. //Temporarily disabled until fixup of materialName->assetId lookup logic is sorted for easy persistance
  365. if (isServerObject())
  366. {
  367. char matFieldName[128];
  368. for (U32 i = 0; i < materialCount; i++)
  369. {
  370. StringTableEntry materialname = StringTable->insert(mShape->materialList->getMaterialName(i).c_str());
  371. dSprintf(matFieldName, 128, "MaterialSlot%d", i);
  372. StringTableEntry matFld = StringTable->insert(matFieldName);
  373. setDataField(matFld, NULL, materialname);
  374. }
  375. }*/
  376. return true;
  377. }
  378. void TSStatic::onDynamicModified(const char* slotName, const char* newValue)
  379. {
  380. if (FindMatch::isMatch("materialslot*", slotName, false))
  381. {
  382. if (!getShape())
  383. return;
  384. S32 slot = -1;
  385. String outStr(String::GetTrailingNumber(slotName, slot));
  386. if (slot == -1)
  387. return;
  388. //Safe to assume the inbound value for the material will be a MaterialAsset, so lets do a lookup on the name
  389. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(newValue);
  390. if (!matAsset)
  391. return;
  392. bool found = false;
  393. for (U32 i = 0; i < mChangingMaterials.size(); i++)
  394. {
  395. if (mChangingMaterials[i].slot == slot)
  396. {
  397. mChangingMaterials[i].matAsset = matAsset;
  398. mChangingMaterials[i].assetId = newValue;
  399. found = true;
  400. }
  401. }
  402. if (!found)
  403. {
  404. matMap newMatMap;
  405. newMatMap.slot = slot;
  406. newMatMap.matAsset = matAsset;
  407. newMatMap.assetId = newValue;
  408. mChangingMaterials.push_back(newMatMap);
  409. }
  410. setMaskBits(MaterialMask);
  411. }
  412. Parent::onDynamicModified(slotName, newValue);
  413. }
  414. void TSStatic::prepCollision()
  415. {
  416. // Let the client know that the collision was updated
  417. setMaskBits(UpdateCollisionMask);
  418. // Allow the ShapeInstance to prep its collision if it hasn't already
  419. if (mShapeInstance)
  420. mShapeInstance->prepCollision();
  421. // Cleanup any old collision data
  422. mCollisionDetails.clear();
  423. mDecalDetails.clear();
  424. mDecalDetailsPtr = 0;
  425. mLOSDetails.clear();
  426. mConvexList->nukeList();
  427. if (mCollisionType == CollisionMesh || mCollisionType == VisibleMesh)
  428. {
  429. mShape->findColDetails(mCollisionType == VisibleMesh, &mCollisionDetails, &mLOSDetails);
  430. if (mDecalType == mCollisionType)
  431. {
  432. mDecalDetailsPtr = &mCollisionDetails;
  433. }
  434. else if (mDecalType == CollisionMesh || mDecalType == VisibleMesh)
  435. {
  436. mShape->findColDetails(mDecalType == VisibleMesh, &mDecalDetails, 0);
  437. mDecalDetailsPtr = &mDecalDetails;
  438. }
  439. }
  440. else if (mDecalType == CollisionMesh || mDecalType == VisibleMesh)
  441. {
  442. mShape->findColDetails(mDecalType == VisibleMesh, &mDecalDetails, 0);
  443. mDecalDetailsPtr = &mDecalDetails;
  444. }
  445. _updatePhysics();
  446. }
  447. void TSStatic::_updatePhysics()
  448. {
  449. SAFE_DELETE(mPhysicsRep);
  450. if (!PHYSICSMGR || mCollisionType == None)
  451. return;
  452. PhysicsCollision* colShape = NULL;
  453. if (mCollisionType == Bounds)
  454. {
  455. MatrixF offset(true);
  456. offset.setPosition(mShape->center);
  457. colShape = PHYSICSMGR->createCollision();
  458. colShape->addBox(getObjBox().getExtents() * 0.5f * mObjScale, offset);
  459. }
  460. else
  461. colShape = mShape->buildColShape(mCollisionType == VisibleMesh, getScale());
  462. if (colShape)
  463. {
  464. PhysicsWorld* world = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  465. mPhysicsRep = PHYSICSMGR->createBody();
  466. mPhysicsRep->init(colShape, 0, 0, this, world);
  467. mPhysicsRep->setTransform(getTransform());
  468. }
  469. }
  470. void TSStatic::onRemove()
  471. {
  472. SAFE_DELETE(mPhysicsRep);
  473. // Accumulation
  474. if (isClientObject() && mShapeInstance)
  475. {
  476. if (mShapeInstance->hasAccumulation())
  477. AccumulationVolume::removeObject(this);
  478. }
  479. mConvexList->nukeList();
  480. removeFromScene();
  481. // Remove the resource change signal.
  482. //ResourceManager::get().getChangedSignal().remove(this, &TSStatic::_onResourceChanged);
  483. delete mShapeInstance;
  484. mShapeInstance = NULL;
  485. mAmbientThread = NULL;
  486. if (isClientObject())
  487. mCubeReflector.unregisterReflector();
  488. Parent::onRemove();
  489. }
  490. void TSStatic::_onResourceChanged(const Torque::Path& path)
  491. {
  492. if (path != Path(mShapeName))
  493. return;
  494. _createShape();
  495. _updateShouldTick();
  496. }
  497. void TSStatic::onShapeChanged()
  498. {
  499. _createShape();
  500. _updateShouldTick();
  501. }
  502. void TSStatic::setSkinName(const char* name)
  503. {
  504. if (!isGhost())
  505. {
  506. if (name[0] != '\0')
  507. {
  508. // Use tags for better network performance
  509. // Should be a tag, but we'll convert to one if it isn't.
  510. if (name[0] == StringTagPrefixByte)
  511. mSkinNameHandle = NetStringHandle(U32(dAtoi(name + 1)));
  512. else
  513. mSkinNameHandle = NetStringHandle(name);
  514. }
  515. else
  516. mSkinNameHandle = NetStringHandle();
  517. setMaskBits(SkinMask);
  518. }
  519. }
  520. void TSStatic::reSkin()
  521. {
  522. if (isGhost() && mShapeInstance)
  523. {
  524. if (mSkinNameHandle.isValidString())
  525. {
  526. mShapeInstance->resetMaterialList();
  527. Vector<String> skins;
  528. String(mSkinNameHandle.getString()).split(";", skins);
  529. for (S32 i = 0; i < skins.size(); i++)
  530. {
  531. String oldSkin(mAppliedSkinName.c_str());
  532. String newSkin(skins[i]);
  533. // Check if the skin handle contains an explicit "old" base string. This
  534. // allows all models to support skinning, even if they don't follow the
  535. // "base_xxx" material naming convention.
  536. S32 split = newSkin.find('='); // "old=new" format skin?
  537. if (split != String::NPos)
  538. {
  539. oldSkin = newSkin.substr(0, split);
  540. newSkin = newSkin.erase(0, split + 1);
  541. }
  542. else
  543. {
  544. oldSkin = "";
  545. }
  546. mShapeInstance->reSkin(newSkin, oldSkin);
  547. mAppliedSkinName = newSkin;
  548. }
  549. }
  550. else
  551. {
  552. mShapeInstance->reSkin("", mAppliedSkinName);
  553. mAppliedSkinName = "";
  554. }
  555. }
  556. }
  557. void TSStatic::processTick(const Move* move)
  558. {
  559. if ( isServerObject() && mPlayAmbient && mAmbientThread )
  560. {
  561. mShapeInstance->setTimeScale(mAmbientThread, mAnimSpeed);
  562. mShapeInstance->advanceTime( TickSec, mAmbientThread );
  563. }
  564. if (isMounted())
  565. {
  566. MatrixF mat(true);
  567. mMount.object->getMountTransform(mMount.node, mMount.xfm, &mat);
  568. setTransform(mat);
  569. }
  570. }
  571. void TSStatic::interpolateTick(F32 delta)
  572. {
  573. }
  574. void TSStatic::advanceTime(F32 dt)
  575. {
  576. if ( mPlayAmbient && mAmbientThread )
  577. {
  578. mShapeInstance->setTimeScale(mAmbientThread, mAnimSpeed);
  579. mShapeInstance->advanceTime( dt, mAmbientThread );
  580. }
  581. if (isMounted())
  582. {
  583. MatrixF mat(true);
  584. mMount.object->getRenderMountTransform(dt, mMount.node, mMount.xfm, &mat);
  585. setRenderTransform(mat);
  586. }
  587. }
  588. void TSStatic::_updateShouldTick()
  589. {
  590. bool shouldTick = (mPlayAmbient && mAmbientThread) || isMounted();
  591. if (isTicking() != shouldTick)
  592. setProcessTick(shouldTick);
  593. }
  594. void TSStatic::prepRenderImage(SceneRenderState* state)
  595. {
  596. if (!mShapeInstance)
  597. return;
  598. Point3F cameraOffset;
  599. getRenderTransform().getColumn(3, &cameraOffset);
  600. cameraOffset -= state->getDiffuseCameraPosition();
  601. F32 dist = cameraOffset.len();
  602. if (dist < 0.01f)
  603. dist = 0.01f;
  604. if (mUseAlphaFade)
  605. {
  606. mAlphaFade = 1.0f;
  607. if ((mAlphaFadeStart < mAlphaFadeEnd) && mAlphaFadeStart > 0.1f)
  608. {
  609. if (mInvertAlphaFade)
  610. {
  611. if (dist <= mAlphaFadeStart)
  612. {
  613. return;
  614. }
  615. if (dist < mAlphaFadeEnd)
  616. {
  617. mAlphaFade = ((dist - mAlphaFadeStart) / (mAlphaFadeEnd - mAlphaFadeStart));
  618. }
  619. }
  620. else
  621. {
  622. if (dist >= mAlphaFadeEnd)
  623. {
  624. return;
  625. }
  626. if (dist > mAlphaFadeStart)
  627. {
  628. mAlphaFade -= ((dist - mAlphaFadeStart) / (mAlphaFadeEnd - mAlphaFadeStart));
  629. }
  630. }
  631. }
  632. }
  633. else if (smUseStaticObjectFade)
  634. {
  635. F32 boundsLen = getWorldSphere().radius;
  636. if (boundsLen < smStaticObjectUnfadeableSize)
  637. {
  638. F32 distAdjust = (boundsLen) / (smStaticObjectUnfadeableSize);
  639. distAdjust = 1 - distAdjust;
  640. dist *= distAdjust;
  641. mAlphaFade = 1.0f;
  642. if ((smStaticObjectFadeStart < smStaticObjectFadeEnd) && smStaticObjectFadeStart > 0.1f)
  643. {
  644. if (dist >= smStaticObjectFadeEnd)
  645. {
  646. return;
  647. }
  648. if (dist > smStaticObjectFadeStart)
  649. {
  650. mAlphaFade -= ((dist - smStaticObjectFadeStart) / (smStaticObjectFadeEnd - smStaticObjectFadeStart));
  651. }
  652. }
  653. }
  654. }
  655. F32 invScale = (1.0f / getMax(getMax(mObjScale.x, mObjScale.y), mObjScale.z));
  656. // If we're currently rendering our own reflection we
  657. // don't want to render ourselves into it.
  658. if (mCubeReflector.isRendering())
  659. return;
  660. if (mForceDetail == -1)
  661. mShapeInstance->setDetailFromDistance(state, dist * invScale);
  662. else
  663. mShapeInstance->setCurrentDetail(mForceDetail);
  664. if (mShapeInstance->getCurrentDetail() < 0)
  665. return;
  666. GFXTransformSaver saver;
  667. // Set up our TS render state.
  668. TSRenderState rdata;
  669. rdata.setSceneState(state);
  670. rdata.setFadeOverride(1.0f);
  671. rdata.setOriginSort(mUseOriginSort);
  672. if (mCubeReflector.isEnabled())
  673. rdata.setCubemap(mCubeReflector.getCubemap());
  674. // Acculumation
  675. rdata.setAccuTex(mAccuTex);
  676. // If we have submesh culling enabled then prepare
  677. // the object space frustum to pass to the shape.
  678. Frustum culler;
  679. if (mMeshCulling)
  680. {
  681. culler = state->getCullingFrustum();
  682. MatrixF xfm(true);
  683. xfm.scale(Point3F::One / getScale());
  684. xfm.mul(getRenderWorldTransform());
  685. xfm.mul(culler.getTransform());
  686. culler.setTransform(xfm);
  687. rdata.setCuller(&culler);
  688. }
  689. // We might have some forward lit materials
  690. // so pass down a query to gather lights.
  691. LightQuery query;
  692. query.init(getWorldSphere());
  693. rdata.setLightQuery(&query);
  694. MatrixF mat = getRenderTransform();
  695. mat.scale(mObjScale);
  696. GFX->setWorldMatrix(mat);
  697. if (state->isDiffusePass() && mCubeReflector.isEnabled() && mCubeReflector.getOcclusionQuery())
  698. {
  699. RenderPassManager* pass = state->getRenderPass();
  700. OccluderRenderInst* ri = pass->allocInst<OccluderRenderInst>();
  701. ri->type = RenderPassManager::RIT_Occluder;
  702. ri->query = mCubeReflector.getOcclusionQuery();
  703. mObjToWorld.mulP(mObjBox.getCenter(), &ri->position);
  704. ri->scale.set(mObjBox.getExtents());
  705. ri->orientation = pass->allocUniqueXform(mObjToWorld);
  706. ri->isSphere = false;
  707. state->getRenderPass()->addInst(ri);
  708. }
  709. if (mShapeInstance)
  710. {
  711. mShapeInstance->animate();
  712. if (mUseAlphaFade || smUseStaticObjectFade)
  713. {
  714. mShapeInstance->setAlphaAlways(mAlphaFade);
  715. S32 s = mShapeInstance->mMeshObjects.size();
  716. for (S32 x = 0; x < s; x++)
  717. {
  718. mShapeInstance->mMeshObjects[x].visible = mAlphaFade;
  719. }
  720. }
  721. }
  722. mShapeInstance->render(rdata);
  723. #ifdef TORQUE_AFX_ENABLED
  724. if (!mIgnoreZodiacs && mDecalDetailsPtr != 0)
  725. afxZodiacMgr::renderPolysoupZodiacs(state, this);
  726. #endif
  727. if (mRenderNormalScalar > 0)
  728. {
  729. ObjectRenderInst* ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  730. ri->renderDelegate.bind(this, &TSStatic::_renderNormals);
  731. ri->type = RenderPassManager::RIT_Editor;
  732. state->getRenderPass()->addInst(ri);
  733. }
  734. }
  735. void TSStatic::_renderNormals(ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat)
  736. {
  737. PROFILE_SCOPE(TSStatic_RenderNormals);
  738. GFXTransformSaver saver;
  739. MatrixF mat = getRenderTransform();
  740. mat.scale(mObjScale);
  741. GFX->multWorld(mat);
  742. S32 dl = mShapeInstance->getCurrentDetail();
  743. mShapeInstance->renderDebugNormals(mRenderNormalScalar, dl);
  744. }
  745. void TSStatic::onScaleChanged()
  746. {
  747. Parent::onScaleChanged();
  748. if (mPhysicsRep)
  749. {
  750. // If the editor is enabled delay the scale operation
  751. // by a few milliseconds so that we're not rebuilding
  752. // during an active scale drag operation.
  753. if (gEditingMission)
  754. mPhysicsRep->queueCallback(500, Delegate<void()>(this, &TSStatic::_updatePhysics));
  755. else
  756. _updatePhysics();
  757. }
  758. setMaskBits(ScaleMask);
  759. }
  760. void TSStatic::setTransform(const MatrixF& mat)
  761. {
  762. Parent::setTransform(mat);
  763. if (!isMounted())
  764. setMaskBits(TransformMask);
  765. if (mPhysicsRep)
  766. mPhysicsRep->setTransform(mat);
  767. // Accumulation
  768. if (isClientObject() && mShapeInstance)
  769. {
  770. if (mShapeInstance->hasAccumulation())
  771. AccumulationVolume::updateObject(this);
  772. }
  773. // Since this is a static it's render transform changes 1
  774. // to 1 with it's collision transform... no interpolation.
  775. setRenderTransform(mat);
  776. }
  777. U32 TSStatic::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  778. {
  779. U32 retMask = Parent::packUpdate(con, mask, stream);
  780. if (stream->writeFlag(mask & TransformMask))
  781. mathWrite(*stream, getTransform());
  782. if (stream->writeFlag(mask & ScaleMask))
  783. {
  784. // Only write one bit if the scale is one.
  785. if (stream->writeFlag(mObjScale != Point3F::One))
  786. mathWrite(*stream, mObjScale);
  787. }
  788. if (stream->writeFlag(mask & UpdateCollisionMask))
  789. stream->write((U32)mCollisionType);
  790. if (stream->writeFlag(mask & SkinMask))
  791. con->packNetStringHandleU(stream, mSkinNameHandle);
  792. if (stream->writeFlag(mask & AdvancedStaticOptionsMask))
  793. {
  794. PACK_ASSET(con, Shape);
  795. stream->write((U32)mDecalType);
  796. stream->writeFlag(mAllowPlayerStep);
  797. stream->writeFlag(mMeshCulling);
  798. stream->writeFlag(mUseOriginSort);
  799. stream->write(mRenderNormalScalar);
  800. stream->write(mForceDetail);
  801. if (stream->writeFlag(mAnimOffset != 0.0f))
  802. stream->writeFloat(mAnimOffset, 7);
  803. if (stream->writeFlag(mAnimSpeed != 1.0f))
  804. stream->writeSignedFloat(mAnimSpeed / AnimSpeedMax, 7);
  805. stream->writeFlag(mPlayAmbient);
  806. }
  807. if (stream->writeFlag(mUseAlphaFade))
  808. {
  809. stream->write(mAlphaFadeStart);
  810. stream->write(mAlphaFadeEnd);
  811. stream->write(mInvertAlphaFade);
  812. }
  813. stream->writeFlag(mIgnoreZodiacs);
  814. if (stream->writeFlag(mHasGradients))
  815. {
  816. stream->writeFlag(mInvertGradientRange);
  817. stream->write(mGradientRange.x);
  818. stream->write(mGradientRange.y);
  819. }
  820. if (mLightPlugin)
  821. retMask |= mLightPlugin->packUpdate(this, AdvancedStaticOptionsMask, con, mask, stream);
  822. if (stream->writeFlag(reflectorDesc != NULL))
  823. {
  824. stream->writeRangedU32(reflectorDesc->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  825. }
  826. stream->write(mOverrideColor);
  827. if (stream->writeFlag(mask & MaterialMask))
  828. {
  829. stream->writeInt(mChangingMaterials.size(), 16);
  830. for (U32 i = 0; i < mChangingMaterials.size(); i++)
  831. {
  832. stream->writeInt(mChangingMaterials[i].slot, 16);
  833. NetStringHandle matNameStr = mChangingMaterials[i].assetId.c_str();
  834. con->packNetStringHandleU(stream, matNameStr);
  835. }
  836. //mChangingMaterials.clear();
  837. }
  838. return retMask;
  839. }
  840. void TSStatic::unpackUpdate(NetConnection* con, BitStream* stream)
  841. {
  842. Parent::unpackUpdate(con, stream);
  843. if (stream->readFlag()) // TransformMask
  844. {
  845. MatrixF mat;
  846. mathRead(*stream, &mat);
  847. setTransform(mat);
  848. setRenderTransform(mat);
  849. }
  850. if (stream->readFlag()) // ScaleMask
  851. {
  852. if (stream->readFlag())
  853. {
  854. VectorF scale;
  855. mathRead(*stream, &scale);
  856. setScale(scale);
  857. }
  858. else
  859. setScale(Point3F::One);
  860. }
  861. if (stream->readFlag()) // UpdateCollisionMask
  862. {
  863. U32 collisionType = CollisionMesh;
  864. stream->read(&collisionType);
  865. // Handle it if we have changed CollisionType's
  866. if ((MeshType)collisionType != mCollisionType)
  867. {
  868. mCollisionType = (MeshType)collisionType;
  869. if (isProperlyAdded() && mShapeInstance)
  870. prepCollision();
  871. }
  872. }
  873. if (stream->readFlag()) // SkinMask
  874. {
  875. NetStringHandle skinDesiredNameHandle = con->unpackNetStringHandleU(stream);;
  876. if (mSkinNameHandle != skinDesiredNameHandle)
  877. {
  878. mSkinNameHandle = skinDesiredNameHandle;
  879. reSkin();
  880. }
  881. }
  882. if (stream->readFlag()) // AdvancedStaticOptionsMask
  883. {
  884. UNPACK_ASSET(con, Shape);
  885. stream->read((U32*)&mDecalType);
  886. mAllowPlayerStep = stream->readFlag();
  887. mMeshCulling = stream->readFlag();
  888. mUseOriginSort = stream->readFlag();
  889. stream->read(&mRenderNormalScalar);
  890. stream->read(&mForceDetail);
  891. if (stream->readFlag())
  892. mAnimOffset = stream->readFloat(7);
  893. if (stream->readFlag())
  894. mAnimSpeed = stream->readSignedFloat(7) * AnimSpeedMax;
  895. mPlayAmbient = stream->readFlag();
  896. //update our shape, figuring that it likely changed
  897. _createShape();
  898. }
  899. mUseAlphaFade = stream->readFlag();
  900. if (mUseAlphaFade)
  901. {
  902. stream->read(&mAlphaFadeStart);
  903. stream->read(&mAlphaFadeEnd);
  904. stream->read(&mInvertAlphaFade);
  905. }
  906. mIgnoreZodiacs = stream->readFlag();
  907. mHasGradients = stream->readFlag();
  908. if (mHasGradients)
  909. {
  910. mInvertGradientRange = stream->readFlag();
  911. stream->read(&mGradientRange.x);
  912. stream->read(&mGradientRange.y);
  913. }
  914. if (mLightPlugin)
  915. {
  916. mLightPlugin->unpackUpdate(this, con, stream);
  917. }
  918. if (stream->readFlag())
  919. {
  920. cubeDescId = stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  921. }
  922. stream->read(&mOverrideColor);
  923. if (stream->readFlag())
  924. {
  925. mChangingMaterials.clear();
  926. U32 materialCount = stream->readInt(16);
  927. for (U32 i = 0; i < materialCount; i++)
  928. {
  929. matMap newMatMap;
  930. newMatMap.slot = stream->readInt(16);
  931. newMatMap.assetId = String(con->unpackNetStringHandleU(stream).getString());
  932. //do the lookup, now
  933. newMatMap.matAsset = AssetDatabase.acquireAsset<MaterialAsset>(newMatMap.assetId);
  934. mChangingMaterials.push_back(newMatMap);
  935. }
  936. updateMaterials();
  937. }
  938. if (isProperlyAdded())
  939. _updateShouldTick();
  940. set_special_typing();
  941. }
  942. //----------------------------------------------------------------------------
  943. bool TSStatic::castRay(const Point3F& start, const Point3F& end, RayInfo* info)
  944. {
  945. if (mCollisionType == None)
  946. return false;
  947. if (!mShapeInstance)
  948. return false;
  949. if (mCollisionType == Bounds)
  950. {
  951. F32 fst;
  952. if (!mObjBox.collideLine(start, end, &fst, &info->normal))
  953. return false;
  954. info->t = fst;
  955. info->object = this;
  956. info->point.interpolate(start, end, fst);
  957. info->material = NULL;
  958. return true;
  959. }
  960. else
  961. {
  962. RayInfo shortest = *info;
  963. RayInfo localInfo;
  964. shortest.t = 1e8f;
  965. localInfo.generateTexCoord = info->generateTexCoord;
  966. for (U32 i = 0; i < mLOSDetails.size(); i++)
  967. {
  968. mShapeInstance->animate(mLOSDetails[i]);
  969. if (mShapeInstance->castRayOpcode(mLOSDetails[i], start, end, &localInfo))
  970. {
  971. localInfo.object = this;
  972. if (localInfo.t < shortest.t)
  973. shortest = localInfo;
  974. }
  975. }
  976. if (shortest.object == this)
  977. {
  978. // Copy out the shortest time...
  979. *info = shortest;
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985. bool TSStatic::castRayRendered(const Point3F& start, const Point3F& end, RayInfo* info)
  986. {
  987. if (!mShapeInstance)
  988. return false;
  989. // Cast the ray against the currently visible detail
  990. RayInfo localInfo;
  991. if (info && info->generateTexCoord)
  992. localInfo.generateTexCoord = true;
  993. bool res = mShapeInstance->castRayOpcode(mShapeInstance->getCurrentDetail(), start, end, &localInfo);
  994. if (res)
  995. {
  996. *info = localInfo;
  997. info->object = this;
  998. return true;
  999. }
  1000. return false;
  1001. }
  1002. bool TSStatic::buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF&)
  1003. {
  1004. if (!mShapeInstance)
  1005. return false;
  1006. // This is safe to set even if we're not outputing
  1007. polyList->setTransform(&mObjToWorld, mObjScale);
  1008. polyList->setObject(this);
  1009. if (context == PLC_Export)
  1010. {
  1011. // Use highest detail level
  1012. S32 dl = 0;
  1013. // Try to call on the client so we can export materials
  1014. if (isServerObject() && getClientObject())
  1015. dynamic_cast<TSStatic*>(getClientObject())->mShapeInstance->buildPolyList(polyList, dl);
  1016. else
  1017. mShapeInstance->buildPolyList(polyList, dl);
  1018. }
  1019. else if (context == PLC_Selection)
  1020. {
  1021. // Use the last rendered detail level
  1022. S32 dl = mShapeInstance->getCurrentDetail();
  1023. mShapeInstance->buildPolyListOpcode(dl, polyList, box);
  1024. }
  1025. else
  1026. {
  1027. // Figure out the mesh type we're looking for.
  1028. MeshType meshType = (context == PLC_Decal) ? mDecalType : mCollisionType;
  1029. if (meshType == None)
  1030. return false;
  1031. else if (meshType == Bounds)
  1032. polyList->addBox(mObjBox);
  1033. else if (meshType == VisibleMesh)
  1034. mShapeInstance->buildPolyList(polyList, 0);
  1035. else if (context == PLC_Decal && mDecalDetailsPtr != 0)
  1036. {
  1037. for (U32 i = 0; i < mDecalDetailsPtr->size(); i++)
  1038. mShapeInstance->buildPolyListOpcode((*mDecalDetailsPtr)[i], polyList, box);
  1039. }
  1040. else
  1041. {
  1042. // Everything else is done from the collision meshes
  1043. // which may be built from either the visual mesh or
  1044. // special collision geometry.
  1045. for (U32 i = 0; i < mCollisionDetails.size(); i++)
  1046. mShapeInstance->buildPolyListOpcode(mCollisionDetails[i], polyList, box);
  1047. }
  1048. }
  1049. return true;
  1050. }
  1051. bool TSStatic::buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F& box, const SphereF&)
  1052. {
  1053. if (!mShapeInstance)
  1054. return false;
  1055. if (mCollisionType == Bounds)
  1056. {
  1057. ColladaUtils::ExportData::colMesh* colMesh;
  1058. exportData->colMeshes.increment();
  1059. colMesh = &exportData->colMeshes.last();
  1060. colMesh->mesh.setTransform(&mObjToWorld, mObjScale);
  1061. colMesh->mesh.setObject(this);
  1062. colMesh->mesh.addBox(mObjBox);
  1063. colMesh->colMeshName = String::ToString("ColBox%d-1", exportData->colMeshes.size());
  1064. }
  1065. else if (mCollisionType == VisibleMesh)
  1066. {
  1067. ColladaUtils::ExportData::colMesh* colMesh;
  1068. exportData->colMeshes.increment();
  1069. colMesh = &exportData->colMeshes.last();
  1070. colMesh->mesh.setTransform(&mObjToWorld, mObjScale);
  1071. colMesh->mesh.setObject(this);
  1072. mShapeInstance->buildPolyList(&colMesh->mesh, 0);
  1073. colMesh->colMeshName = String::ToString("ColMesh%d-1", exportData->colMeshes.size());
  1074. }
  1075. else if (mCollisionType == CollisionMesh)
  1076. {
  1077. // Everything else is done from the collision meshes
  1078. // which may be built from either the visual mesh or
  1079. // special collision geometry.
  1080. for (U32 i = 0; i < mCollisionDetails.size(); i++)
  1081. {
  1082. ColladaUtils::ExportData::colMesh* colMesh;
  1083. exportData->colMeshes.increment();
  1084. colMesh = &exportData->colMeshes.last();
  1085. colMesh->mesh.setTransform(&mObjToWorld, mObjScale);
  1086. colMesh->mesh.setObject(this);
  1087. mShapeInstance->buildPolyListOpcode(mCollisionDetails[i], &colMesh->mesh, box);
  1088. colMesh->colMeshName = String::ToString("ColMesh%d-1", exportData->colMeshes.size());
  1089. }
  1090. }
  1091. //Next, process the LOD levels and materials.
  1092. if (isServerObject() && getClientObject())
  1093. {
  1094. TSStatic* clientShape = dynamic_cast<TSStatic*>(getClientObject());
  1095. exportData->meshData.increment();
  1096. //Prep a meshData for this shape in particular
  1097. ColladaUtils::ExportData::meshLODData* meshData = &exportData->meshData.last();
  1098. //Fill out the info we'll need later to actually append our mesh data for the detail levels during the processing phase
  1099. meshData->shapeInst = clientShape->mShapeInstance;
  1100. meshData->originatingObject = this;
  1101. meshData->meshTransform = mObjToWorld;
  1102. meshData->scale = mObjScale;
  1103. //Iterate over all our detail levels
  1104. for (U32 i = 0; i < clientShape->mShapeInstance->getNumDetails(); i++)
  1105. {
  1106. TSShape::Detail detail = clientShape->mShapeInstance->getShape()->details[i];
  1107. String detailName = String::ToLower(clientShape->mShapeInstance->getShape()->getName(detail.nameIndex));
  1108. //Skip it if it's a collision or line of sight element
  1109. if (detailName.startsWith("col") || detailName.startsWith("los"))
  1110. continue;
  1111. meshData->meshDetailLevels.increment();
  1112. ColladaUtils::ExportData::detailLevel* curDetail = &meshData->meshDetailLevels.last();
  1113. //Make sure we denote the size this detail level has
  1114. curDetail->size = getNextPow2(detail.size);
  1115. }
  1116. }
  1117. return true;
  1118. }
  1119. void TSStatic::buildConvex(const Box3F& box, Convex* convex)
  1120. {
  1121. if (mCollisionType == None)
  1122. return;
  1123. if (mShapeInstance == NULL)
  1124. return;
  1125. // These should really come out of a pool
  1126. mConvexList->collectGarbage();
  1127. if (mCollisionType == Bounds)
  1128. {
  1129. // Just return a box convex for the entire shape...
  1130. Convex* cc = 0;
  1131. CollisionWorkingList& wl = convex->getWorkingList();
  1132. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext)
  1133. {
  1134. if (itr->mConvex->getType() == BoxConvexType &&
  1135. itr->mConvex->getObject() == this)
  1136. {
  1137. cc = itr->mConvex;
  1138. break;
  1139. }
  1140. }
  1141. if (cc)
  1142. return;
  1143. // Create a new convex.
  1144. BoxConvex* cp = new BoxConvex;
  1145. mConvexList->registerObject(cp);
  1146. convex->addToWorkingList(cp);
  1147. cp->init(this);
  1148. mObjBox.getCenter(&cp->mCenter);
  1149. cp->mSize.x = mObjBox.len_x() / 2.0f;
  1150. cp->mSize.y = mObjBox.len_y() / 2.0f;
  1151. cp->mSize.z = mObjBox.len_z() / 2.0f;
  1152. }
  1153. else // CollisionMesh || VisibleMesh
  1154. {
  1155. TSStaticPolysoupConvex::smCurObject = this;
  1156. for (U32 i = 0; i < mCollisionDetails.size(); i++)
  1157. mShapeInstance->buildConvexOpcode(mObjToWorld, mObjScale, mCollisionDetails[i], box, convex, mConvexList);
  1158. TSStaticPolysoupConvex::smCurObject = NULL;
  1159. }
  1160. }
  1161. SceneObject* TSStaticPolysoupConvex::smCurObject = NULL;
  1162. TSStaticPolysoupConvex::TSStaticPolysoupConvex()
  1163. : box(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f),
  1164. normal(0.0f, 0.0f, 0.0f, 0.0f),
  1165. idx(0),
  1166. mesh(NULL)
  1167. {
  1168. mType = TSPolysoupConvexType;
  1169. for (U32 i = 0; i < 4; ++i)
  1170. {
  1171. verts[i].set(0.0f, 0.0f, 0.0f);
  1172. }
  1173. }
  1174. Point3F TSStaticPolysoupConvex::support(const VectorF& vec) const
  1175. {
  1176. F32 bestDot = mDot(verts[0], vec);
  1177. const Point3F* bestP = &verts[0];
  1178. for (S32 i = 1; i < 4; i++)
  1179. {
  1180. F32 newD = mDot(verts[i], vec);
  1181. if (newD > bestDot)
  1182. {
  1183. bestDot = newD;
  1184. bestP = &verts[i];
  1185. }
  1186. }
  1187. return *bestP;
  1188. }
  1189. Box3F TSStaticPolysoupConvex::getBoundingBox() const
  1190. {
  1191. Box3F wbox = box;
  1192. wbox.minExtents.convolve(mObject->getScale());
  1193. wbox.maxExtents.convolve(mObject->getScale());
  1194. mObject->getTransform().mul(wbox);
  1195. return wbox;
  1196. }
  1197. Box3F TSStaticPolysoupConvex::getBoundingBox(const MatrixF& mat, const Point3F& scale) const
  1198. {
  1199. AssertISV(false, "TSStaticPolysoupConvex::getBoundingBox(m,p) - Not implemented. -- XEA");
  1200. return box;
  1201. }
  1202. void TSStaticPolysoupConvex::getPolyList(AbstractPolyList* list)
  1203. {
  1204. // Transform the list into object space and set the pointer to the object
  1205. MatrixF i(mObject->getTransform());
  1206. Point3F iS(mObject->getScale());
  1207. list->setTransform(&i, iS);
  1208. list->setObject(mObject);
  1209. // Add only the original collision triangle
  1210. S32 base = list->addPoint(verts[0]);
  1211. list->addPoint(verts[2]);
  1212. list->addPoint(verts[1]);
  1213. list->begin(0, (U32)idx ^ (uintptr_t)mesh);
  1214. list->vertex(base + 2);
  1215. list->vertex(base + 1);
  1216. list->vertex(base + 0);
  1217. list->plane(base + 0, base + 1, base + 2);
  1218. list->end();
  1219. }
  1220. void TSStaticPolysoupConvex::getFeatures(const MatrixF& mat, const VectorF& n, ConvexFeature* cf)
  1221. {
  1222. cf->material = 0;
  1223. cf->mObject = mObject;
  1224. // For a tetrahedron this is pretty easy... first
  1225. // convert everything into world space.
  1226. Point3F tverts[4];
  1227. mat.mulP(verts[0], &tverts[0]);
  1228. mat.mulP(verts[1], &tverts[1]);
  1229. mat.mulP(verts[2], &tverts[2]);
  1230. mat.mulP(verts[3], &tverts[3]);
  1231. // points...
  1232. S32 firstVert = cf->mVertexList.size();
  1233. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[0];
  1234. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[1];
  1235. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[2];
  1236. cf->mVertexList.increment(); cf->mVertexList.last() = tverts[3];
  1237. // edges...
  1238. cf->mEdgeList.increment();
  1239. cf->mEdgeList.last().vertex[0] = firstVert + 0;
  1240. cf->mEdgeList.last().vertex[1] = firstVert + 1;
  1241. cf->mEdgeList.increment();
  1242. cf->mEdgeList.last().vertex[0] = firstVert + 1;
  1243. cf->mEdgeList.last().vertex[1] = firstVert + 2;
  1244. cf->mEdgeList.increment();
  1245. cf->mEdgeList.last().vertex[0] = firstVert + 2;
  1246. cf->mEdgeList.last().vertex[1] = firstVert + 0;
  1247. cf->mEdgeList.increment();
  1248. cf->mEdgeList.last().vertex[0] = firstVert + 3;
  1249. cf->mEdgeList.last().vertex[1] = firstVert + 0;
  1250. cf->mEdgeList.increment();
  1251. cf->mEdgeList.last().vertex[0] = firstVert + 3;
  1252. cf->mEdgeList.last().vertex[1] = firstVert + 1;
  1253. cf->mEdgeList.increment();
  1254. cf->mEdgeList.last().vertex[0] = firstVert + 3;
  1255. cf->mEdgeList.last().vertex[1] = firstVert + 2;
  1256. // triangles...
  1257. cf->mFaceList.increment();
  1258. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[0]);
  1259. cf->mFaceList.last().vertex[0] = firstVert + 2;
  1260. cf->mFaceList.last().vertex[1] = firstVert + 1;
  1261. cf->mFaceList.last().vertex[2] = firstVert + 0;
  1262. cf->mFaceList.increment();
  1263. cf->mFaceList.last().normal = PlaneF(tverts[1], tverts[0], tverts[3]);
  1264. cf->mFaceList.last().vertex[0] = firstVert + 1;
  1265. cf->mFaceList.last().vertex[1] = firstVert + 0;
  1266. cf->mFaceList.last().vertex[2] = firstVert + 3;
  1267. cf->mFaceList.increment();
  1268. cf->mFaceList.last().normal = PlaneF(tverts[2], tverts[1], tverts[3]);
  1269. cf->mFaceList.last().vertex[0] = firstVert + 2;
  1270. cf->mFaceList.last().vertex[1] = firstVert + 1;
  1271. cf->mFaceList.last().vertex[2] = firstVert + 3;
  1272. cf->mFaceList.increment();
  1273. cf->mFaceList.last().normal = PlaneF(tverts[0], tverts[2], tverts[3]);
  1274. cf->mFaceList.last().vertex[0] = firstVert + 0;
  1275. cf->mFaceList.last().vertex[1] = firstVert + 2;
  1276. cf->mFaceList.last().vertex[2] = firstVert + 3;
  1277. // All done!
  1278. }
  1279. void TSStatic::onMount(SceneObject* obj, S32 node)
  1280. {
  1281. Parent::onMount(obj, node);
  1282. _updateShouldTick();
  1283. }
  1284. void TSStatic::onUnmount(SceneObject* obj, S32 node)
  1285. {
  1286. Parent::onUnmount(obj, node);
  1287. setMaskBits(TransformMask);
  1288. _updateShouldTick();
  1289. }
  1290. U32 TSStatic::getNumDetails()
  1291. {
  1292. if (isServerObject() && getClientObject())
  1293. {
  1294. TSStatic* clientShape = dynamic_cast<TSStatic*>(getClientObject());
  1295. return clientShape->mShapeInstance->getNumDetails();
  1296. }
  1297. return 0;
  1298. };
  1299. void TSStatic::updateMaterials()
  1300. {
  1301. if (mChangingMaterials.empty() || !mShapeInstance)
  1302. return;
  1303. TSMaterialList* pMatList = mShapeInstance->getMaterialList();
  1304. String path;
  1305. if (mShapeAsset->isAssetValid())
  1306. path = mShapeAsset->getShapeFileName();
  1307. else
  1308. path = mShapeName;
  1309. pMatList->setTextureLookupPath(path);
  1310. bool found = false;
  1311. const Vector<String>& materialNames = pMatList->getMaterialNameList();
  1312. for (S32 i = 0; i < materialNames.size(); i++)
  1313. {
  1314. if (found)
  1315. break;
  1316. for (U32 m = 0; m < mChangingMaterials.size(); m++)
  1317. {
  1318. if (mChangingMaterials[m].slot == i)
  1319. {
  1320. //Fetch the actual material asset
  1321. pMatList->renameMaterial(i, mChangingMaterials[m].matAsset->getMaterialDefinitionName());
  1322. found = true;
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. // Initialize the material instances
  1328. mShapeInstance->initMaterialList();
  1329. }
  1330. void TSStatic::getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList)
  1331. {
  1332. if(!mShapeAsset.isNull() && mShapeAsset->getAssetId() != ShapeAsset::smNoShapeAssetFallback)
  1333. usedAssetsList->push_back_unique(mShapeAsset->getAssetId());
  1334. }
  1335. //------------------------------------------------------------------------
  1336. //These functions are duplicated in tsStatic and shapeBase.
  1337. //They each function a little differently; but achieve the same purpose of gathering
  1338. //target names/counts without polluting simObject.
  1339. #ifdef TORQUE_TOOLS
  1340. void TSStatic::onInspect(GuiInspector* inspector)
  1341. {
  1342. //if (mShapeAsset == nullptr)
  1343. return;
  1344. //Put the GameObject group before everything that'd be gameobject-effecting, for orginazational purposes
  1345. GuiInspectorGroup* materialGroup = inspector->findExistentGroup(StringTable->insert("Materials"));
  1346. if (!materialGroup)
  1347. return;
  1348. GuiControl* stack = dynamic_cast<GuiControl*>(materialGroup->findObjectByInternalName(StringTable->insert("Stack")));
  1349. //Do this on both the server and client
  1350. TSMaterialList* matList = mShapeInstance->getMaterialList();
  1351. Vector<String> matListNames = matList->getMaterialNameList();
  1352. S32 materialCount = matListNames.size();
  1353. if (isServerObject())
  1354. {
  1355. //next, get a listing of our materials in the shape, and build our field list for them
  1356. char matFieldName[128];
  1357. for (U32 i = 0; i < materialCount; i++)
  1358. {
  1359. StringTableEntry materialname = StringTable->insert(mShapeInstance->getMaterialList()->getMaterialName(i).c_str());
  1360. AssetPtr<MaterialAsset> matAsset;
  1361. if(MaterialAsset::getAssetByMaterialName(materialname, &matAsset) == MaterialAsset::Ok)
  1362. {
  1363. dSprintf(matFieldName, 128, "MaterialSlot%d", i);
  1364. GuiInspectorField* fieldGui = materialGroup->constructField(TypeMaterialAssetPtr);
  1365. fieldGui->init(inspector, materialGroup);
  1366. fieldGui->setSpecialEditField(true);
  1367. fieldGui->setTargetObject(this);
  1368. StringTableEntry fldnm = StringTable->insert(matFieldName);
  1369. fieldGui->setSpecialEditVariableName(fldnm);
  1370. fieldGui->setInspectorField(NULL, fldnm);
  1371. fieldGui->setDocs("");
  1372. if (fieldGui->registerObject())
  1373. {
  1374. StringTableEntry fieldValue = matAsset->getAssetId();
  1375. GuiInspectorTypeMaterialAssetPtr* matFieldPtr = dynamic_cast<GuiInspectorTypeMaterialAssetPtr*>(fieldGui);
  1376. matFieldPtr->setPreviewImage(fieldValue);
  1377. //Check if we'd already actually changed it, and display the modified value
  1378. for (U32 c = 0; c < mChangingMaterials.size(); c++)
  1379. {
  1380. if (mChangingMaterials[c].slot == i)
  1381. {
  1382. fieldValue = StringTable->insert(mChangingMaterials[i].assetId.c_str());
  1383. break;
  1384. }
  1385. }
  1386. fieldGui->setValue(fieldValue);
  1387. stack->addObject(fieldGui);
  1388. }
  1389. else
  1390. {
  1391. SAFE_DELETE(fieldGui);
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. #endif
  1398. DefineEngineMethod(TSStatic, getTargetName, const char*, (S32 index), (0),
  1399. "Get the name of the indexed shape material.\n"
  1400. "@param index index of the material to get (valid range is 0 - getTargetCount()-1).\n"
  1401. "@return the name of the indexed material.\n"
  1402. "@see getTargetCount()\n")
  1403. {
  1404. TSStatic* obj = dynamic_cast<TSStatic*> (object);
  1405. if (obj)
  1406. {
  1407. // Try to use the client object (so we get the reskinned targets in the Material Editor)
  1408. if ((TSStatic*)obj->getClientObject())
  1409. obj = (TSStatic*)obj->getClientObject();
  1410. return obj->getShapeInstance()->getTargetName(index);
  1411. }
  1412. return "";
  1413. }
  1414. DefineEngineMethod(TSStatic, getTargetCount, S32, (), ,
  1415. "Get the number of materials in the shape.\n"
  1416. "@return the number of materials in the shape.\n"
  1417. "@see getTargetName()\n")
  1418. {
  1419. TSStatic* obj = dynamic_cast<TSStatic*> (object);
  1420. if (obj)
  1421. {
  1422. // Try to use the client object (so we get the reskinned targets in the Material Editor)
  1423. if ((TSStatic*)obj->getClientObject())
  1424. obj = (TSStatic*)obj->getClientObject();
  1425. return obj->getShapeInstance()->getTargetCount();
  1426. }
  1427. return -1;
  1428. }
  1429. // This method is able to change materials per map to with others. The material that is being replaced is being mapped to
  1430. // unmapped_mat as a part of this transition
  1431. DefineEngineMethod(TSStatic, changeMaterial, void, (const char* mapTo, Material* oldMat, Material* newMat), ("", nullAsType<Material*>(), nullAsType<Material*>()),
  1432. "@brief Change one of the materials on the shape.\n\n"
  1433. "This method changes materials per mapTo with others. The material that "
  1434. "is being replaced is mapped to unmapped_mat as a part of this transition.\n"
  1435. "@note Warning, right now this only sort of works. It doesn't do a live "
  1436. "update like it should.\n"
  1437. "@param mapTo the name of the material target to remap (from getTargetName)\n"
  1438. "@param oldMat the old Material that was mapped \n"
  1439. "@param newMat the new Material to map\n\n"
  1440. "@tsexample\n"
  1441. "// remap the first material in the shape\n"
  1442. "%mapTo = %obj.getTargetName( 0 );\n"
  1443. "%obj.changeMaterial( %mapTo, 0, MyMaterial );\n"
  1444. "@endtsexample\n")
  1445. {
  1446. // if no valid new material, theres no reason for doing this
  1447. if (!newMat)
  1448. {
  1449. Con::errorf("TSShape::changeMaterial failed: New material does not exist!");
  1450. return;
  1451. }
  1452. TSMaterialList* shapeMaterialList = object->getShapeResource()->materialList;
  1453. // Check the mapTo name exists for this shape
  1454. S32 matIndex = shapeMaterialList->getMaterialNameList().find_next(String(mapTo));
  1455. if (matIndex < 0)
  1456. {
  1457. Con::errorf("TSShape::changeMaterial failed: Invalid mapTo name '%s'", mapTo);
  1458. return;
  1459. }
  1460. // Lets remap the old material off, so as to let room for our current material room to claim its spot
  1461. if (oldMat)
  1462. oldMat->mMapTo = String("unmapped_mat");
  1463. newMat->mMapTo = mapTo;
  1464. // Map the material by name in the matmgr
  1465. MATMGR->mapMaterial(mapTo, newMat->getName());
  1466. // Replace instances with the new material being traded in. Lets make sure that we only
  1467. // target the specific targets per inst, this is actually doing more than we thought
  1468. delete shapeMaterialList->mMatInstList[matIndex];
  1469. shapeMaterialList->mMatInstList[matIndex] = newMat->createMatInstance();
  1470. // Finish up preparing the material instances for rendering
  1471. const GFXVertexFormat* flags = getGFXVertexFormat<GFXVertexPNTTB>();
  1472. FeatureSet features = MATMGR->getDefaultFeatures();
  1473. shapeMaterialList->getMaterialInst(matIndex)->init(features, flags);
  1474. }
  1475. DefineEngineMethod(TSStatic, getModelFile, const char*, (), ,
  1476. "@brief Get the model filename used by this shape.\n\n"
  1477. "@return the shape filename\n\n"
  1478. "@tsexample\n"
  1479. "// Acquire the model filename used on this shape.\n"
  1480. "%modelFilename = %obj.getModelFile();\n"
  1481. "@endtsexample\n"
  1482. )
  1483. {
  1484. return object->getShape();
  1485. }
  1486. void TSStatic::set_special_typing()
  1487. {
  1488. if (mCollisionType == VisibleMesh || mCollisionType == CollisionMesh)
  1489. mTypeMask |= InteriorLikeObjectType;
  1490. else
  1491. mTypeMask &= ~InteriorLikeObjectType;
  1492. }
  1493. void TSStatic::onStaticModified(const char* slotName, const char* newValue)
  1494. {
  1495. #ifdef TORQUE_AFX_ENABLED
  1496. if (slotName == afxZodiacData::GradientRangeSlot)
  1497. {
  1498. afxZodiacData::convertGradientRangeFromDegrees(mGradientRange, mGradientRangeUser);
  1499. return;
  1500. }
  1501. #endif
  1502. set_special_typing();
  1503. }
  1504. void TSStatic::setSelectionFlags(U8 flags)
  1505. {
  1506. Parent::setSelectionFlags(flags);
  1507. if (!mShapeInstance || !isClientObject())
  1508. return;
  1509. if (!mShapeInstance->ownMaterialList())
  1510. return;
  1511. TSMaterialList* pMatList = mShapeInstance->getMaterialList();
  1512. for (S32 j = 0; j < pMatList->size(); j++)
  1513. {
  1514. BaseMatInstance* bmi = pMatList->getMaterialInst(j);
  1515. bmi->setSelectionHighlighting(needsSelectionHighlighting());
  1516. }
  1517. }
  1518. bool TSStatic::hasNode(const char* nodeName)
  1519. {
  1520. S32 nodeIDx = getShapeResource()->findNode(nodeName);
  1521. return nodeIDx >= 0;
  1522. }
  1523. void TSStatic::getNodeTransform(const char *nodeName, const MatrixF &xfm, MatrixF *outMat)
  1524. {
  1525. S32 nodeIDx = getShapeResource()->findNode(nodeName);
  1526. MatrixF nodeTransform(xfm);
  1527. const Point3F& scale = getScale();
  1528. if (nodeIDx != -1)
  1529. {
  1530. nodeTransform = mShapeInstance->mNodeTransforms[nodeIDx];
  1531. nodeTransform.mul(xfm);
  1532. }
  1533. // The position of the mount point needs to be scaled.
  1534. Point3F position = nodeTransform.getPosition();
  1535. position.convolve(scale);
  1536. nodeTransform.setPosition(position);
  1537. // Also we would like the object to be scaled to the model.
  1538. outMat->mul(mObjToWorld, nodeTransform);
  1539. return;
  1540. }
  1541. DefineEngineMethod(TSStatic, hasNode, bool, (const char* nodeName), ,
  1542. "@brief Get if this model has this node name.\n\n")
  1543. {
  1544. return object->hasNode(nodeName);
  1545. }
  1546. DefineEngineMethod(TSStatic, getNodeTransform, TransformF, (const char *nodeName), ,
  1547. "@brief Get the world transform of the specified node name.\n\n"
  1548. "@param node name query\n"
  1549. "@return the mount transform\n\n")
  1550. {
  1551. MatrixF xf(true);
  1552. object->getNodeTransform(nodeName, MatrixF::Identity, &xf);
  1553. return xf;
  1554. }