BsEditorShaderIncludeHandler.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Library/BsEditorShaderIncludeHandler.h"
  4. #include "Library/BsProjectLibrary.h"
  5. #include "Resources/BsResources.h"
  6. #include "Library/BsProjectResourceMeta.h"
  7. #include "Resources/BsBuiltinResources.h"
  8. #include "Utility/BsBuiltinEditorResources.h"
  9. namespace bs
  10. {
  11. HShaderInclude EditorShaderIncludeHandler::findInclude(const String& name) const
  12. {
  13. Path path = toResourcePath(name);
  14. if (path.isEmpty())
  15. return HShaderInclude();
  16. ResourceLoadFlags loadFlags = ResourceLoadFlag::Default | ResourceLoadFlag::KeepSourceData;
  17. if (name.size() >= 8)
  18. {
  19. if (name.substr(0, 8) == "$ENGINE$" || name.substr(0, 8) == "$EDITOR$")
  20. return static_resource_cast<ShaderInclude>(Resources::instance().load(path, loadFlags));
  21. }
  22. ProjectLibrary::LibraryEntry* entry = gProjectLibrary().findEntry(path).get();
  23. if (entry != nullptr && entry->type == ProjectLibrary::LibraryEntryType::File)
  24. {
  25. ProjectLibrary::FileEntry* fileEntry = static_cast<ProjectLibrary::FileEntry*>(entry);
  26. if (fileEntry->meta != nullptr)
  27. {
  28. auto& resourceMetas = fileEntry->meta->getResourceMetaData();
  29. for(auto& resMeta : resourceMetas)
  30. {
  31. if(resMeta->getTypeID() == TID_ShaderInclude)
  32. return static_resource_cast<ShaderInclude>(Resources::instance().loadFromUUID(resMeta->getUUID(), false, loadFlags));
  33. }
  34. }
  35. }
  36. return HShaderInclude();
  37. }
  38. Path EditorShaderIncludeHandler::toResourcePath(const String& name)
  39. {
  40. if (name.substr(0, 8) == "$ENGINE$")
  41. {
  42. if (name.size() > 8)
  43. {
  44. Path fullPath = BuiltinResources::getShaderIncludeFolder();
  45. Path includePath = name.substr(9, name.size() - 9);
  46. fullPath.append(includePath);
  47. fullPath.setFilename(includePath.getFilename() + ".asset");
  48. return fullPath;
  49. }
  50. }
  51. else if (name.substr(0, 8) == "$EDITOR$")
  52. {
  53. if (name.size() > 8)
  54. {
  55. Path fullPath = BuiltinResources::getEditorShaderIncludeFolder();
  56. Path includePath = name.substr(9, name.size() - 9);
  57. fullPath.append(includePath);
  58. fullPath.setFilename(includePath.getFilename() + ".asset");
  59. return fullPath;
  60. }
  61. }
  62. else
  63. {
  64. return name;
  65. }
  66. return Path::BLANK;
  67. }
  68. }