2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/internal/model-components/model-primitive-impl.h>
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/object/type-registry.h>
28 #include <dali-scene3d/internal/common/image-resource-loader.h>
29 #include <dali-scene3d/internal/light/light-impl.h>
30 #include <dali-scene3d/internal/model-components/material-impl.h>
31 #include <dali-scene3d/public-api/loader/environment-definition.h>
33 #include <dali/integration-api/debug.h>
44 * Creates control through type registry
48 return Scene3D::ModelPrimitive::New();
51 // Setup properties, signals and actions using the type-registry.
52 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::ModelPrimitive, Dali::BaseHandle, Create);
53 DALI_TYPE_REGISTRATION_END()
55 static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10;
57 } // unnamed namespace
59 ModelPrimitivePtr ModelPrimitive::New()
61 ModelPrimitivePtr primitive = new ModelPrimitive();
63 primitive->Initialize();
68 ModelPrimitive::ModelPrimitive()
69 : mShaderManager(new Scene3D::Loader::ShaderManager())
73 ModelPrimitive::~ModelPrimitive()
77 GetImplementation(mMaterial).RemoveObserver(this);
82 void ModelPrimitive::Initialize()
86 void ModelPrimitive::SetRenderer(Dali::Renderer renderer)
89 mGeometry = renderer.GetGeometry();
90 mTextureSet = renderer.GetTextures();
91 mShader = renderer.GetShader();
94 Dali::Renderer ModelPrimitive::GetRenderer() const
99 void ModelPrimitive::SetGeometry(Dali::Geometry geometry)
101 mGeometry = geometry;
105 Dali::Geometry ModelPrimitive::GetGeometry() const
110 void ModelPrimitive::SetMaterial(Dali::Scene3D::Material material, bool updateRenderer)
117 if(mMaterial != material)
119 // Stop observe from previous material.
122 GetImplementation(mMaterial).RemoveObserver(this);
125 mMaterial = material;
127 // Start observe from new material.
131 GetImplementation(mMaterial).AddObserver(this);
136 mIsMaterialChanged = true;
137 if(GetImplementation(mMaterial).IsResourceReady())
139 GetImplementation(mMaterial).UpdateMaterialData();
140 ApplyMaterialToRenderer();
143 UpdateShadowMapTexture();
144 UpdateImageBasedLightTexture();
148 Dali::Scene3D::Material ModelPrimitive::GetMaterial() const
153 void ModelPrimitive::AddPrimitiveObserver(ModelPrimitiveModifyObserver* observer)
155 mObservers.insert(observer);
158 void ModelPrimitive::RemovePrimitiveObserver(ModelPrimitiveModifyObserver* observer)
160 mObservers.erase(observer);
163 void ModelPrimitive::SetShadowMapTexture(Dali::Texture shadowMapTexture)
165 mShadowMapTexture = shadowMapTexture;
166 UpdateShadowMapTexture();
169 void ModelPrimitive::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
171 mDiffuseTexture = diffuseTexture;
172 mSpecularTexture = specularTexture;
173 mIblScaleFactor = iblScaleFactor;
174 mSpecularMipmapLevels = specularMipmapLevels;
176 UpdateImageBasedLightTexture();
179 void ModelPrimitive::SetImageBasedLightScaleFactor(float iblScaleFactor)
181 mIblScaleFactor = iblScaleFactor;
182 if(mRenderer && mMaterial)
184 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), iblScaleFactor);
188 void ModelPrimitive::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager)
190 if(mShaderManager != shaderManager)
192 mShaderManager = (shaderManager) ? shaderManager : new Scene3D::Loader::ShaderManager();
193 if(mMaterial && GetImplementation(mMaterial).IsResourceReady())
195 ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag::SHADER);
200 void ModelPrimitive::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data)
202 mBlendShapeData = std::move(data);
203 Scene3D::Loader::BlendShapes::ConfigureProperties(mBlendShapeData, mRenderer);
206 void ModelPrimitive::SetBlendShapeGeometry(Dali::Texture blendShapeGeometry)
208 mBlendShapeGeometry = blendShapeGeometry;
211 void ModelPrimitive::SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents, Scene3D::Loader::BlendShapes::Version version)
213 mHasPositions = hasPositions;
214 mHasNormals = hasNormals;
215 mHasTangents = hasTangents;
216 mBlendShapeVersion = version;
219 void ModelPrimitive::SetSkinned(bool isSkinned)
221 mHasSkinning = isSkinned;
224 // From MaterialModifyObserver
226 void ModelPrimitive::OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag)
228 ApplyMaterialToRenderer(flag);
231 void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag)
238 uint32_t shaderFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER));
239 if(mIsMaterialChanged || shaderFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER))
241 Scene3D::Loader::ShaderOption shaderOption = GetImplementation(mMaterial).GetShaderOption();
243 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::VEC4_TANGENT);
246 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::SKINNING);
248 if(mHasPositions || mHasNormals || mHasTangents)
252 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_POSITION);
256 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_NORMAL);
260 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_TANGENT);
262 if(mBlendShapeVersion == Scene3D::Loader::BlendShapes::Version::VERSION_2_0)
264 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_VERSION_2_0);
269 mShader = mShaderManager->ProduceShader(shaderOption);
277 mRenderer.SetShader(mShader);
281 uint32_t textureFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE));
282 if(mIsMaterialChanged || textureFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE))
284 mTextureSet = GetImplementation(mMaterial).GetTextureSet();
286 if(mBlendShapeGeometry)
288 TextureSet newTextureSet = TextureSet::New();
289 newTextureSet.SetTexture(0u, mBlendShapeGeometry);
291 const unsigned int numberOfTextures = mTextureSet.GetTextureCount();
292 for(unsigned int index = 0u; index < numberOfTextures; ++index)
294 const unsigned int newIndex = index + 1u;
295 newTextureSet.SetTexture(newIndex, mTextureSet.GetTexture(index));
296 newTextureSet.SetSampler(newIndex, mTextureSet.GetSampler(index));
299 mTextureSet = newTextureSet;
302 uint32_t textureCount = mTextureSet.GetTextureCount();
304 if(!mShadowMapTexture)
306 mShadowMapTexture = Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
308 mTextureSet.SetTexture(textureCount++, mShadowMapTexture);
310 Texture brdfTexture = Scene3D::Loader::EnvironmentDefinition::GetBrdfTexture();
311 if(!mSpecularTexture || !mDiffuseTexture)
313 Scene3D::Loader::EnvironmentMapData environmentMapData;
314 environmentMapData.mPixelData.resize(6);
315 for(auto& face : environmentMapData.mPixelData)
317 face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
319 environmentMapData.SetEnvironmentMapType(Dali::Scene3D::EnvironmentMapType::CUBEMAP);
320 Texture iblTexture = environmentMapData.GetTexture();
321 mDiffuseTexture = iblTexture;
322 mSpecularTexture = iblTexture;
325 mTextureSet.SetTexture(textureCount++, brdfTexture);
326 mTextureSet.SetTexture(textureCount++, mDiffuseTexture);
327 mTextureSet.SetTexture(textureCount, mSpecularTexture);
329 auto specularSampler = Sampler::New();
330 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
331 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
332 mTextureSet.SetSampler(textureCount, specularSampler);
340 mRenderer.SetTextures(mTextureSet);
344 uint32_t uniformFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM));
345 if(mIsMaterialChanged || uniformFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM))
349 mNeedToSetRendererUniform = true;
353 UpdateRendererUniform();
356 mIsMaterialChanged = false;
359 void ModelPrimitive::CreateRenderer()
361 if(!mShader || !mGeometry || !mTextureSet || mRenderer)
366 mRenderer = Renderer::New(mGeometry, mShader);
367 mRenderer.SetTextures(mTextureSet);
368 UpdateRendererUniform();
370 for(auto* observer : mObservers)
372 observer->OnRendererCreated(mRenderer);
376 void ModelPrimitive::UpdateShadowMapTexture()
378 if(mRenderer && mMaterial)
380 Dali::TextureSet textures = mRenderer.GetTextures();
386 uint32_t textureCount = textures.GetTextureCount();
387 if(mShadowMapTexture &&
388 textureCount >= GetImplementation(mMaterial).GetShadowMapTextureOffset() &&
389 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset()) != mShadowMapTexture)
391 Dali::TextureSet newTextures = Dali::TextureSet::New();
393 for(uint32_t index = 0u; index < textureCount; ++index)
395 Dali::Texture texture = textures.GetTexture(index);
396 if(index == textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset())
398 texture = (!!mShadowMapTexture) ? mShadowMapTexture : Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
401 newTextures.SetTexture(index, texture);
402 newTextures.SetSampler(index, textures.GetSampler(index));
405 mRenderer.SetTextures(newTextures);
410 void ModelPrimitive::UpdateImageBasedLightTexture()
412 if(mRenderer && mMaterial)
414 Dali::TextureSet textures = mRenderer.GetTextures();
420 uint32_t textureCount = textures.GetTextureCount();
421 if(textureCount > 2u &&
422 (textures.GetTexture(textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset()) != mDiffuseTexture ||
423 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset()) != mSpecularTexture))
425 Dali::TextureSet newTextures = Dali::TextureSet::New();
427 for(uint32_t index = 0u; index < textureCount; ++index)
429 Dali::Texture texture = textures.GetTexture(index);
430 if(index == textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset())
432 texture = mDiffuseTexture;
434 else if(index == textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset())
436 texture = mSpecularTexture;
439 newTextures.SetTexture(index, texture);
440 newTextures.SetSampler(index, textures.GetSampler(index));
443 mRenderer.SetTextures(newTextures);
445 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
446 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
450 void ModelPrimitive::UpdateRendererUniform()
454 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
455 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
456 GetImplementation(mMaterial).SetRendererUniform(mRenderer);
460 } // namespace Internal
462 } // namespace Scene3D