MaterialX 1.38.2
TextureBaker.h
Go to the documentation of this file.
1//
2// TM & (c) 2019 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_TEXTUREBAKER
7#define MATERIALX_TEXTUREBAKER
8
11
12#include <iostream>
13
14#include <MaterialXCore/Unit.h>
15
17
20
22
23namespace MaterialX
24{
25
27using TextureBakerPtr = shared_ptr<class TextureBaker>;
28
30using BakedDocumentVec = std::vector<std::pair<std::string, DocumentPtr>>;
31
36class MX_RENDERGLSL_API TextureBaker : public GlslRenderer
37{
38 public:
39 static TextureBakerPtr create(unsigned int width = 1024, unsigned int height = 1024, Image::BaseType baseType = Image::BaseType::UINT8)
40 {
41 return TextureBakerPtr(new TextureBaker(width, height, baseType));
42 }
43
45 void setExtension(const string& extension)
46 {
47 _extension = extension;
48 }
49
51 const string& getExtension() const
52 {
53 return _extension;
54 }
55
62 void setColorSpace(const string& colorSpace)
63 {
64 _colorSpace = colorSpace;
65 }
66
68 const string& getColorSpace() const
69 {
70 return _colorSpace;
71 }
72
74 void setDistanceUnit(const string& unitSpace)
75 {
76 _distanceUnit = unitSpace;
77 }
78
80 const string& getDistanceUnit() const
81 {
82 return _distanceUnit;
83 }
84
86 void setAverageImages(bool enable)
87 {
88 _averageImages = enable;
89 }
90
92 bool getAverageImages() const
93 {
94 return _averageImages;
95 }
96
98 void setOptimizeConstants(bool enable)
99 {
100 _optimizeConstants = enable;
101 }
102
105 {
106 return _optimizeConstants;
107 }
108
111 void setOutputImagePath(const FilePath& outputImagePath)
112 {
113 _outputImagePath = outputImagePath;
114 }
115
118 {
119 return _outputImagePath;
120 }
121
123 void setBakedGraphName(const string& name)
124 {
125 _bakedGraphName= name;
126 }
127
129 const string& getBakedGraphName() const
130 {
131 return _bakedGraphName;
132 }
133
135 void setBakedGeomInfoName(const string& name)
136 {
137 _bakedGeomInfoName = name;
138 }
139
141 const string& getBakedGeomInfoName() const
142 {
143 return _bakedGeomInfoName;
144 }
145
147 const string& getTextureFilenameTemplate() const
148 {
149 return _textureFilenameTemplate;
150 }
151
153 void setTextureFilenameTemplate(const string& filenameTemplate)
154 {
155 _textureFilenameTemplate = (filenameTemplate.find("$EXTENSION") == string::npos) ?
156 filenameTemplate + ".$EXTENSION" : filenameTemplate;
157 }
158
160 void setFilenameTemplateVarOverride(const string& key, const string& value)
161 {
162 if (_permittedOverrides.count(key))
163 {
164 _texTemplateOverrides[key] = value;
165 }
166 }
167
169 void setOutputStream(std::ostream* outputStream)
170 {
171 _outputStream = outputStream;
172 }
173
175 std::ostream* getOutputStream() const
176 {
177 return _outputStream;
178 }
179
183 void setHashImageNames(bool enable)
184 {
185 _hashImageNames = enable;
186 }
187
189 bool getHashImageNames() const
190 {
191 return _hashImageNames;
192 }
193
195 void setupUnitSystem(DocumentPtr unitDefinitions);
196
198 void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext& context, const string& udim = EMPTY_STRING);
199
201 void bakeGraphOutput(OutputPtr output, GenContext& context, const StringMap& filenameTemplateMap);
202
204 void optimizeBakedTextures(NodePtr shader);
205
207 DocumentPtr bakeMaterial(NodePtr shader, const StringVec& udimSet);
208
210 BakedDocumentVec createBakeDocuments(DocumentPtr doc, const FileSearchPath& searchPath);
211
214 void bakeAllMaterials(DocumentPtr doc, const FileSearchPath& searchPath, const FilePath& outputFileName);
215
216 protected:
218 {
219 public:
220 FilePath filename;
221 Color4 uniformColor;
222 bool isUniform = false;
223 };
225 {
226 public:
227 Color4 color;
228 bool isDefault = false;
229 };
230 using BakedImageVec = vector<BakedImage>;
231 using BakedImageMap = std::unordered_map<OutputPtr, BakedImageVec>;
232 using BakedConstantMap = std::unordered_map<OutputPtr, BakedConstant>;
233
234 protected:
235 TextureBaker(unsigned int width, unsigned int height, Image::BaseType baseType);
236
237 // Populate file template variable naming map
238 StringMap initializeFileTemplateMap(InputPtr input, NodePtr shader, const string& udim = EMPTY_STRING);
239
240 // Find first occurence of variable in filename from start index onwards
241 size_t findVarInTemplate(const string& filename, const string& var, size_t start = 0);
242
243 // Generate a texture filename for the given graph output.
244 FilePath generateTextureFilename(const StringMap& fileTemplateMap);
245
246 // Write a baked image to disk, returning true if the write was successful.
247 bool writeBakedImage(const BakedImage& baked, ImagePtr image);
248
249 protected:
250 string _extension;
251 string _colorSpace;
252 string _distanceUnit;
253 bool _averageImages;
254 bool _optimizeConstants;
255 FilePath _outputImagePath;
256 string _bakedGraphName;
257 string _bakedGeomInfoName;
258 string _textureFilenameTemplate;
259 std::ostream* _outputStream;
260 bool _hashImageNames;
261
262 ShaderGeneratorPtr _generator;
263 ConstNodePtr _material;
264 ImagePtr _frameCaptureImage;
265 BakedImageMap _bakedImageMap;
266 BakedConstantMap _bakedConstantMap;
267 StringSet _permittedOverrides;
268 StringMap _texTemplateOverrides;
269 StringMap _bakedInputMap;
270
271 std::unordered_map<string, NodePtr> _worldSpaceNodes;
272};
273
274} // namespace MaterialX
275
276#endif
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:23
OpenGL texture handler.
GLSL code renderer.
GLSL shader generator.
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:24
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:32
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:37
std::set< string > StringSet
A set of strings.
Definition: Library.h:60
vector< string > StringVec
A vector of strings.
Definition: Library.h:56
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:58
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
Macros for declaring imported and exported symbols.
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:27
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:25
shared_ptr< class TextureBaker > TextureBakerPtr
A shared pointer to a TextureBaker.
Definition: TextureBaker.h:27
std::vector< std::pair< std::string, DocumentPtr > > BakedDocumentVec
A vector of baked documents with their associated names.
Definition: TextureBaker.h:30
Unit classes.
A four-component color value.
Definition: Types.h:389
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:28
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:213
A context class for shader generation.
Definition: GenContext.h:27
Helper class for rendering generated GLSL code to produce images.
Definition: GlslRenderer.h:43
Definition: TextureBaker.h:225
Definition: TextureBaker.h:218
A helper class for baking procedural material content to textures.
Definition: TextureBaker.h:37
void setBakedGeomInfoName(const string &name)
Set the name of the baked geometry info element.
Definition: TextureBaker.h:135
void setBakedGraphName(const string &name)
Set the name of the baked graph element.
Definition: TextureBaker.h:123
void setFilenameTemplateVarOverride(const string &key, const string &value)
Set texFilenameOverrides if template variable exists.
Definition: TextureBaker.h:160
const FilePath & getOutputImagePath()
Get the current output location for baked texture images.
Definition: TextureBaker.h:117
std::ostream * getOutputStream() const
Return the output stream for reporting progress and warnings.
Definition: TextureBaker.h:175
bool getAverageImages() const
Return whether images should be averaged to generate constants.
Definition: TextureBaker.h:92
bool getHashImageNames() const
Return whether automatic baked texture resolution is set.
Definition: TextureBaker.h:189
void setDistanceUnit(const string &unitSpace)
Set the distance unit to which textures are baked. Defaults to meters.
Definition: TextureBaker.h:74
const string & getBakedGraphName() const
Return the name of the baked graph element.
Definition: TextureBaker.h:129
const string & getColorSpace() const
Return the color space in which color textures are encoded.
Definition: TextureBaker.h:68
void setColorSpace(const string &colorSpace)
Set the color space in which color textures are encoded.
Definition: TextureBaker.h:62
void setOutputStream(std::ostream *outputStream)
Set the output stream for reporting progress and warnings. Defaults to std::cout.
Definition: TextureBaker.h:169
void setOutputImagePath(const FilePath &outputImagePath)
Set the output location for baked texture images.
Definition: TextureBaker.h:111
void setAverageImages(bool enable)
Set whether images should be averaged to generate constants. Defaults to false.
Definition: TextureBaker.h:86
void setOptimizeConstants(bool enable)
Set whether uniform textures should be stored as constants. Defaults to true.
Definition: TextureBaker.h:98
void setExtension(const string &extension)
Set the file extension for baked textures.
Definition: TextureBaker.h:45
void setHashImageNames(bool enable)
Set whether to create a short name for baked images by hashing the baked image filenames This is usef...
Definition: TextureBaker.h:183
bool getOptimizeConstants() const
Return whether uniform textures should be stored as constants.
Definition: TextureBaker.h:104
const string & getDistanceUnit() const
Return the distance unit to which textures are baked.
Definition: TextureBaker.h:80
const string & getBakedGeomInfoName() const
Return the name of the baked geometry info element.
Definition: TextureBaker.h:141
const string & getTextureFilenameTemplate() const
Get the texture filename template.
Definition: TextureBaker.h:147
const string & getExtension() const
Return the file extension for baked textures.
Definition: TextureBaker.h:51
void setTextureFilenameTemplate(const string &filenameTemplate)
Set the texture filename template.
Definition: TextureBaker.h:153