ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Material.cpp
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 
9 
11 
12 namespace cloudViewer {
13 namespace visualization {
14 namespace rendering {
15 
17  material_name_ = "defaultUnlit";
18  SetBaseColor(Eigen::Vector4f(1.f, 1.f, 1.f, 1.f));
19  SetBaseMetallic(0.f);
20  SetBaseRoughness(1.f);
21  SetBaseReflectance(0.5f);
22  SetBaseClearcoat(0.f);
24  SetAnisotropy(0.f);
25  SetThickness(1.f);
26  SetTransmission(1.f);
27  SetAbsorptionColor(Eigen::Vector4f(1.f, 1.f, 1.f, 1.f));
29  SetEmissiveColor(Eigen::Vector4f(0.f, 0.f, 0.f, 1.f));
30  SetPointSize(3.f);
31  SetLineWidth(1.f);
32 }
33 
34 void Material::SetTextureMap(const std::string &key,
35  const t::geometry::Image &image) {
36  // The Image must be on the CPU since GPU-resident images are not currently
37  // supported. Also, we copy the Image here because the image data is
38  // asynchronously copied to the GPU and we want to make sure the Image data
39  // doesn't get modified while being copied.
40  texture_maps_[key] = image.To(core::Device("CPU:0"), true);
41 }
42 
43 std::string Material::ToString() const {
44  if (!IsValid()) {
45  return "Invalid Material\n";
46  }
47  std::ostringstream os;
48  os << "Material " << material_name_ << '\n';
49  for (const auto &kv : scalar_properties_) {
50  os << '\t' << kv.first << ": " << kv.second << '\n';
51  }
52  for (const auto &kv : vector_properties_) {
53  os << '\t' << kv.first << ": " << kv.second.transpose() << '\n';
54  }
55  for (const auto &kv : texture_maps_) {
56  os << '\t' << kv.first << ": " << kv.second.ToString() << '\n';
57  }
58  return os.str();
59 }
60 
62  record.shader = GetMaterialName();
63  // Convert base material properties
64  if (HasBaseColor()) {
65  record.base_color = GetBaseColor();
66  }
67  if (HasBaseMetallic()) {
68  record.base_metallic = GetBaseMetallic();
69  }
70  if (HasBaseRoughness()) {
72  }
73  if (HasBaseReflectance()) {
75  }
76  if (HasBaseClearcoat()) {
78  }
81  }
82  if (HasAnisotropy()) {
83  record.base_anisotropy = GetAnisotropy();
84  }
85  if (HasEmissiveColor()) {
87  }
88  if (HasThickness()) {
89  record.thickness = GetThickness();
90  }
91  if (HasTransmission()) {
92  record.transmission = GetTransmission();
93  }
94  if (HasAbsorptionColor()) {
95  record.absorption_color = Eigen::Vector3f(GetAbsorptionColor().data());
96  }
97  if (HasAbsorptionDistance()) {
99  }
100  if (HasPointSize()) {
101  record.point_size = GetPointSize();
102  }
103  if (HasLineWidth()) {
104  record.line_width = GetLineWidth();
105  }
106  // Convert maps
107  if (HasAlbedoMap()) {
108  record.albedo_img =
109  std::make_shared<geometry::Image>(GetAlbedoMap().ToLegacy());
110  }
111  if (HasNormalMap()) {
112  record.normal_img =
113  std::make_shared<geometry::Image>(GetNormalMap().ToLegacy());
114  }
115  if (HasAOMap()) {
116  record.ao_img =
117  std::make_shared<geometry::Image>(GetAOMap().ToLegacy());
118  }
119  if (HasMetallicMap()) {
120  record.metallic_img =
121  std::make_shared<geometry::Image>(GetMetallicMap().ToLegacy());
122  }
123  if (HasRoughnessMap()) {
124  record.roughness_img =
125  std::make_shared<geometry::Image>(GetRoughnessMap().ToLegacy());
126  }
127  if (HasReflectanceMap()) {
128  record.reflectance_img = std::make_shared<geometry::Image>(
129  GetReflectanceMap().ToLegacy());
130  }
131  if (HasClearcoatMap()) {
132  record.clearcoat_img =
133  std::make_shared<geometry::Image>(GetClearcoatMap().ToLegacy());
134  }
135  if (HasClearcoatRoughnessMap()) {
136  record.clearcoat_roughness_img = std::make_shared<geometry::Image>(
137  GetClearcoatRoughnessMap().ToLegacy());
138  }
139  if (HasAnisotropyMap()) {
140  record.anisotropy_img = std::make_shared<geometry::Image>(
141  GetAnisotropyMap().ToLegacy());
142  }
143  if (HasAORoughnessMetalMap()) {
144  record.ao_rough_metal_img = std::make_shared<geometry::Image>(
145  GetAORoughnessMetalMap().ToLegacy());
146  }
147 }
148 
150  using t::geometry::Image;
151  Material tmat(record.shader);
152  // scalar and vector properties
153  tmat.SetBaseColor(record.base_color);
154  tmat.SetBaseMetallic(record.base_metallic);
155  tmat.SetBaseRoughness(record.base_roughness);
157  tmat.SetBaseClearcoat(record.base_clearcoat);
159  tmat.SetAnisotropy(record.base_anisotropy);
160  tmat.SetEmissiveColor(record.emissive_color);
161  // refractive materials
162  tmat.SetThickness(record.thickness);
163  tmat.SetTransmission(record.transmission);
165  // points and lines
166  tmat.SetPointSize(record.point_size);
167  tmat.SetLineWidth(record.line_width);
168  // maps
169  if (record.albedo_img) {
170  tmat.SetAlbedoMap(Image::FromLegacy(*record.albedo_img));
171  }
172  if (record.normal_img) {
173  tmat.SetNormalMap(Image::FromLegacy(*record.normal_img));
174  }
175  if (record.ao_img) {
176  tmat.SetAOMap(Image::FromLegacy(*record.ao_img));
177  }
178  if (record.metallic_img) {
179  tmat.SetMetallicMap(Image::FromLegacy(*record.metallic_img));
180  }
181  if (record.roughness_img) {
182  tmat.SetRoughnessMap(Image::FromLegacy(*record.roughness_img));
183  }
184  if (record.reflectance_img) {
185  tmat.SetReflectanceMap(Image::FromLegacy(*record.reflectance_img));
186  }
187  if (record.clearcoat_img) {
188  tmat.SetClearcoatMap(Image::FromLegacy(*record.clearcoat_img));
189  }
190  if (record.clearcoat_roughness_img) {
192  Image::FromLegacy(*record.clearcoat_roughness_img));
193  }
194  if (record.anisotropy_img) {
195  tmat.SetAnisotropyMap(Image::FromLegacy(*record.anisotropy_img));
196  }
197  if (record.ao_rough_metal_img) {
199  Image::FromLegacy(*record.ao_rough_metal_img));
200  }
201 
202  return tmat;
203 }
204 
205 } // namespace rendering
206 } // namespace visualization
207 } // 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
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::string ToString() const
String reprentation for printing.
Definition: Material.cpp:43
void SetClearcoatRoughnessMap(const t::geometry::Image &image)
Definition: Material.h:229
const t::geometry::Image & GetAnisotropyMap() const
Definition: Material.h:184
void SetReflectanceMap(const t::geometry::Image &image)
Definition: Material.h:223
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 SetAnisotropyMap(const t::geometry::Image &image)
Definition: Material.h:232
void SetAORoughnessMetalMap(const t::geometry::Image &image)
Definition: Material.h:235
void SetAOMap(const t::geometry::Image &image)
Definition: Material.h:214
static Material FromMaterialRecord(const MaterialRecord &mat)
Convert from MaterialRecord.
Definition: Material.cpp:149
void SetAbsorptionColor(const Eigen::Vector4f &value)
Definition: Material.h:306
const t::geometry::Image & GetNormalMap() const
Definition: Material.h:163
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
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 t::geometry::Image & GetAlbedoMap() const
Definition: Material.h:160
const t::geometry::Image & GetClearcoatRoughnessMap() const
Definition: Material.h:181
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.
std::shared_ptr< geometry::Image > normal_img
std::shared_ptr< geometry::Image > clearcoat_roughness_img
std::shared_ptr< geometry::Image > clearcoat_img
std::shared_ptr< geometry::Image > albedo_img
std::shared_ptr< geometry::Image > ao_rough_metal_img
std::shared_ptr< geometry::Image > metallic_img
std::shared_ptr< geometry::Image > roughness_img
std::shared_ptr< geometry::Image > anisotropy_img
std::shared_ptr< geometry::Image > reflectance_img