• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INTERFACE_IMATERIAL_H
17 #define SCENE_INTERFACE_IMATERIAL_H
18 
19 #include <scene/base/namespace.h>
20 #include <scene/interface/intf_shader.h>
21 #include <scene/interface/intf_texture.h>
22 
23 #include <meta/base/interface_macros.h>
24 #include <meta/interface/intf_metadata.h>
25 
26 SCENE_BEGIN_NAMESPACE()
27 
28 enum class MaterialType : uint8_t {
29     /** Enumeration for Metallic roughness workflow */
30     METALLIC_ROUGHNESS = 0,
31     /** Enumeration for Specular glossiness workflow */
32     SPECULAR_GLOSSINESS = 1,
33     /** Enumeration for KHR materials unlit workflow */
34     UNLIT = 2,
35     /** Enumeration for special unlit shadow receiver */
36     UNLIT_SHADOW_ALPHA = 3,
37     /** Custom material. Could be used with custom material model e.g. with shader graph.
38      * Disables automatic factor based modifications for flags.
39      * Note: that base color is always automatically pre-multiplied in all cases
40      */
41     CUSTOM = 4,
42     /** Custom complex material. Could be used with custom material model e.g. with shader graph.
43      * Disables automatic factor based modifications for flags.
44      * Does not use deferred rendering path in any case due to complex material model.
45      * Note: that base color is always automatically pre-multiplied in all cases
46      */
47     CUSTOM_COMPLEX = 5
48 };
49 
50 /** Default render sort layer id */
51 static constexpr uint32_t DEFAULT_RENDER_SORT_LAYER_ID = 32u;
52 
53 struct RenderSort {
54     /** Render sort layer. Within a render slot a layer can define a sort layer order.
55      * There are 0-63 values available. Default id value is 32.
56      * 0 first, 63 last
57      * 1. Typical use case is to set render sort layer to objects which render with depth test without depth write.
58      * 2. Typical use case is to always render character and/or camera object first to cull large parts of the view.
59      * 3. Sort e.g. plane layers.
60      */
61     /** Sort layer used sorting submeshes in rendering in render slots. Valid ID values 0 - 63. */
62     uint8_t renderSortLayer { DEFAULT_RENDER_SORT_LAYER_ID };
63     /** Sort layer order to describe fine order within sort layer. Valid order 0 - 255 */
64     uint8_t renderSortLayerOrder {};
65 };
66 
67 enum class LightingFlags : uint32_t {
68     /** Defines whether this material receives shadow */
69     SHADOW_RECEIVER_BIT = (1 << 0),
70     /** Defines whether this material is a shadow caster */
71     SHADOW_CASTER_BIT = (1 << 1),
72     /** Defines whether this material will receive light from punctual lights (points, spots, directional) */
73     PUNCTUAL_LIGHT_RECEIVER_BIT = (1 << 2),
74     /** Defines whether this material will receive indirect light from SH and cubemaps */
75     INDIRECT_LIGHT_RECEIVER_BIT = (1 << 3),
76 };
77 
78 inline LightingFlags operator|(LightingFlags l, LightingFlags r)
79 {
80     return LightingFlags(static_cast<uint32_t>(l) | static_cast<uint32_t>(r));
81 }
82 
83 class IMaterial : public CORE_NS::IInterface {
84     META_INTERFACE(CORE_NS::IInterface, IMaterial, "c531141e-9d59-4ba3-9aca-b10986ed8805")
85 public:
86     /**
87      * @brief Type of the material.
88      */
89     META_PROPERTY(MaterialType, Type)
90 
91     /**
92      * @brief Alpha cut off value, set the cutting value for alpha (0.0 - 1.0). Below 1.0 starts to affect.
93      * @return pointer to property
94      */
95     META_PROPERTY(float, AlphaCutoff)
96 
97     /**
98      * @brief Bitfield of the material lighting flags.
99      * @return pointer to property
100      */
101     META_PROPERTY(SCENE_NS::LightingFlags, LightingFlags)
102 
103     META_PROPERTY(IShader::Ptr, MaterialShader)
104     META_PROPERTY(IShader::Ptr, DepthShader)
105 
106     META_PROPERTY(SCENE_NS::RenderSort, RenderSort)
107 
108     META_READONLY_ARRAY_PROPERTY(ITexture::Ptr, Textures)
109     META_READONLY_PROPERTY(META_NS::IMetadata::Ptr, CustomProperties);
110     /**
111      * @brief Synchronize custom properties from ECS to engine and return them.
112      * @return CustomProperties members whose types registered to the engine, if any. Null pointer otherwise.
113      */
114     virtual META_NS::IMetadata::Ptr GetCustomProperties() const = 0;
115     /**
116      * @brief Synchronize custom properties from ECS to engine and return the requested sub property.
117      * @param name The name of the property. "MaterialComponent.customProperties." prefix is optional.
118      * @return The requested property, if it exists and its type is registered to the engine. Null pointer otherwise.
119      */
120     virtual META_NS::IProperty::Ptr GetCustomProperty(BASE_NS::string_view name) const = 0;
121 
122     template<typename Type>
GetCustomProperty(BASE_NS::string_view name)123     META_NS::Property<Type> GetCustomProperty(BASE_NS::string_view name) const
124     {
125         return META_NS::Property<Type>(GetCustomProperty(name));
126     }
127     template<typename Type>
GetCustomArrayProperty(BASE_NS::string_view name)128     META_NS::ArrayProperty<Type> GetCustomArrayProperty(BASE_NS::string_view name) const
129     {
130         return META_NS::ArrayProperty<Type>(GetCustomProperty(name));
131     }
132 };
133 
134 META_REGISTER_CLASS(Material, "ffcb25d5-18fd-42ad-8df5-ebd5197bc8a6", META_NS::ObjectCategoryBits::NO_CATEGORY)
135 
136 SCENE_END_NAMESPACE()
137 
138 META_TYPE(SCENE_NS::MaterialType)
139 META_TYPE(SCENE_NS::RenderSort)
140 META_TYPE(SCENE_NS::LightingFlags)
141 META_INTERFACE_TYPE(SCENE_NS::IMaterial)
142 
143 #endif
144