tsStatic.cpp 57 KB

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