MaterialX 1.38.10
Loading...
Searching...
No Matches
TextureBaker.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_TEXTUREBAKER
7#define MATERIALX_TEXTUREBAKER
8
11
12#include <iostream>
13
14#include <MaterialXCore/Unit.h>
15
20
21MATERIALX_NAMESPACE_BEGIN
22
24using BakedDocumentVec = std::vector<std::pair<std::string, DocumentPtr>>;
25
30template <typename Renderer, typename ShaderGen>
31class TextureBaker : public Renderer
32{
33 public:
35 void setExtension(const string& extension)
36 {
37 _extension = extension;
38 }
39
41 const string& getExtension() const
42 {
43 return _extension;
44 }
45
52 void setColorSpace(const string& colorSpace)
53 {
54 _colorSpace = colorSpace;
55 }
56
58 const string& getColorSpace() const
59 {
60 return _colorSpace;
61 }
62
64 void setDistanceUnit(const string& unitSpace)
65 {
66 _distanceUnit = unitSpace;
67 }
68
70 const string& getDistanceUnit() const
71 {
72 return _distanceUnit;
73 }
74
76 void setAverageImages(bool enable)
77 {
78 _averageImages = enable;
79 }
80
82 bool getAverageImages() const
83 {
84 return _averageImages;
85 }
86
88 void setOptimizeConstants(bool enable)
89 {
90 _optimizeConstants = enable;
91 }
92
95 {
96 return _optimizeConstants;
97 }
98
101 void setOutputImagePath(const FilePath& outputImagePath)
102 {
103 _outputImagePath = outputImagePath;
104 }
105
108 {
109 return _outputImagePath;
110 }
111
113 void setBakedGraphName(const string& name)
114 {
115 _bakedGraphName = name;
116 }
117
119 const string& getBakedGraphName() const
120 {
121 return _bakedGraphName;
122 }
123
125 void setBakedGeomInfoName(const string& name)
126 {
127 _bakedGeomInfoName = name;
128 }
129
131 const string& getBakedGeomInfoName() const
132 {
133 return _bakedGeomInfoName;
134 }
135
137 const string& getTextureFilenameTemplate() const
138 {
139 return _textureFilenameTemplate;
140 }
141
143 void setTextureFilenameTemplate(const string& filenameTemplate)
144 {
145 _textureFilenameTemplate = (filenameTemplate.find("$EXTENSION") == string::npos) ?
146 filenameTemplate + ".$EXTENSION" : filenameTemplate;
147 }
148
150 void setFilenameTemplateVarOverride(const string& key, const string& value)
151 {
152 if (_permittedOverrides.count(key))
153 {
154 _texTemplateOverrides[key] = value;
155 }
156 }
157
159 void setOutputStream(std::ostream* outputStream)
160 {
161 _outputStream = outputStream;
162 }
163
165 std::ostream* getOutputStream() const
166 {
167 return _outputStream;
168 }
169
173 void setHashImageNames(bool enable)
174 {
175 _hashImageNames = enable;
176 }
177
179 bool getHashImageNames() const
180 {
181 return _hashImageNames;
182 }
183
186 {
187 _textureSpaceMin = min;
188 }
189
192 {
193 return _textureSpaceMin;
194 }
195
198 {
199 _textureSpaceMax = max;
200 }
201
204 {
205 return _textureSpaceMax;
206 }
207
209 void setupUnitSystem(DocumentPtr unitDefinitions);
210
212 void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext& context, const string& udim = EMPTY_STRING);
213
215 void bakeGraphOutput(OutputPtr output, GenContext& context, const StringMap& filenameTemplateMap);
216
218 void optimizeBakedTextures(NodePtr shader);
219
221 DocumentPtr bakeMaterialToDoc(DocumentPtr doc, const FileSearchPath& searchPath, const string& materialPath,
222 const StringVec& udimSet, std::string& documentName);
223
226 void bakeAllMaterials(DocumentPtr doc, const FileSearchPath& searchPath, const FilePath& outputFileName);
227
231 {
232 _writeDocumentPerMaterial = value;
233 }
234
235 string getValueStringFromColor(const Color4& color, const string& type);
236
237 protected:
239 {
240 public:
241 FilePath filename;
242 Color4 uniformColor;
243 bool isUniform = false;
244 };
246 {
247 public:
248 Color4 color;
249 bool isDefault = false;
250 };
251 using BakedImageVec = vector<BakedImage>;
252 using BakedImageMap = std::unordered_map<OutputPtr, BakedImageVec>;
253 using BakedConstantMap = std::unordered_map<OutputPtr, BakedConstant>;
254
255 protected:
256 TextureBaker(unsigned int width, unsigned int height, Image::BaseType baseType, bool flipSavedImage);
257
258 // Populate file template variable naming map
259 StringMap initializeFileTemplateMap(InputPtr input, NodePtr shader, const string& udim = EMPTY_STRING);
260
261 // Find first occurence of variable in filename from start index onwards
262 size_t findVarInTemplate(const string& filename, const string& var, size_t start = 0);
263
264 // Generate a texture filename for the given graph output.
265 FilePath generateTextureFilename(const StringMap& fileTemplateMap);
266
267 // Create document that links shader outputs to a material.
268 DocumentPtr generateNewDocumentFromShader(NodePtr shader, const StringVec& udimSet);
269
270 // Write a baked image to disk, returning true if the write was successful.
271 bool writeBakedImage(const BakedImage& baked, ImagePtr image);
272
273 protected:
274 string _extension;
275 string _colorSpace;
276 string _distanceUnit;
277 bool _averageImages;
278 bool _optimizeConstants;
279 FilePath _outputImagePath;
280 string _bakedGraphName;
281 string _bakedGeomInfoName;
282 string _textureFilenameTemplate;
283 std::ostream* _outputStream;
284 bool _hashImageNames;
285 Vector2 _textureSpaceMin;
286 Vector2 _textureSpaceMax;
287
288 ShaderGeneratorPtr _generator;
289 ConstNodePtr _material;
290 ImagePtr _frameCaptureImage;
291 BakedImageMap _bakedImageMap;
292 BakedConstantMap _bakedConstantMap;
293 StringSet _permittedOverrides;
294 StringMap _texTemplateOverrides;
295 StringMap _bakedInputMap;
296
297 std::unordered_map<string, NodePtr> _worldSpaceNodes;
298
299 bool _flipSavedImage;
300
301 bool _writeDocumentPerMaterial;
302 DocumentPtr _bakedTextureDoc;
303};
304
305MATERIALX_NAMESPACE_END
306
307#include <MaterialXRender/TextureBaker.inl>
308
309#endif
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
Cross-platform support for file and search paths.
Context classes for shader generation.
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
Image handler interfaces.
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:31
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
Macros for declaring imported and exported symbols.
std::vector< std::pair< std::string, DocumentPtr > > BakedDocumentVec
A vector of baked documents with their associated names.
Definition: TextureBaker.h:24
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24
Unit classes.
A four-component color value.
Definition: Types.h:364
A generic file path, supporting both syntactic and file system operations.
Definition: File.h:27
A sequence of file paths, which may be queried to find the first instance of a given filename on the ...
Definition: File.h:219
A context class for shader generation.
Definition: GenContext.h:31
Definition: TextureBaker.h:246
Definition: TextureBaker.h:239
A helper class for baking procedural material content to textures.
Definition: TextureBaker.h:32
void setBakedGeomInfoName(const string &name)
Set the name of the baked geometry info element.
Definition: TextureBaker.h:125
void setBakedGraphName(const string &name)
Set the name of the baked graph element.
Definition: TextureBaker.h:113
void setFilenameTemplateVarOverride(const string &key, const string &value)
Set texFilenameOverrides if template variable exists.
Definition: TextureBaker.h:150
void setTextureSpaceMax(const Vector2 &max)
Set the maximum texcoords used in texture baking. Defaults to 1, 1.
Definition: TextureBaker.h:197
const FilePath & getOutputImagePath()
Get the current output location for baked texture images.
Definition: TextureBaker.h:107
std::ostream * getOutputStream() const
Return the output stream for reporting progress and warnings.
Definition: TextureBaker.h:165
bool getAverageImages() const
Return whether images should be averaged to generate constants.
Definition: TextureBaker.h:82
bool getHashImageNames() const
Return whether automatic baked texture resolution is set.
Definition: TextureBaker.h:179
void setDistanceUnit(const string &unitSpace)
Set the distance unit to which textures are baked. Defaults to meters.
Definition: TextureBaker.h:64
const string & getBakedGraphName() const
Return the name of the baked graph element.
Definition: TextureBaker.h:119
const string & getColorSpace() const
Return the color space in which color textures are encoded.
Definition: TextureBaker.h:58
void setColorSpace(const string &colorSpace)
Set the color space in which color textures are encoded.
Definition: TextureBaker.h:52
void setOutputStream(std::ostream *outputStream)
Set the output stream for reporting progress and warnings. Defaults to std::cout.
Definition: TextureBaker.h:159
void setOutputImagePath(const FilePath &outputImagePath)
Set the output location for baked texture images.
Definition: TextureBaker.h:101
void bakeAllMaterials(DocumentPtr doc, const FileSearchPath &searchPath, const FilePath &outputFileName)
Bake materials in the given document and write them to disk.
Definition: TextureBaker.inl:552
void setAverageImages(bool enable)
Set whether images should be averaged to generate constants. Defaults to false.
Definition: TextureBaker.h:76
void setOptimizeConstants(bool enable)
Set whether uniform textures should be stored as constants. Defaults to true.
Definition: TextureBaker.h:88
DocumentPtr bakeMaterialToDoc(DocumentPtr doc, const FileSearchPath &searchPath, const string &materialPath, const StringVec &udimSet, std::string &documentName)
Bake material to document in memory and write baked textures to disk.
Definition: TextureBaker.inl:484
void setupUnitSystem(DocumentPtr unitDefinitions)
Set up the unit definitions to be used in baking.
Definition: TextureBaker.inl:629
Vector2 getTextureSpaceMax() const
Return the maximum texcoords used in texture baking.
Definition: TextureBaker.h:203
void setTextureSpaceMin(const Vector2 &min)
Set the minimum texcoords used in texture baking. Defaults to 0, 0.
Definition: TextureBaker.h:185
void setExtension(const string &extension)
Set the file extension for baked textures.
Definition: TextureBaker.h:35
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:173
bool getOptimizeConstants() const
Return whether uniform textures should be stored as constants.
Definition: TextureBaker.h:94
const string & getDistanceUnit() const
Return the distance unit to which textures are baked.
Definition: TextureBaker.h:70
void optimizeBakedTextures(NodePtr shader)
Optimize baked textures before writing.
Definition: TextureBaker.inl:259
void writeDocumentPerMaterial(bool value)
Set whether to write a separate document per material when calling bakeAllMaterials.
Definition: TextureBaker.h:230
const string & getBakedGeomInfoName() const
Return the name of the baked geometry info element.
Definition: TextureBaker.h:131
Vector2 getTextureSpaceMin() const
Return the minimum texcoords used in texture baking.
Definition: TextureBaker.h:191
const string & getTextureFilenameTemplate() const
Get the texture filename template.
Definition: TextureBaker.h:137
const string & getExtension() const
Return the file extension for baked textures.
Definition: TextureBaker.h:41
void setTextureFilenameTemplate(const string &filenameTemplate)
Set the texture filename template.
Definition: TextureBaker.h:143
void bakeGraphOutput(OutputPtr output, GenContext &context, const StringMap &filenameTemplateMap)
Bake a texture for the given graph output.
Definition: TextureBaker.inl:218
void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext &context, const string &udim=EMPTY_STRING)
Bake textures for all graph inputs of the given shader.
Definition: TextureBaker.inl:178
A vector of two floating-point values.
Definition: Types.h:286