MaterialCache2D.cpp 3.1 KB

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