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-toolkit/internal/controls/scene3d-view/scene3d-view-impl.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
26 #include <dali-toolkit/internal/controls/scene3d-view/gltf-loader.h>
36 const char* const IMAGE_BRDF_FILE_NAME = "brdfLUT.png";
38 // glTF file extension
39 const std::string GLTF_EXT(".gltf");
44 const uint32_t CUBEMAP_INDEX_X[2][6] = {{2, 0, 1, 1, 1, 3}, {0, 1, 2, 3, 4, 5}};
45 const uint32_t CUBEMAP_INDEX_Y[2][6] = {{1, 1, 0, 2, 1, 1}, {0, 0, 0, 0, 0, 0}};
49 Scene3dView::Scene3dView()
50 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
54 mDefaultCamera(CameraActor::New()),
56 mLightType(Toolkit::Scene3dView::LightType::NONE),
57 mLightVector(Vector3::ONE),
58 mLightColor(Vector3::ONE),
63 Scene3dView::~Scene3dView()
67 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath)
69 Scene3dView* impl = new Scene3dView();
71 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
73 // Second-phase init of the implementation
74 // This can only be done after the CustomActor connection has been made...
75 impl->mFilePath = filePath;
81 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
83 Scene3dView* impl = new Scene3dView();
85 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
87 // Second-phase init of the implementation
88 // This can only be done after the CustomActor connection has been made...
89 impl->mFilePath = filePath;
90 impl->SetCubeMap(diffuseTexturePath, specularTexturePath, scaleFactor);
96 bool Scene3dView::CreateScene()
98 if(std::string::npos != mFilePath.rfind(GLTF_EXT))
100 Internal::Gltf::Loader gltfloader;
101 return (gltfloader.LoadScene(mFilePath, *this));
107 uint32_t Scene3dView::GetAnimationCount()
109 return mAnimationArray.size();
112 bool Scene3dView::PlayAnimation(uint32_t index)
114 if(GetAnimationCount() <= index)
119 mAnimationArray[index].Play();
123 bool Scene3dView::PlayAnimations()
125 for(auto&& animation : mAnimationArray)
133 bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor)
136 mLightVector = lightVector;
137 mLightColor = lightColor;
139 for(auto&& shader : mShaderArray)
141 float hasLightSource = static_cast<float>(!!(GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)));
142 float isPointLight = static_cast<float>(!!(GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT));
143 shader.RegisterProperty("uHasLightSource", hasLightSource);
144 shader.RegisterProperty("uIsPointLight", isPointLight);
145 shader.RegisterProperty("uLightVector", lightVector);
146 shader.RegisterProperty("uLightColor", lightColor);
152 uint8_t* Scene3dView::GetCroppedBuffer(uint8_t* sourceBuffer, uint32_t bytesPerPixel, uint32_t width, uint32_t height, uint32_t xOffset, uint32_t yOffset, uint32_t xFaceSize, uint32_t yFaceSize)
154 uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize;
155 uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
157 int32_t srcStride = width * bytesPerPixel;
158 int32_t destStride = xFaceSize * bytesPerPixel;
159 int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride;
160 int32_t destOffset = 0;
161 for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
163 memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
164 srcOffset += srcStride;
165 destOffset += destStride;
171 void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBuffer, uint32_t faceIndex)
173 uint8_t* imageBuffer = pixelBuffer.GetBuffer();
174 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat());
175 uint32_t imageWidth = pixelBuffer.GetWidth();
176 uint32_t imageHeight = pixelBuffer.GetHeight();
178 CubeType cubeType = (imageWidth / 4 == imageHeight / 3) ? CROSS_HORIZONTAL : ((imageWidth / 6 == imageHeight) ? ARRAY_HORIZONTAL : NONE);
180 uint32_t faceSize = 0;
181 if(cubeType == CROSS_HORIZONTAL)
183 faceSize = imageWidth / 4;
185 else if(cubeType == ARRAY_HORIZONTAL)
187 faceSize = imageWidth / 6;
194 uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize;
195 uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize;
197 uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
198 PixelData pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
199 texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize);
202 void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
205 const std::string imageDirPath = AssetManager::GetDaliImagePath();
206 const std::string imageBrdfUrl = imageDirPath + IMAGE_BRDF_FILE_NAME;
207 mBRDFTexture = LoadTexture(imageBrdfUrl.c_str(), true);
214 Devel::PixelBuffer diffusePixelBuffer = LoadImageFromFile(diffuseTexturePath);
215 uint32_t diffuseFaceSize = diffusePixelBuffer.GetWidth() / 4;
216 mDiffuseTexture = Texture::New(TextureType::TEXTURE_CUBE, diffusePixelBuffer.GetPixelFormat(), diffuseFaceSize, diffuseFaceSize);
217 for(uint32_t i = 0; i < 6; ++i)
219 UploadTextureFace(mDiffuseTexture, diffusePixelBuffer, i);
221 mDiffuseTexture.GenerateMipmaps();
224 Devel::PixelBuffer specularPixelBuffer = LoadImageFromFile(specularTexturePath);
225 uint32_t specularFaceSize = specularPixelBuffer.GetWidth() / 4;
226 mSpecularTexture = Texture::New(TextureType::TEXTURE_CUBE, specularPixelBuffer.GetPixelFormat(), specularFaceSize, specularFaceSize);
227 for(uint32_t i = 0; i < 6; ++i)
229 UploadTextureFace(mSpecularTexture, specularPixelBuffer, i);
231 mSpecularTexture.GenerateMipmaps();
233 mIBLScaleFactor = scaleFactor;
237 bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition)
239 mDefaultCamera.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
240 mDefaultCamera.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
241 mDefaultCamera.SetType(type);
242 mDefaultCamera.SetNearClippingPlane(nearPlane);
243 mDefaultCamera.SetProperty(Actor::Property::POSITION, cameraPosition);
247 void Scene3dView::AddCamera(CameraActor cameraActor)
249 mCameraActorArray.push_back(cameraActor);
252 void Scene3dView::AddAnimation(Animation animation)
254 mAnimationArray.push_back(animation);
257 void Scene3dView::AddShader(Shader shader)
259 mShaderArray.push_back(shader);
262 Actor Scene3dView::GetRoot()
267 CameraActor Scene3dView::GetDefaultCamera()
269 return mDefaultCamera;
272 uint32_t Scene3dView::GetCameraCount()
274 return mCameraActorArray.size();
277 CameraActor Scene3dView::GetCamera(uint32_t cameraIndex)
279 CameraActor cameraActor;
280 if(cameraIndex >= mCameraActorArray.size())
284 cameraActor = mCameraActorArray[cameraIndex];
288 Toolkit::Scene3dView::LightType Scene3dView::GetLightType()
293 Vector3 Scene3dView::GetLightVector()
298 Vector3 Scene3dView::GetLightColor()
303 Vector4 Scene3dView::GetIBLScaleFactor()
305 return mIBLScaleFactor;
308 Texture Scene3dView::GetBRDFTexture()
313 Texture Scene3dView::GetSpecularTexture()
315 return mSpecularTexture;
318 bool Scene3dView::HasImageBasedLighting()
323 Texture Scene3dView::GetDiffuseTexture()
325 return mDiffuseTexture;
328 Texture Scene3dView::LoadTexture(const char* imageUrl, bool generateMipmaps)
332 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(imageUrl);
335 texture = Texture::New(TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
336 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
337 texture.Upload(pixelData);
341 texture.GenerateMipmaps();
348 void Scene3dView::OnInitialize()
350 mRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
351 mRoot.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
353 Layer layer = Layer::New();
354 layer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
355 layer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
356 layer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
360 // Apply some default resizing rules.
361 self.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
362 self.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
368 } //namespace Internal
370 } //namespace Toolkit