MaterialCache2D.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "Material.h"
  25. #include "MaterialCache2D.h"
  26. #include "Technique.h"
  27. #include "Texture2D.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. MaterialCache2D::MaterialCache2D(Context* context) :
  32. Component(context)
  33. {
  34. }
  35. MaterialCache2D::~MaterialCache2D()
  36. {
  37. }
  38. void MaterialCache2D::RegisterObject(Context* context)
  39. {
  40. context->RegisterFactory<MaterialCache2D>();
  41. }
  42. Material* MaterialCache2D::GetMaterial(Texture2D* texture, BlendMode blendMode)
  43. {
  44. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > >::Iterator t = materials_.Find(texture);
  45. if (t == materials_.End())
  46. {
  47. Material* material(CreateMaterial(texture, blendMode));
  48. materials_[texture][blendMode] = material;
  49. return material;
  50. }
  51. HashMap<int, SharedPtr<Material> >& materials = t->second_;
  52. HashMap<int, SharedPtr<Material> >::Iterator b = materials.Find(blendMode);
  53. if (b != materials.End())
  54. return b->second_;
  55. Material* material(CreateMaterial(texture, blendMode));
  56. materials[blendMode] = material;
  57. return material;
  58. }
  59. Material* MaterialCache2D::CreateMaterial(Texture2D* texture, BlendMode blendMode)
  60. {
  61. Material* material = new Material(context_);
  62. Technique* tech = new Technique(context_);
  63. Pass* pass = tech->CreatePass(PASS_ALPHA);
  64. pass->SetBlendMode(blendMode);
  65. pass->SetVertexShader("Basic");
  66. pass->SetVertexShaderDefines("DIFFMAP VERTEXCOLOR");
  67. pass->SetPixelShader("Basic");
  68. pass->SetPixelShaderDefines("DIFFMAP VERTEXCOLOR");
  69. pass->SetDepthWrite(false);
  70. material->SetTechnique(0, tech);
  71. material->SetCullMode(CULL_NONE);
  72. material->SetTexture(TU_DIFFUSE, texture);
  73. return material;
  74. }
  75. }