-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImportMaterial.cpp
124 lines (99 loc) · 3.54 KB
/
ImportMaterial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "ImportMaterial.hpp"
#include "ImportPath.hpp"
#include "ImportTexture.hpp"
#include "ResourceManifest.hpp"
#include <FileSystem/BsFileSystem.h>
#include <Importer/BsImporter.h>
#include <Material/BsMaterial.h>
#include <Material/BsShader.h>
#include <Resources/BsBuiltinResources.h>
#include <Resources/BsResources.h>
#include <zenload/zTypes.h>
using namespace bs;
/**
* Stores shaders to be used if the importer encouters a specific kind of material.
*
* See BsZenLib::SetShaderFor().
*/
bs::Map<BsZenLib::ShaderKind, bs::HShader> s_ShadersByKind;
static HTexture loadOrCacheTexture(const String& virtualFilePath, const VDFS::FileIndex& vdfs);
static BsZenLib::ShaderKind guessShaderKindFromZMaterial(const ZenLoad::zCMaterialData& mat);
// - Implementation --------------------------------------------------------------------------------
bs::HShader BsZenLib::GetShaderOfKind(ShaderKind kind)
{
auto it = s_ShadersByKind.find(kind);
if (it == s_ShadersByKind.end())
{
return gBuiltinResources().getBuiltinShader(BuiltinShader::Standard);
}
else
{
return it->second;
}
}
static HShader getShaderForZMaterial(const ZenLoad::zCMaterialData& mat)
{
using namespace BsZenLib;
ShaderKind kind = guessShaderKindFromZMaterial(mat);
return BsZenLib::GetShaderOfKind(kind);
}
static BsZenLib::ShaderKind guessShaderKindFromZMaterial(const ZenLoad::zCMaterialData& mat)
{
// TODO: Implement
return BsZenLib::ShaderKind::Opaque;
}
static HShader loadWorldShader()
{
String shaderName = "World.bsl";
Path cachedShader = BsZenLib::GothicPathToCachedMaterial(shaderName);
if (FileSystem::isFile(cachedShader)) return gResources().load<Shader>(cachedShader);
HShader shader = gImporter().import<Shader>("content/shaders/" + shaderName);
// Save to cache
const bool overwrite = true;
gResources().save(shader, BsZenLib::GothicPathToCachedMaterial(shaderName), overwrite);
BsZenLib::AddToResourceManifest(shader, BsZenLib::GothicPathToCachedMaterial(shaderName));
return shader;
}
void BsZenLib::SetShaderFor(ShaderKind kind, bs::HShader shader)
{
//
s_ShadersByKind[kind] = shader;
}
bs::String BsZenLib::BuildMaterialNameForSubmesh(const bs::String& meshName, UINT32 submesh)
{
return meshName + "-material-" + toString(submesh);
}
HMaterial BsZenLib::ImportAndCacheMaterialWithTextures(const String& cacheName,
const ZenLoad::zCMaterialData& material,
const VDFS::FileIndex& vdfs)
{
HShader shader = getShaderForZMaterial(material);
HMaterial bsfMaterial = Material::create(shader);
// Load Textures
HTexture albedo = loadOrCacheTexture(material.texture.c_str(), vdfs);
bsfMaterial->setTexture("gAlbedoTex", albedo);
// Save to cache
const bool overwrite = true;
gResources().save(bsfMaterial, GothicPathToCachedMaterial(cacheName), overwrite);
AddToResourceManifest(bsfMaterial, GothicPathToCachedMaterial(cacheName));
return bsfMaterial;
}
static HTexture loadOrCacheTexture(const String& virtualFilePath, const VDFS::FileIndex& vdfs)
{
if (BsZenLib::HasCachedTexture(virtualFilePath))
{
return BsZenLib::LoadCachedTexture(virtualFilePath);
}
else
{
return BsZenLib::ImportAndCacheTexture(virtualFilePath, vdfs);
}
}
HMaterial BsZenLib::LoadCachedMaterial(const String& cacheName)
{
return gResources().load<Material>(GothicPathToCachedMaterial(cacheName));
}
bool BsZenLib::HasCachedMaterial(const String& cacheName)
{
return HasCachedResource(GothicPathToCachedMaterial(cacheName));
}