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)
61 Scene3dView::~Scene3dView()
65 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath)
67 Scene3dView* impl = new Scene3dView();
69 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
71 // Second-phase init of the implementation
72 // This can only be done after the CustomActor connection has been made...
73 impl->mFilePath = filePath;
79 Toolkit::Scene3dView Scene3dView::New(const std::string& filePath, const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
81 Scene3dView* impl = new Scene3dView();
83 Dali::Toolkit::Scene3dView handle = Dali::Toolkit::Scene3dView(*impl);
85 // Second-phase init of the implementation
86 // This can only be done after the CustomActor connection has been made...
87 impl->mFilePath = filePath;
88 impl->SetCubeMap(diffuseTexturePath, specularTexturePath, scaleFactor);
94 bool Scene3dView::CreateScene()
96 if(std::string::npos != mFilePath.rfind(GLTF_EXT))
98 Internal::Gltf::Loader gltfloader;
99 return (gltfloader.LoadScene(mFilePath, *this));
105 uint32_t Scene3dView::GetAnimationCount()
107 return mAnimationArray.size();
110 bool Scene3dView::PlayAnimation(uint32_t index)
112 if(GetAnimationCount() <= index)
117 mAnimationArray[index].Play();
121 bool Scene3dView::PlayAnimations()
123 for(auto&& animation : mAnimationArray)
131 bool Scene3dView::SetLight(Toolkit::Scene3dView::LightType type, Vector3 lightVector, Vector3 lightColor)
133 if(type > Toolkit::Scene3dView::LightType::DIRECTIONAL_LIGHT)
138 mLightType = static_cast<Toolkit::Scene3dView::LightType>(
139 (mLightType >= Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT) ? Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT + type : type);
141 mLightVector = lightVector;
142 mLightColor = lightColor;
144 for(auto&& shader : mShaderArray)
146 shader.RegisterProperty("uLightType", (GetLightType() & ~Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT));
147 shader.RegisterProperty("uLightVector", lightVector);
148 shader.RegisterProperty("uLightColor", lightColor);
154 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)
156 uint32_t byteSize = bytesPerPixel * xFaceSize * yFaceSize;
157 uint8_t* destBuffer = reinterpret_cast<uint8_t*>(malloc(byteSize + 4u));
159 int32_t srcStride = width * bytesPerPixel;
160 int32_t destStride = xFaceSize * bytesPerPixel;
161 int32_t srcOffset = xOffset * bytesPerPixel + yOffset * srcStride;
162 int32_t destOffset = 0;
163 for(uint16_t row = yOffset; row < yOffset + yFaceSize; ++row)
165 memcpy(destBuffer + destOffset, sourceBuffer + srcOffset, destStride);
166 srcOffset += srcStride;
167 destOffset += destStride;
173 void Scene3dView::UploadTextureFace(Texture& texture, Devel::PixelBuffer pixelBuffer, uint32_t faceIndex)
175 uint8_t* imageBuffer = pixelBuffer.GetBuffer();
176 uint32_t bytesPerPixel = Pixel::GetBytesPerPixel(pixelBuffer.GetPixelFormat());
177 uint32_t imageWidth = pixelBuffer.GetWidth();
178 uint32_t imageHeight = pixelBuffer.GetHeight();
180 CubeType cubeType = (imageWidth / 4 == imageHeight / 3) ? CROSS_HORIZONTAL : ((imageWidth / 6 == imageHeight) ? ARRAY_HORIZONTAL : NONE);
182 uint32_t faceSize = 0;
183 if(cubeType == CROSS_HORIZONTAL)
185 faceSize = imageWidth / 4;
187 else if(cubeType == ARRAY_HORIZONTAL)
189 faceSize = imageWidth / 6;
196 uint32_t xOffset = CUBEMAP_INDEX_X[cubeType][faceIndex] * faceSize;
197 uint32_t yOffset = CUBEMAP_INDEX_Y[cubeType][faceIndex] * faceSize;
199 uint8_t* tempImageBuffer = GetCroppedBuffer(imageBuffer, bytesPerPixel, imageWidth, imageHeight, xOffset, yOffset, faceSize, faceSize);
200 PixelData pixelData = PixelData::New(tempImageBuffer, faceSize * faceSize * bytesPerPixel, faceSize, faceSize, pixelBuffer.GetPixelFormat(), PixelData::FREE);
201 texture.Upload(pixelData, CubeMapLayer::POSITIVE_X + faceIndex, 0, 0, 0, faceSize, faceSize);
204 void Scene3dView::SetCubeMap(const std::string& diffuseTexturePath, const std::string& specularTexturePath, Vector4 scaleFactor)
206 mLightType = Toolkit::Scene3dView::LightType::IMAGE_BASED_LIGHT;
209 const std::string imageDirPath = AssetManager::GetDaliImagePath();
210 const std::string imageBrdfUrl = imageDirPath + IMAGE_BRDF_FILE_NAME;
211 mBRDFTexture = LoadTexture(imageBrdfUrl.c_str(), true);
218 Devel::PixelBuffer diffusePixelBuffer = LoadImageFromFile(diffuseTexturePath);
219 uint32_t diffuseFaceSize = diffusePixelBuffer.GetWidth() / 4;
220 mDiffuseTexture = Texture::New(TextureType::TEXTURE_CUBE, diffusePixelBuffer.GetPixelFormat(), diffuseFaceSize, diffuseFaceSize);
221 for(uint32_t i = 0; i < 6; ++i)
223 UploadTextureFace(mDiffuseTexture, diffusePixelBuffer, i);
225 mDiffuseTexture.GenerateMipmaps();
228 Devel::PixelBuffer specularPixelBuffer = LoadImageFromFile(specularTexturePath);
229 uint32_t specularFaceSize = specularPixelBuffer.GetWidth() / 4;
230 mSpecularTexture = Texture::New(TextureType::TEXTURE_CUBE, specularPixelBuffer.GetPixelFormat(), specularFaceSize, specularFaceSize);
231 for(uint32_t i = 0; i < 6; ++i)
233 UploadTextureFace(mSpecularTexture, specularPixelBuffer, i);
235 mSpecularTexture.GenerateMipmaps();
237 mIBLScaleFactor = scaleFactor;
240 bool Scene3dView::SetDefaultCamera(const Dali::Camera::Type type, const float nearPlane, const Vector3 cameraPosition)
242 mDefaultCamera.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
243 mDefaultCamera.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
244 mDefaultCamera.SetType(type);
245 mDefaultCamera.SetNearClippingPlane(nearPlane);
246 mDefaultCamera.SetProperty(Actor::Property::POSITION, cameraPosition);
250 void Scene3dView::AddCamera(CameraActor cameraActor)
252 mCameraActorArray.push_back(cameraActor);
255 void Scene3dView::AddAnimation(Animation animation)
257 mAnimationArray.push_back(animation);
260 void Scene3dView::AddShader(Shader shader)
262 mShaderArray.push_back(shader);
265 Actor Scene3dView::GetRoot()
270 CameraActor Scene3dView::GetDefaultCamera()
272 return mDefaultCamera;
275 uint32_t Scene3dView::GetCameraCount()
277 return mCameraActorArray.size();
280 CameraActor Scene3dView::GetCamera(uint32_t cameraIndex)
282 CameraActor cameraActor;
283 if(cameraIndex >= mCameraActorArray.size())
287 cameraActor = mCameraActorArray[cameraIndex];
291 Toolkit::Scene3dView::LightType Scene3dView::GetLightType()
296 Vector3 Scene3dView::GetLightVector()
301 Vector3 Scene3dView::GetLightColor()
306 Vector4 Scene3dView::GetIBLScaleFactor()
308 return mIBLScaleFactor;
311 Texture Scene3dView::GetBRDFTexture()
316 Texture Scene3dView::GetSpecularTexture()
318 return mSpecularTexture;
321 Texture Scene3dView::GetDiffuseTexture()
323 return mDiffuseTexture;
326 Texture Scene3dView::LoadTexture(const char* imageUrl, bool generateMipmaps)
330 Devel::PixelBuffer pixelBuffer = LoadImageFromFile(imageUrl);
333 texture = Texture::New(TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight());
334 PixelData pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
335 texture.Upload(pixelData);
339 texture.GenerateMipmaps();
346 void Scene3dView::OnInitialize()
348 mRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
349 mRoot.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
351 Layer layer = Layer::New();
352 layer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
353 layer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
354 layer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
358 // Apply some default resizing rules.
359 self.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
360 self.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
366 } //namespace Internal
368 } //namespace Toolkit