Browse Source

Add DrawableProxy2D (Not finish, currently on cache material).[ci skip]

aster2013 11 years ago
parent
commit
82c0cd6ae2

+ 21 - 44
Source/Engine/Urho2D/Drawable2D.cpp

@@ -24,10 +24,13 @@
 #include "Camera.h"
 #include "Camera.h"
 #include "Context.h"
 #include "Context.h"
 #include "Drawable2D.h"
 #include "Drawable2D.h"
+#include "DrawableProxy2D.h"
 #include "Geometry.h"
 #include "Geometry.h"
+#include "Log.h"
 #include "Material.h"
 #include "Material.h"
 #include "Node.h"
 #include "Node.h"
 #include "ResourceCache.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "Sprite2D.h"
 #include "Sprite2D.h"
 #include "SpriteSheet2D.h"
 #include "SpriteSheet2D.h"
 #include "Technique.h"
 #include "Technique.h"
@@ -35,7 +38,6 @@
 #include "VertexBuffer.h"
 #include "VertexBuffer.h"
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
-#include "Log.h"
 
 
 namespace Urho3D
 namespace Urho3D
 {
 {
@@ -56,11 +58,12 @@ Drawable2D::Drawable2D(Context* context) :
     geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
     geometry_->SetVertexBuffer(0, vertexBuffer_, MASK_VERTEX2D);
     batches_.Resize(1);
     batches_.Resize(1);
     batches_[0].geometry_ = geometry_;
     batches_[0].geometry_ = geometry_;
-    CreateDefaultMaterial();
 }
 }
 
 
 Drawable2D::~Drawable2D()
 Drawable2D::~Drawable2D()
 {
 {
+    if (drawableProxy_)
+        drawableProxy_->RemoveDrawable(this);
 }
 }
 
 
 void Drawable2D::RegisterObject(Context* context)
 void Drawable2D::RegisterObject(Context* context)
@@ -154,12 +157,7 @@ void Drawable2D::SetMaterial(Material* material)
         return;
         return;
 
 
     material_ = material;
     material_ = material;
-    // If a null material was specified, create one with defaults, otherwise clone the material so that
-    // the diffuse texture can be changed according to the sprite being used
-    if (!material_)
-        CreateDefaultMaterial();
-    else
-        batches_[0].material_ = material_->Clone();
+
     UpdateMaterial();
     UpdateMaterial();
     MarkNetworkUpdate();
     MarkNetworkUpdate();
 }
 }
@@ -264,6 +262,17 @@ void Drawable2D::SetBlendModeAttr(BlendMode mode)
     SetBlendMode(mode);
     SetBlendMode(mode);
 }
 }
 
 
+void Drawable2D::OnNodeSet(Node* node)
+{
+    Drawable::OnNodeSet(node);
+
+    if (node)
+    {
+        drawableProxy_ = GetScene()->GetOrCreateComponent<DrawableProxy2D>();
+        drawableProxy_->AddDrawable(this);
+    }
+}
+
 void Drawable2D::OnWorldBoundingBoxUpdate()
 void Drawable2D::OnWorldBoundingBoxUpdate()
 {
 {
     if (verticesDirty_)
     if (verticesDirty_)
@@ -278,48 +287,16 @@ void Drawable2D::OnWorldBoundingBoxUpdate()
     worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
     worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
 }
 }
 
 
