Open3D (C++ API)  0.17.0
Material.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <string>
11 
13 
14 namespace open3d {
15 namespace visualization {
16 namespace rendering {
17 
18 struct MaterialRecord;
19 
20 class Material {
21 public:
22  using TextureMaps = std::unordered_map<std::string, t::geometry::Image>;
23  using ScalarPropertyMap = std::unordered_map<std::string, float>;
24  using VectorPropertyMap = std::unordered_map<
25  std::string,
26  Eigen::Vector4f,
27  std::hash<std::string>,
28  std::equal_to<std::string>,
29  Eigen::aligned_allocator<
30  std::pair<const std::string, Eigen::Vector4f>>>;
31 
33  Material() = default;
34 
35  Material(const Material &mat) = default;
36 
37  Material &operator=(const Material &other) = default;
38 
40  Material(const std::string &material_name)
41  : material_name_(material_name) {}
42 
45  void SetDefaultProperties();
46 
49  bool IsValid() const { return !material_name_.empty(); }
50 
52  const std::string &GetMaterialName() const { return material_name_; }
53 
55  const TextureMaps &GetTextureMaps() const { return texture_maps_; }
56 
61  const t::geometry::Image &GetTextureMap(const std::string &key) const {
62  return texture_maps_.at(key);
63  }
64 
67  return scalar_properties_;
68  }
69 
74  float GetScalarProperty(const std::string &key) const {
75  return scalar_properties_.at(key);
76  }
77 
80  return vector_properties_;
81  }
82 
87  Eigen::Vector4f GetVectorProperty(const std::string &key) const {
88  return vector_properties_.at(key);
89  }
90 
96  void SetTextureMap(const std::string &key, const t::geometry::Image &image);
97 
103  void SetScalarProperty(const std::string &key, float value) {
104  scalar_properties_[key] = value;
105  }
106 
112  void SetVectorProperty(const std::string &key,
113  const Eigen::Vector4f &value) {
114  vector_properties_[key] = value;
115  }
116 
118  // in or user specified shader. The name is NOT checked to ensure it is
119  // valid.
122  void SetMaterialName(const std::string &material_name) {
123  material_name_ = material_name;
124  }
125 
129  bool HasTextureMap(const std::string &key) const {
130  return texture_maps_.count(key) > 0;
131  }
132 
136  bool HasScalarProperty(const std::string &key) const {
137  return scalar_properties_.count(key) > 0;
138  }
139 
143  bool HasVectorProperty(const std::string &key) const {
144  return vector_properties_.count(key) > 0;
145  }
146 
153  return GetTextureMap("albedo");
154  }
156  return GetTextureMap("normal");
157  }
158  const t::geometry::Image &GetAOMap() const {
159  return GetTextureMap("ambient_occlusion");
160  }
162  return GetTextureMap("metallic");
163  }
165  return GetTextureMap("roughness");
166  }
168  return GetTextureMap("reflectance");
169  }
171  return GetTextureMap("clear_coat");
172  }
174  return GetTextureMap("clear_coat_roughness");
175  }
177  return GetTextureMap("anisotropy");
178  }
182  return GetTextureMap("ao_rough_metal");
183  }
184 
185  bool HasAlbedoMap() const { return HasTextureMap("albedo"); }
186  bool HasNormalMap() const { return HasTextureMap("normal"); }
187  bool HasAOMap() const { return HasTextureMap("ambient_occlusion"); }
188  bool HasMetallicMap() const { return HasTextureMap("metallic"); }
189  bool HasRoughnessMap() const { return HasTextureMap("roughness"); }
190  bool HasReflectanceMap() const { return HasTextureMap("reflectance"); }
191  bool HasClearcoatMap() const { return HasTextureMap("clear_coat"); }
193  return HasTextureMap("clear_coat_roughness");
194  }
195  bool HasAnisotropyMap() const { return HasTextureMap("anisotropy"); }
196  bool HasAORoughnessMetalMap() const {
197  return HasTextureMap("ao_rough_metal");
198  }
199 
201  SetTextureMap("albedo", image);
202  }
204  SetTextureMap("normal", image);
205  }
207  SetTextureMap("ambient_occlusion", image);
208  }
210  SetTextureMap("metallic", image);
211  }
213  SetTextureMap("roughness", image);
214  }
216  SetTextureMap("reflectance", image);
217  }
219  SetTextureMap("clear_coat", image);
220  }
222  SetTextureMap("clear_coat_roughness", image);
223  }
225  SetTextureMap("anisotropy", image);
226  }
228  SetTextureMap("ao_rough_metal", image);
229  }
230 
231  Eigen::Vector4f GetBaseColor() const {
232  return GetVectorProperty("base_color");
233  }
234  float GetBaseMetallic() const { return GetScalarProperty("metallic"); }
235  float GetBaseRoughness() const { return GetScalarProperty("roughness"); }
236  float GetBaseReflectance() const {
237  return GetScalarProperty("reflectance");
238  }
239  float GetBaseClearcoat() const { return GetScalarProperty("clear_coat"); }
241  return GetScalarProperty("clear_coat_roughness");
242  }
243  float GetAnisotropy() const { return GetScalarProperty("anisotropy"); }
244  float GetThickness() const { return GetScalarProperty("thickness"); }
245  float GetTransmission() const { return GetScalarProperty("transmission"); }
246  Eigen::Vector4f GetAbsorptionColor() const {
247  return GetVectorProperty("absorption_color");
248  }
249  float GetAbsorptionDistance() const {
250  return GetScalarProperty("absorption_distance");
251  }
252 
253  bool HasBaseColor() const { return HasVectorProperty("base_color"); }
254  bool HasBaseMetallic() const { return HasScalarProperty("metallic"); }
255  bool HasBaseRoughness() const { return HasScalarProperty("roughness"); }
256  bool HasBaseReflectance() const { return HasScalarProperty("reflectance"); }
257  bool HasBaseClearcoat() const { return HasScalarProperty("clear_coat"); }
259  return HasScalarProperty("clear_coat_roughness");
260  }
261  bool HasAnisotropy() const { return HasScalarProperty("anisotropy"); }
262  bool HasThickness() const { return HasScalarProperty("thickness"); }
263  bool HasTransmission() const { return HasScalarProperty("transmission"); }
264  bool HasAbsorptionColor() const {
265  return HasVectorProperty("absorption_color");
266  }
267  bool HasAbsorptionDistance() const {
268  return HasScalarProperty("absorption_distance");
269  }
270 
271  void SetBaseColor(const Eigen::Vector4f &value) {
272  SetVectorProperty("base_color", value);
273  }
274  void SetBaseMetallic(float value) { SetScalarProperty("metallic", value); }
275  void SetBaseRoughness(float value) {
276  SetScalarProperty("roughness", value);
277  }
278  void SetBaseReflectance(float value) {
279  SetScalarProperty("reflectance", value);
280  }
281  void SetBaseClearcoat(float value) {
282  SetScalarProperty("clear_coat", value);
283  }
284  void SetBaseClearcoatRoughness(float value) {
285  SetScalarProperty("clear_coat_roughness", value);
286  }
287  void SetAnisotropy(float value) { SetScalarProperty("anisotropy", value); }
288  void SetThickness(float value) { SetScalarProperty("thickness", value); }
289  void SetTransmission(float value) {
290  SetScalarProperty("transmission", value);
291  }
292  void SetAbsorptionColor(const Eigen::Vector4f &value) {
293  SetVectorProperty("absorption_color", value);
294  }
295  void SetAbsorptionDistance(float value) {
296  SetScalarProperty("absorption_distance", value);
297  }
298 
305  float GetPointSize() const { return GetScalarProperty("point_size"); }
306  float GetLineWidth() const { return GetScalarProperty("line_width"); }
307 
308  bool HasPointSize() const { return HasScalarProperty("point_size"); }
309  bool HasLineWidth() const { return HasScalarProperty("line_width"); }
310 
311  void SetPointSize(float value) { SetScalarProperty("point_size", value); }
312  void SetLineWidth(float value) { SetScalarProperty("line_width", value); }
313 
315  void ToMaterialRecord(MaterialRecord &record) const;
316 
317 private:
318  std::string material_name_;
319  TextureMaps texture_maps_;
320  ScalarPropertyMap scalar_properties_;
321  VectorPropertyMap vector_properties_;
322 };
323 
324 } // namespace rendering
325 } // namespace visualization
326 } // namespace open3d
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:183
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
void SetBaseRoughness(float value)
Definition: Material.h:275
std::unordered_map< std::string, t::geometry::Image > TextureMaps
Definition: Material.h:22
bool HasScalarProperty(const std::string &key) const
Definition: Material.h:136
void SetBaseClearcoatRoughness(float value)
Definition: Material.h:284
void SetPointSize(float value)
Definition: Material.h:311
void SetAlbedoMap(const t::geometry::Image &image)
Definition: Material.h:200
void SetAbsorptionColor(const Eigen::Vector4f &value)
Definition: Material.h:292
bool HasReflectanceMap() const
Definition: Material.h:190
bool HasBaseMetallic() const
Definition: Material.h:254
bool HasAORoughnessMetalMap() const
Definition: Material.h:196
bool HasThickness() const
Definition: Material.h:262
bool HasRoughnessMap() const
Definition: Material.h:189
void SetMaterialName(const std::string &material_name)
Set material name. The material name should match the name of a built.
Definition: Material.h:122
bool HasBaseReflectance() const
Definition: Material.h:256
const TextureMaps & GetTextureMaps() const
Returns the texture map map.
Definition: Material.h:55
Eigen::Vector4f GetBaseColor() const
Definition: Material.h:231
void SetAOMap(const t::geometry::Image &image)
Definition: Material.h:206
void SetScalarProperty(const std::string &key, float value)
Definition: Material.h:103
void SetAnisotropy(float value)
Definition: Material.h:287
const t::geometry::Image & GetTextureMap(const std::string &key) const
Definition: Material.h:61
void SetDefaultProperties()
Definition: Material.cpp:16
bool HasAnisotropyMap() const
Definition: Material.h:195
bool HasAOMap() const
Definition: Material.h:187
bool HasLineWidth() const
Definition: Material.h:309
const ScalarPropertyMap & GetScalarProperties() const
Returns the map of scalar properties.
Definition: Material.h:66
const t::geometry::Image & GetReflectanceMap() const
Definition: Material.h:167
bool IsValid() const
Definition: Material.h:49
bool HasAnisotropy() const
Definition: Material.h:261
void SetAORoughnessMetalMap(const t::geometry::Image &image)
Definition: Material.h:227
bool HasBaseClearcoatRoughness() const
Definition: Material.h:258
void SetThickness(float value)
Definition: Material.h:288
const std::string & GetMaterialName() const
Get the name of the material.
Definition: Material.h:52
void SetReflectanceMap(const t::geometry::Image &image)
Definition: Material.h:215
float GetThickness() const
Definition: Material.h:244
std::unordered_map< std::string, float > ScalarPropertyMap
Definition: Material.h:23
const t::geometry::Image & GetRoughnessMap() const
Definition: Material.h:164
bool HasVectorProperty(const std::string &key) const
Definition: Material.h:143
void SetBaseReflectance(float value)
Definition: Material.h:278
bool HasBaseClearcoat() const
Definition: Material.h:257
const t::geometry::Image & GetAOMap() const
Definition: Material.h:158
const t::geometry::Image & GetNormalMap() const
Definition: Material.h:155
float GetBaseClearcoat() const
Definition: Material.h:239
float GetPointSize() const
Definition: Material.h:305
void SetTextureMap(const std::string &key, const t::geometry::Image &image)
Definition: Material.cpp:33
float GetBaseMetallic() const
Definition: Material.h:234
void SetAbsorptionDistance(float value)
Definition: Material.h:295
bool HasClearcoatRoughnessMap() const
Definition: Material.h:192
void SetAnisotropyMap(const t::geometry::Image &image)
Definition: Material.h:224
void SetVectorProperty(const std::string &key, const Eigen::Vector4f &value)
Definition: Material.h:112
void SetNormalMap(const t::geometry::Image &image)
Definition: Material.h:203
bool HasAlbedoMap() const
Definition: Material.h:185
bool HasMetallicMap() const
Definition: Material.h:188
Eigen::Vector4f GetVectorProperty(const std::string &key) const
Definition: Material.h:87
float GetBaseReflectance() const
Definition: Material.h:236
bool HasBaseColor() const
Definition: Material.h:253
const t::geometry::Image & GetClearcoatRoughnessMap() const
Definition: Material.h:173
void SetBaseClearcoat(float value)
Definition: Material.h:281
float GetScalarProperty(const std::string &key) const
Definition: Material.h:74
float GetBaseRoughness() const
Definition: Material.h:235
const t::geometry::Image & GetAnisotropyMap() const
Definition: Material.h:176
float GetAbsorptionDistance() const
Definition: Material.h:249
const t::geometry::Image & GetAlbedoMap() const
Definition: Material.h:152
void SetTransmission(float value)
Definition: Material.h:289
Eigen::Vector4f GetAbsorptionColor() const
Definition: Material.h:246
Material()=default
Create an empty, invalid material.
bool HasBaseRoughness() const
Definition: Material.h:255
const t::geometry::Image & GetClearcoatMap() const
Definition: Material.h:170
const VectorPropertyMap & GetVectorProperties() const
Returns the map of vector properties.
Definition: Material.h:79
const t::geometry::Image & GetMetallicMap() const
Definition: Material.h:161
std::unordered_map< std::string, Eigen::Vector4f, std::hash< std::string >, std::equal_to< std::string >, Eigen::aligned_allocator< std::pair< const std::string, Eigen::Vector4f > >> VectorPropertyMap
Definition: Material.h:30
const t::geometry::Image & GetAORoughnessMetalMap() const
Definition: Material.h:181
void SetClearcoatRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:221
void SetBaseMetallic(float value)
Definition: Material.h:274
bool HasNormalMap() const
Definition: Material.h:186
bool HasPointSize() const
Definition: Material.h:308
float GetAnisotropy() const
Definition: Material.h:243
bool HasTextureMap(const std::string &key) const
Definition: Material.h:129
bool HasTransmission() const
Definition: Material.h:263
float GetBaseClearcoatRoughness() const
Definition: Material.h:240
float GetTransmission() const
Definition: Material.h:245
void SetClearcoatMap(const t::geometry::Image &image)
Definition: Material.h:218
Material & operator=(const Material &other)=default
void SetRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:212
void ToMaterialRecord(MaterialRecord &record) const
Fills a legacy MaterialRecord constructed from this Material.
Definition: Material.cpp:42
Material(const Material &mat)=default
void SetMetallicMap(const t::geometry::Image &image)
Definition: Material.h:209
bool HasAbsorptionColor() const
Definition: Material.h:264
Material(const std::string &material_name)
Create an empty but valid material for the specified material name.
Definition: Material.h:40
bool HasClearcoatMap() const
Definition: Material.h:191
float GetLineWidth() const
Definition: Material.h:306
void SetLineWidth(float value)
Definition: Material.h:312
bool HasAbsorptionDistance() const
Definition: Material.h:267
void SetBaseColor(const Eigen::Vector4f &value)
Definition: Material.h:271
Definition: PinholeCameraIntrinsic.cpp:16