Material.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "Log.h"
  27. #include "Material.h"
  28. #include "Matrix3x4.h"
  29. #include "Profiler.h"
  30. #include "ResourceCache.h"
  31. #include "Technique.h"
  32. #include "Texture2D.h"
  33. #include "TextureCube.h"
  34. #include "ValueAnimation.h"
  35. #include "XMLFile.h"
  36. #include "DebugNew.h"
  37. namespace Urho3D
  38. {
  39. extern const char* wrapModeNames[];
  40. static const char* textureUnitNames[] =
  41. {
  42. "diffuse",
  43. "normal",
  44. "specular",
  45. "emissive",
  46. "environment",
  47. "lightramp",
  48. "lightshape",
  49. "shadowmap",
  50. "faceselect",
  51. "indirection",
  52. "depth",
  53. "light",
  54. "volume",
  55. 0
  56. };
  57. static const char* cullModeNames[] =
  58. {
  59. "none",
  60. "ccw",
  61. "cw",
  62. 0
  63. };
  64. TextureUnit ParseTextureUnitName(String name)
  65. {
  66. name = name.ToLower().Trimmed();
  67. TextureUnit unit = (TextureUnit)GetStringListIndex(name.CString(), textureUnitNames, MAX_TEXTURE_UNITS);
  68. if (unit == MAX_TEXTURE_UNITS)
  69. {
  70. // Check also for shorthand names
  71. if (name == "diff")
  72. unit = TU_DIFFUSE;
  73. else if (name == "albedo")
  74. unit = TU_DIFFUSE;
  75. else if (name == "norm")
  76. unit = TU_NORMAL;
  77. else if (name == "spec")
  78. unit = TU_SPECULAR;
  79. else if (name == "env")
  80. unit = TU_ENVIRONMENT;
  81. // Finally check for specifying the texture unit directly as a number
  82. else if (name.Length() < 3)
  83. unit = (TextureUnit)Clamp(ToInt(name), 0, MAX_TEXTURE_UNITS);
  84. }
  85. if (unit == MAX_TEXTURE_UNITS)
  86. LOGERROR("Unknown texture unit name " + name);
  87. return unit;
  88. }
  89. static TechniqueEntry noEntry;
  90. bool CompareTechniqueEntries(const TechniqueEntry& lhs, const TechniqueEntry& rhs)
  91. {
  92. if (lhs.lodDistance_ != rhs.lodDistance_)
  93. return lhs.lodDistance_ > rhs.lodDistance_;
  94. else
  95. return lhs.qualityLevel_ > rhs.qualityLevel_;
  96. }
  97. TechniqueEntry::TechniqueEntry() :
  98. qualityLevel_(0),
  99. lodDistance_(0.0f)
  100. {
  101. }
  102. TechniqueEntry::TechniqueEntry(Technique* tech, unsigned qualityLevel, float lodDistance) :
  103. technique_(tech),
  104. qualityLevel_(qualityLevel),
  105. lodDistance_(lodDistance)
  106. {
  107. }
  108. TechniqueEntry::~TechniqueEntry()
  109. {
  110. }
  111. ShaderParameterAnimationInfo::ShaderParameterAnimationInfo(Material* target, const String& name, ValueAnimation* attributeAnimation, WrapMode wrapMode, float speed) :
  112. ValueAnimationInfo(target, attributeAnimation, wrapMode, speed),
  113. name_(name)
  114. {
  115. }
  116. ShaderParameterAnimationInfo::ShaderParameterAnimationInfo(const ShaderParameterAnimationInfo& other) :
  117. ValueAnimationInfo(other),
  118. name_(other.name_)
  119. {
  120. }
  121. ShaderParameterAnimationInfo::~ShaderParameterAnimationInfo()
  122. {
  123. }
  124. void ShaderParameterAnimationInfo::ApplyValue(const Variant& newValue)
  125. {
  126. static_cast<Material*>(target_.Get())->SetShaderParameter(name_, newValue);
  127. }
  128. Material::Material(Context* context) :
  129. Resource(context),
  130. auxViewFrameNumber_(0),
  131. occlusion_(true),
  132. specular_(false),
  133. animationFrameNumber_(0)
  134. {
  135. ResetToDefaults();
  136. }
  137. Material::~Material()
  138. {
  139. }
  140. void Material::RegisterObject(Context* context)
  141. {
  142. context->RegisterFactory<Material>();
  143. }
  144. bool Material::Load(Deserializer& source)
  145. {
  146. PROFILE(LoadMaterial);
  147. // In headless mode, do not actually load the material, just return success
  148. Graphics* graphics = GetSubsystem<Graphics>();
  149. if (!graphics)
  150. return true;
  151. SharedPtr<XMLFile> xml(new XMLFile(context_));
  152. if (!xml->Load(source))
  153. {
  154. ResetToDefaults();
  155. return false;
  156. }
  157. XMLElement rootElem = xml->GetRoot();
  158. return Load(rootElem);
  159. }
  160. bool Material::Save(Serializer& dest) const
  161. {
  162. SharedPtr<XMLFile> xml(new XMLFile(context_));
  163. XMLElement materialElem = xml->CreateRoot("material");
  164. Save(materialElem);
  165. return xml->Save(dest);
  166. }
  167. bool Material::Load(const XMLElement& source)
  168. {
  169. ResetToDefaults();
  170. if (source.IsNull())
  171. {
  172. LOGERROR("Can not load material from null XML element");
  173. return false;
  174. }
  175. ResourceCache* cache = GetSubsystem<ResourceCache>();
  176. XMLElement techniqueElem = source.GetChild("technique");
  177. techniques_.Clear();
  178. while (techniqueElem)
  179. {
  180. Technique* tech = cache->GetResource<Technique>(techniqueElem.GetAttribute("name"));
  181. if (tech)
  182. {
  183. TechniqueEntry newTechnique;
  184. newTechnique.technique_ = tech;
  185. if (techniqueElem.HasAttribute("quality"))
  186. newTechnique.qualityLevel_ = techniqueElem.GetInt("quality");
  187. if (techniqueElem.HasAttribute("loddistance"))
  188. newTechnique.lodDistance_ = techniqueElem.GetFloat("loddistance");
  189. techniques_.Push(newTechnique);
  190. }
  191. techniqueElem = techniqueElem.GetNext("technique");
  192. }
  193. SortTechniques();
  194. XMLElement textureElem = source.GetChild("texture");
  195. while (textureElem)
  196. {
  197. TextureUnit unit = TU_DIFFUSE;
  198. if (textureElem.HasAttribute("unit"))
  199. unit = ParseTextureUnitName(textureElem.GetAttribute("unit"));
  200. if (unit < MAX_MATERIAL_TEXTURE_UNITS)
  201. {
  202. String name = textureElem.GetAttribute("name");
  203. // Detect cube maps by file extension: they are defined by an XML file
  204. if (GetExtension(name) == ".xml")
  205. SetTexture(unit, cache->GetResource<TextureCube>(name));
  206. else
  207. SetTexture(unit, cache->GetResource<Texture2D>(name));
  208. }
  209. textureElem = textureElem.GetNext("texture");
  210. }
  211. XMLElement parameterElem = source.GetChild("parameter");
  212. while (parameterElem)
  213. {
  214. String name = parameterElem.GetAttribute("name");
  215. SetShaderParameter(name, ParseShaderParameterValue(parameterElem.GetAttribute("value")));
  216. parameterElem = parameterElem.GetNext("parameter");
  217. }
  218. XMLElement parameterAnimationElem = source.GetChild("parameteranimation");
  219. while (parameterAnimationElem)
  220. {
  221. String name = parameterAnimationElem.GetAttribute("name");
  222. SharedPtr<ValueAnimation> animation(new ValueAnimation(context_));
  223. if (!animation->LoadXML(parameterAnimationElem))
  224. {
  225. LOGERROR("Could not load parameter animation");
  226. return false;
  227. }
  228. String wrapModeString = parameterAnimationElem.GetAttribute("wrapmode");
  229. WrapMode wrapMode = WM_LOOP;
  230. for (int i = 0; i <= WM_CLAMP; ++i)
  231. {
  232. if (wrapModeString == wrapModeNames[i])
  233. {
  234. wrapMode = (WrapMode)i;
  235. break;
  236. }
  237. }
  238. float speed = parameterAnimationElem.GetFloat("speed");
  239. SetShaderParameterAnimation(name, animation, wrapMode, speed);
  240. parameterAnimationElem = parameterAnimationElem.GetNext("parameteranimation");
  241. }
  242. XMLElement cullElem = source.GetChild("cull");
  243. if (cullElem)
  244. SetCullMode((CullMode)GetStringListIndex(cullElem.GetAttribute("value").CString(), cullModeNames, CULL_CCW));
  245. XMLElement shadowCullElem = source.GetChild("shadowcull");
  246. if (shadowCullElem)
  247. SetShadowCullMode((CullMode)GetStringListIndex(shadowCullElem.GetAttribute("value").CString(), cullModeNames, CULL_CCW));
  248. XMLElement depthBiasElem = source.GetChild("depthbias");
  249. if (depthBiasElem)
  250. SetDepthBias(BiasParameters(depthBiasElem.GetFloat("constant"), depthBiasElem.GetFloat("slopescaled")));
  251. // Calculate memory use
  252. RefreshMemoryUse();
  253. CheckOcclusion();
  254. return true;
  255. }
  256. bool Material::Save(XMLElement& dest) const
  257. {
  258. if (dest.IsNull())
  259. {
  260. LOGERROR("Can not save material to null XML element");
  261. return false;
  262. }
  263. // Write techniques
  264. for (unsigned i = 0; i < techniques_.Size(); ++i)
  265. {
  266. const TechniqueEntry& entry = techniques_[i];
  267. if (!entry.technique_)
  268. continue;
  269. XMLElement techniqueElem = dest.CreateChild("technique");
  270. techniqueElem.SetString("name", entry.technique_->GetName());
  271. techniqueElem.SetInt("quality", entry.qualityLevel_);
  272. techniqueElem.SetFloat("loddistance", entry.lodDistance_);
  273. }
  274. // Write texture units
  275. for (unsigned j = 0; j < MAX_MATERIAL_TEXTURE_UNITS; ++j)
  276. {
  277. Texture* texture = GetTexture((TextureUnit)j);
  278. if (texture)
  279. {
  280. XMLElement textureElem = dest.CreateChild("texture");
  281. textureElem.SetString("unit", textureUnitNames[j]);
  282. textureElem.SetString("name", texture->GetName());
  283. }
  284. }
  285. // Write shader parameters
  286. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator j = shaderParameters_.Begin(); j != shaderParameters_.End(); ++j)
  287. {
  288. XMLElement parameterElem = dest.CreateChild("parameter");
  289. parameterElem.SetString("name", j->second_.name_);
  290. parameterElem.SetVectorVariant("value", j->second_.value_);
  291. }
  292. // Write shader parameter animations
  293. for (HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> >::ConstIterator j = shaderParameterAnimationInfos_.Begin(); j != shaderParameterAnimationInfos_.End(); ++j)
  294. {
  295. ShaderParameterAnimationInfo* info = j->second_;
  296. XMLElement parameterAnimationElem = dest.CreateChild("parameteranimation");
  297. parameterAnimationElem.SetString("name", info->GetName());
  298. if (!info->GetAnimation()->SaveXML(parameterAnimationElem))
  299. return false;
  300. parameterAnimationElem.SetAttribute("wrapmode", wrapModeNames[info->GetWrapMode()]);
  301. parameterAnimationElem.SetFloat("speed", info->GetSpeed());
  302. }
  303. // Write culling modes
  304. XMLElement cullElem = dest.CreateChild("cull");
  305. cullElem.SetString("value", cullModeNames[cullMode_]);
  306. XMLElement shadowCullElem = dest.CreateChild("shadowcull");
  307. shadowCullElem.SetString("value", cullModeNames[shadowCullMode_]);
  308. // Write depth bias
  309. XMLElement depthBiasElem = dest.CreateChild("depthbias");
  310. depthBiasElem.SetFloat("constant", depthBias_.constantBias_);
  311. depthBiasElem.SetFloat("slopescaled", depthBias_.slopeScaledBias_);
  312. return true;
  313. }
  314. void Material::SetNumTechniques(unsigned num)
  315. {
  316. if (!num)
  317. return;
  318. techniques_.Resize(num);
  319. RefreshMemoryUse();
  320. }
  321. void Material::SetTechnique(unsigned index, Technique* tech, unsigned qualityLevel, float lodDistance)
  322. {
  323. if (index >= techniques_.Size())
  324. return;
  325. techniques_[index] = TechniqueEntry(tech, qualityLevel, lodDistance);
  326. CheckOcclusion();
  327. }
  328. void Material::SetShaderParameter(const String& name, const Variant& value)
  329. {
  330. MaterialShaderParameter newParam;
  331. newParam.name_ = name;
  332. newParam.value_ = value;
  333. StringHash nameHash(name);
  334. shaderParameters_[nameHash] = newParam;
  335. if (nameHash == PSP_MATSPECCOLOR)
  336. {
  337. VariantType type = value.GetType();
  338. if (type == VAR_VECTOR3)
  339. {
  340. const Vector3& vec = value.GetVector3();
  341. specular_ = vec.x_ > 0.0f || vec.y_ > 0.0f || vec.z_ > 0.0f;
  342. }
  343. else if (type == VAR_VECTOR4)
  344. {
  345. const Vector4& vec = value.GetVector4();
  346. specular_ = vec.x_ > 0.0f || vec.y_ > 0.0f || vec.z_ > 0.0f;
  347. }
  348. }
  349. RefreshMemoryUse();
  350. }
  351. void Material::SetShaderParameterAnimation(const String& name, ValueAnimation* animation, WrapMode wrapMode, float speed)
  352. {
  353. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  354. if (animation)
  355. {
  356. if (info && info->GetAnimation() == animation)
  357. {
  358. info->SetWrapMode(wrapMode);
  359. info->SetSpeed(speed);
  360. return;
  361. }
  362. if (shaderParameters_.Find(name) == shaderParameters_.End())
  363. {
  364. LOGERROR(GetName() + " has no shader parameter: " + name);
  365. return;
  366. }
  367. StringHash nameHash(name);
  368. shaderParameterAnimationInfos_[nameHash] = new ShaderParameterAnimationInfo(this, name, animation, wrapMode, speed);
  369. }
  370. else
  371. {
  372. if (info)
  373. {
  374. StringHash nameHash(name);
  375. shaderParameterAnimationInfos_.Erase(nameHash);
  376. }
  377. }
  378. }
  379. void Material::SetShaderParameterAnimationWrapMode(const String& name, WrapMode wrapMode)
  380. {
  381. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  382. if (info)
  383. info->SetWrapMode(wrapMode);
  384. }
  385. void Material::SetShaderParameterAnimationSpeed(const String& name, float speed)
  386. {
  387. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  388. if (info)
  389. info->SetSpeed(speed);
  390. }
  391. void Material::SetTexture(TextureUnit unit, Texture* texture)
  392. {
  393. if (unit < MAX_MATERIAL_TEXTURE_UNITS)
  394. textures_[unit] = texture;
  395. }
  396. void Material::SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat)
  397. {
  398. Matrix3x4 transform(Matrix3x4::IDENTITY);
  399. transform.m00_ = repeat.x_;
  400. transform.m11_ = repeat.y_;
  401. transform.m03_ = -0.5f * transform.m00_ + 0.5f;
  402. transform.m13_ = -0.5f * transform.m11_ + 0.5f;
  403. Matrix3x4 rotationMatrix(Matrix3x4::IDENTITY);
  404. rotationMatrix.m00_ = Cos(rotation);
  405. rotationMatrix.m01_ = Sin(rotation);
  406. rotationMatrix.m10_ = -rotationMatrix.m01_;
  407. rotationMatrix.m11_ = rotationMatrix.m00_;
  408. rotationMatrix.m03_ = 0.5f - 0.5f * (rotationMatrix.m00_ + rotationMatrix.m01_);
  409. rotationMatrix.m13_ = 0.5f - 0.5f * (rotationMatrix.m10_ + rotationMatrix.m11_);
  410. transform = rotationMatrix * transform;
  411. Matrix3x4 offsetMatrix = Matrix3x4::IDENTITY;
  412. offsetMatrix.m03_ = offset.x_;
  413. offsetMatrix.m13_ = offset.y_;
  414. transform = offsetMatrix * transform;
  415. SetShaderParameter("UOffset", Vector4(transform.m00_, transform.m01_, transform.m02_, transform.m03_));
  416. SetShaderParameter("VOffset", Vector4(transform.m10_, transform.m11_, transform.m12_, transform.m13_));
  417. }
  418. void Material::SetUVTransform(const Vector2& offset, float rotation, float repeat)
  419. {
  420. SetUVTransform(offset, rotation, Vector2(repeat, repeat));
  421. }
  422. void Material::SetCullMode(CullMode mode)
  423. {
  424. cullMode_ = mode;
  425. }
  426. void Material::SetShadowCullMode(CullMode mode)
  427. {
  428. shadowCullMode_ = mode;
  429. }
  430. void Material::SetDepthBias(const BiasParameters& parameters)
  431. {
  432. depthBias_ = parameters;
  433. depthBias_.Validate();
  434. }
  435. void Material::RemoveShaderParameter(const String& name)
  436. {
  437. StringHash nameHash(name);
  438. shaderParameters_.Erase(nameHash);
  439. if (nameHash == PSP_MATSPECCOLOR)
  440. specular_ = false;
  441. RefreshMemoryUse();
  442. }
  443. void Material::ReleaseShaders()
  444. {
  445. for (unsigned i = 0; i < techniques_.Size(); ++i)
  446. {
  447. Technique* tech = techniques_[i].technique_;
  448. if (tech)
  449. tech->ReleaseShaders();
  450. }
  451. }
  452. SharedPtr<Material> Material::Clone(const String& cloneName) const
  453. {
  454. SharedPtr<Material> ret(new Material(context_));
  455. ret->SetName(cloneName);
  456. ret->techniques_ = techniques_;
  457. ret->shaderParameters_ = shaderParameters_;
  458. for (unsigned i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
  459. ret->textures_[i] = textures_[i];
  460. ret->occlusion_ = occlusion_;
  461. ret->specular_ = specular_;
  462. ret->cullMode_ = cullMode_;
  463. ret->shadowCullMode_ = shadowCullMode_;
  464. ret->RefreshMemoryUse();
  465. return ret;
  466. }
  467. void Material::SortTechniques()
  468. {
  469. Sort(techniques_.Begin(), techniques_.End(), CompareTechniqueEntries);
  470. }
  471. void Material::MarkForAuxView(unsigned frameNumber)
  472. {
  473. auxViewFrameNumber_ = frameNumber;
  474. }
  475. void Material::UpdateShaderParameterAnimations()
  476. {
  477. if (shaderParameterAnimationInfos_.Empty())
  478. return;
  479. Time* time = GetSubsystem<Time>();
  480. if (time->GetFrameNumber() == animationFrameNumber_)
  481. return;
  482. animationFrameNumber_ = time->GetFrameNumber();
  483. float timeStep = time->GetTimeStep();
  484. Vector<String> finishedNames;
  485. for (HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> >::ConstIterator i = shaderParameterAnimationInfos_.Begin(); i != shaderParameterAnimationInfos_.End(); ++i)
  486. {
  487. if (i->second_->Update(timeStep))
  488. finishedNames.Push(i->second_->GetName());
  489. }
  490. // Remove finished animation
  491. for (unsigned i = 0; i < finishedNames.Size(); ++i)
  492. SetShaderParameterAnimation(finishedNames[i], 0);
  493. }
  494. const TechniqueEntry& Material::GetTechniqueEntry(unsigned index) const
  495. {
  496. return index < techniques_.Size() ? techniques_[index] : noEntry;
  497. }
  498. Technique* Material::GetTechnique(unsigned index) const
  499. {
  500. return index < techniques_.Size() ? techniques_[index].technique_ : (Technique*)0;
  501. }
  502. Pass* Material::GetPass(unsigned index, StringHash passType) const
  503. {
  504. Technique* tech = index < techniques_.Size() ? techniques_[index].technique_ : (Technique*)0;
  505. return tech ? tech->GetPass(passType) : 0;
  506. }
  507. Texture* Material::GetTexture(TextureUnit unit) const
  508. {
  509. return unit < MAX_MATERIAL_TEXTURE_UNITS ? textures_[unit] : (Texture*)0;
  510. }
  511. const Variant& Material::GetShaderParameter(const String& name) const
  512. {
  513. HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = shaderParameters_.Find(name);
  514. return i != shaderParameters_.End() ? i->second_.value_ : Variant::EMPTY;
  515. }
  516. ValueAnimation* Material::GetShaderParameterAnimation(const String& name) const
  517. {
  518. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  519. return info == 0 ? 0 : info->GetAnimation();
  520. }
  521. WrapMode Material::GetShaderParameterAnimationWrapMode(const String& name) const
  522. {
  523. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  524. return info == 0 ? WM_LOOP : info->GetWrapMode();
  525. }
  526. float Material::GetShaderParameterAnimationSpeed(const String& name) const
  527. {
  528. ShaderParameterAnimationInfo* info = GetShaderParameterAnimationInfo(name);
  529. return info == 0 ? 0 : info->GetSpeed();
  530. }
  531. String Material::GetTextureUnitName(TextureUnit unit)
  532. {
  533. return textureUnitNames[unit];
  534. }
  535. Variant Material::ParseShaderParameterValue(const String& value)
  536. {
  537. String valueTrimmed = value.Trimmed();
  538. if (valueTrimmed.Length() && IsAlpha(valueTrimmed[0]))
  539. return Variant(ToBool(valueTrimmed));
  540. else
  541. return ToVectorVariant(valueTrimmed);
  542. }
  543. void Material::CheckOcclusion()
  544. {
  545. // Determine occlusion by checking the base pass of each technique
  546. occlusion_ = false;
  547. for (unsigned i = 0; i < techniques_.Size(); ++i)
  548. {
  549. Technique* tech = techniques_[i].technique_;
  550. if (tech)
  551. {
  552. Pass* pass = tech->GetPass(PASS_BASE);
  553. if (pass && pass->GetDepthWrite() && !pass->GetAlphaMask())
  554. occlusion_ = true;
  555. }
  556. }
  557. }
  558. void Material::ResetToDefaults()
  559. {
  560. SetNumTechniques(1);
  561. SetTechnique(0, GetSubsystem<ResourceCache>()->GetResource<Technique>("Techniques/NoTexture.xml"));
  562. for (unsigned i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
  563. textures_[i] = 0;
  564. shaderParameters_.Clear();
  565. SetShaderParameter("UOffset", Vector4(1.0f, 0.0f, 0.0f, 0.0f));
  566. SetShaderParameter("VOffset", Vector4(0.0f, 1.0f, 0.0f, 0.0f));
  567. SetShaderParameter("MatDiffColor", Vector4::ONE);
  568. SetShaderParameter("MatEmissiveColor", Vector3::ZERO);
  569. SetShaderParameter("MatEnvMapColor", Vector3::ONE);
  570. SetShaderParameter("MatSpecColor", Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  571. cullMode_ = CULL_CCW;
  572. shadowCullMode_ = CULL_CCW;
  573. depthBias_ = BiasParameters(0.0f, 0.0f);
  574. RefreshMemoryUse();
  575. }
  576. void Material::RefreshMemoryUse()
  577. {
  578. unsigned memoryUse = sizeof(Material);
  579. memoryUse += techniques_.Size() * sizeof(TechniqueEntry);
  580. memoryUse += MAX_MATERIAL_TEXTURE_UNITS * sizeof(SharedPtr<Texture>);
  581. memoryUse += shaderParameters_.Size() * sizeof(MaterialShaderParameter);
  582. SetMemoryUse(memoryUse);
  583. }
  584. ShaderParameterAnimationInfo* Material::GetShaderParameterAnimationInfo(const String& name) const
  585. {
  586. StringHash nameHash(name);
  587. HashMap<StringHash, SharedPtr<ShaderParameterAnimationInfo> >::ConstIterator i = shaderParameterAnimationInfos_.Find(nameHash);
  588. if (i == shaderParameterAnimationInfos_.End())
  589. return 0;
  590. return i->second_;
  591. }
  592. }