Debian Codegraph @

plugins/script/interfaces/ShaderSystemInterface.h

raw
1 #pragma once
2
3 #include <pybind11/pybind11.h>
4
5 #include "ishaders.h"
6 #include "iscript.h"
7 #include "iscriptinterface.h"
8
9 namespace script
10 {
11
12 class ScriptMaterialStage
13 {
14 private:
15 IShaderLayer::Ptr _layer;
16
17 public:
18 struct Transformation
19 {
20 IShaderLayer::TransformType type;
21 std::string expression1;
22 std::string expression2;
23 };
24
25 ScriptMaterialStage(const IShaderLayer::Ptr& layer) :
26 _layer(layer)
27 {}
28
29 IShaderLayer::Type getType()
30 {
31 return _layer ? _layer->getType() : IShaderLayer::BLEND;
32 }
33
34 std::pair<std::string, std::string> getBlendFuncStrings()
35 {
36 return _layer ? _layer->getBlendFuncStrings() : std::pair<std::string, std::string>();
37 }
38
39 std::string getMapExpressionString()
40 {
41 return _layer && _layer->getMapExpression() ? _layer->getMapExpression()->getExpressionString() : std::string();
42 }
43
44 int getStageFlags()
45 {
46 return _layer ? _layer->getStageFlags() : 0;
47 }
48
49 ClampType getClampType()
50 {
51 return _layer ? _layer->getClampType() : CLAMP_REPEAT;
52 }
53
54 IShaderLayer::TexGenType getTexGenType()
55 {
56 return _layer ? _layer->getTexGenType() : IShaderLayer::TEXGEN_NORMAL;
57 }
58
59 std::string getTexGenExpressionString(std::size_t index)
60 {
61 return _layer && index < 2 && _layer->getTexGenExpression(index) ?
62 _layer->getTexGenExpression(index)->getExpressionString() : std::string();
63 }
64
65 std::string getColourExpressionString(IShaderLayer::ColourComponentSelector component)
66 {
67 return _layer && _layer->getColourExpression(component) ?
68 _layer->getColourExpression(component)->getExpressionString() : std::string();
69 }
70
71 IShaderLayer::VertexColourMode getVertexColourMode()
72 {
73 return _layer ? _layer->getVertexColourMode() : IShaderLayer::VERTEX_COLOUR_NONE;
74 }
75
76 IShaderLayer::MapType getMapType()
77 {
78 return _layer ? _layer->getMapType() : IShaderLayer::MapType::Map;
79 }
80
81 std::vector<Transformation> getTransformations()
82 {
83 std::vector<Transformation> list;
84
85 if (!_layer) return list;
86
87 for (const auto& transform : _layer->getTransformations())
88 {
89 list.emplace_back(Transformation
90 {
91 transform.type,
92 transform.expression1 ? transform.expression1->getExpressionString() : "",
93 transform.expression2 ? transform.expression2->getExpressionString() : "",
94 });
95 }
96
97 return list;
98 }
99
100 Vector2 getRenderMapSize()
101 {
102 return _layer ? _layer->getRenderMapSize() : Vector2(0,0);
103 }
104
105 std::string getAlphaTestExpressionString()
106 {
107 return _layer && _layer->getAlphaTestExpression() ?
108 _layer->getAlphaTestExpression()->getExpressionString() : std::string();
109 }
110
111 std::string getConditionExpressionString()
112 {
113 return _layer && _layer->getConditionExpression() ?
114 _layer->getConditionExpression()->getExpressionString() : std::string();
115 }
116
117 std::string getVertexProgram()
118 {
119 return _layer ? _layer->getVertexProgram() : std::string();
120 }
121
122 std::string getFragmentProgram()
123 {
124 return _layer ? _layer->getFragmentProgram() : std::string();
125 }
126
127 std::size_t getNumVertexParms()
128 {
129 return _layer ? _layer->getNumVertexParms() : 0;
130 }
131
132 std::size_t getNumFragmentMaps()
133 {
134 return _layer ? _layer->getNumFragmentMaps() : 0;
135 }
136
137 struct VertexParm
138 {
139 std::size_t index;
140 std::vector<std::string> expressions;
141 };
142
143 VertexParm getVertexParm(int index)
144 {
145 VertexParm parm;
146
147 if (_layer && index >= 0 && index < 4)
148 {
149 const auto& vp = _layer->getVertexParm(index);
150
151 parm.index = vp.index;
152
153 for (auto i = 0; i < 4; ++i)
154 {
155 if (vp.expressions[i])
156 {
157 parm.expressions.emplace_back(vp.expressions[i]->getExpressionString());
158 }
159 }
160 }
161
162 return parm;
163 }
164
165 struct FragmentMap
166 {
167 int index = -1;
168 std::vector<std::string> options;
169 std::string mapExpression;
170 };
171
172 FragmentMap getFragmentMap(int index)
173 {
174 FragmentMap map;
175
176 if (_layer && index >= 0 && index < _layer->getNumFragmentMaps())
177 {
178 const auto& fm = _layer->getFragmentMap(index);
179
180 map.index = fm.index;
181 map.options = fm.options;
182 map.mapExpression = fm.map ? fm.map->getExpressionString() : std::string();
183 }
184
185 return map;
186 }
187
188 float getPrivatePolygonOffset()
189 {
190 return _layer ? _layer->getPrivatePolygonOffset() : 0;
191 }
192 };
193
194 // Editable shader stage interface
195 class ScriptEditableMaterialStage :
196 public ScriptMaterialStage
197 {
198 private:
199 IEditableShaderLayer::Ptr _layer;
200
201 public:
202 ScriptEditableMaterialStage(const IEditableShaderLayer::Ptr& layer) :
203 ScriptMaterialStage(layer),
204 _layer(layer)
205 {}
206
207 void setStageFlag(IShaderLayer::Flags flag)
208 {
209 if (_layer) _layer->setStageFlag(flag);
210 }
211
212 void clearStageFlag(IShaderLayer::Flags flag)
213 {
214 if (_layer) _layer->clearStageFlag(flag);
215 }
216
217 void setMapType(IShaderLayer::MapType mapType)
218 {
219 if (_layer) _layer->setMapType(mapType);
220 }
221
222 void setBlendFuncStrings(const std::pair<std::string, std::string>& pair)
223 {
224 if (_layer) _layer->setBlendFuncStrings(pair);
225 }
226
227 void setAlphaTestExpressionFromString(const std::string& expression)
228 {
229 if (_layer) _layer->setAlphaTestExpressionFromString(expression);
230 }
231
232 void setMapExpressionFromString(const std::string& expression)
233 {
234 if (_layer) _layer->setMapExpressionFromString(expression);
235 }
236
237 std::size_t addTransformation(IShaderLayer::TransformType type, const std::string& expression1, const std::string& expression2)
238 {
239 return _layer ? _layer->addTransformation(type, expression1, expression2) : -1;
240 }
241
242 void removeTransformation(std::size_t index)
243 {
244 if (_layer) _layer->removeTransformation(index);
245 }
246
247 void updateTransformation(std::size_t index, IShaderLayer::TransformType type,
248 const std::string& expression1, const std::string& expression2)
249 {
250 if (_layer) _layer->updateTransformation(index, type, expression1, expression2);
251 }
252
253 void setColourExpressionFromString(IShaderLayer::ColourComponentSelector component, const std::string& expression)
254 {
255 if (_layer) _layer->setColourExpressionFromString(component, expression);
256 }
257
258 void setConditionExpressionFromString(const std::string& expression)
259 {
260 if (_layer) _layer->setConditionExpressionFromString(expression);
261 }
262
263 void setTexGenType(IShaderLayer::TexGenType type)
264 {
265 if (_layer) _layer->setTexGenType(type);
266 }
267
268 void setTexGenExpressionFromString(std::size_t index, const std::string& expression)
269 {
270 if (_layer) _layer->setTexGenExpressionFromString(index, expression);
271 }
272
273 void setVertexColourMode(IShaderLayer::VertexColourMode mode)
274 {
275 if (_layer) _layer->setVertexColourMode(mode);
276 }
277
278 void setClampType(ClampType clampType)
279 {
280 if (_layer) _layer->setClampType(clampType);
281 }
282
283 void setPrivatePolygonOffset(double offset)
284 {
285 if (_layer) _layer->setPrivatePolygonOffset(offset);
286 }
287
288 void setRenderMapSize(const Vector2& size)
289 {
290 if (_layer) _layer->setRenderMapSize(size);
291 }
292
293 void setSoundMapWaveForm(bool waveForm)
294 {
295 if (_layer) _layer->setSoundMapWaveForm(waveForm);
296 }
297
298 void setVideoMapProperties(const std::string& filePath, bool looping)
299 {
300 if (_layer) _layer->setVideoMapProperties(filePath, looping);
301 }
302 };
303
304 /**
305 * This class represents a single Material as seen by the Python script.
306 */
307 class ScriptMaterial
308 {
309 // The contained shader (can be empty)
310 MaterialPtr _material;
311 public:
312 ScriptMaterial(const MaterialPtr& material) :
313 _material(material)
314 {}
315
316 std::string getName() {
317 return _material ? _material->getName() : "";
318 }
319
320 std::string getShaderFileName() {
321 return _material ? _material->getShaderFileName() : "";
322 }
323
324 void setShaderFileName(const std::string& filename)
325 {
326 throwIfMaterialCannotBeModified();
327 if (_material) _material->setShaderFileName(filename);
328 }
329
330 std::string getDescription() {
331 return _material ? _material->getDescription() : "";
332 }
333
334 void setDescription(const std::string& description)
335 {
336 throwIfMaterialCannotBeModified();
337 _material->setDescription(description);
338 }
339
340 std::vector<ScriptMaterialStage> getAllStages()
341 {
342 std::vector<ScriptMaterialStage> list;
343
344 if (!_material) return list;
345
346 _material->foreachLayer([&](const IShaderLayer::Ptr& layer)
347 {
348 list.emplace_back(layer);
349 return true;
350 });
351
352 return list;
353 }
354
355 std::size_t getNumStages()
356 {
357 return _material ? _material->getNumLayers() : 0;
358 }
359
360 ScriptMaterialStage getStage(std::size_t index)
361 {
362 if (!_material || index >= _material->getNumLayers())
363 {
364 return ScriptMaterialStage(IShaderLayer::Ptr());
365 }
366
367 return ScriptMaterialStage(_material->getLayer(index));
368 }
369
370 ScriptEditableMaterialStage getEditableStage(std::size_t index)
371 {
372 throwIfMaterialCannotBeModified();
373
374 return ScriptEditableMaterialStage(index >= 0 && index < getNumStages() ?
375 _material->getEditableLayer(index) : IEditableShaderLayer::Ptr());
376 }
377
378 std::size_t addStage(IShaderLayer::Type type)
379 {
380 throwIfMaterialCannotBeModified();
381 return _material->addLayer(type);
382 }
383
384 void removeStage(std::size_t index)
385 {
386 throwIfMaterialCannotBeModified();
387 _material->removeLayer(index);
388 }
389
390 std::size_t duplicateStage(std::size_t index)
391 {
392 throwIfMaterialCannotBeModified();
393 return _material->duplicateLayer(index);
394 }
395
396 void swapStagePosition(std::size_t first, std::size_t second)
397 {
398 throwIfMaterialCannotBeModified();
399 _material->swapLayerPosition(first, second);
400 }
401
402 std::string getDefinition() {
403 return _material ? _material->getDefinition() : "";
404 }
405
406 bool isVisible() {
407 return _material ? _material->isVisible() : false;
408 }
409
410 bool isAmbientLight() {
411 return _material ? _material->isAmbientLight() : false;
412 }
413
414 bool isBlendLight() {
415 return _material ? _material->isBlendLight() : false;
416 }
417
418 bool isFogLight() {
419 return _material ? _material->isFogLight() : false;
420 }
421
422 bool isCubicLight() {
423 return _material ? _material->isCubicLight() : false;
424 }
425
426 void setIsAmbientLight(bool newValue)
427 {
428 throwIfMaterialCannotBeModified();
429 _material->setIsAmbientLight(newValue);
430 }
431
432 void setIsBlendLight(bool newValue)
433 {
434 throwIfMaterialCannotBeModified();
435 _material->setIsBlendLight(newValue);
436 }
437
438 void setIsFogLight(bool newValue)
439 {
440 throwIfMaterialCannotBeModified();
441 _material->setIsFogLight(newValue);
442 }
443
444 void setIsCubicLight(bool newValue)
445 {
446 throwIfMaterialCannotBeModified();
447 _material->setIsCubicLight(newValue);
448 }
449
450 bool isNull() const {
451 return _material == nullptr;
452 }
453
454 std::string getEditorImageExpressionString()
455 {
456 return _material && _material->getEditorImageExpression() ?
457 _material->getEditorImageExpression()->getExpressionString() : std::string();
458 }
459
460 void setEditorImageExpressionFromString(const std::string& editorImagePath)
461 {
462 throwIfMaterialCannotBeModified();
463 _material->setEditorImageExpressionFromString(editorImagePath);
464 }
465
466 float getSortRequest()
467 {
468 return _material ? _material->getSortRequest() : -1;
469 }
470
471 void setSortRequest(float sortRequest)
472 {
473 throwIfMaterialCannotBeModified();
474 if (_material) _material->setSortRequest(sortRequest);
475 }
476
477 void setSortRequest(Material::SortRequest sortRequest) // overload needed for python
478 {
479 throwIfMaterialCannotBeModified();
480 if (_material) _material->setSortRequest(static_cast<float>(sortRequest));
481 }
482
483 void resetSortRequest()
484 {
485 throwIfMaterialCannotBeModified();
486 if (_material) _material->resetSortRequest();
487 }
488
489 float getPolygonOffset()
490 {
491 return _material ? _material->getPolygonOffset() : 0;
492 }
493
494 void setPolygonOffset(float offset)
495 {
496 throwIfMaterialCannotBeModified();
497 if (_material) _material->setPolygonOffset(offset);
498 }
499
500 void clearPolygonOffset()
501 {
502 throwIfMaterialCannotBeModified();
503 if (_material) _material->clearMaterialFlag(Material::FLAG_POLYGONOFFSET);
504 }
505
506 ClampType getClampType()
507 {
508 return _material ? _material->getClampType() : CLAMP_REPEAT;
509 }
510
511 void setClampType(ClampType type)
512 {
513 throwIfMaterialCannotBeModified();
514 if (_material) _material->setClampType(type);
515 }
516
517 Material::CullType getCullType()
518 {
519 return _material ? _material->getCullType() : Material::CULL_BACK;
520 }
521
522 void setCullType(Material::CullType type)
523 {
524 throwIfMaterialCannotBeModified();
525 if (_material) _material->setCullType(type);
526 }
527
528 int getMaterialFlags()
529 {
530 return _material ? _material->getMaterialFlags() : 0;
531 }
532
533 void setMaterialFlag(Material::Flags flag)
534 {
535 throwIfMaterialCannotBeModified();
536 if (_material) _material->setMaterialFlag(flag);
537 }
538
539 void clearMaterialFlag(Material::Flags flag)
540 {
541 throwIfMaterialCannotBeModified();
542 if (_material) _material->clearMaterialFlag(flag);
543 }
544
545 int getSurfaceFlags()
546 {
547 return _material ? _material->getSurfaceFlags() : 0;
548 }
549
550 void setSurfaceFlag(Material::SurfaceFlags flag)
551 {
552 throwIfMaterialCannotBeModified();
553 if (_material) _material->setSurfaceFlag(flag);
554 }
555
556 void clearSurfaceFlag(Material::SurfaceFlags flag)
557 {
558 throwIfMaterialCannotBeModified();
559 if (_material) _material->clearSurfaceFlag(flag);
560 }
561
562 Material::SurfaceType getSurfaceType()
563 {
564 return _material ? _material->getSurfaceType() : Material::SURFTYPE_DEFAULT;
565 }
566
567 void setSurfaceType(Material::SurfaceType type)
568 {
569 throwIfMaterialCannotBeModified();
570 if (_material) _material->setSurfaceType(type);
571 }
572
573 Material::DeformType getDeformType()
574 {
575 return _material ? _material->getDeformType() : Material::DEFORM_NONE;
576 }
577
578 std::string getDeformExpressionString(std::size_t index)
579 {
580 return _material && _material->getDeformExpression(index) ?
581 _material->getDeformExpression(index)->getExpressionString() : std::string();
582 }
583
584 std::string getDeformDeclName()
585 {
586 return _material ? _material->getDeformDeclName() : std::string();
587 }
588
589 int getSpectrum()
590 {
591 return _material ? _material->getSpectrum() : 0;
592 }
593
594 void setSpectrum(int spectrum)
595 {
596 throwIfMaterialCannotBeModified();
597 if (_material) _material->setSpectrum(spectrum);
598 }
599
600 Material::DecalInfo getDecalInfo()
601 {
602 return _material ? _material->getDecalInfo() : Material::DecalInfo();
603 }
604
605 void setDecalInfo(const Material::DecalInfo& decalInfo)
606 {
607 if (_material) _material->setDecalInfo(decalInfo);
608 }
609
610 Material::Coverage getCoverage()
611 {
612 return _material ? _material->getCoverage() : Material::MC_UNDETERMINED;
613 }
614
615 std::string getLightFalloffExpressionString()
616 {
617 return _material && _material->getLightFalloffExpression() ?
618 _material->getLightFalloffExpression()->getExpressionString() : std::string();
619 }
620
621 void setLightFalloffExpressionFromString(const std::string& expressionString)
622 {
623 throwIfMaterialCannotBeModified();
624 if (_material) _material->setLightFalloffExpressionFromString(expressionString);
625 }
626
627 IShaderLayer::MapType getLightFalloffCubeMapType()
628 {
629 return _material ? _material->getLightFalloffCubeMapType() : IShaderLayer::MapType::Map;
630 }
631
632 void setLightFalloffCubeMapType(IShaderLayer::MapType type)
633 {
634 throwIfMaterialCannotBeModified();
635 if (_material) _material->setLightFalloffCubeMapType(type);
636 }
637
638 Material::FrobStageType getFrobStageType()
639 {
640 return _material ? _material->getFrobStageType() : Material::FrobStageType::Default;
641 }
642
643 void setFrobStageType(Material::FrobStageType type)
644 {
645 throwIfMaterialCannotBeModified();
646 if (_material) _material->setFrobStageType(type);
647 }
648
649 std::string getFrobStageMapExpressionString()
650 {
651 return _material && _material->getFrobStageMapExpression() ? _material->getFrobStageMapExpression()->getExpressionString() : "";
652 }
653
654 void setFrobStageMapExpressionFromString(const std::string& expr)
655 {
656 throwIfMaterialCannotBeModified();
657 if (_material) _material->setFrobStageMapExpressionFromString(expr);
658 }
659
660 Vector3 getFrobStageRgbParameter(std::size_t index)
661 {
662 return _material ? _material->getFrobStageRgbParameter(index) : Vector3(0, 0, 0);
663 }
664
665 void setFrobStageRgbParameter(std::size_t index, const Vector3& value)
666 {
667 throwIfMaterialCannotBeModified();
668 if (_material) _material->setFrobStageRgbParameter(index, value);
669 }
670
671 void setFrobStageParameter(std::size_t index, double value)
672 {
673 setFrobStageRgbParameter(index, Vector3(value, value, value));
674 }
675
676 std::string getRenderBumpArguments()
677 {
678 return _material ? _material->getRenderBumpArguments() : std::string();
679 }
680
681 std::string getRenderBumpFlatArguments()
682 {
683 return _material ? _material->getRenderBumpFlatArguments() : std::string();
684 }
685
686 std::string getGuiSurfArgument()
687 {
688 return _material ? _material->getGuiSurfArgument() : std::string();
689 }
690
691 bool isModified()
692 {
693 return _material ? _material->isModified() : false;
694 }
695
696 void revertModifications()
697 {
698 throwIfMaterialCannotBeModified();
699 if (_material) _material->revertModifications();
700 }
701
702 private:
703 void throwIfMaterialCannotBeModified()
704 {
705 if (!_material || !GlobalMaterialManager().materialCanBeModified(_material->getName()))
706 {
707 throw std::runtime_error("Material cannot be modified");
708 }
709 }
710 };
711
712 class MaterialVisitor
713 {
714 public:
715 virtual ~MaterialVisitor() {}
716 virtual void visit(const MaterialPtr& shader) = 0;
717 };
718
719 // Wrap around the EntityClassVisitor interface
720 class MaterialVisitorWrapper :
721 public MaterialVisitor
722 {
723 public:
724 void visit(const MaterialPtr& shader) override
725 {
726 // Wrap this method to python
727 PYBIND11_OVERLOAD_PURE(
728 void, /* Return type */
729 MaterialVisitor, /* Parent class */
730 visit, /* Name of function in C++ (must match Python name) */
731 ScriptMaterial(shader) /* Argument(s) */
732 );
733 }
734 };
735
736 /**
737 * greebo: This class provides the script interface for the GlobalMaterialManager module.
738 */
739 class ShaderSystemInterface :
740 public IScriptInterface
741 {
742 public:
743 void foreachMaterial(MaterialVisitor& visitor);
744 ScriptMaterial getMaterial(const std::string& name);
745 bool materialExists(const std::string& name);
746 bool materialCanBeModified(const std::string& name);
747 ScriptMaterial createEmptyMaterial(const std::string& name);
748 ScriptMaterial copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy);
749 bool renameMaterial(const std::string& oldName, const std::string& newName);
750 void removeMaterial(const std::string& name);
751 void saveMaterial(const std::string& name);
752
753 // IScriptInterface implementation
754 void registerInterface(py::module& scope, py::dict& globals) override;
755 };
756
757 } // namespace script