-void Drawable2D::CreateDefaultMaterial()
-{
-    SharedPtr<Material> material(new Material(context_));
-    
-    Technique* tech = new Technique(context_);
-    Pass* pass = tech->CreatePass(PASS_ALPHA);
-    pass->SetVertexShader("Basic");
-    pass->SetVertexShaderDefines("DIFFMAP VERTEXCOLOR");
-
-    pass->SetPixelShader("Basic");
-    pass->SetPixelShaderDefines("DIFFMAP VERTEXCOLOR");
-    
-    pass->SetDepthWrite(false);
-
-    material->SetTechnique(0, tech);
-    material->SetCullMode(CULL_NONE);
-
-    batches_[0].material_ = material;
-}
-
 void Drawable2D::UpdateMaterial()
 void Drawable2D::UpdateMaterial()
 {
 {
     // Delay the material update
     // Delay the material update
     if (materialUpdatePending_)
     if (materialUpdatePending_)
         return;
         return;
 
 
-    Material* material = batches_[0].material_;
-    assert(material);
-    
-    // If we are using the default created material, we can update blend mode. Otherwise must respect what is set in the material
-    if (!material_)
-    {
-        Technique* technique = material->GetTechnique(0);
-        Pass* pass = technique->GetPass(PASS_ALPHA);
-        if (pass)
-            pass->SetBlendMode(blendMode_);
-    }
-    
-    // Update diffuse texture
-    Texture2D* texture = GetTexture();
-    if (texture)
-        material->SetTexture(TU_DIFFUSE, texture);
+    if (material_)
+        batches_[0].material_ = material_;
+    else
+        batches_[0].material_ = drawableProxy_->GetMaterial(this);
 }
 }
 
 
 }
 }

+ 5 - 2
Source/Engine/Urho2D/Drawable2D.h

@@ -28,6 +28,7 @@
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
+class DrawableProxy2D;
 class VertexBuffer;
 class VertexBuffer;
 
 
 /// Pixel size (equal 0.01f).
 /// Pixel size (equal 0.01f).
@@ -92,12 +93,12 @@ public:
     void SetBlendModeAttr(BlendMode mode);
     void SetBlendModeAttr(BlendMode mode);
 
 
 protected:
 protected:
+    /// Handle node being assigned.
+    virtual void OnNodeSet(Node* node);
     /// Recalculate the world-space bounding box.
     /// Recalculate the world-space bounding box.
     virtual void OnWorldBoundingBoxUpdate();
     virtual void OnWorldBoundingBoxUpdate();
     /// Update vertices.
     /// Update vertices.
     virtual void UpdateVertices() = 0;
     virtual void UpdateVertices() = 0;
-    /// Create a default material when a material is not specified.
-    void CreateDefaultMaterial();
     /// Update the material's properties (blend mode and texture).
     /// Update the material's properties (blend mode and texture).
     void UpdateMaterial();
     void UpdateMaterial();
 
 
@@ -122,6 +123,8 @@ protected:
     bool geometryDirty_;
     bool geometryDirty_;
     /// Material update pending flag.
     /// Material update pending flag.
     bool materialUpdatePending_;
     bool materialUpdatePending_;
+    /// Drawable proxy.
+    WeakPtr<DrawableProxy2D> drawableProxy_;
 };
 };
 
 
 }
 }

+ 125 - 0
Source/Engine/Urho2D/DrawableProxy2D.cpp

