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>
34 #include <dali/public-api/object/property-array.h>
35 #include <dali/public-api/object/property-map.h>
37 #if defined(DEBUG_ENABLED)
38 #include <sys/types.h>
41 namespace fs = std::filesystem;
52 #if defined(DEBUG_ENABLED)
53 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE3D_MODEL_PRIMITIVE");
55 std::string tmpFilename(std::string prefix, std::string suffix)
59 std::ostringstream oss;
60 oss << prefix << getpid() << "_" << std::setfill('0') << std::setw(4) << id << suffix;
64 #define DALI_LOG_WRITE_FILE(filename, stream) \
66 fs::path tmp = fs::temp_directory_path(); \
68 std::ofstream ostrm(tmp, std::ios::out); \
73 inline Property::Map GetMap(Shader shader)
75 Property::Value program = shader[Shader::Property::PROGRAM];
76 Property::Map* map{nullptr};
77 if(program.GetType() == Property::ARRAY)
79 Property::Array* array = program.GetArray();
82 Property::Value& value = array->GetElementAt(0);
83 if(value.GetType() == Property::MAP)
89 else if(program.GetType() == Property::MAP)
91 map = program.GetMap();
97 return Property::Map();
102 * Creates control through type registry
106 return Scene3D::ModelPrimitive::New();
109 // Setup properties, signals and actions using the type-registry.
110 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::ModelPrimitive, Dali::BaseHandle, Create);
111 DALI_TYPE_REGISTRATION_END()
113 static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10;
115 } // unnamed namespace
117 ModelPrimitivePtr ModelPrimitive::New()
119 ModelPrimitivePtr primitive = new ModelPrimitive();
121 primitive->Initialize();
126 ModelPrimitive::ModelPrimitive()
127 : mShaderManager(new Scene3D::Loader::ShaderManager())
131 ModelPrimitive::~ModelPrimitive()
135 GetImplementation(mMaterial).RemoveObserver(this);
140 void ModelPrimitive::Initialize()
144 void ModelPrimitive::SetRenderer(Dali::Renderer renderer)
146 mRenderer = renderer;
147 mGeometry = renderer.GetGeometry();
148 mTextureSet = renderer.GetTextures();
149 mShader = renderer.GetShader();
152 Dali::Renderer ModelPrimitive::GetRenderer() const
157 void ModelPrimitive::SetGeometry(Dali::Geometry geometry)
159 mGeometry = geometry;
163 Dali::Geometry ModelPrimitive::GetGeometry() const
168 void ModelPrimitive::SetMaterial(Dali::Scene3D::Material material, bool updateRenderer)
175 if(mMaterial != material)
177 // Stop observe from previous material.
180 GetImplementation(mMaterial).RemoveObserver(this);
183 mMaterial = material;
185 // Start observe from new material.
189 GetImplementation(mMaterial).AddObserver(this);
194 mIsMaterialChanged = true;
195 if(GetImplementation(mMaterial).IsResourceReady())
197 GetImplementation(mMaterial).UpdateMaterialData();
198 ApplyMaterialToRenderer();
201 UpdateShadowMapTexture();
202 UpdateImageBasedLightTexture();
206 Dali::Scene3D::Material ModelPrimitive::GetMaterial() const
211 void ModelPrimitive::AddPrimitiveObserver(ModelPrimitiveModifyObserver* observer)
213 mObservers.insert(observer);
216 void ModelPrimitive::RemovePrimitiveObserver(ModelPrimitiveModifyObserver* observer)
218 mObservers.erase(observer);
221 void ModelPrimitive::SetShadowMapTexture(Dali::Texture shadowMapTexture)
223 mShadowMapTexture = shadowMapTexture;
224 UpdateShadowMapTexture();
227 void ModelPrimitive::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
229 mDiffuseTexture = diffuseTexture;
230 mSpecularTexture = specularTexture;
231 mIblScaleFactor = iblScaleFactor;
232 mSpecularMipmapLevels = specularMipmapLevels;
234 UpdateImageBasedLightTexture();
237 void ModelPrimitive::SetImageBasedLightScaleFactor(float iblScaleFactor)
239 mIblScaleFactor = iblScaleFactor;
240 if(mRenderer && mMaterial)
242 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), iblScaleFactor);
246 void ModelPrimitive::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager, Loader::ShaderOption::HashType hash)
248 if(mShaderManager != shaderManager)
250 mShaderManager = (shaderManager) ? shaderManager : new Scene3D::Loader::ShaderManager();
251 if(mMaterial && GetImplementation(mMaterial).IsResourceReady())
253 ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag::SHADER, hash);
258 void ModelPrimitive::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data)
260 mBlendShapeData = std::move(data);
261 Scene3D::Loader::BlendShapes::ConfigureProperties(mBlendShapeData, mRenderer);
264 void ModelPrimitive::SetBlendShapeGeometry(Dali::Texture blendShapeGeometry)
266 mBlendShapeGeometry = blendShapeGeometry;
269 void ModelPrimitive::SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents, Scene3D::Loader::BlendShapes::Version version)
271 mHasPositions = hasPositions;
272 mHasNormals = hasNormals;
273 mHasTangents = hasTangents;
274 mBlendShapeVersion = version;
277 void ModelPrimitive::SetSkinned(bool isSkinned, uint32_t numberOfJointSets)
279 mHasSkinning = isSkinned;
280 mNumberOfJointSets = numberOfJointSets;
283 void ModelPrimitive::SetVertexColor(bool hasVertexColor)
285 mHasVertexColor = hasVertexColor;
288 // From MaterialModifyObserver
290 void ModelPrimitive::OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag)
292 ApplyMaterialToRenderer(flag);
295 void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag, Loader::ShaderOption::HashType oldHash)
302 uint32_t shaderFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER));
303 if(mIsMaterialChanged || shaderFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER))
305 Scene3D::Loader::ShaderOption shaderOption = GetImplementation(mMaterial).GetShaderOption();
307 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::VEC4_TANGENT);
310 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::SKINNING);
311 shaderOption.AddJointMacros(mNumberOfJointSets);
315 shaderOption.AddJointMacros(0);
319 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::COLOR_ATTRIBUTE);
321 if(mHasPositions || mHasNormals || mHasTangents)
325 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_POSITION);
329 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_NORMAL);
333 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_TANGENT);
335 if(mBlendShapeVersion == Scene3D::Loader::BlendShapes::Version::VERSION_2_0)
337 shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_VERSION_2_0);
341 Shader newShader = mShaderManager->ProduceShader(shaderOption);
342 if(mShader != newShader)
344 DALI_LOG_INFO(gLogFilter, Debug::General, "Warning! Model primitive shader changed: OldHash:%x NewHash:%x\n", oldHash, shaderOption.GetOptionHash());
346 #if defined(DEBUG_ENABLED)
349 Property::Map oldMap = GetMap(mShader);
350 DALI_LOG_WRITE_FILE(tmpFilename("oldShader", ".txt"), "Vertex Shader:\n"
351 << oldMap["vertex"] << "\n\nFragmentShader: " << oldMap["fragment"] << "\n");
355 Property::Map newMap = GetMap(newShader);
356 DALI_LOG_WRITE_FILE(tmpFilename("newShader", ".txt"), "Vertex Shader:\n"
357 << newMap["vertex"] << "\n\nFragmentShader: " << newMap["fragment"] << "\n");
369 mRenderer.SetShader(mShader);
373 uint32_t textureFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE));
374 if(mIsMaterialChanged || textureFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE))
376 mTextureSet = GetImplementation(mMaterial).GetTextureSet();
378 if(mBlendShapeGeometry)
380 TextureSet newTextureSet = TextureSet::New();
381 newTextureSet.SetTexture(0u, mBlendShapeGeometry);
383 const unsigned int numberOfTextures = mTextureSet.GetTextureCount();
384 for(unsigned int index = 0u; index < numberOfTextures; ++index)
386 const unsigned int newIndex = index + 1u;
387 newTextureSet.SetTexture(newIndex, mTextureSet.GetTexture(index));
388 newTextureSet.SetSampler(newIndex, mTextureSet.GetSampler(index));
391 mTextureSet = newTextureSet;
394 uint32_t textureCount = mTextureSet.GetTextureCount();
396 if(!mShadowMapTexture)
398 mShadowMapTexture = Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
400 mTextureSet.SetTexture(textureCount++, mShadowMapTexture);
402 Texture brdfTexture = Scene3D::Loader::EnvironmentDefinition::GetBrdfTexture();
403 if(!mSpecularTexture || !mDiffuseTexture)
405 Scene3D::Loader::EnvironmentMapData environmentMapData;
406 environmentMapData.mPixelData.resize(6);
407 for(auto& face : environmentMapData.mPixelData)
409 face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
411 environmentMapData.SetEnvironmentMapType(Dali::Scene3D::EnvironmentMapType::CUBEMAP);
412 Texture iblTexture = environmentMapData.GetTexture();
413 mDiffuseTexture = iblTexture;
414 mSpecularTexture = iblTexture;
417 mTextureSet.SetTexture(textureCount++, brdfTexture);
418 mTextureSet.SetTexture(textureCount++, mDiffuseTexture);
419 mTextureSet.SetTexture(textureCount, mSpecularTexture);
421 auto specularSampler = Sampler::New();
422 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
423 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
424 mTextureSet.SetSampler(textureCount, specularSampler);
432 mRenderer.SetTextures(mTextureSet);
436 uint32_t uniformFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM));
437 if(mIsMaterialChanged || uniformFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM))
441 mNeedToSetRendererUniform = true;
445 UpdateRendererUniform();
448 mIsMaterialChanged = false;
451 void ModelPrimitive::CreateRenderer()
453 if(!mShader || !mGeometry || !mTextureSet || mRenderer)
458 mRenderer = Renderer::New(mGeometry, mShader);
459 mRenderer.SetTextures(mTextureSet);
460 UpdateRendererUniform();
462 for(auto* observer : mObservers)
464 observer->OnRendererCreated(mRenderer);
468 void ModelPrimitive::UpdateShadowMapTexture()
470 if(mRenderer && mMaterial)
472 Dali::TextureSet textures = mRenderer.GetTextures();
478 uint32_t textureCount = textures.GetTextureCount();
479 if(mShadowMapTexture &&
480 textureCount >= GetImplementation(mMaterial).GetShadowMapTextureOffset() &&
481 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset()) != mShadowMapTexture)
483 Dali::TextureSet newTextures = Dali::TextureSet::New();
485 for(uint32_t index = 0u; index < textureCount; ++index)
487 Dali::Texture texture = textures.GetTexture(index);
488 if(index == textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset())
490 texture = (!!mShadowMapTexture) ? mShadowMapTexture : Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
493 newTextures.SetTexture(index, texture);
494 newTextures.SetSampler(index, textures.GetSampler(index));
497 mRenderer.SetTextures(newTextures);
502 void ModelPrimitive::UpdateImageBasedLightTexture()
504 if(mRenderer && mMaterial)
506 Dali::TextureSet textures = mRenderer.GetTextures();
512 uint32_t textureCount = textures.GetTextureCount();
513 if(textureCount > 2u &&
514 (textures.GetTexture(textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset()) != mDiffuseTexture ||
515 textures.GetTexture(textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset()) != mSpecularTexture))
517 Dali::TextureSet newTextures = Dali::TextureSet::New();
519 for(uint32_t index = 0u; index < textureCount; ++index)
521 Dali::Texture texture = textures.GetTexture(index);
522 if(index == textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset())
524 texture = mDiffuseTexture;
526 else if(index == textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset())
528 texture = mSpecularTexture;
531 newTextures.SetTexture(index, texture);
532 newTextures.SetSampler(index, textures.GetSampler(index));
535 mRenderer.SetTextures(newTextures);
537 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
538 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
542 void ModelPrimitive::UpdateRendererUniform()
546 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
547 mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
548 GetImplementation(mMaterial).SetRendererUniform(mRenderer);
552 } // namespace Internal
554 } // namespace Scene3D