ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Material.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <sstream>
12 #include <string>
13 #include <unordered_map>
14 
16 
17 namespace cloudViewer {
18 namespace visualization {
19 namespace rendering {
20 struct MaterialRecord;
21 
22 class Material {
23 public:
24  using TextureMaps = std::unordered_map<std::string, t::geometry::Image>;
25  using ScalarPropertyMap = std::unordered_map<std::string, float>;
26  using VectorPropertyMap = std::unordered_map<
27  std::string,
28  Eigen::Vector4f,
29  std::hash<std::string>,
30  std::equal_to<std::string>,
31  Eigen::aligned_allocator<
32  std::pair<const std::string, Eigen::Vector4f>>>;
33 
35  Material() = default;
36 
37  Material(const Material &mat) = default;
38 
40  static Material FromMaterialRecord(const MaterialRecord &mat);
41 
42  Material &operator=(const Material &other) = default;
43 
45  Material(const std::string &material_name)
46  : material_name_(material_name) {}
47 
50  void SetDefaultProperties();
51 
54  bool IsValid() const { return !material_name_.empty(); }
55 
57  const std::string &GetMaterialName() const { return material_name_; }
58 
60  std::string ToString() const;
61 
63  const TextureMaps &GetTextureMaps() const { return texture_maps_; }
64 
69  const t::geometry::Image &GetTextureMap(const std::string &key) const {
70  return texture_maps_.at(key);
71  }
72 
75  return scalar_properties_;
76  }
77 
82  float GetScalarProperty(const std::string &key) const {
83  return scalar_properties_.at(key);
84  }
85 
88  return vector_properties_;
89  }
90 
95  Eigen::Vector4f GetVectorProperty(const std::string &key) const {
96  return vector_properties_.at(key);
97  }
98 
104  void SetTextureMap(const std::string &key, const t::geometry::Image &image);
105 
111  void SetScalarProperty(const std::string &key, float value) {
112  scalar_properties_[key] = value;
113  }
114 
120  void SetVectorProperty(const std::string &key,
121  const Eigen::Vector4f &value) {
122  vector_properties_[key] = value;
123  }
124 
126  // in or user specified shader. The name is NOT checked to ensure it is
127  // valid.
130  void SetMaterialName(const std::string &material_name) {
131  material_name_ = material_name;
132  }
133 
137  bool HasTextureMap(const std::string &key) const {
138  return texture_maps_.count(key) > 0;
139  }
140 
144  bool HasScalarProperty(const std::string &key) const {
145  return scalar_properties_.count(key) > 0;
146  }
147 
151  bool HasVectorProperty(const std::string &key) const {
152  return vector_properties_.count(key) > 0;
153  }
154 
161  return GetTextureMap("albedo");
162  }
164  return GetTextureMap("normal");
165  }
166  const t::geometry::Image &GetAOMap() const {
167  return GetTextureMap("ambient_occlusion");
168  }
170  return GetTextureMap("metallic");
171  }
173  return GetTextureMap("roughness");
174  }
176  return GetTextureMap("reflectance");
177  }
179  return GetTextureMap("clear_coat");
180  }
182  return GetTextureMap("clear_coat_roughness");
183  }
185  return GetTextureMap("anisotropy");
186  }
190  return GetTextureMap("ao_rough_metal");
191  }
192 
193  bool HasAlbedoMap() const { return HasTextureMap("albedo"); }
194  bool HasNormalMap() const { return HasTextureMap("normal"); }
195  bool HasAOMap() const { return HasTextureMap("ambient_occlusion"); }
196  bool HasMetallicMap() const { return HasTextureMap("metallic"); }
197  bool HasRoughnessMap() const { return HasTextureMap("roughness"); }
198  bool HasReflectanceMap() const { return HasTextureMap("reflectance"); }
199  bool HasClearcoatMap() const { return HasTextureMap("clear_coat"); }
201  return HasTextureMap("clear_coat_roughness");
202  }
203  bool HasAnisotropyMap() const { return HasTextureMap("anisotropy"); }
204  bool HasAORoughnessMetalMap() const {
205  return HasTextureMap("ao_rough_metal");
206  }
207 
209  SetTextureMap("albedo", image);
210  }
212  SetTextureMap("normal", image);
213  }
215  SetTextureMap("ambient_occlusion", image);
216  }
218  SetTextureMap("metallic", image);
219  }
221  SetTextureMap("roughness", image);
222  }
224  SetTextureMap("reflectance", image);
225  }
227  SetTextureMap("clear_coat", image);
228  }
230  SetTextureMap("clear_coat_roughness", image);
231  }
233  SetTextureMap("anisotropy", image);
234  }
236  SetTextureMap("ao_rough_metal", image);
237  }
238 
239  Eigen::Vector4f GetBaseColor() const {
240  return GetVectorProperty("base_color");
241  }
242  float GetBaseMetallic() const { return GetScalarProperty("metallic"); }
243  float GetBaseRoughness() const { return GetScalarProperty("roughness"); }
244  float GetBaseReflectance() const {
245  return GetScalarProperty("reflectance");
246  }
247  float GetBaseClearcoat() const { return GetScalarProperty("clear_coat"); }
249  return GetScalarProperty("clear_coat_roughness");
250  }
251  float GetAnisotropy() const { return GetScalarProperty("anisotropy"); }
252  float GetThickness() const { return GetScalarProperty("thickness"); }
253  float GetTransmission() const { return GetScalarProperty("transmission"); }
254  Eigen::Vector4f GetAbsorptionColor() const {
255  return GetVectorProperty("absorption_color");
256  }
257  float GetAbsorptionDistance() const {
258  return GetScalarProperty("absorption_distance");
259  }
260  Eigen::Vector4f GetEmissiveColor() const {
261  return GetVectorProperty("emissive_color");
262  }
263 
264  bool HasBaseColor() const { return HasVectorProperty("base_color"); }
265  bool HasBaseMetallic() const { return HasScalarProperty("metallic"); }
266  bool HasBaseRoughness() const { return HasScalarProperty("roughness"); }
267  bool HasBaseReflectance() const { return HasScalarProperty("reflectance"); }
268  bool HasBaseClearcoat() const { return HasScalarProperty("clear_coat"); }
270  return HasScalarProperty("clear_coat_roughness");
271  }
272  bool HasAnisotropy() const { return HasScalarProperty("anisotropy"); }
273  bool HasThickness() const { return HasScalarProperty("thickness"); }
274  bool HasTransmission() const { return HasScalarProperty("transmission"); }
275  bool HasAbsorptionColor() const {
276  return HasVectorProperty("absorption_color");
277  }
278  bool HasAbsorptionDistance() const {
279  return HasScalarProperty("absorption_distance");
280  }
281  bool HasEmissiveColor() const {
282  return HasVectorProperty("emissive_color");
283  }
284 
285  void SetBaseColor(const Eigen::Vector4f &value) {
286  SetVectorProperty("base_color", value);
287  }
288  void SetBaseMetallic(float value) { SetScalarProperty("metallic", value); }
289  void SetBaseRoughness(float value) {
290  SetScalarProperty("roughness", value);
291  }
292  void SetBaseReflectance(float value) {
293  SetScalarProperty("reflectance", value);
294  }
295  void SetBaseClearcoat(float value) {
296  SetScalarProperty("clear_coat", value);
297  }
298  void SetBaseClearcoatRoughness(float value) {
299  SetScalarProperty("clear_coat_roughness", value);
300  }
301  void SetAnisotropy(float value) { SetScalarProperty("anisotropy", value); }
302  void SetThickness(float value) { SetScalarProperty("thickness", value); }
303  void SetTransmission(float value) {
304  SetScalarProperty("transmission", value);
305  }
306  void SetAbsorptionColor(const Eigen::Vector4f &value) {
307  SetVectorProperty("absorption_color", value);
308  }
309  void SetAbsorptionDistance(float value) {
310  SetScalarProperty("absorption_distance", value);
311  }
312  void SetEmissiveColor(const Eigen::Vector4f &value) {
313  SetVectorProperty("emissive_color", value);
314  }
315 
322  float GetPointSize() const { return GetScalarProperty("point_size"); }
323  float GetLineWidth() const { return GetScalarProperty("line_width"); }
324 
325  bool HasPointSize() const { return HasScalarProperty("point_size"); }
326  bool HasLineWidth() const { return HasScalarProperty("line_width"); }
327 
328  void SetPointSize(float value) { SetScalarProperty("point_size", value); }
329  void SetLineWidth(float value) { SetScalarProperty("line_width", value); }
330 
332  void ToMaterialRecord(MaterialRecord &record) const;
333 
334 private:
335  std::string material_name_;
336  TextureMaps texture_maps_;
337  ScalarPropertyMap scalar_properties_;
338  VectorPropertyMap vector_properties_;
339 };
340 
341 } // namespace rendering
342 } // namespace visualization
343 } // namespace cloudViewer
std::shared_ptr< core::Tensor > image
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
void SetMaterialName(const std::string &material_name)
Set material name. The material name should match the name of a built.
Definition: Material.h:130
Eigen::Vector4f GetAbsorptionColor() const
Definition: Material.h:254
const std::string & GetMaterialName() const
Get the name of the material.
Definition: Material.h:57
const t::geometry::Image & GetMetallicMap() const
Definition: Material.h:169
void SetRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:220
std::unordered_map< std::string, t::geometry::Image > TextureMaps
Definition: Material.h:24
std::string ToString() const
String reprentation for printing.
Definition: Material.cpp:43
void SetClearcoatRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:229
bool HasVectorProperty(const std::string &key) const
Definition: Material.h:151
const VectorPropertyMap & GetVectorProperties() const
Returns the map of vector properties.
Definition: Material.h:87
const t::geometry::Image & GetAnisotropyMap() const
Definition: Material.h:184
void SetReflectanceMap(const t::geometry::Image &image)
Definition: Material.h:223
const ScalarPropertyMap & GetScalarProperties() const
Returns the map of scalar properties.
Definition: Material.h:74
Eigen::Vector4f GetVectorProperty(const std::string &key) const
Definition: Material.h:95
void ToMaterialRecord(MaterialRecord &record) const
Fills a legacy MaterialRecord constructed from this Material.
Definition: Material.cpp:61
void SetClearcoatMap(const t::geometry::Image &image)
Definition: Material.h:226
const t::geometry::Image & GetRoughnessMap() const
Definition: Material.h:172
void SetVectorProperty(const std::string &key, const Eigen::Vector4f &value)
Definition: Material.h:120
void SetAnisotropyMap(const t::geometry::Image &image)
Definition: Material.h:232
void SetAORoughnessMetalMap(const t::geometry::Image &image)
Definition: Material.h:235
bool HasTextureMap(const std::string &key) const
Definition: Material.h:137
void SetAOMap(const t::geometry::Image &image)
Definition: Material.h:214
static Material FromMaterialRecord(const MaterialRecord &mat)
Convert from MaterialRecord.
Definition: Material.cpp:149
std::unordered_map< std::string, float > ScalarPropertyMap
Definition: Material.h:25
void SetAbsorptionColor(const Eigen::Vector4f &value)
Definition: Material.h:306
Material & operator=(const Material &other)=default
const t::geometry::Image & GetNormalMap() const
Definition: Material.h:163
void SetScalarProperty(const std::string &key, float value)
Definition: Material.h:111
Material(const std::string &material_name)
Create an empty but valid material for the specified material name.
Definition: Material.h:45
void SetTextureMap(const std::string &key, const t::geometry::Image &image)
Definition: Material.cpp:34
void SetBaseColor(const Eigen::Vector4f &value)
Definition: Material.h:285
void SetEmissiveColor(const Eigen::Vector4f &value)
Definition: Material.h:312
void SetAlbedoMap(const t::geometry::Image &image)
Definition: Material.h:208
void SetMetallicMap(const t::geometry::Image &image)
Definition: Material.h:217
Material()=default
Create an empty, invalid material.
const t::geometry::Image & GetTextureMap(const std::string &key) const
Definition: Material.h:69
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:32
bool HasScalarProperty(const std::string &key) const
Definition: Material.h:144
const t::geometry::Image & GetClearcoatMap() const
Definition: Material.h:178
const t::geometry::Image & GetAOMap() const
Definition: Material.h:166
const t::geometry::Image & GetAORoughnessMetalMap() const
Definition: Material.h:189
const TextureMaps & GetTextureMaps() const
Returns the texture map map.
Definition: Material.h:63
const t::geometry::Image & GetAlbedoMap() const
Definition: Material.h:160
const t::geometry::Image & GetClearcoatRoughnessMap() const
Definition: Material.h:181
float GetScalarProperty(const std::string &key) const
Definition: Material.h:82
void SetNormalMap(const t::geometry::Image &image)
Definition: Material.h:211
const t::geometry::Image & GetReflectanceMap() const
Definition: Material.h:175
Generic file read and write utility for python interface.