DrawableProxy2D.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "Drawable2D.h"
  25. #include "DrawableProxy2D.h"
  26. #include "Material.h"
  27. #include "Scene.h"
  28. #include "Technique.h"
  29. #include "Texture2D.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. DrawableProxy2D::DrawableProxy2D(Context* context) :
  34. Component(context)
  35. {
  36. }
  37. DrawableProxy2D::~DrawableProxy2D()
  38. {
  39. }
  40. void DrawableProxy2D::RegisterObject(Context* context)
  41. {
  42. context->RegisterFactory<DrawableProxy2D>();
  43. }
  44. void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
  45. {
  46. if (!drawable)
  47. return;
  48. WeakPtr<Drawable2D> drawablePtr(drawable);
  49. if (drawables_.Contains(drawablePtr))
  50. return;
  51. drawables_.Push(drawablePtr);
  52. }
  53. void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
  54. {
  55. if (!drawable)
  56. return;
  57. WeakPtr<Drawable2D> drawablePtr(drawable);
  58. drawables_.Remove(drawablePtr);
  59. }
  60. Material* DrawableProxy2D::GetMaterial(Drawable2D* drawable)
  61. {
  62. if (!drawable)
  63. return 0;
  64. Texture2D* texture = drawable->GetTexture();
  65. if (!texture)
  66. return 0;
  67. BlendMode blendMode = drawable->GetBlendMode();
  68. HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > >::Iterator t = materials_.Find(texture);
  69. if (t == materials_.End())
  70. {
  71. Material* material(CreateMaterial(texture, blendMode));
  72. materials_[texture][blendMode] = material;
  73. return material;
  74. }
  75. HashMap<int, SharedPtr<Material> >& materials = t->second_;
  76. HashMap<int, SharedPtr<Material> >::Iterator b = materials.Find(blendMode);
  77. if (b != materials.End())
  78. return b->second_;
  79. Material* material(CreateMaterial(texture, blendMode));
  80. materials[blendMode] = material;
  81. return material;
  82. }
  83. Material* DrawableProxy2D::CreateMaterial(Texture2D* texture, BlendMode blendMode) const
  84. {
  85. Material* material = new Material(context_);
  86. Technique* tech = new Technique(context_);
  87. Pass* pass = tech->CreatePass(PASS_ALPHA);
  88. pass->SetBlendMode(blendMode);
  89. pass->SetVertexShader("Basic");
  90. pass->SetVertexShaderDefines("DIFFMAP VERTEXCOLOR");
  91. pass->SetPixelShader("Basic");
  92. pass->SetPixelShaderDefines("DIFFMAP VERTEXCOLOR");
  93. pass->SetDepthWrite(false);
  94. material->SetTechnique(0, tech);
  95. material->SetCullMode(CULL_NONE);
  96. material->SetTexture(TU_DIFFUSE, texture);
  97. return material;
  98. }
  99. }