@@ -0,0 +1,125 @@
+//
+// Copyright (c) 2008-2014 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Precompiled.h"
+#include "Context.h"
+#include "Drawable2D.h"
+#include "DrawableProxy2D.h"
+#include "Material.h"
+#include "Scene.h"
+#include "Technique.h"
+#include "Texture2D.h"
+
+#include "DebugNew.h"
+
+namespace Urho3D
+{
+
+DrawableProxy2D::DrawableProxy2D(Context* context) :
+    Component(context)
+{
+}
+
+DrawableProxy2D::~DrawableProxy2D()
+{
+}
+
+void DrawableProxy2D::RegisterObject(Context* context)
+{
+    context->RegisterFactory<DrawableProxy2D>();
+}
+
+void DrawableProxy2D::AddDrawable(Drawable2D* drawable)
+{
+    if (!drawable)
+        return;
+
+    WeakPtr<Drawable2D> drawablePtr(drawable);
+    if (drawables_.Contains(drawablePtr))
+        return;
+
+    drawables_.Push(drawablePtr);
+}
+
+void DrawableProxy2D::RemoveDrawable(Drawable2D* drawable)
+{
+    if (!drawable)
+        return;
+
+    WeakPtr<Drawable2D> drawablePtr(drawable);
+    drawables_.Remove(drawablePtr);
+}
+
+Material* DrawableProxy2D::GetMaterial(Drawable2D* drawable)
+{
+    if (!drawable)
+        return 0;
+
+    Texture2D* texture = drawable->GetTexture();
+    if (!texture)
+        return 0;
+
+    BlendMode blendMode = drawable->GetBlendMode();
+    HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > >::Iterator t = materials_.Find(texture);
+    if (t == materials_.End())
+    {
+        Material* material(CreateMaterial(texture, blendMode));
+        materials_[texture][blendMode] = material;
+        return material;
+    }
+
+    HashMap<int, SharedPtr<Material> >& materials = t->second_;
+    HashMap<int, SharedPtr<Material> >::Iterator b = materials.Find(blendMode);
+    if (b != materials.End())
+        return b->second_;
+
+    Material* material(CreateMaterial(texture, blendMode));
+    materials[blendMode] = material;
+
+    return material;
+}
+
+Material* DrawableProxy2D::CreateMaterial(Texture2D* texture, BlendMode blendMode) const
+{
+    Material* material = new Material(context_);
+    
+    Technique* tech = new Technique(context_);
+    Pass* pass = tech->CreatePass(PASS_ALPHA);
+    pass->SetBlendMode(blendMode);
+
+    pass->SetVertexShader("Basic");
+    pass->SetVertexShaderDefines("DIFFMAP VERTEXCOLOR");
+
+    pass->SetPixelShader("Basic");
+    pass->SetPixelShaderDefines("DIFFMAP VERTEXCOLOR");
+    
+    pass->SetDepthWrite(false);
+
+    material->SetTechnique(0, tech);
+    material->SetCullMode(CULL_NONE);
+
+    material->SetTexture(TU_DIFFUSE, texture);
+
+    return material;
+}
+
+}

+ 63 - 0
Source/Engine/Urho2D/DrawableProxy2D.h

@@ -0,0 +1,63 @@
+//
+// Copyright (c) 2008-2014 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Drawable.h"
+
+namespace Urho3D
+{
+
+class Texture2D;
+class Drawable2D;
+
+/// 2D drawable proxy components.
+class URHO3D_API DrawableProxy2D : public Component
+{
+    OBJECT(DrawableProxy2D);
+
+public:
+    /// Construct.
+    DrawableProxy2D(Context* context);
+    /// Destruct.
+    ~DrawableProxy2D();
+    /// Register object factory. Drawable must be registered first.
+    static void RegisterObject(Context* context);
+
+    /// Add drawable.
+    void AddDrawable(Drawable2D* drawable);
+    /// Remove drawable.
+    void RemoveDrawable(Drawable2D* drawable);
+    /// Return material.
+    Material* GetMaterial(Drawable2D* drawable);
+
+protected:
+    /// Create material by texture and blend mode.
+    Material* CreateMaterial(Texture2D* Texture, BlendMode blendMode) const;
+
+    /// Drawables.
+    Vector<WeakPtr<Drawable2D> > drawables_;
+    /// Materials.
+    HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > materials_;
+};
+
+}

+ 2 - 0
Source/Engine/Urho2D/Urho2D.cpp

@@ -30,6 +30,7 @@
 #include "CollisionPolygon2D.h"
 #include "CollisionPolygon2D.h"
 #include "CollisionShape2D.h"
 #include "CollisionShape2D.h"
 #include "Drawable2D.h"
 #include "Drawable2D.h"
+#include "DrawableProxy2D.h"
 #include "ParticleEmitter2D.h"
 #include "ParticleEmitter2D.h"
 #include "ParticleModel2D.h"
 #include "ParticleModel2D.h"
 #include "PhysicsWorld2D.h"
 #include "PhysicsWorld2D.h"
@@ -49,6 +50,7 @@ void RegisterUrho2DLibrary(Context* context)
 {
 {
     // Must register objects from base to derived order
     // Must register objects from base to derived order
     Drawable2D::RegisterObject(context);
     Drawable2D::RegisterObject(context);
+    DrawableProxy2D::RegisterObject(context);
     StaticSprite2D::RegisterObject(context);
     StaticSprite2D::RegisterObject(context);
     AnimatedSprite2D::RegisterObject(context);
     AnimatedSprite2D::RegisterObject(context);
     Animation2D::RegisterObject(context);
     Animation2D::RegisterObject(context);