/
MaterialSourceGenerator.cpp
259 lines (221 loc) · 7.39 KB
/
MaterialSourceGenerator.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "MaterialSourceGenerator.h"
#include "ShaderTemplate.h"
#include "string/replace.h"
#include "materials/ParseLib.h"
#include <fmt/format.h>
namespace shaders
{
// Write a single layer to the given stream, including curly braces (contents indented by two tabs)
std::ostream& operator<<(std::ostream& stream, Doom3ShaderLayer& layer)
{
stream << "\t{\n";
// Blend types
const auto& blendFunc = layer.getBlendFuncStrings();
if (!blendFunc.first.empty())
{
stream << "\t\tblend " << blendFunc.first;
if (!blendFunc.second.empty())
{
stream << ", " << blendFunc.second << "\n";
}
else
{
stream << "\n";
}
}
// Map
if (layer.getMapExpression())
{
stream << "\t\tmap " << layer.getMapExpression()->getExpressionString() << "\n";
}
stream << "\t}\n";
return stream;
}
// Write the material to the given stream (one tab indentation)
std::ostream& operator<<(std::ostream& stream, ShaderTemplate& shaderTemplate)
{
stream << "\n";
if (shaderTemplate.getSurfaceType() != Material::SURFTYPE_DEFAULT)
{
stream << "\t" << getStringForSurfaceType(shaderTemplate.getSurfaceType()) << "\n";
}
if (!shaderTemplate.getDescription().empty())
{
stream << "\tdescription \"" << string::replace_all_copy(shaderTemplate.getDescription(), "\"", "'") << "\"\n";
stream << "\n";
}
// Go through the material flags which reflect a single keyword
for (const auto& pair : shaders::MaterialFlagKeywords)
{
if (shaderTemplate.getMaterialFlags() & pair.second)
{
stream << "\t" << pair.first << "\n";
}
}
// Polygon Offset
if (shaderTemplate.getMaterialFlags() & Material::FLAG_POLYGONOFFSET)
{
stream << fmt::format("\tpolygonOffset {0}\n", shaderTemplate.getPolygonOffset());
}
// Clamping
if (shaderTemplate.getClampType() != CLAMP_REPEAT)
{
stream << "\t" << getStringForClampType(shaderTemplate.getClampType()) << "\n";
}
// Culling
if (shaderTemplate.getCullType() != Material::CULL_BACK)
{
stream << "\t" << getStringForCullType(shaderTemplate.getCullType()) << "\n";
}
// GuiSurf
if (shaderTemplate.getSurfaceFlags() & Material::SURF_GUISURF)
{
stream << "\tguisurf ";
if (shaderTemplate.getSurfaceFlags() & Material::SURF_ENTITYGUI)
{
stream << "entity\n";
}
else if (shaderTemplate.getSurfaceFlags() & Material::SURF_ENTITYGUI2)
{
stream << "entity2\n";
}
else if (shaderTemplate.getSurfaceFlags() & Material::SURF_ENTITYGUI3)
{
stream << "entity3\n";
}
else
{
stream << shaderTemplate.getGuiSurfArgument() << "\n";
}
}
// Sort
if (shaderTemplate.getMaterialFlags() & Material::FLAG_HAS_SORT_DEFINED)
{
stream << "\tsort ";
auto predefinedName = getStringForSortRequestValue(shaderTemplate.getSortRequest());
if (!predefinedName.empty())
{
stream << predefinedName << "\n";
}
else
{
stream << shaderTemplate.getSortRequest() << "\n";
}
}
// Spectrum
if (shaderTemplate.getSpectrum() != 0)
{
stream << "\tspectrum " << shaderTemplate.getSpectrum() << "\n";
}
if (shaderTemplate.getDeformType() != Material::DEFORM_NONE)
{
stream << "\tdeform " << getStringForDeformType(shaderTemplate.getDeformType());
switch (shaderTemplate.getDeformType())
{
case Material::DEFORM_SPRITE:
case Material::DEFORM_TUBE:
case Material::DEFORM_EYEBALL:
stream << "\n";
break;
case Material::DEFORM_FLARE:
case Material::DEFORM_EXPAND:
case Material::DEFORM_MOVE:
{
auto deformExpression = shaderTemplate.getDeformExpression(0);
stream << " " << (deformExpression ? deformExpression->getExpressionString() : "") << "\n";
break;
}
case Material::DEFORM_TURBULENT:
{
auto deformExpression0 = shaderTemplate.getDeformExpression(0);
auto deformExpression1 = shaderTemplate.getDeformExpression(1);
auto deformExpression2 = shaderTemplate.getDeformExpression(2);
stream << " " << shaderTemplate.getDeformDeclName() << " "
<< (deformExpression0 ? deformExpression0->getExpressionString() : "") << " "
<< (deformExpression1 ? deformExpression1->getExpressionString() : "") << " "
<< (deformExpression2 ? deformExpression2->getExpressionString() : "") << "\n";
break;
}
case Material::DEFORM_PARTICLE:
case Material::DEFORM_PARTICLE2:
stream << " " << shaderTemplate.getDeformDeclName() << "\n";
break;
}
}
// DecalInfo
if (shaderTemplate.getParseFlags() & Material::PF_HasDecalInfo)
{
const auto& decalInfo = shaderTemplate.getDecalInfo();
stream << "\tdecalinfo " << (decalInfo.stayMilliSeconds / 1000.0f) << " "
<< (decalInfo.fadeMilliSeconds / 1000.0f) << " "
<< "( " << decalInfo.startColour.x() << " " << decalInfo.startColour.y() << " "
<< decalInfo.startColour.z() << " " << decalInfo.startColour.w() << " ) "
<< "( " << decalInfo.endColour.x() << " " << decalInfo.endColour.y() << " "
<< decalInfo.endColour.z() << " " << decalInfo.endColour.w() << " )\n";
}
// Renderbump
if (!shaderTemplate.getRenderBumpArguments().empty())
{
stream << "\trenderbump " << shaderTemplate.getRenderBumpArguments() << "\n";
}
// Renderbumpflat
if (!shaderTemplate.getRenderBumpFlatArguments().empty())
{
stream << "\trenderbumpflat " << shaderTemplate.getRenderBumpFlatArguments() << "\n";
}
// Light Flags
if (shaderTemplate.isAmbientLight() && shaderTemplate.isCubicLight())
{
stream << "\tambientCubicLight\n";
}
else
{
if (shaderTemplate.isAmbientLight())
{
stream << "\tambientLight\n";
}
else if (shaderTemplate.isCubicLight())
{
stream << "\tcubicLight\n";
}
}
if (shaderTemplate.isFogLight())
{
stream << "\tfogLight\n";
}
else if (shaderTemplate.isBlendLight())
{
stream << "\tblendLight\n";
}
if (shaderTemplate.getLightFalloff())
{
if (shaderTemplate.getLightFalloffCubeMapType() == IShaderLayer::MapType::CameraCubeMap)
{
stream << "\tlightFalloffCubeMap " << shaderTemplate.getLightFalloff()->getExpressionString() << "\n";
}
else
{
stream << "\tlightFalloffImage " << shaderTemplate.getLightFalloff()->getExpressionString() << "\n";
}
}
// Surface flags
for (const auto& pair : SurfaceFlags)
{
if (shaderTemplate.getSurfaceFlags() & pair.second)
{
stream << "\t" << pair.first << "\n";
}
}
for (const auto& layer : shaderTemplate.getLayers())
{
stream << *layer;
}
return stream;
}
std::string MaterialSourceGenerator::GenerateDefinitionBlock(ShaderTemplate& shaderTemplate)
{
std::stringstream output;
output << shaderTemplate;
return output.str();
}
}