Browse Source

Make sure blend mode of existing Urho2D materials is not overwritten. Closes #742.

Lasse Öörni 10 years ago
parent
commit
afd714ac00
2 changed files with 18 additions and 5 deletions
  1. 14 4
      Source/Urho3D/Urho2D/Renderer2D.cpp
  2. 4 1
      Source/Urho3D/Urho2D/Renderer2D.h

+ 14 - 4
Source/Urho3D/Urho2D/Renderer2D.cpp

@@ -70,6 +70,7 @@ Renderer2D::Renderer2D(Context* context) :
     pass->SetVertexShader("Urho2D");
     pass->SetPixelShader("Urho2D");
     pass->SetDepthWrite(false);
+    cachedTechniques_[BLEND_REPLACE] = tech;
 
     material_->SetTechnique(0, tech);
     material_->SetCullMode(CULL_NONE);
@@ -238,9 +239,6 @@ void Renderer2D::RemoveDrawable(Drawable2D* drawable)
 
 Material* Renderer2D::GetMaterial(Texture2D* texture, BlendMode blendMode)
 {
-    if (!material_)
-        return 0;
-
     if (!texture)
         return material_;
 
@@ -283,9 +281,21 @@ SharedPtr<Material> Renderer2D::CreateMaterial(Texture2D* texture, BlendMode ble
 {
     SharedPtr<Material> newMaterial = material_->Clone();
 
+    HashMap<int, SharedPtr<Technique> >::Iterator techIt = cachedTechniques_.Find((int)blendMode);
+    if (techIt == cachedTechniques_.End())
+    {
+        SharedPtr<Technique> tech(new Technique(context_));
+        Pass* pass = tech->CreatePass("alpha");
+        pass->SetVertexShader("Urho2D");
+        pass->SetPixelShader("Urho2D");
+        pass->SetDepthWrite(false);
+        pass->SetBlendMode(blendMode);
+        techIt = cachedTechniques_.Insert(MakePair((int)blendMode, tech));
+    }
+
+    newMaterial->SetTechnique(0, techIt->second_.Get());
     newMaterial->SetName(texture->GetName() + "_" + blendModeNames[blendMode]);
     newMaterial->SetTexture(TU_DIFFUSE, texture);
-    newMaterial->GetTechnique(0)->GetPass("alpha")->SetBlendMode(blendMode);
 
     return newMaterial;
 }

+ 4 - 1
Source/Urho3D/Urho2D/Renderer2D.h

@@ -30,6 +30,7 @@ namespace Urho3D
 class Drawable2D;
 class IndexBuffer;
 class Material;
+class Technique;
 class VertexBuffer;
 struct FrameInfo;
 struct SourceBatch2D;
@@ -60,7 +61,7 @@ struct ViewBatchInfo2D
     Vector<SharedPtr<Geometry> > geometries_;
 };
 
-/// 2D renderer components.
+/// 2D renderer component.
 class URHO3D_API Renderer2D : public Drawable
 {
     OBJECT(Renderer2D);
@@ -124,6 +125,8 @@ private:
     BoundingBox frustumBoundingBox_;
     /// Cached materials.
     HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
+    /// Cached techniques per blend mode.
+    HashMap<int, SharedPtr<Technique> > cachedTechniques_;
 };
 
 }