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 using System.Runtime.InteropServices;
20 using System.ComponentModel;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.BaseComponents;
25 namespace Tizen.NUI.Scene3D
28 /// SceneView is a Class to show multiple 3D objects in a single 2D screen.
29 /// Each SceneView has its own 3D space, and 3D objects added to SceneView are positioned in the space.
30 /// SceneView uses left-handed coordinate system same as NUI. X as right, Y as down, and Z as forward.
32 /// SceneView has internal root container to control inner rendering process like depth test.
33 /// When a View is added to the SceneView with <see cref="View.Add(View)"/> method, it is actually added on the root container.
34 /// Therefore, the added Views exist in the sub tree of SceneView, but are not direct children.
35 /// The sub tree of Views will be rendered with the SceneView's own Camera.
37 /// SceneView has one built-in camera by default.
38 /// The default Camera is not removed by using <see cref="RemoveCamera(Camera)"/> method.
39 /// <see cref="GetCamera(uint)"/> method with index "0" returns the default camera,
40 /// and the minimum value returned by <see cref="GetCameraCount()"/> method is 1.
42 /// SceneView also provides multiple Camera and one of them can be used to render multiple objects.
43 /// <see cref="AddCamera(Camera)"/>, <see cref="RemoveCamera(Camera)"/>, <see cref="GetCamera(uint)"/>,
44 /// and <see cref="SelectCamera(uint)"/> are methods to manage Cameras of the SceneView.
45 /// User can place multiple cameras in a scene to display the entire scene or to display individual objects.
46 /// User can use the <see cref="SelectCamera(uint)"/> method to select the currently required camera.
48 /// When the SceneView's size changes, some camera properties that depend on its size may also change.
49 /// The changing properties are as follows: AspectRatio, LeftPlaneDistance, RightPlaneDistance, TopPlaneDistance, and BottomPlaneDistance.
50 /// The Camera's FieldOfView is vertical fov. The horizontal fov is updated internally according to the SceneView size.
52 /// The <see cref="SetImageBasedLightSource(string, string, float)"/> method sets the same IBL to all Model objects added to the SceneView.
53 /// For the IBL, two cube map textures(diffuse and specular) are required.
54 /// SceneView supports 4 types layout for Cube Map: Vertical/Horizontal Cross layouts, and Vertical/Horizontal Array layouts.
55 /// And also, ktx format with cube map is supported.
56 /// If a model already has an IBL, it is batch overridden with the IBL of the SceneView.
57 /// If the SceneView has IBL, the IBL of newly added models is also overridden.
59 /// The IBL textures start to be loaded asynchronously when <see cref="SetImageBasedLightSource(string, string, float)"/> method is called.
60 /// ResourcesLoaded signal notifies that the loading of the IBL resources have been completed.
62 /// If FBO is used, the rendering result of SceneView is drawn on the FBO and it is mapped on the plane of the SceneView.
63 /// It could decreases performance slightly, but it is useful to show SceneView according to the rendering order with other Views.
65 /// And since SceneView is a View, it can be placed together with other 2D UI components in the NUI window.
67 /// <since_tizen> 10 </since_tizen>
68 public class SceneView : View
70 private bool inCameraTransition = false;
71 private Animation cameraTransition;
73 internal SceneView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
78 /// Create an initialized SceneView.
80 /// <since_tizen> 10 </since_tizen>
81 public SceneView() : this(Interop.SceneView.SceneNew(), true)
83 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
89 /// <param name="sceneView">The source object.</param>
90 /// <since_tizen> 10 </since_tizen>
91 public SceneView(SceneView sceneView) : this(Interop.SceneView.NewScene(SceneView.getCPtr(sceneView)), true)
93 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
97 /// Assignment operator.
99 /// <param name="sceneView">Handle to an object.</param>
100 /// <returns>Reference to this.</returns>
101 internal SceneView Assign(SceneView sceneView)
103 SceneView ret = new SceneView(Interop.SceneView.SceneAssign(SwigCPtr, SceneView.getCPtr(sceneView)), false);
104 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109 /// Set/Get the ImageBasedLight ScaleFactor.
110 /// Scale factor controls light source intensity in [0.0f, 1.0f]
112 /// <since_tizen> 10 </since_tizen>
113 public float ImageBasedLightScaleFactor
117 SetImageBasedLightScaleFactor(value);
121 return GetImageBasedLightScaleFactor();
126 /// Set/Get the UseFramebuffer.
127 /// If this property is true, rendering result of SceneView is drawn on FBO and it is mapping on this SceneView plane.
128 /// If this property is false, each item in SceneView is rendered on window directly.
129 /// Default is false.
132 /// If UseFramebuffer is true, it could decrease performance but entire rendering order is satisfied.
133 /// If UseFramebuffer is false, the performance becomes better but SceneView is rendered on the top of the other 2D components regardless tree order.
135 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
136 [EditorBrowsable(EditorBrowsableState.Never)]
137 public bool UseFramebuffer
141 SetUseFramebuffer(value);
145 return IsUsingFramebuffer();
150 /// Adds a Camera to the SceneView at the end of the camera list of SceneView.
151 /// The Camera can be used as a selected camera to render the scene by using <see cref="SelectCamera(uint)"/> or <see cref="SelectCamera(string)"/>
153 /// <param name="camera">Camera added on this SceneView.</param>
155 /// Some properties of the Camera will be change depending on the Size of this SceneView.
156 /// Those properties are as follows:
157 /// AspectRatio, LeftPlaneDistance, RightPlaneDistance, TopPlaneDistance, and BottomPlaneDistance.
159 /// The FieldOfView of Camera is for vertical fov.
160 /// When the size of the SceneView is changed, the vertical fov is maintained
161 /// and the horizontal fov is automatically calculated according to the SceneView's AspectRatio.
163 /// <since_tizen> 10 </since_tizen>
164 public void AddCamera(Camera camera)
168 Interop.SceneView.AddCamera(SwigCPtr, camera.SwigCPtr);
169 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174 /// Removes a Camera from this SceneView.
175 /// If removed Camera is selected Camera,
176 /// first camera in the list becomes the selected Camera.
178 /// <param name="camera"> camera Camera to be removed from this Camera.</param>
180 /// When Camera.Dispose() is called, the NUI object is disposed, but camera information is maintained internally.
181 /// Therefore, even if Camera.Dispose() is called, RemoveCamera() or RemoveCamera() methods can be used.
182 /// If RemoveCamera() is called too, all information is deleted together.
184 /// <since_tizen> 10 </since_tizen>
185 public void RemoveCamera(Camera camera)
189 Interop.SceneView.RemoveCamera(SwigCPtr, camera.SwigCPtr);
190 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195 /// Retrieves the number of cameras.
197 /// <returns>The number of Cameras.</returns>
198 /// <since_tizen> 10 </since_tizen>
199 public uint GetCameraCount()
201 uint count = Interop.SceneView.GetCameraCount(SwigCPtr);
202 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207 /// Retrieves a Camera of the index.
209 /// <param name="index"> Index of Camera to be retrieved.</param>
210 /// <returns>Camera of the index.</returns>
211 /// <since_tizen> 10 </since_tizen>
212 public Camera GetCamera(uint index)
214 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, index);
215 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
218 // Register new camera into Registry.
219 camera = new Camera(cPtr, true);
223 // We found matched NUI camera. Reduce cPtr reference count.
224 HandleRef handle = new HandleRef(this, cPtr);
225 Interop.Camera.DeleteCameraProperty(handle);
226 handle = new HandleRef(null, IntPtr.Zero);
228 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
233 /// Retrieves a Camera of the input name.
235 /// <param name="name"> string keyword of Camera to be retrieved.</param>
236 /// <returns>Camera that has the name as a View.Name property</returns>
237 /// <since_tizen> 10 </since_tizen>
238 public Camera GetCamera(string name)
240 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, name);
241 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
244 // Register new camera into Registry.
245 camera = new Camera(cPtr, true);
249 // We found matched NUI camera. Reduce cPtr reference count.
250 HandleRef handle = new HandleRef(this, cPtr);
251 Interop.Camera.DeleteCameraProperty(handle);
252 handle = new HandleRef(null, IntPtr.Zero);
254 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
259 /// Makes SceneView use a Camera of index as a selected camera.
261 /// <param name="index"> Index of Camera to be used as a selected camera.</param>
262 /// <since_tizen> 10 </since_tizen>
263 public void SelectCamera(uint index)
265 if(inCameraTransition)
269 Interop.SceneView.SelectCamera(SwigCPtr, index);
270 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274 /// Makes SceneView use a Camera of a name as a selected camera.
276 /// <param name="name"> string keyword of Camera to be used as a selected camera.</param>
277 /// <since_tizen> 10 </since_tizen>
278 public void SelectCamera(string name)
280 if(inCameraTransition)
284 Interop.SceneView.SelectCamera(SwigCPtr, name);
285 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
289 /// Starts camera transition from currently selected camera to a camera of index.
290 /// Camera Position, Orientation and FieldOfView are smoothly animated.
293 /// The selected camera is switched when the transition is started.
294 /// During camera transition, Selected Camera cannot be changed by using SelectCamera() or CameraTransition() method.
296 /// <param name="index"> Index of destination Camera of Camera transition.</param>
297 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
298 /// <param name="alphaFunction">The alpha function to apply.</param>
299 /// <since_tizen> 10 </since_tizen>
300 public void CameraTransition(uint index, int durationMilliSeconds, AlphaFunction alphaFunction = null)
302 if(inCameraTransition)
306 Camera source = GetSelectedCamera();
308 Camera destination = GetSelectedCamera();
309 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
313 /// Starts camera transition from currently selected camera to a camera of input name.
314 /// Camera Position, Orientation and FieldOfView are smoothly animated.
317 /// The selected camera is switched when the transition is started.
318 /// During camera transition, Selected Camera cannot be changed by using SelectCamera() or CameraTransition() method.
320 /// <param name="name"> string keyword of destination Camera of Camera transition.</param>
321 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
322 /// <param name="alphaFunction">The alpha function to apply.</param>
323 /// <since_tizen> 10 </since_tizen>
324 public void CameraTransition(string name, int durationMilliSeconds, AlphaFunction alphaFunction = null)
326 if(inCameraTransition)
330 Camera source = GetSelectedCamera();
332 Camera destination = GetSelectedCamera();
333 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
337 /// Retrieves selected Camera.
339 /// <returns> Camera currently used in SceneView as a selected Camera.</returns>
340 /// <since_tizen> 10 </since_tizen>
341 public Camera GetSelectedCamera()
343 global::System.IntPtr cPtr = Interop.SceneView.GetSelectedCamera(SwigCPtr);
344 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
347 // Register new camera into Registry.
348 camera = new Camera(cPtr, true);
352 // We found matched NUI camera. Reduce cPtr reference count.
353 HandleRef handle = new HandleRef(this, cPtr);
354 Interop.Camera.DeleteCameraProperty(handle);
355 handle = new HandleRef(null, IntPtr.Zero);
357 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
362 /// Changes Image Based Light as the input textures.
364 /// <param name="diffuseUrl">The path of Cube map image that can be used as a diffuse IBL source.</param>
365 /// <param name="specularUrl">The path of Cube map image that can be used as a specular IBL source.</param>
366 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f]. Default value is 1.0f.</param>
368 /// http://tizen.org/privilege/mediastorage for local files in media storage.
369 /// http://tizen.org/privilege/externalstorage for local files in external storage.
371 /// <since_tizen> 10 </since_tizen>
372 public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f)
374 Interop.SceneView.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor);
375 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
378 internal void SetUseFramebuffer(bool useFramebuffer)
380 Interop.SceneView.UseFramebuffer(SwigCPtr, useFramebuffer);
381 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
384 internal bool IsUsingFramebuffer()
386 bool result = Interop.SceneView.IsUsingFramebuffer(SwigCPtr);
387 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
392 /// Set the ImageBasedLight ScaleFactor.
394 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f].</param>
395 private void SetImageBasedLightScaleFactor(float scaleFactor)
397 Interop.SceneView.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor);
398 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
402 /// Get the ImageBasedLight ScaleFactor.
404 /// <returns>ImageBasedLightScaleFactor that controls light source intensity.</returns>
405 private float GetImageBasedLightScaleFactor()
407 float scaleFactor = Interop.SceneView.GetImageBasedLightScaleFactor(SwigCPtr);
408 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
412 private void CameraTransition(Camera sourceCamera, Camera destinationCamera, int durationMilliSeconds, AlphaFunction alphaFunction)
414 inCameraTransition = true;
416 Position sourcePosition = sourceCamera.Position;
417 Rotation sourceOrientation = sourceCamera.Orientation;
418 Radian sourceFieldOfView = sourceCamera.FieldOfView;
420 Position destinationPosition = destinationCamera.Position;
421 Rotation destinationOrientation = destinationCamera.Orientation;
422 Radian destinationFieldOfView = destinationCamera.FieldOfView;
424 // If ProjectionDirection is not equal, match the value.
425 if (sourceCamera.ProjectionDirection != destinationCamera.ProjectionDirection)
427 float aspect = destinationCamera.AspectRatio;
428 if (destinationCamera.ProjectionDirection == Camera.ProjectionDirectionType.Vertical)
430 sourceFieldOfView = Camera.ConvertFovFromHorizontalToVertical(aspect, sourceFieldOfView);
434 sourceFieldOfView = Camera.ConvertFovFromVerticalToHorizontal(aspect, sourceFieldOfView);
438 cameraTransition = new Animation(durationMilliSeconds);
440 KeyFrames positionKeyFrames = new KeyFrames();
441 positionKeyFrames.Add(0.0f, sourcePosition);
442 positionKeyFrames.Add(1.0f, destinationPosition);
444 KeyFrames orientationKeyFrames = new KeyFrames();
445 orientationKeyFrames.Add(0.0f, sourceOrientation);
446 orientationKeyFrames.Add(1.0f, destinationOrientation);
448 KeyFrames fieldOfViewKeyFrames = new KeyFrames();
449 fieldOfViewKeyFrames.Add(0.0f, sourceFieldOfView.ConvertToFloat());
450 fieldOfViewKeyFrames.Add(1.0f, destinationFieldOfView.ConvertToFloat());
452 cameraTransition.AnimateBetween(destinationCamera, "Position", positionKeyFrames, Animation.Interpolation.Linear, alphaFunction);
453 cameraTransition.AnimateBetween(destinationCamera, "Orientation", orientationKeyFrames, Animation.Interpolation.Linear, alphaFunction);
454 cameraTransition.AnimateBetween(destinationCamera, "FieldOfView", fieldOfViewKeyFrames, Animation.Interpolation.Linear, alphaFunction);
456 cameraTransition.Finished += (s, e) =>
458 inCameraTransition = false;
460 cameraTransition.Play();
464 /// Release swigCPtr.
466 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
467 [EditorBrowsable(EditorBrowsableState.Never)]
468 protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
470 Interop.SceneView.DeleteScene(swigCPtr);