1 #ifndef DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H
2 #define DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali/public-api/common/vector-wrapper.h>
22 #include <dali/public-api/images/image-operations.h>
23 #include <dali/public-api/math/vector4.h>
27 #include <dali-scene3d/public-api/api.h>
28 #include <dali-scene3d/public-api/loader/environment-definition.h>
29 #include <dali-scene3d/public-api/loader/index.h>
30 #include <dali-scene3d/public-api/loader/utils.h>
32 namespace Dali::Scene3D::Loader
35 * @brief Helper enum for encoding and decoding sampler states.
37 struct DALI_SCENE3D_API SamplerFlags
45 FILTER_LINEAR = NthBit(0),
46 FILTER_MIPMAP_NEAREST = NthBit(1),
47 FILTER_MIPMAP_LINEAR = NthBit(2),
51 WRAP_CLAMP = NthBit(0),
52 WRAP_MIRROR = NthBit(1),
54 // Layout - apply shift, then mask
56 FILTER_MIN_MASK = NthBit(FILTER_MIN_BITS) - 1,
59 FILTER_MAG_SHIFT = FILTER_MIN_BITS,
60 FILTER_MAG_MASK = NthBit(FILTER_MAG_BITS) - 1,
63 WRAP_S_SHIFT = FILTER_MAG_SHIFT + FILTER_MAG_BITS,
64 WRAP_S_MASK = NthBit(WRAP_S_BITS) - 1,
67 WRAP_T_SHIFT = WRAP_S_SHIFT + WRAP_S_BITS,
68 WRAP_T_MASK = NthBit(WRAP_T_BITS) - 1,
71 MIPMAP_MASK = FILTER_MIPMAP_LINEAR | FILTER_MIPMAP_NEAREST,
74 DEFAULT = FILTER_LINEAR | (FILTER_LINEAR << FILTER_MAG_SHIFT) | (WRAP_REPEAT << WRAP_S_SHIFT) | (WRAP_REPEAT << WRAP_T_SHIFT), // LINEAR filters, REPEAT wraps
78 * @return SamplerFlags bit pattern calculated from the given Dali Sampler settings.
80 static Type Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT);
83 * @brief Decodes the minification filter patter of @a flags into the corresponding FilterMode.
85 static FilterMode::Type GetMinFilter(Type flags);
88 * @brief Decodes the magnification filter patter of @a flags into the corresponding FilterMode.
90 static FilterMode::Type GetMagFilter(Type flags);
93 * @brief Decodes the horizontal wrap pattern of @a flags into the corresponding WrapMode.
95 static WrapMode::Type GetWrapS(Type flags);
98 * @brief Decodes the vertical wrap pattern of @a flags into the corresponding WrapMode.
100 static WrapMode::Type GetWrapT(Type flags);
103 * @brief Creates a Sampler with the settings encoded in @a flags.
105 static Sampler MakeSampler(Type flags);
109 * @brief Defines a texture from a combination of an image URI and its sampler definition.
111 struct DALI_SCENE3D_API TextureDefinition
113 std::string mImageUri; // When the texture is loaded from embedded resources, this URI is used as a data stream.
114 SamplerFlags::Type mSamplerFlags;
115 ImageDimensions mMinImageDimensions;
116 SamplingMode::Type mSamplingMode;
117 std::vector<uint8_t> mTextureBuffer;
119 TextureDefinition(const std::string& imageUri = "", SamplerFlags::Type samplerFlags = SamplerFlags::DEFAULT, ImageDimensions minImageDimensions = ImageDimensions(), SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
120 TextureDefinition(std::string&& imageUri, SamplerFlags::Type samplerFlags = SamplerFlags::DEFAULT, ImageDimensions minImageDimensions = ImageDimensions(), SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
121 TextureDefinition(std::vector<uint8_t>&& textureBuffer, SamplerFlags::Type samplerFlags = SamplerFlags::DEFAULT, ImageDimensions minImageDimensions = ImageDimensions(), SamplingMode::Type samplingMode = SamplingMode::BOX_THEN_LINEAR);
125 * @brief Defines a material with a number of texture stages, whether mipmappping
126 * is enabled, and an index of an environment (usually of all environments in a
127 * scene). Textures from the environment are added last when the DALi TextureSet
130 struct DALI_SCENE3D_API MaterialDefinition
132 enum Flags : uint32_t
136 METALLIC = NthBit(1),
137 ROUGHNESS = NthBit(2),
139 EMISSIVE = NthBit(4),
140 OCCLUSION = NthBit(5),
141 SPECULAR = NthBit(6),
142 SPECULAR_COLOR = NthBit(7),
143 SUBSURFACE = NthBit(8), // Note: dli-only
145 // Other binary options
146 TRANSPARENCY = NthBit(20),
147 GLTF_CHANNELS = NthBit(21), // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#pbrmetallicroughnessmetallicroughnesstexture
149 // Alpha cutoff - reserved from the 24th bit
150 ALPHA_CUTOFF_BITS = 8,
151 ALPHA_CUTOFF_SHIFT = sizeof(uint32_t) * 8 - ALPHA_CUTOFF_BITS,
152 ALPHA_CUTOFF_MASK = (1 << ALPHA_CUTOFF_BITS) - 1,
156 * @brief A(n image based) texture that's used in a material.
161 TextureDefinition mTexture;
164 using Vector = std::vector<std::pair<MaterialDefinition, TextureSet>>;
171 SamplerFlags::Type mSamplerFlags;
174 std::vector<TextureData> mTextures;
177 MaterialDefinition() = default;
179 MaterialDefinition(const MaterialDefinition&) = delete;
180 MaterialDefinition& operator=(const MaterialDefinition&) = delete;
182 MaterialDefinition(MaterialDefinition&&) = default;
183 MaterialDefinition& operator=(MaterialDefinition&&) = default;
186 * @brief Loads (or, in the case of solid color materials, creates) raw pixel data,
187 * which is then returned.
188 * @note This may be called from any thread.
190 RawData LoadRaw(const std::string& imagesPath);
193 * @brief Creates Textures from the pixel data in @a raw, gets the
194 * the cube maps from the iEnvironment'th element of @a environments,
195 * then creates a DALi TextureSet and returns it.
196 * @note This must be called from the event thread.
197 * @note The textures are added in the following order: 2D, cube maps.
199 TextureSet Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const;
202 * @brief Checks if the given mask matches any of the textures defined.
204 bool CheckTextures(uint32_t flags) const;
207 * @return The alpha test reference value.
208 * @note A value of 0.f means no alpha testing.
210 float GetAlphaCutoff() const
212 return ((mFlags >> ALPHA_CUTOFF_SHIFT) & ALPHA_CUTOFF_MASK) / 255.f;
216 * @brief Encodes the alpha test reference @a value in flags.
217 * @note A value of 0.f means no alpha testing.
219 void SetAlphaCutoff(float value)
221 DALI_ASSERT_DEBUG(value >= 0.f && value <= 1.f);
222 mFlags |= static_cast<uint8_t>(std::round(value * 255.f)) << ALPHA_CUTOFF_SHIFT;
226 std::shared_ptr<RawData> mRawData;
227 uint32_t mFlags = 0x0;
229 Index mEnvironmentIdx = 0;
230 Vector4 mColor = Color::WHITE;
231 float mMetallic = 1.f;
232 float mRoughness = 1.f;
233 Vector4 mBaseColorFactor = Vector4::ONE;
234 float mNormalScale = 1.f;
235 float mOcclusionStrength = 1.f;
236 Vector3 mEmissiveFactor = Vector3::ZERO;
237 float mDielectricSpecular = 0.04f;
238 float mSpecularFactor = 1.0f;
239 Vector3 mSpecularColorFactor = Vector3::ONE;
241 // For the glTF, each of albedo, metallicRoughness, normal textures are not essential.
242 bool mNeedAlbedoTexture = true;
243 bool mNeedMetallicRoughnessTexture = true;
244 bool mNeedNormalTexture = true;
245 bool mDoubleSided = false;
247 bool mIsOpaque = true;
248 bool mIsMask = false;
250 std::vector<TextureStage> mTextureStages;
253 } // namespace Dali::Scene3D::Loader
255 #endif //DALI_SCENE3D_LOADER_MATERIAL_DEFINITION_H