Material.cpp 22 KB

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