forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterial.h
576 lines (480 loc) · 17.9 KB
/
Material.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_MATERIAL_H
#define MATERIALX_MATERIAL_H
/// @file
/// Material element subclasses
#include <MaterialXCore/Library.h>
#include <MaterialXCore/Geom.h>
#include <MaterialXCore/Node.h>
#include <MaterialXCore/Value.h>
namespace MaterialX
{
class Material;
class ShaderRef;
class BindParam;
class BindInput;
class BindToken;
class Override;
class MaterialInherit;
class MaterialAssign;
class Collection;
/// A shared pointer to a Material
using MaterialPtr = shared_ptr<Material>;
/// A shared pointer to a const Material
using ConstMaterialPtr = shared_ptr<const Material>;
/// A shared pointer to a ShaderRef
using ShaderRefPtr = shared_ptr<ShaderRef>;
/// A shared pointer to a const ShaderRef
using ConstShaderRefPtr = shared_ptr<const ShaderRef>;
/// A shared pointer to a BindParam
using BindParamPtr = shared_ptr<BindParam>;
/// A shared pointer to a const BindParam
using ConstBindParamPtr = shared_ptr<const BindParam>;
/// A shared pointer to a BindInput
using BindInputPtr = shared_ptr<BindInput>;
/// A shared pointer to a const BindInput
using ConstBindInputPtr = shared_ptr<const BindInput>;
/// A shared pointer to a BindToken
using BindTokenPtr = shared_ptr<BindToken>;
/// A shared pointer to a const BindToken
using ConstBindTokenPtr = shared_ptr<const BindToken>;
/// @class Material
/// A material element within a Document.
///
/// A Material instantiates one or more shader nodes with a specific set of
/// data bindings.
class Material : public Element
{
public:
Material(ElementPtr parent, const string& name) :
Element(parent, CATEGORY, name)
{
}
virtual ~Material() { }
protected:
using MaterialAssignPtr = shared_ptr<MaterialAssign>;
using CollectionPtr = shared_ptr<Collection>;
public:
/// @name ShaderRef Elements
/// @{
/// Add a ShaderRef to the material.
/// @param name The name of the new ShaderRef.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @param node An optional node string, which should match the node
/// attribute of the NodeDef to be referenced.
/// @return A shared pointer to the new ShaderRef.
ShaderRefPtr addShaderRef(const string& name = EMPTY_STRING,
const string& node = EMPTY_STRING);
/// Return the ShaderRef, if any, with the given name.
ShaderRefPtr getShaderRef(const string& name) const
{
return getChildOfType<ShaderRef>(name);
}
/// Return a vector of all ShaderRef elements in the material.
vector<ShaderRefPtr> getShaderRefs() const
{
return getChildrenOfType<ShaderRef>();
}
/// Return a vector of all ShaderRef elements that belong to this material,
/// taking material inheritance into account.
vector<ShaderRefPtr> getActiveShaderRefs() const;
/// Remove the ShaderRef, if any, with the given name.
void removeShaderRef(const string& name)
{
removeChildOfType<ShaderRef>(name);
}
/// @}
/// @name NodeDef References
/// @{
/// Return a vector of all shader nodedefs referenced by this material,
/// optionally filtered by the given target name and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are returned.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are returned.
/// @return A vector of shared pointers to NodeDef elements.
vector<NodeDefPtr> getShaderNodeDefs(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const;
/// @}
/// @name Primary Shader
/// @{
/// Return the nodedef of the first shader referenced by this material,
/// optionally filtered by the given target and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are considered.
/// @return The nodedef of the first matching shader referenced by this
/// material, or an empty shared pointer if no matching shader was found.
NodeDefPtr getPrimaryShaderNodeDef(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const
{
vector<NodeDefPtr> shaderDefs = getShaderNodeDefs(target, type);
return shaderDefs.empty() ? NodeDefPtr() : shaderDefs[0];
}
/// Return the node name of the first shader referenced by this material,
/// optionally filtered by the given target and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are considered.
/// @return The node name of the first matching shader referenced by this
/// material, or an empty string if no matching shader was found.
string getPrimaryShaderName(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const
{
NodeDefPtr nodeDef = getPrimaryShaderNodeDef(target, type);
return nodeDef ? nodeDef->getNodeString() : EMPTY_STRING;
}
/// Return the parameters of the first shader referenced by this material,
/// optionally filtered by the given target and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are considered.
/// @return The parameters of the first matching shader referenced by this
/// material, or an empty vector if no matching shader was found.
vector<ParameterPtr> getPrimaryShaderParameters(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const;
/// Return the inputs of the first shader referenced by this material,
/// optionally filtered by the given target and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are considered.
/// @return The inputs of the first matching shader referenced by this
/// material, or an empty vector if no matching shader was found.
vector<InputPtr> getPrimaryShaderInputs(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const;
/// Return the tokens of the first shader referenced by this material,
/// optionally filtered by the given target and shader type.
/// @param target An optional target name, which will be used to filter
/// the shader nodedefs that are considered.
/// @param type An optional shader type (e.g. "surfaceshader"), which will
/// be used to filter the shader nodedefs that are considered.
/// @return The tokens of the first matching shader referenced by this
/// material, or an empty vector if no matching shader was found.
vector<TokenPtr> getPrimaryShaderTokens(const string& target = EMPTY_STRING,
const string& type = EMPTY_STRING) const;
/// @}
/// @name Geometry Bindings
/// @{
/// Return a vector of all MaterialAssign elements that bind this material
/// to the given geometry string.
/// @param geom The geometry for which material bindings should be returned.
/// By default, this argument is the universal geometry string "/", and
/// all material bindings are returned.
vector<MaterialAssignPtr> getGeometryBindings(const string& geom = UNIVERSAL_GEOM_NAME) const;
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string CATEGORY;
};
/// @class BindParam
/// A bind parameter element within a ShaderRef.
///
/// A BindParam binds uniform data to a Parameter of a shader NodeDef within
/// the scope of a Material.
class BindParam : public ValueElement
{
public:
BindParam(ElementPtr parent, const string& name) :
ValueElement(parent, CATEGORY, name)
{
}
virtual ~BindParam() { }
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string CATEGORY;
};
/// @class BindInput
/// A bind input element within a ShaderRef.
///
/// A BindInput binds spatially-varying data to an Input of a shader NodeDef
/// within the scope of a material.
class BindInput : public ValueElement
{
public:
BindInput(ElementPtr parent, const string& name) :
ValueElement(parent, CATEGORY, name)
{
}
virtual ~BindInput() { }
/// @}
/// @name NodeGraph String
/// @{
/// Set the node graph string of this element.
void setNodeGraphString(const string& graph)
{
setAttribute(NODE_GRAPH_ATTRIBUTE, graph);
}
/// Return true if this element has a node graph string.
bool hasNodeGraphString() const
{
return hasAttribute(NODE_GRAPH_ATTRIBUTE);
}
/// Return the node graph string of this element.
const string& getNodeGraphString() const
{
return getAttribute(NODE_GRAPH_ATTRIBUTE);
}
/// @}
/// @name Output String
/// @{
/// Set the output string of this element.
void setOutputString(const string& output)
{
setAttribute(OUTPUT_ATTRIBUTE, output);
}
/// Return true if this element has an output string.
bool hasOutputString() const
{
return hasAttribute(OUTPUT_ATTRIBUTE);
}
/// Return the output string of this element.
const string& getOutputString() const
{
return getAttribute(OUTPUT_ATTRIBUTE);
}
/// @}
/// @name Connections
/// @{
/// Set the output to which the BindInput is connected.
void setConnectedOutput(ConstOutputPtr output);
/// Return the output, if any, to which the BindInput is connected.
OutputPtr getConnectedOutput() const;
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string CATEGORY;
static const string NODE_GRAPH_ATTRIBUTE;
static const string OUTPUT_ATTRIBUTE;
};
/// @class BindToken
/// A bind token element within a ShaderRef.
///
/// A BindToken binds a string value to a Token of a shader NodeDef within
/// the scope of a material.
class BindToken : public ValueElement
{
public:
BindToken(ElementPtr parent, const string& name) :
ValueElement(parent, CATEGORY, name)
{
}
virtual ~BindToken() { }
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
public:
static const string CATEGORY;
};
/// @class ShaderRef
/// A shader reference element within a Material.
///
/// A ShaderRef instantiates a shader NodeDef within the context of a Material.
class ShaderRef : public TypedElement
{
public:
ShaderRef(ElementPtr parent, const string& name) :
TypedElement(parent, CATEGORY, name)
{
}
virtual ~ShaderRef() { }
/// @name Node String
/// @{
/// Set the node string of the ShaderRef. This attribute declares a
/// ShaderRef as a reference to the first NodeDef with the matching
/// node string.
void setNodeString(const string& node)
{
setAttribute(NODE_ATTRIBUTE, node);
}
/// Return true if the given ShaderRef has a node string.
bool hasNodeString() const
{
return hasAttribute(NODE_ATTRIBUTE);
}
/// Return the node string of the ShaderRef.
const string& getNodeString() const
{
return getAttribute(NODE_ATTRIBUTE);
}
/// @}
/// @name NodeDef String
/// @{
/// Set the NodeDef string for the ShaderRef. This attribute declares a
/// ShaderRef as a reference to the unique NodeDef with the given name.
void setNodeDefString(const string& nodeDef)
{
setAttribute(NODE_DEF_ATTRIBUTE, nodeDef);
}
/// Return true if the given ShaderRef has a NodeDef string.
bool hasNodeDefString() const
{
return hasAttribute(NODE_DEF_ATTRIBUTE);
}
/// Return the NodeDef string for the ShaderRef.
const string& getNodeDefString() const
{
return getAttribute(NODE_DEF_ATTRIBUTE);
}
/// @}
/// @name BindParam Elements
/// @{
/// Add a BindParam to the ShaderRef.
/// @param name The name of the new BindParam.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @param type An optional type string.
/// @return A shared pointer to the new BindParam.
BindParamPtr addBindParam(const string& name, const string& type = DEFAULT_TYPE_STRING)
{
BindParamPtr child = addChild<BindParam>(name);
child->setType(type);
return child;
}
/// Return the BindParam, if any, with the given name.
BindParamPtr getBindParam(const string& name) const
{
return getChildOfType<BindParam>(name);
}
/// Return a vector of all BindParam elements in the ShaderRef.
vector<BindParamPtr> getBindParams() const
{
return getChildrenOfType<BindParam>();
}
/// Remove the BindParam, if any, with the given name.
void removeBindParam(const string& name)
{
removeChildOfType<BindParam>(name);
}
/// @}
/// @name BindInput Elements
/// @{
/// Add a BindInput to the ShaderRef.
/// @param name The name of the new BindInput.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @param type An optional type string.
/// @return A shared pointer to the new BindInput.
BindInputPtr addBindInput(const string& name, const string& type = DEFAULT_TYPE_STRING)
{
BindInputPtr child = addChild<BindInput>(name);
child->setType(type);
return child;
}
/// Return the BindInput, if any, with the given name.
BindInputPtr getBindInput(const string& name) const
{
return getChildOfType<BindInput>(name);
}
/// Return a vector of all BindInput elements in the ShaderRef.
vector<BindInputPtr> getBindInputs() const
{
return getChildrenOfType<BindInput>();
}
/// Remove the BindInput, if any, with the given name.
void removeBindInput(const string& name)
{
removeChildOfType<BindInput>(name);
}
/// @}
/// @name BindToken Elements
/// @{
/// Add a BindToken to the ShaderRef.
/// @param name The name of the new BindToken.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new BindToken.
BindTokenPtr addBindToken(const string& name)
{
return addChild<BindToken>(name);
}
/// Return the BindToken, if any, with the given name.
BindTokenPtr getBindToken(const string& name) const
{
return getChildOfType<BindToken>(name);
}
/// Return a vector of all BindInput elements in the ShaderRef.
vector<BindTokenPtr> getBindTokens() const
{
return getChildrenOfType<BindToken>();
}
/// Remove the BindToken, if any, with the given name.
void removeBindToken(const string& name)
{
removeChildOfType<BindToken>(name);
}
/// @}
/// @name NodeDef References
/// @{
/// Return the NodeDef, if any, that this element references.
NodeDefPtr getNodeDef() const;
/// @}
/// @name Output References
/// @{
/// Return a vector of all outputs that this element references.
vector<OutputPtr> getReferencedOutputs() const
{
vector<OutputPtr> outputVec;
std::set<OutputPtr> outputSet;
for (BindInputPtr bindInput : getBindInputs())
{
OutputPtr output = bindInput->getConnectedOutput();
if (output && !outputSet.count(output))
{
outputVec.push_back(output);
outputSet.insert(output);
}
}
return outputVec;
}
/// @}
/// @name Validation
/// @{
/// Validate that the given element tree, including all descendants, is
/// consistent with the MaterialX specification.
bool validate(string* message = nullptr) const override;
/// @}
/// @name Traversal
/// @{
/// Return the Edge with the given index that lies directly upstream from
/// this element in the dataflow graph.
Edge getUpstreamEdge(ConstMaterialPtr material = nullptr,
size_t index = 0) const override;
/// Return the number of queriable upstream edges for this element.
size_t getUpstreamEdgeCount() const override
{
return getBindInputs().size();
}
/// @}
public:
static const string CATEGORY;
static const string NODE_ATTRIBUTE;
static const string NODE_DEF_ATTRIBUTE;
};
} // namespace MaterialX
#endif