6#ifndef MATERIALX_TEXTUREBAKER
7#define MATERIALX_TEXTUREBAKER
21MATERIALX_NAMESPACE_BEGIN
30template <
typename Renderer,
typename ShaderGen>
31class TextureBaker :
public Renderer
37 _extension = extension;
54 _colorSpace = colorSpace;
66 _distanceUnit = unitSpace;
78 _averageImages = enable;
84 return _averageImages;
90 _optimizeConstants = enable;
96 return _optimizeConstants;
103 _outputImagePath = outputImagePath;
109 return _outputImagePath;
115 _bakedGraphName = name;
121 return _bakedGraphName;
127 _bakedGeomInfoName = name;
133 return _bakedGeomInfoName;
139 return _textureFilenameTemplate;
145 _textureFilenameTemplate = (filenameTemplate.find(
"$EXTENSION") == string::npos) ?
146 filenameTemplate +
".$EXTENSION" : filenameTemplate;
152 if (_permittedOverrides.count(key))
154 _texTemplateOverrides[key] = value;
161 _outputStream = outputStream;
167 return _outputStream;
175 _hashImageNames = enable;
181 return _hashImageNames;
187 _textureSpaceMin = min;
193 return _textureSpaceMin;
199 _textureSpaceMax = max;
205 return _textureSpaceMax;
222 const StringVec& udimSet, std::string& documentName);
232 _writeDocumentPerMaterial = value;
235 string getValueStringFromColor(
const Color4& color,
const string& type);
243 bool isUniform =
false;
249 bool isDefault =
false;
251 using BakedImageVec = vector<BakedImage>;
252 using BakedImageMap = std::unordered_map<OutputPtr, BakedImageVec>;
253 using BakedConstantMap = std::unordered_map<OutputPtr, BakedConstant>;
256 TextureBaker(
unsigned int width,
unsigned int height, Image::BaseType baseType,
bool flipSavedImage);
262 size_t findVarInTemplate(
const string& filename,
const string& var,
size_t start = 0);
276 string _distanceUnit;
278 bool _optimizeConstants;
280 string _bakedGraphName;
281 string _bakedGeomInfoName;
282 string _textureFilenameTemplate;
283 std::ostream* _outputStream;
284 bool _hashImageNames;
291 BakedImageMap _bakedImageMap;
292 BakedConstantMap _bakedConstantMap;
297 std::unordered_map<string, NodePtr> _worldSpaceNodes;
299 bool _flipSavedImage;
301 bool _writeDocumentPerMaterial;
305MATERIALX_NAMESPACE_END
307#include <MaterialXRender/TextureBaker.inl>
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:64
vector< string > StringVec
A vector of strings.
Definition Library.h:60
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition Library.h:62
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
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
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.
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.
void setupUnitSystem(DocumentPtr unitDefinitions)
Set up the unit definitions to be used in baking.
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.
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.
void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext &context, const string &udim=EMPTY_STRING)
Bake textures for all graph inputs of the given shader.
A vector of two floating-point values.
Definition Types.h:286