1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SCENE_API_RESOURCE_H
17 #define SCENE_API_RESOURCE_H
18
19 #include <scene/interface/intf_environment.h>
20 #include <scene/interface/intf_image.h>
21 #include <scene/interface/intf_material.h>
22 #include <scene/interface/intf_mesh.h>
23 #include <scene/interface/intf_postprocess.h>
24 #include <scene/interface/intf_shader.h>
25
26 #include <meta/api/object.h>
27 #include <meta/api/object_name.h>
28 #include <meta/interface/resource/intf_resource.h>
29
SCENE_BEGIN_NAMESPACE()30 SCENE_BEGIN_NAMESPACE()
31
32 class Resource : public META_NS::Named {
33 public:
34 META_INTERFACE_OBJECT(Resource, META_NS::Named, CORE_NS::IResource)
35 /// @see IResource::GetResourceType
36 CORE_NS::ResourceType GetResourceType() const
37 {
38 return META_INTERFACE_OBJECT_CALL_PTR(GetResourceType());
39 }
40 /// @see IResource::GetResourceId
41 CORE_NS::ResourceId GetResourceId() const
42 {
43 return META_INTERFACE_OBJECT_CALL_PTR(GetResourceId());
44 }
45 };
46
47 class Image : public Resource {
48 public:
49 META_INTERFACE_OBJECT(Image, Resource, IImage)
50 /// @see IBitmap::Size
51 META_INTERFACE_OBJECT_READONLY_PROPERTY(BASE_NS::Math::UVec2, Size)
52 };
53
54 class Environment : public Resource {
55 public:
56 META_INTERFACE_OBJECT(Environment, Resource, IEnvironment)
57 /// @see IEnvironment::Background
58 META_INTERFACE_OBJECT_PROPERTY(EnvBackgroundType, Background)
59 /// @see IEnvironment::IndirectDiffuseFactor
60 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec4, IndirectDiffuseFactor)
61 /// @see IEnvironment::IndirectSpecularFactor
62 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec4, IndirectSpecularFactor)
63 /// @see IEnvironment::EnvMapFactor
64 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec4, EnvMapFactor)
65 /// @see IEnvironment::RadianceImage
66 META_INTERFACE_OBJECT_PROPERTY(Image, RadianceImage)
67 /// @see IEnvironment::RadianceCubemapMipCount
68 META_INTERFACE_OBJECT_PROPERTY(uint32_t, RadianceCubemapMipCount)
69 /// @see IEnvironment::EnvironmentImage
70 META_INTERFACE_OBJECT_PROPERTY(Image, EnvironmentImage)
71 /// @see IEnvironment::EnvironmentMapLodLevel
72 META_INTERFACE_OBJECT_PROPERTY(float, EnvironmentMapLodLevel)
73 /// @see IEnvironment::IrradianceCoefficients
74 META_INTERFACE_OBJECT_ARRAY_PROPERTY(BASE_NS::Math::Vec3, IrradianceCoefficients, IrradianceCoefficient)
75 /// @see IEnvironment::EnvironmentRotation
76 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Quat, EnvironmentRotation)
77 };
78
79 class Shader : public Resource {
80 public:
81 META_INTERFACE_OBJECT(Shader, Resource, IShader)
82 /// @see IShader::CullMode
83 META_INTERFACE_OBJECT_PROPERTY(CullModeFlags, CullMode)
84 /// @see IShader::Blend
85 META_INTERFACE_OBJECT_PROPERTY(bool, Blend)
86 };
87
88 class Sampler : public META_NS::ResetableObject {
89 public:
90 META_INTERFACE_OBJECT(Sampler, META_NS::ResetableObject, ISampler)
91 /// @see ISampler::MinFilter
92 META_INTERFACE_OBJECT_PROPERTY(SamplerFilter, MinFilter)
93 /// @see ISampler::MagFilter
94 META_INTERFACE_OBJECT_PROPERTY(SamplerFilter, MagFilter)
95 /// @see ISampler::MipMapMode
96 META_INTERFACE_OBJECT_PROPERTY(SamplerFilter, MipMapMode)
97 /// @see ISampler::AddressModeU
98 META_INTERFACE_OBJECT_PROPERTY(SamplerAddressMode, AddressModeU)
99 /// @see ISampler::AddressModeV
100 META_INTERFACE_OBJECT_PROPERTY(SamplerAddressMode, AddressModeV)
101 /// @see ISampler::AddressModeW
102 META_INTERFACE_OBJECT_PROPERTY(SamplerAddressMode, AddressModeW)
103 };
104
105 class MaterialProperty : public META_NS::Object {
106 public:
107 META_INTERFACE_OBJECT(MaterialProperty, META_NS::Object, ITexture)
108 /// @see ITexture::Image
109 META_INTERFACE_OBJECT_PROPERTY(SCENE_NS::Image, Image)
110 /// @see ITexture::Sampler
111 META_INTERFACE_OBJECT_READONLY_PROPERTY(SCENE_NS::Sampler, Sampler)
112 /// @see ITexture::Factor
113 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec4, Factor)
114 /// @see ITexture::Translation
115 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec2, Translation)
116 /// @see ITexture::Rotation
117 META_INTERFACE_OBJECT_PROPERTY(float, Rotation)
118 /// @see ITexture::Scale
119 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec2, Scale)
120 };
121
122 /**
123 * @brief The base IMaterial::Ptr wrapper for all materials
124 */
125 class Material : public Resource {
126 public:
META_INTERFACE_OBJECT(Material,Resource,IMaterial)127 META_INTERFACE_OBJECT(Material, Resource, IMaterial)
128 /// @see IMaterial::Type
129 META_INTERFACE_OBJECT_PROPERTY(MaterialType, Type)
130 /// @see IMaterial::AlphaCutoff
131 META_INTERFACE_OBJECT_PROPERTY(float, AlphaCutoff)
132 /// @see IMaterial::LightingFlags
133 META_INTERFACE_OBJECT_PROPERTY(SCENE_NS::LightingFlags, LightingFlags)
134 /// @see IMaterial::MaterialShader
135 META_INTERFACE_OBJECT_PROPERTY(Shader, MaterialShader)
136 /// @see IMaterial::DepthShader
137 META_INTERFACE_OBJECT_PROPERTY(Shader, DepthShader)
138 /// @see IMaterial::RenderSort
139 META_INTERFACE_OBJECT_PROPERTY(SCENE_NS::RenderSort, RenderSort)
140 /// Returns custom properties assigned to this material.
141 auto GetCustomProperties() const
142 {
143 return META_NS::Metadata(META_INTERFACE_OBJECT_CALL_PTR(GetCustomProperties()));
144 }
145 /// Returns a named custom property with given type. Null if property does not exist or given type is not compatible
146 /// with the property value.
147 template<typename Type>
GetCustomProperty(BASE_NS::string_view name)148 auto GetCustomProperty(BASE_NS::string_view name) const
149 {
150 return META_INTERFACE_OBJECT_CALL_PTR(GetCustomProperty(name));
151 }
152 /// Returns a named custom array property with given type. Null if property does not exist or given type is not
153 /// compatible with the property value.
154 template<typename Type>
GetCustomArrayProperty(BASE_NS::string_view name)155 META_NS::ArrayProperty<Type> GetCustomArrayProperty(BASE_NS::string_view name) const
156 {
157 return META_INTERFACE_OBJECT_CALL_PTR(GetCustomArrayProperty(name));
158 }
159 /**
160 * @brief Returns MaterialProperty with given name.
161 * @param name Name of the material property to find.
162 * @return MaterialProperty with given name or an invalid object if match not found.
163 */
GetMaterialProperty(BASE_NS::string_view name)164 auto GetMaterialProperty(BASE_NS::string_view name) const
165 {
166 auto tis = META_INTERFACE_OBJECT_CALL_PTR(Textures()->GetValue());
167 for (auto&& ti : tis) {
168 if (META_NS::GetName(ti) == name) {
169 return MaterialProperty(ti);
170 }
171 }
172 return MaterialProperty(nullptr);
173 }
174 /**
175 * @brief Returns the MaterialProperty at given texture slot index.
176 * @param index The texture slot index to retrieve.
177 * @return The MaterialProperty at given index or an invalid object in case of invalid index.
178 */
GetMaterialPropertyAt(size_t index)179 auto GetMaterialPropertyAt(size_t index) const
180 {
181 auto ts = META_INTERFACE_OBJECT_CALL_PTR(Textures()->GetValueAt(index));
182 return MaterialProperty(ts);
183 }
184 /**
185 * @brief Returns all material properties associated with the material.
186 */
GetMaterialProperties()187 auto GetMaterialProperties()
188 {
189 auto tis = META_INTERFACE_OBJECT_CALL_PTR(Textures()->GetValue());
190 return META_NS::Internal::ArrayCast<MaterialProperty>(BASE_NS::move(tis));
191 }
192 };
193 /**
194 * @brief Represents a material as a custom shader
195 */
196 class ShaderMaterial : public Material {
197 public:
198 META_INTERFACE_OBJECT(ShaderMaterial, Material, IMaterial)
199 };
200
201 /**
202 * @brief Represents a material as a physically-based metallic roughenss material.
203 */
204 class MetallicRoughnessMaterial : public Material {
205 public:
206 META_INTERFACE_OBJECT(MetallicRoughnessMaterial, Material, IMaterial)
207
208 /// MaterialProperty indices for metallic roughness materials
209 enum MaterialPropertyIndex {
210 BASE_COLOR = 0,
211 NORMAL,
212 MATERIAL,
213 EMISSIVE,
214 AO,
215 CLEARCOAT,
216 CLEARCOAT_ROUGHNESS,
217 CLEARCOAT_NORMAL,
218 SHEEN,
219 TRANSMISSION,
220 SPECULAR
221 };
222
223 /**
224 * @brief Base color factor of pbr material.
225 * Value of factor.xyzw defines rgba color.
226 */
GetBaseColor()227 auto GetBaseColor() const
228 {
229 return GetMaterialPropertyAt(BASE_COLOR);
230 }
231 /**
232 * @brief Normal factor of pbr material.
233 * Value of factor.x defines normal scale.
234 */
GetNormal()235 MaterialProperty GetNormal()
236 {
237 return GetMaterialPropertyAt(NORMAL);
238 }
239 /**
240 * @brief Metallic roughness material parameters.
241 * Value of factor.y defines roughness, factor.z defines metallic and factor.a defines reflectance.
242 */
GetMaterialParameters()243 MaterialProperty GetMaterialParameters()
244 {
245 return GetMaterialPropertyAt(MATERIAL);
246 }
247 /**
248 * @brief Ambient occlusion of pbr material.
249 * Value of factor.x defines ambient occlusion factor.
250 */
GetAmbientOcclusion()251 MaterialProperty GetAmbientOcclusion()
252 {
253 return GetMaterialPropertyAt(AO);
254 }
255 /**
256 * @brief Clearcoat normal.
257 * Value of factor.xyz defines RGB clearcoat normal scale.
258 */
GetEmissive()259 MaterialProperty GetEmissive()
260 {
261 return GetMaterialPropertyAt(EMISSIVE);
262 }
263 /**
264 * @brief Clearcoat intensity.
265 * Value of factor.x defines clearcoat layer intensity.
266 */
GetClearCoat()267 MaterialProperty GetClearCoat()
268 {
269 return GetMaterialPropertyAt(CLEARCOAT);
270 }
271 /**
272 * @brief Clearcoat roughness.
273 * Value of factor.y defines clearcoat layer roughness.
274 */
GetClearCoatRoughness()275 MaterialProperty GetClearCoatRoughness()
276 {
277 return GetMaterialPropertyAt(CLEARCOAT_ROUGHNESS);
278 }
279 /**
280 * @brief Clearcoat normal.
281 * Value of factor.xyz defines RGB clearcoat normal scale.
282 */
GetClearCoatNormal()283 MaterialProperty GetClearCoatNormal()
284 {
285 return GetMaterialPropertyAt(CLEARCOAT_NORMAL);
286 }
287 /**
288 * @brief Sheen color of pbr material.
289 * Value of factor.xyz defines RGB sheen color,
290 * Value of factor.w defines sheen roughness.
291 */
GetSheen()292 MaterialProperty GetSheen()
293 {
294 return GetMaterialPropertyAt(SHEEN);
295 }
296 /**
297 * @brief Transmission percentage texture.
298 * Value of factor.r defines the percentage of light that is transmitted through the surface.
299 * If an image is specified, this value is multiplied with the texel values.
300 */
GetTransmission()301 MaterialProperty GetTransmission()
302 {
303 return GetMaterialPropertyAt(TRANSMISSION);
304 }
305 /**
306 * @brief Specular color of pbr material.
307 * Value of factor.xyz defines RGB specular color,
308 * Value of factor.w defines specular intensity.
309 */
GetSpecular()310 MaterialProperty GetSpecular()
311 {
312 return GetMaterialPropertyAt(SPECULAR);
313 }
314 };
315
316 class Morpher : public META_NS::Object {
317 public:
318 META_INTERFACE_OBJECT(Morpher, META_NS::Object, IMorpher)
319 /// @see IMorpher::MorphNames
320 META_INTERFACE_OBJECT_ARRAY_PROPERTY(BASE_NS::string, MorphNames, MorphName)
321 /// @see IMorpher::MorphWeights
322 META_INTERFACE_OBJECT_ARRAY_PROPERTY(float, MorphWeights, MorphWeight)
323 };
324
325 class SubMesh : public META_NS::Object {
326 public:
327 META_INTERFACE_OBJECT(SubMesh, META_NS::Object, ISubMesh)
328 /// @see ISubMesh::Material
329 META_INTERFACE_OBJECT_PROPERTY(SCENE_NS::Material, Material)
330 /// @see ISubMesh::AABBMin
331 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec3, AABBMin)
332 /// @see ISubMesh::AABBMax
333 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::Math::Vec3, AABBMax)
334 };
335
336 class Mesh : public Resource {
337 public:
338 META_INTERFACE_OBJECT(Mesh, Resource, IMesh)
339 /// @see IMesh::AABBMin
340 META_INTERFACE_OBJECT_READONLY_PROPERTY(BASE_NS::Math::Vec3, AABBMin)
341 /// @see IMesh::AABBMax
342 META_INTERFACE_OBJECT_READONLY_PROPERTY(BASE_NS::Math::Vec3, AABBMax)
343 /// @see IMesh::SubMeshes
344 META_INTERFACE_OBJECT_READONLY_ARRAY_PROPERTY(SubMesh, SubMeshes, SubMesh)
345 };
346
347 SCENE_END_NAMESPACE()
348
349 #endif // SCENE_API_RESOURCE_H
350