forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorManagementSystem.cpp
90 lines (75 loc) · 2.65 KB
/
ColorManagementSystem.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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenShader/ColorManagementSystem.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
MATERIALX_NAMESPACE_BEGIN
//
// ColorSpaceTransform methods
//
ColorSpaceTransform::ColorSpaceTransform(const string& ss, const string& ts, const TypeDesc* t) :
sourceSpace(ss),
targetSpace(ts),
type(t)
{
if (type != Type::COLOR3 && type != Type::COLOR4)
{
throw ExceptionShaderGenError("Color space transform can only be a color3 or color4.");
}
}
ColorManagementSystem::ColorManagementSystem()
{
}
void ColorManagementSystem::loadLibrary(DocumentPtr document)
{
_document = document;
}
bool ColorManagementSystem::supportsTransform(const ColorSpaceTransform& transform) const
{
if (!_document)
{
throw ExceptionShaderGenError("No library loaded for color management system");
}
ImplementationPtr impl = getImplementation(transform);
return impl != nullptr;
}
ShaderNodePtr ColorManagementSystem::createNode(const ShaderGraph* parent, const ColorSpaceTransform& transform, const string& name,
GenContext& context) const
{
ImplementationPtr impl = getImplementation(transform);
if (!impl)
{
throw ExceptionShaderGenError("No implementation found for transform: ('" + transform.sourceSpace + "', '" + transform.targetSpace + "').");
}
// Check if it's created and cached already,
// otherwise create and cache it.
ShaderNodeImplPtr nodeImpl = context.findNodeImplementation(impl->getName());
if (!nodeImpl)
{
nodeImpl = SourceCodeNode::create();
nodeImpl->initialize(*impl, context);
context.addNodeImplementation(impl->getName(), nodeImpl);
}
// Create the node.
ShaderNodePtr shaderNode = ShaderNode::create(parent, name, nodeImpl, ShaderNode::Classification::TEXTURE);
// Create ports on the node.
ShaderInput* input = shaderNode->addInput("in", transform.type);
if (transform.type == Type::COLOR3)
{
input->setValue(Value::createValue(Color3(0.0f, 0.0f, 0.0f)));
}
else if (transform.type == Type::COLOR4)
{
input->setValue(Value::createValue(Color4(0.0f, 0.0f, 0.0f, 1.0)));
}
else
{
throw ExceptionShaderGenError("Invalid type specified to createColorTransform: '" + transform.type->getName() + "'");
}
shaderNode->addOutput("out", transform.type);
return shaderNode;
}
MATERIALX_NAMESPACE_END