2 * Copyright (c) 2022 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-scene-loader/public-api/node-definition.h"
20 #include "dali-scene-loader/public-api/renderer-state.h"
21 #include "dali-scene-loader/public-api/utils.h"
27 constexpr std::string_view IBL_INTENSITY_STRING("uIblIntensity");
28 constexpr std::string_view IBL_Y_DIRECTION("uYDirection");
33 bool NodeDefinition::Renderable::GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const
38 void NodeDefinition::Renderable::RegisterResources(IResourceReceiver& receiver) const
40 receiver.Register(ResourceType::Shader, mShaderIdx);
43 void NodeDefinition::Renderable::ReflectResources(IResourceReflector& reflector)
45 reflector.Reflect(ResourceType::Shader, mShaderIdx);
48 void NodeDefinition::Renderable::OnCreate(const NodeDefinition& node, CreateParams& params, Actor& actor) const
50 DALI_ASSERT_DEBUG(mShaderIdx != INVALID_INDEX);
51 auto& resources = params.mResources;
52 Shader shader = resources.mShaders[mShaderIdx].second;
54 static Geometry defaultGeometry = Geometry::New();
55 Renderer renderer = Renderer::New(defaultGeometry, shader);
57 RendererState::Apply(resources.mShaders[mShaderIdx].first.mRendererState, renderer);
59 actor.AddRenderer(renderer);
62 const std::string NodeDefinition::ORIGINAL_MATRIX_PROPERTY_NAME = "originalMatrix";
64 Actor NodeDefinition::CreateActor(CreateParams& params) const
66 Actor actor = Actor::New();
67 SetActorCentered(actor);
69 actor.SetProperty(Actor::Property::NAME, mName);
70 actor.SetProperty(Actor::Property::POSITION, mPosition);
71 actor.SetProperty(Actor::Property::ORIENTATION, mOrientation);
72 actor.SetProperty(Actor::Property::SCALE, mScale);
73 actor.SetProperty(Actor::Property::SIZE, mSize);
74 actor.SetProperty(Actor::Property::VISIBLE, mIsVisible);
76 actor.RegisterProperty(ORIGINAL_MATRIX_PROPERTY_NAME, GetLocalSpace(), Property::AccessMode::READ_ONLY);
80 mRenderable->OnCreate(*this, params, actor);
83 for(auto& e : mExtras)
85 actor.RegisterProperty(e.mKey, e.mValue);
88 for(auto& c : mConstraints)
90 params.mConstrainables.push_back(ConstraintRequest{&c, actor});
96 Matrix NodeDefinition::GetLocalSpace() const
98 Matrix localSpace{false};
99 localSpace.SetTransformComponents(mScale, mOrientation, mPosition);
103 std::string_view NodeDefinition::GetIblScaleFactorUniformName()
105 return IBL_INTENSITY_STRING;
108 std::string_view NodeDefinition::GetIblYDirectionUniformName()
110 return IBL_Y_DIRECTION;
113 bool NodeDefinition::GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const
117 if(!mRenderable->GetExtents(resources, min, max))
119 // If the renderable node don't have mesh accessor, use size to compute extents.
128 bool ModelNode::GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const
130 auto& mesh = resources.mMeshes[mMeshIdx];
131 uint32_t minSize = mesh.first.mPositions.mBlob.mMin.size();
132 uint32_t maxSize = mesh.first.mPositions.mBlob.mMax.size();
133 if(minSize == maxSize && minSize >= 2u && maxSize >= 2u)
135 min = Vector3(mesh.first.mPositions.mBlob.mMin[0], mesh.first.mPositions.mBlob.mMin[1], 0.0f);
136 max = Vector3(mesh.first.mPositions.mBlob.mMax[0], mesh.first.mPositions.mBlob.mMax[1], 0.0f);
139 min.z = mesh.first.mPositions.mBlob.mMin[2];
140 max.z = mesh.first.mPositions.mBlob.mMax[2];
147 void ModelNode::RegisterResources(IResourceReceiver& receiver) const
149 Renderable::RegisterResources(receiver);
150 receiver.Register(ResourceType::Mesh, mMeshIdx);
151 receiver.Register(ResourceType::Material, mMaterialIdx);
154 void ModelNode::ReflectResources(IResourceReflector& reflector)
156 Renderable::ReflectResources(reflector);
157 reflector.Reflect(ResourceType::Mesh, mMeshIdx);
158 reflector.Reflect(ResourceType::Material, mMaterialIdx);
161 void ModelNode::OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const
163 DALI_ASSERT_DEBUG(mMeshIdx != INVALID_INDEX);
164 Renderable::OnCreate(node, params, actor);
166 auto& resources = params.mResources;
167 auto& mesh = resources.mMeshes[mMeshIdx];
169 auto renderer = actor.GetRendererAt(0);
170 Geometry geometry = mesh.second.geometry;
171 renderer.SetGeometry(geometry);
173 auto shader = renderer.GetShader();
175 if(mesh.first.IsSkinned())
177 params.mSkinnables.push_back(SkinningShaderConfigurationRequest{mesh.first.mSkeletonIdx, shader});
180 if(mesh.first.HasBlendShapes())
182 params.mBlendshapeRequests.push_back(BlendshapeShaderConfigurationRequest{node.mName, mMeshIdx, shader});
185 TextureSet textures = resources.mMaterials[mMaterialIdx].second;
187 // Set the blend shape texture.
188 if(mesh.second.blendShapeGeometry)
190 TextureSet newTextureSet = TextureSet::New();
191 newTextureSet.SetTexture(0u, mesh.second.blendShapeGeometry);
193 const unsigned int numberOfTextures = textures.GetTextureCount();
194 for(unsigned int index = 0u; index < numberOfTextures; ++index)
196 const unsigned int newIndex = index + 1u;
197 newTextureSet.SetTexture(newIndex, textures.GetTexture(index));
198 newTextureSet.SetSampler(newIndex, textures.GetSampler(index));
201 textures = newTextureSet;
204 renderer.SetTextures(textures);
206 actor.SetProperty(Actor::Property::COLOR, mColor);
208 actor.RegisterProperty("uHasVertexColor", static_cast<float>(mesh.first.mColors.IsDefined()));
210 auto& matDef = resources.mMaterials[mMaterialIdx].first;
211 actor.RegisterProperty("uColorFactor", matDef.mBaseColorFactor);
212 actor.RegisterProperty("uMetallicFactor", matDef.mMetallic);
213 actor.RegisterProperty("uRoughnessFactor", matDef.mRoughness);
214 actor.RegisterProperty("uNormalScale", matDef.mNormalScale);
215 if(matDef.mFlags & MaterialDefinition::OCCLUSION)
217 actor.RegisterProperty("uOcclusionStrength", matDef.mOcclusionStrength);
219 if(matDef.mFlags & MaterialDefinition::EMISSIVE)
221 actor.RegisterProperty("uEmissiveFactor", matDef.mEmissiveFactor);
224 Index envIdx = matDef.mEnvironmentIdx;
225 actor.RegisterProperty(IBL_INTENSITY_STRING.data(), resources.mEnvironmentMaps[envIdx].first.mIblIntensity);
226 actor.RegisterProperty(IBL_Y_DIRECTION.data(), resources.mEnvironmentMaps[envIdx].first.mYDirection);
230 float alphaCutoff = matDef.GetAlphaCutoff();
231 if(!MaskMatch(matDef.mFlags, MaterialDefinition::TRANSPARENCY))
237 if(alphaCutoff > 0.f)
242 actor.RegisterProperty("uOpaque", opaque);
243 actor.RegisterProperty("uMask", mask);
244 actor.RegisterProperty("uAlphaThreshold", alphaCutoff);
247 void ArcNode::OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const
249 ModelNode::OnCreate(node, params, actor);
251 actor.RegisterProperty("antiAliasing", mAntiAliasing ? 1 : 0);
252 actor.RegisterProperty("arcCaps", mArcCaps);
253 actor.RegisterProperty("radius", mRadius);
255 const float startAngleRadians = mStartAngleDegrees * Math::PI_OVER_180;
256 Vector2 startPolar{std::cos(startAngleRadians), std::sin(startAngleRadians)};
257 actor.RegisterProperty("startAngle", startPolar);
259 const float endAngleRadians = mEndAngleDegrees * Math::PI_OVER_180;
260 Vector2 endPolar{std::cos(endAngleRadians), std::sin(endAngleRadians)};
261 actor.RegisterProperty("endAngle", endPolar);
264 void ArcNode::GetEndVectorWithDiffAngle(float startAngle, float diffAngle, Vector2& endVector)
266 float endAngle = 0.f;
268 if(diffAngle <= 0.001f)
270 //0.001 is used to ensure is empty arc when startAngle = endAngle + 360 * N
271 endAngle = startAngle + 0.001f;
273 else if(diffAngle >= 360.f)
275 endAngle = diffAngle + 359.99f;
279 endAngle = startAngle + 360.0f + diffAngle;
281 endVector.x = cosf(endAngle * Math::PI_OVER_180);
282 endVector.y = sinf(endAngle * Math::PI_OVER_180);
285 } // namespace SceneLoader