Browse Source

Refactor FindNamedTexture() so that programmatically stored cubemap textures without a proper file extension can be used for texture binding in the renderpath. Expose GetExistingResource() to script.

Lasse Öörni 10 years ago
parent
commit
9f044cb1fa

+ 12 - 9
Source/Urho3D/Graphics/View.cpp

@@ -2956,6 +2956,18 @@ Texture* View::FindNamedTexture(const String& name, bool isRenderTarget, bool is
     // Then the resource system
     ResourceCache* cache = GetSubsystem<ResourceCache>();
 
+    // Check existing resources first. This does not load resources, so we can afford to guess the resource type wrong
+    // without having to rely on the file extension
+    Texture* texture = cache->GetExistingResource<Texture2D>(name);
+    if (!texture)
+        texture = cache->GetExistingResource<TextureCube>(name);
+    if (!texture)
+        texture = cache->GetExistingResource<Texture3D>(name);
+    if (texture)
+        return texture;
+
+    // If not a rendertarget (which will never be loaded from a file), finally also try to load the texture
+    // This will log an error if not found; the texture binding will be cleared in that case to not constantly spam the log
     if (!isRenderTarget)
     {
         if (GetExtension(name) == ".xml")
@@ -2971,15 +2983,6 @@ Texture* View::FindNamedTexture(const String& name, bool isRenderTarget, bool is
         else
             return cache->GetResource<Texture2D>(name);
     }
-    else
-    {
-        // Rendertargets can only be manually added existing resources; do not attempt to load files through ResourceCache
-        // Try both Texture2D & TextureCube
-        Texture* texture = cache->GetExistingResource<Texture2D>(name);
-        if (!texture)
-            texture = cache->GetExistingResource<TextureCube>(name);
-        return texture;
-    }
 }
 
 }

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Resource/ResourceCache.pkg

@@ -17,6 +17,7 @@ class ResourceCache
     tolua_outside File* ResourceCacheGetFile @ GetFile(const String name);
 
     Resource* GetResource(const String type, const String name, bool sendEventOnFailure = true);
+    Resource* GetExistingResource(const String type, const String name);
     tolua_outside bool ResourceCacheBackgroundLoadResource @ BackgroundLoadResource(const String type, const String name, bool sendEventOnFailure = true);
     unsigned GetNumBackgroundLoadResources() const;
     const Vector<String>& GetResourceDirs() const;

+ 1 - 1
Source/Urho3D/Resource/ResourceCache.h

@@ -141,7 +141,7 @@ public:
     unsigned GetNumBackgroundLoadResources() const;
     /// Return all loaded resources of a specific type.
     void GetResources(PODVector<Resource*>& result, StringHash type) const;
-    /// Return an already loaded resource of specific type & name. Will not load if does not exist.
+    /// Return an already loaded resource of specific type & name, or null if not found. Will not load if does not exist.
     Resource* GetExistingResource(StringHash type, const String& name);
     /// Return all loaded resources.
     const HashMap<StringHash, ResourceGroup>& GetAllResources() const { return resourceGroups_; }

+ 7 - 0
Source/Urho3D/Script/ResourceAPI.cpp

@@ -42,6 +42,11 @@ static Resource* ResourceCacheGetResource(const String& type, const String& name
     return ptr->GetResource(StringHash(type), name, sendEventOnFailure);
 }
 
+static Resource* ResourceCacheGetExistingResource(const String& type, const String& name, ResourceCache* ptr)
+{
+    return ptr->GetResource(StringHash(type), name);
+}
+
 static File* ResourceCacheGetFile(const String& name, ResourceCache* ptr)
 {
     SharedPtr<File> file = ptr->GetFile(name);
@@ -122,6 +127,8 @@ static void RegisterResourceCache(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ResourceCache", "String GetResourceFileName(const String&in) const", asMETHOD(ResourceCache, GetResourceFileName), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetResource(const String&in, const String&in, bool sendEventOnFailure = true)", asFUNCTION(ResourceCacheGetResource), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetResource(StringHash, const String&in, bool sendEventOnFailure = true)", asMETHODPR(ResourceCache, GetResource, (StringHash, const String&, bool), Resource*), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetExistingResource(const String&in, const String&in)", asFUNCTION(ResourceCacheGetExistingResource), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("ResourceCache", "Resource@+ GetExistingResource(StringHash, const String&in)", asMETHODPR(ResourceCache, GetExistingResource, (StringHash, const String&), Resource*), asCALL_THISCALL);
     engine->RegisterObjectMethod("ResourceCache", "bool BackgroundLoadResource(const String&in, const String&in, bool sendEventOnFailure = true)", asFUNCTION(ResourceCacheBackgroundLoadResource), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "void set_memoryBudget(const String&in, uint)", asFUNCTION(ResourceCacheSetMemoryBudget), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("ResourceCache", "uint get_memoryBudget(const String&in) const", asFUNCTION(ResourceCacheGetMemoryBudget), asCALL_CDECL_OBJLAST);