/
ShaderSystemInterface.h
454 lines (371 loc) · 11.4 KB
/
ShaderSystemInterface.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#pragma once
#include <pybind11/pybind11.h>
#include "ishaders.h"
#include "iscript.h"
#include "iscriptinterface.h"
namespace script
{
class ScriptMaterialStage
{
private:
IShaderLayer::Ptr _layer;
public:
ScriptMaterialStage(const IShaderLayer::Ptr& layer) :
_layer(layer)
{}
IShaderLayer::Type getType()
{
return _layer ? _layer->getType() : IShaderLayer::BLEND;
}
int getStageFlags()
{
return _layer ? _layer->getStageFlags() : 0;
}
ClampType getClampType()
{
return _layer ? _layer->getClampType() : CLAMP_REPEAT;
}
IShaderLayer::TexGenType getTexGenType()
{
return _layer ? _layer->getTexGenType() : IShaderLayer::TEXGEN_NORMAL;
}
float getTexGenParam(std::size_t index)
{
return _layer && index < 2 ? _layer->getTexGenParam(index) : 0;
}
std::string getTexGenExpressionString(std::size_t index)
{
return _layer && index < 2 && _layer->getTexGenExpression(index) ?
_layer->getTexGenExpression(index)->getExpressionString() : std::string();
}
};
/**
* This class represents a single Material as seen by the Python script.
*/
class ScriptMaterial
{
// The contained shader (can be empty)
MaterialPtr _material;
public:
ScriptMaterial(const MaterialPtr& material) :
_material(material)
{}
std::string getName() {
return _material ? _material->getName() : "";
}
std::string getShaderFileName() {
return _material ? _material->getShaderFileName() : "";
}
void setShaderFileName(const std::string& filename)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setShaderFileName(filename);
}
std::string getDescription() {
return _material ? _material->getDescription() : "";
}
void setDescription(const std::string& description)
{
throwIfMaterialCannotBeModified();
_material->setDescription(description);
}
std::vector<ScriptMaterialStage> getAllStages()
{
std::vector<ScriptMaterialStage> list;
if (!_material) return list;
for (const auto& layer : _material->getAllLayers())
{
list.emplace_back(layer);
}
return list;
}
std::size_t getNumStages()
{
return _material ? _material->getAllLayers().size() : 0;
}
ScriptMaterialStage getStage(int index)
{
if (!_material) return ScriptMaterialStage(IShaderLayer::Ptr());
const auto& layers = _material->getAllLayers();
return ScriptMaterialStage(index >= 0 && index < layers.size() ? layers[index] : IShaderLayer::Ptr());
}
std::size_t addStage(IShaderLayer::Type type)
{
throwIfMaterialCannotBeModified();
return _material->addLayer(type);
}
void removeStage(std::size_t index)
{
throwIfMaterialCannotBeModified();
_material->removeLayer(index);
}
std::size_t duplicateStage(std::size_t index)
{
throwIfMaterialCannotBeModified();
return _material->duplicateLayer(index);
}
void swapStagePosition(std::size_t first, std::size_t second)
{
throwIfMaterialCannotBeModified();
_material->swapLayerPosition(first, second);
}
std::string getDefinition() {
return _material ? _material->getDefinition() : "";
}
bool isVisible() {
return _material ? _material->isVisible() : false;
}
bool isAmbientLight() {
return _material ? _material->isAmbientLight() : false;
}
bool isBlendLight() {
return _material ? _material->isBlendLight() : false;
}
bool isFogLight() {
return _material ? _material->isFogLight() : false;
}
bool isCubicLight() {
return _material ? _material->isCubicLight() : false;
}
void setIsAmbientLight(bool newValue)
{
throwIfMaterialCannotBeModified();
_material->setIsAmbientLight(newValue);
}
void setIsBlendLight(bool newValue)
{
throwIfMaterialCannotBeModified();
_material->setIsBlendLight(newValue);
}
void setIsFogLight(bool newValue)
{
throwIfMaterialCannotBeModified();
_material->setIsFogLight(newValue);
}
void setIsCubicLight(bool newValue)
{
throwIfMaterialCannotBeModified();
_material->setIsCubicLight(newValue);
}
bool isNull() const {
return _material == nullptr;
}
std::string getEditorImageExpressionString()
{
return _material && _material->getEditorImageExpression() ?
_material->getEditorImageExpression()->getExpressionString() : std::string();
}
void setEditorImageExpressionFromString(const std::string& editorImagePath)
{
throwIfMaterialCannotBeModified();
_material->setEditorImageExpressionFromString(editorImagePath);
}
float getSortRequest()
{
return _material ? _material->getSortRequest() : -1;
}
void setSortRequest(float sortRequest)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSortRequest(sortRequest);
}
void setSortRequest(Material::SortRequest sortRequest) // overload needed for python
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSortRequest(static_cast<float>(sortRequest));
}
void resetSortRequest()
{
throwIfMaterialCannotBeModified();
if (_material) _material->resetSortRequest();
}
float getPolygonOffset()
{
return _material ? _material->getPolygonOffset() : 0;
}
void setPolygonOffset(float offset)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setPolygonOffset(offset);
}
void clearPolygonOffset()
{
throwIfMaterialCannotBeModified();
if (_material) _material->clearMaterialFlag(Material::FLAG_POLYGONOFFSET);
}
ClampType getClampType()
{
return _material ? _material->getClampType() : CLAMP_REPEAT;
}
void setClampType(ClampType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setClampType(type);
}
Material::CullType getCullType()
{
return _material ? _material->getCullType() : Material::CULL_BACK;
}
void setCullType(Material::CullType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setCullType(type);
}
int getMaterialFlags()
{
return _material ? _material->getMaterialFlags() : 0;
}
void setMaterialFlag(Material::Flags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setMaterialFlag(flag);
}
void clearMaterialFlag(Material::Flags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->clearMaterialFlag(flag);
}
int getSurfaceFlags()
{
return _material ? _material->getSurfaceFlags() : 0;
}
void setSurfaceFlag(Material::SurfaceFlags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSurfaceFlag(flag);
}
void clearSurfaceFlag(Material::SurfaceFlags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->clearSurfaceFlag(flag);
}
Material::SurfaceType getSurfaceType()
{
return _material ? _material->getSurfaceType() : Material::SURFTYPE_DEFAULT;
}
void setSurfaceType(Material::SurfaceType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSurfaceType(type);
}
Material::DeformType getDeformType()
{
return _material ? _material->getDeformType() : Material::DEFORM_NONE;
}
std::string getDeformExpressionString(std::size_t index)
{
return _material && _material->getDeformExpression(index) ?
_material->getDeformExpression(index)->getExpressionString() : std::string();
}
std::string getDeformDeclName()
{
return _material ? _material->getDeformDeclName() : std::string();
}
int getSpectrum()
{
return _material ? _material->getSpectrum() : 0;
}
void setSpectrum(int spectrum)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSpectrum(spectrum);
}
Material::DecalInfo getDecalInfo()
{
return _material ? _material->getDecalInfo() : Material::DecalInfo();
}
Material::Coverage getCoverage()
{
return _material ? _material->getCoverage() : Material::MC_UNDETERMINED;
}
std::string getLightFalloffExpressionString()
{
return _material && _material->getLightFalloffExpression() ?
_material->getLightFalloffExpression()->getExpressionString() : std::string();
}
void setLightFalloffExpressionFromString(const std::string& expressionString)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setLightFalloffExpressionFromString(expressionString);
}
IShaderLayer::MapType getLightFalloffCubeMapType()
{
return _material ? _material->getLightFalloffCubeMapType() : IShaderLayer::MapType::Map;
}
void setLightFalloffCubeMapType(IShaderLayer::MapType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setLightFalloffCubeMapType(type);
}
std::string getRenderBumpArguments()
{
return _material ? _material->getRenderBumpArguments() : std::string();
}
std::string getRenderBumpFlatArguments()
{
return _material ? _material->getRenderBumpFlatArguments() : std::string();
}
std::string getGuiSurfArgument()
{
return _material ? _material->getGuiSurfArgument() : std::string();
}
bool isModified()
{
return _material ? _material->isModified() : false;
}
void revertModifications()
{
throwIfMaterialCannotBeModified();
if (_material) _material->revertModifications();
}
private:
void throwIfMaterialCannotBeModified()
{
if (!_material || !GlobalMaterialManager().materialCanBeModified(_material->getName()))
{
throw std::runtime_error("Material cannot be modified");
}
}
};
class MaterialVisitor
{
public:
virtual ~MaterialVisitor() {}
virtual void visit(const MaterialPtr& shader) = 0;
};
// Wrap around the EntityClassVisitor interface
class MaterialVisitorWrapper :
public MaterialVisitor
{
public:
void visit(const MaterialPtr& shader) override
{
// Wrap this method to python
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
MaterialVisitor, /* Parent class */
visit, /* Name of function in C++ (must match Python name) */
ScriptMaterial(shader) /* Argument(s) */
);
}
};
/**
* greebo: This class provides the script interface for the GlobalMaterialManager module.
*/
class ShaderSystemInterface :
public IScriptInterface
{
public:
void foreachMaterial(MaterialVisitor& visitor);
ScriptMaterial getMaterial(const std::string& name);
bool materialExists(const std::string& name);
bool materialCanBeModified(const std::string& name);
ScriptMaterial createEmptyMaterial(const std::string& name);
ScriptMaterial copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy);
bool renameMaterial(const std::string& oldName, const std::string& newName);
void removeMaterial(const std::string& name);
void saveMaterial(const std::string& name);
// IScriptInterface implementation
void registerInterface(py::module& scope, py::dict& globals) override;
};
} // namespace script