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;
167 UpdateShadowMapTexture();
170 void ModelPrimitive::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
172 mDiffuseTexture = diffuseTexture;
173 mSpecularTexture = specularTexture;
174 mIblScaleFactor = iblScaleFactor;
175 mSpecularMipmapLevels = specularMipmapLevels;
177 UpdateImageBasedLightTexture();
180 void ModelPrimitive::SetImageBasedLightScaleFactor(float iblScaleFactor)
182 mIblScaleFactor = iblScaleFactor;
183 if(mRenderer && mMaterial)
185 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), iblScaleFactor);
189 void ModelPrimitive::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager)
191 if(mShaderManager != shaderManager)
193 mShaderManager = (shaderManager) ? shaderManager : new Scene3D::Loader::ShaderManager();
194 if(mMaterial && GetImplementation(mMaterial).IsResourceReady())
196 ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag::SHADER);
201 void ModelPrimitive::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data)
203 mBlendShapeData = std::move(data);
204 Scene3D::Loader::BlendShapes::ConfigureProperties(mBlendShapeData, mRenderer);
207 void ModelPrimitive::SetBlendShapeGeometry(Dali::Texture blendShapeGeometry)
209 mBlendShapeGeometry = blendShapeGeometry;
212 void ModelPrimitive::SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents, Scene3D::Loader::BlendShapes::Version version)
214 mHasPositions = hasPositions;
215 mHasNormals = hasNormals;
216 mHasTangents = hasTangents;
217 mBlendShapeVersion = version;
220 void ModelPrimitive::SetSkinned(bool isSkinned)
222 mHasSkinning = isSkinned;
225 // From MaterialModifyObserver
227 void ModelPrimitive::OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag)
229 ApplyMaterialToRenderer(flag);
232 void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag)
239 uint32_t shaderFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER));
240 if(mIsMaterialChanged || shaderFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER))
242 Scene3D::Loader::ShaderOption shaderOption = GetImplementation(mMaterial).GetShaderOption();
244 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::VEC4_TANGENT);
247 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::SKINNING);
249 if(mHasPositions || mHasNormals || mHasTangents)
253 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_POSITION);
257 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_NORMAL);
261 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_TANGENT);
263 if(mBlendShapeVersion == Scene3D::Loader::BlendShapes::Version::VERSION_2_0)
265 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_VERSION_2_0);
270 mShader = mShaderManager->ProduceShader(shaderOption);
278 mRenderer.SetShader(mShader);
282 uint32_t textureFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE));
283 if(mIsMaterialChanged || textureFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE))
285 mTextureSet = GetImplementation(mMaterial).GetTextureSet();
287 if(mBlendShapeGeometry)
289 TextureSet newTextureSet = TextureSet::New();
290 newTextureSet.SetTexture(0u, mBlendShapeGeometry);
292 const unsigned int numberOfTextures = mTextureSet.GetTextureCount();
293 for(unsigned int index = 0u; index < numberOfTextures; ++index)
295 const unsigned int newIndex = index + 1u;
296 newTextureSet.SetTexture(newIndex, mTextureSet.GetTexture(index));
297 newTextureSet.SetSampler(newIndex, mTextureSet.GetSampler(index));
300 mTextureSet = newTextureSet;
303 uint32_t textureCount = mTextureSet.GetTextureCount();
305 if(!mShadowMapTexture)
307 mShadowMapTexture = Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
309 mTextureSet.SetTexture(textureCount++, mShadowMapTexture);
311 Texture brdfTexture = Scene3D::Loader::EnvironmentDefinition::GetBrdfTexture();
312 if(!mSpecularTexture || !mDiffuseTexture)
314 Scene3D::Loader::EnvironmentMapData environmentMapData;
315 environmentMapData.mPixelData.resize(6);
316 for(auto& face : environmentMapData.mPixelData)
318 face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
320 environmentMapData.SetEnvironmentMapType(Dali::Scene3D::EnvironmentMapType::CUBEMAP);
321 Texture iblTexture = environmentMapData.GetTexture();
322 mDiffuseTexture = iblTexture;
323 mSpecularTexture = iblTexture;
326 mTextureSet.SetTexture(textureCount++, brdfTexture);
327 mTextureSet.SetTexture(textureCount++, mDiffuseTexture);
328 mTextureSet.SetTexture(textureCount, mSpecularTexture);
330 auto specularSampler = Sampler::New();
331 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
332 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
333 mTextureSet.SetSampler(textureCount, specularSampler);
341 mRenderer.SetTextures(mTextureSet);
345 uint32_t uniformFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM));
346 if(mIsMaterialChanged || uniformFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM))
350 mNeedToSetRendererUniform = true;
354 UpdateRendererUniform();
357 mIsMaterialChanged = false;
360 void ModelPrimitive::CreateRenderer()
362 if(!mShader || !mGeometry || !mTextureSet || mRenderer)
367 mRenderer = Renderer::New(mGeometry, mShader);
368 mRenderer.SetTextures(mTextureSet);
369 UpdateRendererUniform();
371 for(auto* observer : mObservers)
373 observer->OnRendererCreated(mRenderer);
377 void ModelPrimitive::UpdateShadowMapTexture()
379 if(mRenderer && mMaterial)
381 Dali::TextureSet textures = mRenderer.GetTextures();
387 uint32_t textureCount = textures.GetTextureCount();
388 if(mShadowMapTexture &&
389 textureCount >= GetImplementation(mMaterial).GetShadowMapTextureOffset() &&
390 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset()) != mShadowMapTexture)
392 Dali::TextureSet newTextures = Dali::TextureSet::New();
394 for(uint32_t index = 0u; index < textureCount; ++index)
396 Dali::Texture texture = textures.GetTexture(index);
397 if(index == textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset())
399 texture = (!!mShadowMapTexture) ? mShadowMapTexture : Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
402 newTextures.SetTexture(index, texture);
403 newTextures.SetSampler(index, textures.GetSampler(index));
406 mRenderer.SetTextures(newTextures);
411 void ModelPrimitive::UpdateImageBasedLightTexture()
413 if(mRenderer && mMaterial)
415 Dali::TextureSet textures = mRenderer.GetTextures();
421 uint32_t textureCount = textures.GetTextureCount();
422 if(textureCount > 2u &&
423 (textures.GetTexture(textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset()) != mDiffuseTexture ||
424 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset()) != mSpecularTexture))
426 Dali::TextureSet newTextures = Dali::TextureSet::New();
428 for(uint32_t index = 0u; index < textureCount; ++index)
430 Dali::Texture texture = textures.GetTexture(index);
431 if(index == textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset())
433 texture = mDiffuseTexture;
435 else if(index == textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset())
437 texture = mSpecularTexture;
440 newTextures.SetTexture(index, texture);
441 newTextures.SetSampler(index, textures.GetSampler(index));
444 mRenderer.SetTextures(newTextures);
446 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
447 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
451 void ModelPrimitive::UpdateRendererUniform()
455 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
456 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
457 GetImplementation(mMaterial).SetRendererUniform(mRenderer);
461 } // namespace Internal
463 } // namespace Scene3D