Open3D (C++ API)  0.18.0+252c867
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 <sstream>
11 #include <string>
12 
14 
15 namespace open3d {
16 namespace visualization {
17 namespace rendering {
18 
19 struct MaterialRecord;
20 
21 class Material {
22 public:
23  using TextureMaps = std::unordered_map<std::string, t::geometry::Image>;
24  using ScalarPropertyMap = std::unordered_map<std::string, float>;
25  using VectorPropertyMap = std::unordered_map<
26  std::string,
27  Eigen::Vector4f,
28  std::hash<std::string>,
29  std::equal_to<std::string>,
30  Eigen::aligned_allocator<
31  std::pair<const std::string, Eigen::Vector4f>>>;
32 
34  Material() = default;
35 
36  Material(const Material &mat) = default;
37 
39  static Material FromMaterialRecord(const MaterialRecord &mat);
40 
41  Material &operator=(const Material &other) = default;
42 
44  Material(const std::string &material_name)
45  : material_name_(material_name) {}
46 
49  void SetDefaultProperties();
50 
53  bool IsValid() const { return !material_name_.empty(); }
54 
56  const std::string &GetMaterialName() const { return material_name_; }
57 
59  std::string ToString() const;
60 
62  const TextureMaps &GetTextureMaps() const { return texture_maps_; }
63 
68  const t::geometry::Image &GetTextureMap(const std::string &key) const {
69  return texture_maps_.at(key);
70  }
71 
74  return scalar_properties_;
75  }
76 
81  float GetScalarProperty(const std::string &key) const {
82  return scalar_properties_.at(key);
83  }
84 
87  return vector_properties_;
88  }
89 
94  Eigen::Vector4f GetVectorProperty(const std::string &key) const {
95  return vector_properties_.at(key);
96  }
97 
103  void SetTextureMap(const std::string &key, const t::geometry::Image &image);
104 
110  void SetScalarProperty(const std::string &key, float value) {
111  scalar_properties_[key] = value;
112  }
113 
119  void SetVectorProperty(const std::string &key,
120  const Eigen::Vector4f &value) {
121  vector_properties_[key] = value;
122  }
123 
125  // in or user specified shader. The name is NOT checked to ensure it is
126  // valid.
129  void SetMaterialName(const std::string &material_name) {
130  material_name_ = material_name;
131  }
132 
136  bool HasTextureMap(const std::string &key) const {
137  return texture_maps_.count(key) > 0;
138  }
139 
143  bool HasScalarProperty(const std::string &key) const {
144  return scalar_properties_.count(key) > 0;
145  }
146 
150  bool HasVectorProperty(const std::string &key) const {
151  return vector_properties_.count(key) > 0;
152  }
153 
160  return GetTextureMap("albedo");
161  }
163  return GetTextureMap("normal");
164  }
165  const t::geometry::Image &GetAOMap() const {
166  return GetTextureMap("ambient_occlusion");
167  }
169  return GetTextureMap("metallic");
170  }
172  return GetTextureMap("roughness");
173  }
175  return GetTextureMap("reflectance");
176  }
178  return GetTextureMap("clear_coat");
179  }
181  return GetTextureMap("clear_coat_roughness");
182  }
184  return GetTextureMap("anisotropy");
185  }
189  return GetTextureMap("ao_rough_metal");
190  }
191 
192  bool HasAlbedoMap() const { return HasTextureMap("albedo"); }
193  bool HasNormalMap() const { return HasTextureMap("normal"); }
194  bool HasAOMap() const { return HasTextureMap("ambient_occlusion"); }
195  bool HasMetallicMap() const { return HasTextureMap("metallic"); }
196  bool HasRoughnessMap() const { return HasTextureMap("roughness"); }
197  bool HasReflectanceMap() const { return HasTextureMap("reflectance"); }
198  bool HasClearcoatMap() const { return HasTextureMap("clear_coat"); }
200  return HasTextureMap("clear_coat_roughness");
201  }
202  bool HasAnisotropyMap() const { return HasTextureMap("anisotropy"); }
203  bool HasAORoughnessMetalMap() const {
204  return HasTextureMap("ao_rough_metal");
205  }
206 
208  SetTextureMap("albedo", image);
209  }
211  SetTextureMap("normal", image);
212  }
214  SetTextureMap("ambient_occlusion", image);
215  }
217  SetTextureMap("metallic", image);
218  }
220  SetTextureMap("roughness", image);
221  }
223  SetTextureMap("reflectance", image);
224  }
226  SetTextureMap("clear_coat", image);
227  }
229  SetTextureMap("clear_coat_roughness", image);
230  }
232  SetTextureMap("anisotropy", image);
233  }
235  SetTextureMap("ao_rough_metal", image);
236  }
237 
238  Eigen::Vector4f GetBaseColor() const {
239  return GetVectorProperty("base_color");
240  }
241  float GetBaseMetallic() const { return GetScalarProperty("metallic"); }
242  float GetBaseRoughness() const { return GetScalarProperty("roughness"); }
243  float GetBaseReflectance() const {
244  return GetScalarProperty("reflectance");
245  }
246  float GetBaseClearcoat() const { return GetScalarProperty("clear_coat"); }
248  return GetScalarProperty("clear_coat_roughness");
249  }
250  float GetAnisotropy() const { return GetScalarProperty("anisotropy"); }
251  float GetThickness() const { return GetScalarProperty("thickness"); }
252  float GetTransmission() const { return GetScalarProperty("transmission"); }
253  Eigen::Vector4f GetAbsorptionColor() const {
254  return GetVectorProperty("absorption_color");
255  }
256  float GetAbsorptionDistance() const {
257  return GetScalarProperty("absorption_distance");
258  }
259  Eigen::Vector4f GetEmissiveColor() const {
260  return GetVectorProperty("emissive_color");
261  }
262 
263  bool HasBaseColor() const { return HasVectorProperty("base_color"); }
264  bool HasBaseMetallic() const { return HasScalarProperty("metallic"); }
265  bool HasBaseRoughness() const { return HasScalarProperty("roughness"); }
266  bool HasBaseReflectance() const { return HasScalarProperty("reflectance"); }
267  bool HasBaseClearcoat() const { return HasScalarProperty("clear_coat"); }
269  return HasScalarProperty("clear_coat_roughness");
270  }
271  bool HasAnisotropy() const { return HasScalarProperty("anisotropy"); }
272  bool HasThickness() const { return HasScalarProperty("thickness"); }
273  bool HasTransmission() const { return HasScalarProperty("transmission"); }
274  bool HasAbsorptionColor() const {
275  return HasVectorProperty("absorption_color");
276  }
277  bool HasAbsorptionDistance() const {
278  return HasScalarProperty("absorption_distance");
279  }
280  bool HasEmissiveColor() const {
281  return HasVectorProperty("emissive_color");
282  }
283 
284  void SetBaseColor(const Eigen::Vector4f &value) {
285  SetVectorProperty("base_color", value);
286  }
287  void SetBaseMetallic(float value) { SetScalarProperty("metallic", value); }
288  void SetBaseRoughness(float value) {
289  SetScalarProperty("roughness", value);
290  }
291  void SetBaseReflectance(float value) {
292  SetScalarProperty("reflectance", value);
293  }
294  void SetBaseClearcoat(float value) {
295  SetScalarProperty("clear_coat", value);
296  }
297  void SetBaseClearcoatRoughness(float value) {
298  SetScalarProperty("clear_coat_roughness", value);
299  }
300  void SetAnisotropy(float value) { SetScalarProperty("anisotropy", value); }
301  void SetThickness(float value) { SetScalarProperty("thickness", value); }
302  void SetTransmission(float value) {
303  SetScalarProperty("transmission", value);
304  }
305  void SetAbsorptionColor(const Eigen::Vector4f &value) {
306  SetVectorProperty("absorption_color", value);
307  }
308  void SetAbsorptionDistance(float value) {
309  SetScalarProperty("absorption_distance", value);
310  }
311  void SetEmissiveColor(const Eigen::Vector4f &value) {
312  SetVectorProperty("emissive_color", value);
313  }
314 
321  float GetPointSize() const { return GetScalarProperty("point_size"); }
322  float GetLineWidth() const { return GetScalarProperty("line_width"); }
323 
324  bool HasPointSize() const { return HasScalarProperty("point_size"); }
325  bool HasLineWidth() const { return HasScalarProperty("line_width"); }
326 
327  void SetPointSize(float value) { SetScalarProperty("point_size", value); }
328  void SetLineWidth(float value) { SetScalarProperty("line_width", value); }
329 
331  void ToMaterialRecord(MaterialRecord &record) const;
332 
333 private:
334  std::string material_name_;
335  TextureMaps texture_maps_;
336  ScalarPropertyMap scalar_properties_;
337  VectorPropertyMap vector_properties_;
338 };
339 
340 } // namespace rendering
341 } // namespace visualization
342 } // 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:288
std::unordered_map< std::string, t::geometry::Image > TextureMaps
Definition: Material.h:23
bool HasScalarProperty(const std::string &key) const
Definition: Material.h:143
void SetBaseClearcoatRoughness(float value)
Definition: Material.h:297
void SetPointSize(float value)
Definition: Material.h:327
void SetAlbedoMap(const t::geometry::Image &image)
Definition: Material.h:207
void SetAbsorptionColor(const Eigen::Vector4f &value)
Definition: Material.h:305
bool HasReflectanceMap() const
Definition: Material.h:197
static Material FromMaterialRecord(const MaterialRecord &mat)
Convert from MaterialRecord.
Definition: Material.cpp:149
bool HasBaseMetallic() const
Definition: Material.h:264
bool HasAORoughnessMetalMap() const
Definition: Material.h:203
bool HasThickness() const
Definition: Material.h:272
bool HasRoughnessMap() const
Definition: Material.h:196
void SetMaterialName(const std::string &material_name)
Set material name. The material name should match the name of a built.
Definition: Material.h:129
bool HasBaseReflectance() const
Definition: Material.h:266
const TextureMaps & GetTextureMaps() const
Returns the texture map map.
Definition: Material.h:62
Eigen::Vector4f GetBaseColor() const
Definition: Material.h:238
void SetAOMap(const t::geometry::Image &image)
Definition: Material.h:213
void SetScalarProperty(const std::string &key, float value)
Definition: Material.h:110
void SetAnisotropy(float value)
Definition: Material.h:300
const t::geometry::Image & GetTextureMap(const std::string &key) const
Definition: Material.h:68
void SetDefaultProperties()
Definition: Material.cpp:16
bool HasAnisotropyMap() const
Definition: Material.h:202
bool HasAOMap() const
Definition: Material.h:194
bool HasLineWidth() const
Definition: Material.h:325
const ScalarPropertyMap & GetScalarProperties() const
Returns the map of scalar properties.
Definition: Material.h:73
const t::geometry::Image & GetReflectanceMap() const
Definition: Material.h:174
bool IsValid() const
Definition: Material.h:53
bool HasAnisotropy() const
Definition: Material.h:271
void SetAORoughnessMetalMap(const t::geometry::Image &image)
Definition: Material.h:234
bool HasBaseClearcoatRoughness() const
Definition: Material.h:268
void SetThickness(float value)
Definition: Material.h:301
const std::string & GetMaterialName() const
Get the name of the material.
Definition: Material.h:56
void SetReflectanceMap(const t::geometry::Image &image)
Definition: Material.h:222
float GetThickness() const
Definition: Material.h:251
bool HasEmissiveColor() const
Definition: Material.h:280
std::unordered_map< std::string, float > ScalarPropertyMap
Definition: Material.h:24
const t::geometry::Image & GetRoughnessMap() const
Definition: Material.h:171
bool HasVectorProperty(const std::string &key) const
Definition: Material.h:150
void SetBaseReflectance(float value)
Definition: Material.h:291
bool HasBaseClearcoat() const
Definition: Material.h:267
const t::geometry::Image & GetAOMap() const
Definition: Material.h:165
const t::geometry::Image & GetNormalMap() const
Definition: Material.h:162
float GetBaseClearcoat() const
Definition: Material.h:246
float GetPointSize() const
Definition: Material.h:321
void SetTextureMap(const std::string &key, const t::geometry::Image &image)
Definition: Material.cpp:34
float GetBaseMetallic() const
Definition: Material.h:241
void SetAbsorptionDistance(float value)
Definition: Material.h:308
bool HasClearcoatRoughnessMap() const
Definition: Material.h:199
void SetAnisotropyMap(const t::geometry::Image &image)
Definition: Material.h:231
void SetVectorProperty(const std::string &key, const Eigen::Vector4f &value)
Definition: Material.h:119
void SetNormalMap(const t::geometry::Image &image)
Definition: Material.h:210
bool HasAlbedoMap() const
Definition: Material.h:192
bool HasMetallicMap() const
Definition: Material.h:195
Eigen::Vector4f GetVectorProperty(const std::string &key) const
Definition: Material.h:94
float GetBaseReflectance() const
Definition: Material.h:243
bool HasBaseColor() const
Definition: Material.h:263
const t::geometry::Image & GetClearcoatRoughnessMap() const
Definition: Material.h:180
void SetBaseClearcoat(float value)
Definition: Material.h:294
float GetScalarProperty(const std::string &key) const
Definition: Material.h:81
float GetBaseRoughness() const
Definition: Material.h:242
const t::geometry::Image & GetAnisotropyMap() const
Definition: Material.h:183
float GetAbsorptionDistance() const
Definition: Material.h:256
const t::geometry::Image & GetAlbedoMap() const
Definition: Material.h:159
void SetTransmission(float value)
Definition: Material.h:302
Eigen::Vector4f GetAbsorptionColor() const
Definition: Material.h:253
Material()=default
Create an empty, invalid material.
bool HasBaseRoughness() const
Definition: Material.h:265
const t::geometry::Image & GetClearcoatMap() const
Definition: Material.h:177
const VectorPropertyMap & GetVectorProperties() const
Returns the map of vector properties.
Definition: Material.h:86
const t::geometry::Image & GetMetallicMap() const
Definition: Material.h:168
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:31
Eigen::Vector4f GetEmissiveColor() const
Definition: Material.h:259
const t::geometry::Image & GetAORoughnessMetalMap() const
Definition: Material.h:188
void SetClearcoatRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:228
void SetBaseMetallic(float value)
Definition: Material.h:287
bool HasNormalMap() const
Definition: Material.h:193
bool HasPointSize() const
Definition: Material.h:324
float GetAnisotropy() const
Definition: Material.h:250
bool HasTextureMap(const std::string &key) const
Definition: Material.h:136
bool HasTransmission() const
Definition: Material.h:273
float GetBaseClearcoatRoughness() const
Definition: Material.h:247
float GetTransmission() const
Definition: Material.h:252
void SetClearcoatMap(const t::geometry::Image &image)
Definition: Material.h:225
std::string ToString() const
String reprentation for printing.
Definition: Material.cpp:43
Material & operator=(const Material &other)=default
void SetRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:219
void ToMaterialRecord(MaterialRecord &record) const
Fills a legacy MaterialRecord constructed from this Material.
Definition: Material.cpp:61
Material(const Material &mat)=default
void SetMetallicMap(const t::geometry::Image &image)
Definition: Material.h:216
bool HasAbsorptionColor() const
Definition: Material.h:274
Material(const std::string &material_name)
Create an empty but valid material for the specified material name.
Definition: Material.h:44
bool HasClearcoatMap() const
Definition: Material.h:198
float GetLineWidth() const
Definition: Material.h:322
void SetLineWidth(float value)
Definition: Material.h:328
void SetEmissiveColor(const Eigen::Vector4f &value)
Definition: Material.h:311
bool HasAbsorptionDistance() const
Definition: Material.h:277
void SetBaseColor(const Eigen::Vector4f &value)
Definition: Material.h:284
Definition: PinholeCameraIntrinsic.cpp:16