HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
9 /// @file
10 /// Texture baking functionality
11 
12 #include <iostream>
13 
14 #include <MaterialXCore/Unit.h>
15 
17 
20 
22 
24 
25 /// A shared pointer to a TextureBaker
26 using TextureBakerPtr = shared_ptr<class TextureBaker>;
27 
28 /// A vector of baked documents with their associated names.
29 using BakedDocumentVec = std::vector<std::pair<std::string, DocumentPtr>>;
30 
31 /// @class TextureBaker
32 /// A helper class for baking procedural material content to textures.
33 /// TODO: Add support for graphs containing geometric nodes such as position
34 /// and normal.
36 {
37  public:
38  static TextureBakerPtr create(unsigned int width = 1024, unsigned int height = 1024, Image::BaseType baseType = Image::BaseType::UINT8)
39  {
40  return TextureBakerPtr(new TextureBaker(width, height, baseType));
41  }
42 
43  /// Set the file extension for baked textures.
44  void setExtension(const string& extension)
45  {
46  _extension = extension;
47  }
48 
49  /// Return the file extension for baked textures.
50  const string& getExtension() const
51  {
52  return _extension;
53  }
54 
55  /// Set the color space in which color textures are encoded.
56  ///
57  /// By default, this color space is srgb_texture, and color inputs are
58  /// automatically transformed to this space by the baker. If another color
59  /// space is set, then the input graph is responsible for transforming
60  /// colors to this space.
61  void setColorSpace(const string& colorSpace)
62  {
63  _colorSpace = colorSpace;
64  }
65 
66  /// Return the color space in which color textures are encoded.
67  const string& getColorSpace() const
68  {
69  return _colorSpace;
70  }
71 
72  /// Set the distance unit to which textures are baked. Defaults to meters.
73  void setDistanceUnit(const string& unitSpace)
74  {
75  _distanceUnit = unitSpace;
76  }
77 
78  /// Return the distance unit to which textures are baked.
79  const string& getDistanceUnit() const
80  {
81  return _distanceUnit;
82  }
83 
84  /// Set whether images should be averaged to generate constants. Defaults to false.
86  {
87  _averageImages = enable;
88  }
89 
90  /// Return whether images should be averaged to generate constants.
91  bool getAverageImages() const
92  {
93  return _averageImages;
94  }
95 
96  /// Set whether uniform textures should be stored as constants. Defaults to true.
98  {
99  _optimizeConstants = enable;
100  }
101 
102  /// Return whether uniform textures should be stored as constants.
103  bool getOptimizeConstants() const
104  {
105  return _optimizeConstants;
106  }
107 
108  /// Set the output location for baked texture images. Defaults to the root folder
109  /// of the destination material.
110  void setOutputImagePath(const FilePath& outputImagePath)
111  {
112  _outputImagePath = outputImagePath;
113  }
114 
115  /// Get the current output location for baked texture images.
117  {
118  return _outputImagePath;
119  }
120 
121  /// Set the name of the baked graph element.
122  void setBakedGraphName(const string& name)
123  {
124  _bakedGraphName= name;
125  }
126 
127  /// Return the name of the baked graph element.
128  const string& getBakedGraphName() const
129  {
130  return _bakedGraphName;
131  }
132 
133  /// Set the name of the baked geometry info element.
134  void setBakedGeomInfoName(const string& name)
135  {
136  _bakedGeomInfoName = name;
137  }
138 
139  /// Return the name of the baked geometry info element.
140  const string& getBakedGeomInfoName() const
141  {
142  return _bakedGeomInfoName;
143  }
144 
145  /// Get the texture filename template.
146  const string& getTextureFilenameTemplate() const
147  {
148  return _textureFilenameTemplate;
149  }
150 
151  /// Set the texture filename template.
152  void setTextureFilenameTemplate(const string& filenameTemplate)
153  {
154  _textureFilenameTemplate = (filenameTemplate.find("$EXTENSION") == string::npos) ?
155  filenameTemplate + ".$EXTENSION" : filenameTemplate;
156  }
157 
158  /// Set texFilenameOverrides if template variable exists.
159  void setFilenameTemplateVarOverride(const string& key, const string& value)
160  {
161  if (_permittedOverrides.count(key))
162  {
163  _texTemplateOverrides[key] = value;
164  }
165  }
166 
167  /// Set the output stream for reporting progress and warnings. Defaults to std::cout.
168  void setOutputStream(std::ostream* outputStream)
169  {
170  _outputStream = outputStream;
171  }
172 
173  /// Return the output stream for reporting progress and warnings.
174  std::ostream* getOutputStream() const
175  {
176  return _outputStream;
177  }
178 
179  /// Set whether to create a short name for baked images by hashing the baked image filenames
180  /// This is useful for file systems which may have a maximum limit on filename size.
181  /// By default names are not hashed.
183  {
184  _hashImageNames = enable;
185  }
186 
187  /// Return whether automatic baked texture resolution is set.
188  bool getHashImageNames() const
189  {
190  return _hashImageNames;
191  }
192 
193  /// Set the minimum texcoords used in texture baking. Defaults to 0, 0.
195  {
196  _textureSpaceMin = min;
197  }
198 
199  /// Return the minimum texcoords used in texture baking.
201  {
202  return _textureSpaceMin;
203  }
204 
205  /// Set the maximum texcoords used in texture baking. Defaults to 1, 1.
207  {
208  _textureSpaceMax = max;
209  }
210 
211  /// Return the maximum texcoords used in texture baking.
213  {
214  return _textureSpaceMax;
215  }
216 
217  /// Set up the unit definitions to be used in baking.
218  void setupUnitSystem(DocumentPtr unitDefinitions);
219 
220  /// Bake textures for all graph inputs of the given shader.
221  void bakeShaderInputs(NodePtr material, NodePtr shader, GenContext& context, const string& udim = EMPTY_STRING);
222 
223  /// Bake a texture for the given graph output.
224  void bakeGraphOutput(OutputPtr output, GenContext& context, const StringMap& filenameTemplateMap);
225 
226  /// Optimize baked textures before writing.
227  void optimizeBakedTextures(NodePtr shader);
228 
229  /// Bake material to document in memory and write baked textures to disk.
230  DocumentPtr bakeMaterialToDoc(DocumentPtr doc, const FileSearchPath& searchPath, const string& materialPath,
231  const StringVec udimSet, std::string& documentName);
232 
233  /// Bake materials in the given document and write them to disk. If multiple documents are written,
234  /// then the given output filename will be used as a template.
235  void bakeAllMaterials(DocumentPtr doc, const FileSearchPath& searchPath, const FilePath& outputFileName);
236 
237  protected:
239  {
240  public:
243  bool isUniform = false;
244  };
246  {
247  public:
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);
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;
283  std::ostream* _outputStream;
287 
296 
297  std::unordered_map<string, NodePtr> _worldSpaceNodes;
298 };
299 
301 
302 #endif
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
GLboolean enable
Definition: glew.h:2750
void setOptimizeConstants(bool enable)
Set whether uniform textures should be stored as constants. Defaults to true.
Definition: TextureBaker.h:97
std::ostream * getOutputStream() const
Return the output stream for reporting progress and warnings.
Definition: TextureBaker.h:174
GT_API const UT_StringHolder filename
FilePath _outputImagePath
Definition: TextureBaker.h:279
shared_ptr< class TextureBaker > TextureBakerPtr
A shared pointer to a TextureBaker.
Definition: TextureBaker.h:26
std::unordered_map< string, NodePtr > _worldSpaceNodes
Definition: TextureBaker.h:297
Definition: File.h:26
const FilePath & getOutputImagePath()
Get the current output location for baked texture images.
Definition: TextureBaker.h:116
void setAverageImages(bool enable)
Set whether images should be averaged to generate constants. Defaults to false.
Definition: TextureBaker.h:85
bool _averageImages
Definition: TextureBaker.h:277
Vector2 _textureSpaceMax
Definition: TextureBaker.h:286
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
void setTextureSpaceMax(const Vector2 &max)
Set the maximum texcoords used in texture baking. Defaults to 1, 1.
Definition: TextureBaker.h:206
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
std::ostream * _outputStream
Definition: TextureBaker.h:283
GLuint start
Definition: glcorearb.h:475
GLenum GLenum GLenum input
Definition: glew.h:14162
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4973
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
string _colorSpace
Definition: TextureBaker.h:275
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool getAverageImages() const
Return whether images should be averaged to generate constants.
Definition: TextureBaker.h:91
void setHashImageNames(bool enable)
Definition: TextureBaker.h:182
GLuint const GLchar * name
Definition: glcorearb.h:786
void setTextureSpaceMin(const Vector2 &min)
Set the minimum texcoords used in texture baking. Defaults to 0, 0.
Definition: TextureBaker.h:194
std::vector< std::pair< std::string, DocumentPtr >> BakedDocumentVec
A vector of baked documents with their associated names.
Definition: TextureBaker.h:29
void setExtension(const string &extension)
Set the file extension for baked textures.
Definition: TextureBaker.h:44
const string & getExtension() const
Return the file extension for baked textures.
Definition: TextureBaker.h:50
Vector2 getTextureSpaceMin() const
Return the minimum texcoords used in texture baking.
Definition: TextureBaker.h:200
bool _optimizeConstants
Definition: TextureBaker.h:278
void setFilenameTemplateVarOverride(const string &key, const string &value)
Set texFilenameOverrides if template variable exists.
Definition: TextureBaker.h:159
BakedConstantMap _bakedConstantMap
Definition: TextureBaker.h:292
const string & getDistanceUnit() const
Return the distance unit to which textures are baked.
Definition: TextureBaker.h:79
BaseType
Definition: Image.h:45
bool _hashImageNames
Definition: TextureBaker.h:284
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
void setOutputImagePath(const FilePath &outputImagePath)
Definition: TextureBaker.h:110
BakedImageMap _bakedImageMap
Definition: TextureBaker.h:291
void setColorSpace(const string &colorSpace)
Definition: TextureBaker.h:61
string _distanceUnit
Definition: TextureBaker.h:276
vector< BakedImage > BakedImageVec
Definition: TextureBaker.h:251
const string & getBakedGraphName() const
Return the name of the baked graph element.
Definition: TextureBaker.h:128
shared_ptr< Document > DocumentPtr
A shared pointer to a Document.
Definition: Document.h:22
Definition: Types.h:282
string _textureFilenameTemplate
Definition: TextureBaker.h:282
#define MX_RENDERGLSL_API
Definition: Export.h:18
GLint GLsizei width
Definition: glcorearb.h:103
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:31
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
Vector2 getTextureSpaceMax() const
Return the maximum texcoords used in texture baking.
Definition: TextureBaker.h:212
GLuint shader
Definition: glcorearb.h:785
void setBakedGeomInfoName(const string &name)
Set the name of the baked geometry info element.
Definition: TextureBaker.h:134
const string & getTextureFilenameTemplate() const
Get the texture filename template.
Definition: TextureBaker.h:146
StringMap _bakedInputMap
Definition: TextureBaker.h:295
const string & getColorSpace() const
Return the color space in which color textures are encoded.
Definition: TextureBaker.h:67
bool getHashImageNames() const
Return whether automatic baked texture resolution is set.
Definition: TextureBaker.h:188
StringMap _texTemplateOverrides
Definition: TextureBaker.h:294
ImagePtr _frameCaptureImage
Definition: TextureBaker.h:290
std::unordered_map< OutputPtr, BakedConstant > BakedConstantMap
Definition: TextureBaker.h:253
ConstNodePtr _material
Definition: TextureBaker.h:289
string _bakedGraphName
Definition: TextureBaker.h:280
void setOutputStream(std::ostream *outputStream)
Set the output stream for reporting progress and warnings. Defaults to std::cout. ...
Definition: TextureBaker.h:168
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLsizei const GLfloat * value
Definition: glcorearb.h:824
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
StringSet _permittedOverrides
Definition: TextureBaker.h:293
std::unordered_map< OutputPtr, BakedImageVec > BakedImageMap
Definition: TextureBaker.h:252
void setDistanceUnit(const string &unitSpace)
Set the distance unit to which textures are baked. Defaults to meters.
Definition: TextureBaker.h:73
std::set< string > StringSet
A set of strings.
Definition: Library.h:61
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
static TextureBakerPtr create(unsigned int width=1024, unsigned int height=1024, Image::BaseType baseType=Image::BaseType::UINT8)
Definition: TextureBaker.h:38
void setTextureFilenameTemplate(const string &filenameTemplate)
Set the texture filename template.
Definition: TextureBaker.h:152
shared_ptr< ShaderGenerator > ShaderGeneratorPtr
Shared pointer to a ShaderGenerator.
Definition: Library.h:38
string _bakedGeomInfoName
Definition: TextureBaker.h:281
string _extension
Definition: TextureBaker.h:274
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
ShaderGeneratorPtr _generator
Definition: TextureBaker.h:288
bool getOptimizeConstants() const
Return whether uniform textures should be stored as constants.
Definition: TextureBaker.h:103
void setBakedGraphName(const string &name)
Set the name of the baked graph element.
Definition: TextureBaker.h:122
Vector2 _textureSpaceMin
Definition: TextureBaker.h:285
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24
const string & getBakedGeomInfoName() const
Return the name of the baked geometry info element.
Definition: TextureBaker.h:140