Material.cpp 22 KB

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