2 * Copyright (c) 2021 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>
35 const char* const IMAGE_BRDF_FILE_NAME = "brdfLUT.png";
37 // glTF file extension
38 const std::string GLTF_EXT(".gltf");
43 const uint32_t CUBEMAP_INDEX_X[2][6] = {{2, 0, 1, 1, 1, 3}, {0, 1, 2, 3, 4, 5}};
44 const uint32_t CUBEMAP_INDEX_Y[2][6] = {{1, 1, 0, 2, 1, 1}, {0, 0, 0, 0, 0, 0}};
48 Scene3dView::Scene3dView()
49 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
53 mDefaultCamera(CameraActor::New()),
55 mLightType(Toolkit::Scene3dView::LightType::NONE),
56 mLightVector(Vector3::ONE),
57 mLightColor(Vector3::ONE),
62 Scene3dView::~Scene3dView()
66 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath)
68 Scene3dView* impl = new Scene3dView();
70 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
72 // Second-phase init of the implementation
73 // This can only be done after the CustomActor connection has been made...
74 impl->mFilePath = filePath;
80 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
82 Scene3dView* impl = new Scene3dView();
84 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
86 // Second-phase init of the implementation
87 // This can only be done after the CustomActor connection has been made...
88 impl->mFilePath = filePath;
89 impl->SetCubeMap(diffuseTexturePath, specularTexturePath, scaleFactor);
95 bool Scene3dView::CreateScene()
97 if(std::string::npos != mFilePath.rfind(GLTF_EXT))
99 Internal::Gltf::Loader gltfloader;
100 return (gltfloader.LoadScene(mFilePath, *this));
106 uint32_t Scene3dView::GetAnimationCount()
108 return mAnimationArray.size();
111 bool Scene3dView::PlayAnimation(uint32_t index)
113 if(GetAnimationCount() <= index)
118 mAnimationArray[index].Play();
122 bool Scene3dView::PlayAnimations()
124 for(auto&& animation : mAnimationArray)
132 bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor)
135 mLightVector = lightVector;
136 mLightColor = lightColor;
138 for(auto&& shader : mShaderArray)
140 float hasLightSource = static_cast<float>(!!(GetLightType() & (Toolkit::Scene3dView::LightType::POINT_LIGHT | Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)));
141 float isPointLight = static_cast<float>(!!(GetLightType() & Toolkit::Scene3dView::LightType::POINT_LIGHT));
142 shader.RegisterProperty("uHasLightSource", hasLightSource);
143 shader.RegisterProperty("uIsPointLight", isPointLight);
144 shader.RegisterProperty("uLightVector", lightVector);
145 shader.RegisterProperty("uLightColor", lightColor);
151 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)
153 uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize;
154 uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
156 int32_t srcStride = width * bytesPerPixel;
157 int32_t destStride = xFaceSize * bytesPerPixel;
158 int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride;
159 int32_t destOffset = 0;
160 for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
162 memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
163 srcOffset += srcStride;
164 destOffset += destStride;
170 void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBuffer, uint32_t faceIndex)
172 uint8_t* imageBuffer = pixelBuffer.GetBuffer();
173 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat());
174 uint32_t imageWidth = pixelBuffer.GetWidth();
175 uint32_t imageHeight = pixelBuffer.GetHeight();
177 CubeType cubeType = (imageWidth / 4 == imageHeight / 3) ? CROSS_HORIZONTAL : ((imageWidth / 6 == imageHeight) ? ARRAY_HORIZONTAL : NONE);
179 uint32_t faceSize = 0;
180 if(cubeType == CROSS_HORIZONTAL)
182 faceSize = imageWidth / 4;
184 else if(cubeType == ARRAY_HORIZONTAL)
186 faceSize = imageWidth / 6;
193 uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize;
194 uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize;
196 uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
197 PixelData pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
198 texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize);
201 void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
204 const std::string imageDirPath = AssetManager::GetDaliImagePath();
205 const std::string imageBrdfUrl = imageDirPath + IMAGE_BRDF_FILE_NAME;
206 mBRDFTexture = LoadTexture(imageBrdfUrl.c_str(), true);
213 Devel::PixelBuffer diffusePixelBuffer = LoadImageFromFile(diffuseTexturePath);
214 uint32_t diffuseFaceSize = diffusePixelBuffer.GetWidth() / 4;
215 mDiffuseTexture = Texture::New(TextureType::TEXTURE_CUBE, diffusePixelBuffer.GetPixelFormat(), diffuseFaceSize, diffuseFaceSize);
216 for(uint32_t i = 0; i < 6; ++i)
218 UploadTextureFace(mDiffuseTexture, diffusePixelBuffer, i);
220 mDiffuseTexture.GenerateMipmaps();
223 Devel::PixelBuffer specularPixelBuffer = LoadImageFromFile(specularTexturePath);
224 uint32_t specularFaceSize = specularPixelBuffer.GetWidth() / 4;
225 mSpecularTexture = Texture::New(TextureType::TEXTURE_CUBE, specularPixelBuffer.GetPixelFormat(), specularFaceSize, specularFaceSize);
226 for(uint32_t i = 0; i < 6; ++i)
228 UploadTextureFace(mSpecularTexture, specularPixelBuffer, i);
230 mSpecularTexture.GenerateMipmaps();
232 mIBLScaleFactor = scaleFactor;
236 bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition)
238 mDefaultCamera.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
239 mDefaultCamera.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
240 mDefaultCamera.SetType(type);
241 mDefaultCamera.SetNearClippingPlane(nearPlane);
242 mDefaultCamera.SetProperty(Actor::Property::POSITION, cameraPosition);
246 void Scene3dView::AddCamera(CameraActor cameraActor)
248 mCameraActorArray.push_back(cameraActor);
251 void Scene3dView::AddAnimation(Animation animation)
253 mAnimationArray.push_back(animation);
256 void Scene3dView::AddShader(Shader shader)
258 mShaderArray.push_back(shader);
261 Actor Scene3dView::GetRoot()
266 CameraActor Scene3dView::GetDefaultCamera()
268 return mDefaultCamera;
271 uint32_t Scene3dView::GetCameraCount()
273 return mCameraActorArray.size();
276 CameraActor Scene3dView::GetCamera(uint32_t cameraIndex)
278 CameraActor cameraActor;
279 if(cameraIndex >= mCameraActorArray.size())
283 cameraActor = mCameraActorArray[cameraIndex];
287 Toolkit::Scene3dView::LightType Scene3dView::GetLightType()
292 Vector3 Scene3dView::GetLightVector()
297 Vector3 Scene3dView::GetLightColor()
302 Vector4 Scene3dView::GetIBLScaleFactor()
304 return mIBLScaleFactor;
307 Texture Scene3dView::GetBRDFTexture()
312 Texture Scene3dView::GetSpecularTexture()
314 return mSpecularTexture;
317 bool Scene3dView::HasImageBasedLighting()
322 Texture Scene3dView::GetDiffuseTexture()
324 return mDiffuseTexture;
327 Texture Scene3dView::LoadTexture(const char* imageUrl, bool generateMipmaps)
331 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(imageUrl);
334 texture = Texture::New(TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
335 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
336 texture.Upload(pixelData);
340 texture.GenerateMipmaps();
347 void Scene3dView::OnInitialize()
349 mRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
350 mRoot.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
352 Layer layer = Layer::New();
353 layer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
354 layer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
355 layer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
359 // Apply some default resizing rules.
360 self.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
361 self.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
367 } //namespace Internal
369 } //namespace Toolkit