[NUI.Scene3D] Transition animation with FieldOfView
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Scene3D / src / public / Controls / SceneView.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using System.ComponentModel;
21 using Tizen.NUI;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.BaseComponents;
24
25 namespace Tizen.NUI.Scene3D
26 {
27     /// <summary>
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.
31     ///
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.
36     ///
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.
41     ///
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.
47     ///
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.
51     ///
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.
58     ///
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.
61     ///
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.
64     ///
65     /// And since SceneView is a View, it can be placed together with other 2D UI components in the NUI window.
66     /// </summary>
67     /// <since_tizen> 10 </since_tizen>
68     public class SceneView : View
69     {
70         private bool inCameraTransition = false;
71         private Animation cameraTransition;
72
73         internal SceneView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
74         {
75         }
76
77         /// <summary>
78         /// Create an initialized SceneView.
79         /// </summary>
80         /// <since_tizen> 10 </since_tizen>
81         public SceneView() : this(Interop.SceneView.SceneNew(), true)
82         {
83             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
84         }
85
86         /// <summary>
87         /// Copy constructor.
88         /// </summary>
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)
92         {
93             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
94         }
95
96         /// <summary>
97         /// Assignment operator.
98         /// </summary>
99         /// <param name="sceneView">Handle to an object.</param>
100         /// <returns>Reference to this.</returns>
101         internal SceneView Assign(SceneView sceneView)
102         {
103             SceneView ret = new SceneView(Interop.SceneView.SceneAssign(SwigCPtr, SceneView.getCPtr(sceneView)), false);
104             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
105             return ret;
106         }
107
108         /// <summary>
109         /// Set/Get the ImageBasedLight ScaleFactor.
110         /// Scale factor controls light source intensity in [0.0f, 1.0f]
111         /// </summary>
112         /// <since_tizen> 10 </since_tizen>
113         public float ImageBasedLightScaleFactor
114         {
115             set
116             {
117                 SetImageBasedLightScaleFactor(value);
118             }
119             get
120             {
121                 return GetImageBasedLightScaleFactor();
122             }
123         }
124
125         /// <summary>
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.
130         /// </summary>
131         /// <remarks>
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.
134         /// </remarks>
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
138         {
139             set
140             {
141                 SetUseFramebuffer(value);
142             }
143             get
144             {
145                 return IsUsingFramebuffer();
146             }
147         }
148
149         /// <summary>
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)"/>
152         /// </summary>
153         /// <param name="camera">Camera added on this SceneView.</param>
154         /// <remarks>
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.
158         ///
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.
162         /// </remarks>
163         /// <since_tizen> 10 </since_tizen>
164         public void AddCamera(Camera camera)
165         {
166             if(camera != null)
167             {
168                 Interop.SceneView.AddCamera(SwigCPtr, camera.SwigCPtr);
169                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170             }
171         }
172
173         /// <summary>
174         /// Removes a Camera from this SceneView.
175         /// If removed Camera is selected Camera,
176         /// first camera in the list becomes the selected Camera.
177         /// </summary>
178         /// <param name="camera"> camera Camera to be removed from this Camera.</param>
179         /// <remarks>
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.
183         /// </remarks>
184         /// <since_tizen> 10 </since_tizen>
185         public void RemoveCamera(Camera camera)
186         {
187             if(camera != null)
188             {
189                 Interop.SceneView.RemoveCamera(SwigCPtr, camera.SwigCPtr);
190                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191             }
192         }
193
194         /// <summary>
195         /// Retrieves the number of cameras.
196         /// </summary>
197         /// <returns>The number of Cameras.</returns>
198         /// <since_tizen> 10 </since_tizen>
199         public uint GetCameraCount()
200         {
201             uint count = Interop.SceneView.GetCameraCount(SwigCPtr);
202             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
203             return count;
204         }
205
206         /// <summary>
207         /// Retrieves a Camera of the index.
208         /// </summary>
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)
213         {
214             global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, index);
215             Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
216             if(camera == null)
217             {
218                 // Register new camera into Registry.
219                 camera = new Camera(cPtr, true);
220             }
221             else
222             {
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);
227             }
228             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
229             return camera;
230         }
231
232         /// <summary>
233         /// Retrieves a Camera of the input name.
234         /// </summary>
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)
239         {
240             global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, name);
241             Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
242             if(camera == null)
243             {
244                 // Register new camera into Registry.
245                 camera = new Camera(cPtr, true);
246             }
247             else
248             {
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);
253             }
254             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
255             return camera;
256         }
257
258         /// <summary>
259         /// Makes SceneView use a Camera of index as a selected camera.
260         /// </summary>
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)
264         {
265             if(inCameraTransition)
266             {
267                 return;
268             }
269             Interop.SceneView.SelectCamera(SwigCPtr, index);
270             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
271         }
272
273         /// <summary>
274         /// Makes SceneView use a Camera of a name as a selected camera.
275         /// </summary>
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)
279         {
280             if(inCameraTransition)
281             {
282                 return;
283             }
284             Interop.SceneView.SelectCamera(SwigCPtr, name);
285             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
286         }
287
288         /// <summary>
289         /// Starts camera transition from currently selected camera to a camera of index.
290         /// Camera Position, Orientation and FieldOfView are smoothly animated.
291         /// </summary>
292         /// <remarks>
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.
295         /// </remarks>
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)
301         {
302             if(inCameraTransition)
303             {
304                 return;
305             }
306             Camera source = GetSelectedCamera();
307             SelectCamera(index);
308             Camera destination = GetSelectedCamera();
309             CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
310         }
311
312         /// <summary>
313         /// Starts camera transition from currently selected camera to a camera of input name.
314         /// Camera Position, Orientation and FieldOfView are smoothly animated.
315         /// </summary>
316         /// <remarks>
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.
319         /// </remarks>
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)
325         {
326             if(inCameraTransition)
327             {
328                 return;
329             }
330             Camera source = GetSelectedCamera();
331             SelectCamera(name);
332             Camera destination = GetSelectedCamera();
333             CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
334         }
335
336         /// <summary>
337         /// Retrieves selected Camera.
338         /// </summary>
339         /// <returns> Camera currently used in SceneView as a selected Camera.</returns>
340         /// <since_tizen> 10 </since_tizen>
341         public Camera GetSelectedCamera()
342         {
343             global::System.IntPtr cPtr = Interop.SceneView.GetSelectedCamera(SwigCPtr);
344             Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
345             if(camera == null)
346             {
347                 // Register new camera into Registry.
348                 camera = new Camera(cPtr, true);
349             }
350             else
351             {
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);
356             }
357             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
358             return camera;
359         }
360
361         /// <summary>
362         /// Changes Image Based Light as the input textures.
363         /// </summary>
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>
367         /// <remarks>
368         /// http://tizen.org/privilege/mediastorage for local files in media storage.
369         /// http://tizen.org/privilege/externalstorage for local files in external storage.
370         /// </remarks>
371         /// <since_tizen> 10 </since_tizen>
372         public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f)
373         {
374             Interop.SceneView.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor);
375             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376         }
377
378         internal void SetUseFramebuffer(bool useFramebuffer)
379         {
380             Interop.SceneView.UseFramebuffer(SwigCPtr, useFramebuffer);
381             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
382         }
383
384         internal bool IsUsingFramebuffer()
385         {
386             bool result = Interop.SceneView.IsUsingFramebuffer(SwigCPtr);
387             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
388             return result;
389         }
390
391         /// <summary>
392         /// Set the ImageBasedLight ScaleFactor.
393         /// </summary>
394         /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f].</param>
395         private void SetImageBasedLightScaleFactor(float scaleFactor)
396         {
397             Interop.SceneView.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor);
398             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
399         }
400
401         /// <summary>
402         /// Get the ImageBasedLight ScaleFactor.
403         /// </summary>
404         /// <returns>ImageBasedLightScaleFactor that controls light source intensity.</returns>
405         private float GetImageBasedLightScaleFactor()
406         {
407             float scaleFactor = Interop.SceneView.GetImageBasedLightScaleFactor(SwigCPtr);
408             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
409             return scaleFactor;
410         }
411
412         private void CameraTransition(Camera sourceCamera, Camera destinationCamera, int durationMilliSeconds, AlphaFunction alphaFunction)
413         {
414             inCameraTransition = true;
415
416             Position sourcePosition = sourceCamera.Position;
417             Rotation sourceOrientation = sourceCamera.Orientation;
418             Radian   sourceFieldOfView = sourceCamera.FieldOfView;
419
420             Position destinationPosition = destinationCamera.Position;
421             Rotation destinationOrientation = destinationCamera.Orientation;
422             Radian   destinationFieldOfView = destinationCamera.FieldOfView;
423
424             // If ProjectionDirection is not equal, match the value.
425             if (sourceCamera.ProjectionDirection != destinationCamera.ProjectionDirection)
426             {
427                 float aspect = destinationCamera.AspectRatio;
428                 if (destinationCamera.ProjectionDirection == Camera.ProjectionDirectionType.Vertical)
429                 {
430                     sourceFieldOfView = Camera.ConvertFovFromHorizontalToVertical(aspect, sourceFieldOfView);
431                 }
432                 else
433                 {
434                     sourceFieldOfView = Camera.ConvertFovFromVerticalToHorizontal(aspect, sourceFieldOfView);
435                 }
436             }
437
438             cameraTransition = new Animation(durationMilliSeconds);
439
440             KeyFrames positionKeyFrames = new KeyFrames();
441             positionKeyFrames.Add(0.0f, sourcePosition);
442             positionKeyFrames.Add(1.0f, destinationPosition);
443
444             KeyFrames orientationKeyFrames = new KeyFrames();
445             orientationKeyFrames.Add(0.0f, sourceOrientation);
446             orientationKeyFrames.Add(1.0f, destinationOrientation);
447
448             KeyFrames fieldOfViewKeyFrames = new KeyFrames();
449             fieldOfViewKeyFrames.Add(0.0f, sourceFieldOfView.ConvertToFloat());
450             fieldOfViewKeyFrames.Add(1.0f, destinationFieldOfView.ConvertToFloat());
451
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);
455
456             cameraTransition.Finished += (s, e) =>
457             {
458                 inCameraTransition = false;
459             };
460             cameraTransition.Play();
461         }
462
463         /// <summary>
464         /// Release swigCPtr.
465         /// </summary>
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)
469         {
470             Interop.SceneView.DeleteScene(swigCPtr);
471         }
472     }
473 }