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 /// An event emitted when Camera Transition is finished.
111 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
112 [EditorBrowsable(EditorBrowsableState.Never)]
113 public event EventHandler CameraTransitionFinished;
116 /// Set/Get the ImageBasedLight ScaleFactor.
117 /// Scale factor controls light source intensity in [0.0f, 1.0f]
119 /// <since_tizen> 10 </since_tizen>
120 public float ImageBasedLightScaleFactor
124 SetImageBasedLightScaleFactor(value);
128 return GetImageBasedLightScaleFactor();
133 /// Set/Get the UseFramebuffer.
134 /// If this property is true, rendering result of SceneView is drawn on FBO and it is mapping on this SceneView plane.
135 /// If this property is false, each item in SceneView is rendered on window directly.
136 /// Default is false.
139 /// If UseFramebuffer is true, it could decrease performance but entire rendering order is satisfied.
140 /// If UseFramebuffer is false, the performance becomes better but SceneView is rendered on the top of the other 2D components regardless tree order.
142 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 public bool UseFramebuffer
148 SetUseFramebuffer(value);
152 return IsUsingFramebuffer();
157 /// Adds a Camera to the SceneView at the end of the camera list of SceneView.
158 /// The Camera can be used as a selected camera to render the scene by using <see cref="SelectCamera(uint)"/> or <see cref="SelectCamera(string)"/>
160 /// <param name="camera">Camera added on this SceneView.</param>
162 /// Some properties of the Camera will be change depending on the Size of this SceneView.
163 /// Those properties are as follows:
164 /// AspectRatio, LeftPlaneDistance, RightPlaneDistance, TopPlaneDistance, and BottomPlaneDistance.
166 /// The FieldOfView of Camera is for vertical fov.
167 /// When the size of the SceneView is changed, the vertical fov is maintained
168 /// and the horizontal fov is automatically calculated according to the SceneView's AspectRatio.
170 /// <since_tizen> 10 </since_tizen>
171 public void AddCamera(Camera camera)
175 Interop.SceneView.AddCamera(SwigCPtr, camera.SwigCPtr);
176 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181 /// Removes a Camera from this SceneView.
182 /// If removed Camera is selected Camera,
183 /// first camera in the list becomes the selected Camera.
185 /// <param name="camera"> camera Camera to be removed from this Camera.</param>
187 /// When Camera.Dispose() is called, the NUI object is disposed, but camera information is maintained internally.
188 /// Therefore, even if Camera.Dispose() is called, RemoveCamera() or RemoveCamera() methods can be used.
189 /// If RemoveCamera() is called too, all information is deleted together.
191 /// <since_tizen> 10 </since_tizen>
192 public void RemoveCamera(Camera camera)
196 Interop.SceneView.RemoveCamera(SwigCPtr, camera.SwigCPtr);
197 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
202 /// Retrieves the number of cameras.
204 /// <returns>The number of Cameras.</returns>
205 /// <since_tizen> 10 </since_tizen>
206 public uint GetCameraCount()
208 uint count = Interop.SceneView.GetCameraCount(SwigCPtr);
209 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214 /// Retrieves a Camera of the index.
216 /// <param name="index"> Index of Camera to be retrieved.</param>
217 /// <returns>Camera of the index.</returns>
218 /// <since_tizen> 10 </since_tizen>
219 public Camera GetCamera(uint index)
221 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, index);
222 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
225 // Register new camera into Registry.
226 camera = new Camera(cPtr, true);
230 // We found matched NUI camera. Reduce cPtr reference count.
231 HandleRef handle = new HandleRef(this, cPtr);
232 Interop.Camera.DeleteCameraProperty(handle);
233 handle = new HandleRef(null, IntPtr.Zero);
235 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
240 /// Retrieves a Camera of the input name.
242 /// <param name="name"> string keyword of Camera to be retrieved.</param>
243 /// <returns>Camera that has the name as a View.Name property</returns>
244 /// <since_tizen> 10 </since_tizen>
245 public Camera GetCamera(string name)
247 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, name);
248 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
251 // Register new camera into Registry.
252 camera = new Camera(cPtr, true);
256 // We found matched NUI camera. Reduce cPtr reference count.
257 HandleRef handle = new HandleRef(this, cPtr);
258 Interop.Camera.DeleteCameraProperty(handle);
259 handle = new HandleRef(null, IntPtr.Zero);
261 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
266 /// Makes SceneView use a Camera of index as a selected camera.
268 /// <param name="index"> Index of Camera to be used as a selected camera.</param>
269 /// <since_tizen> 10 </since_tizen>
270 public void SelectCamera(uint index)
272 if(inCameraTransition)
276 Interop.SceneView.SelectCamera(SwigCPtr, index);
277 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281 /// Makes SceneView use a Camera of a name as a selected camera.
283 /// <param name="name"> string keyword of Camera to be used as a selected camera.</param>
284 /// <since_tizen> 10 </since_tizen>
285 public void SelectCamera(string name)
287 if(inCameraTransition)
291 Interop.SceneView.SelectCamera(SwigCPtr, name);
292 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296 /// Starts camera transition from currently selected camera to a camera of index.
297 /// Camera Position, Orientation and FieldOfView are smoothly animated.
300 /// The selected camera is switched to the Camera of the index when the transition is started.
301 /// During camera transition, Selected Camera should not be changed by using SelectCamera() or CameraTransition() method.
302 /// During camera transition, Camera properties of Selected Camera should not be changed.
304 /// <param name="index"> Index of destination Camera of Camera transition.</param>
305 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
306 /// <param name="alphaFunction">The alpha function to apply.</param>
307 /// <since_tizen> 10 </since_tizen>
308 public void CameraTransition(uint index, int durationMilliSeconds, AlphaFunction alphaFunction = null)
310 if(inCameraTransition || GetSelectedCamera() == GetCamera(index))
314 Camera source = GetSelectedCamera();
316 Camera destination = GetSelectedCamera();
317 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
321 /// Starts camera transition from currently selected camera to a camera of input name.
322 /// Camera Position, Orientation and FieldOfView are smoothly animated.
325 /// The selected camera is switched to the Camera of the input name when the transition is started.
326 /// During camera transition, Selected Camera should not be changed by using SelectCamera() or CameraTransition() method.
327 /// During camera transition, Camera properties of Selected Camera should not be changed.
329 /// <param name="name"> string keyword of destination Camera of Camera transition.</param>
330 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
331 /// <param name="alphaFunction">The alpha function to apply.</param>
332 /// <since_tizen> 10 </since_tizen>
333 public void CameraTransition(string name, int durationMilliSeconds, AlphaFunction alphaFunction = null)
335 if(inCameraTransition || GetSelectedCamera() == GetCamera(name))
339 Camera source = GetSelectedCamera();
341 Camera destination = GetSelectedCamera();
342 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
346 /// Retrieves selected Camera.
348 /// <returns> Camera currently used in SceneView as a selected Camera.</returns>
349 /// <since_tizen> 10 </since_tizen>
350 public Camera GetSelectedCamera()
352 global::System.IntPtr cPtr = Interop.SceneView.GetSelectedCamera(SwigCPtr);
353 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
356 // Register new camera into Registry.
357 camera = new Camera(cPtr, true);
361 // We found matched NUI camera. Reduce cPtr reference count.
362 HandleRef handle = new HandleRef(this, cPtr);
363 Interop.Camera.DeleteCameraProperty(handle);
364 handle = new HandleRef(null, IntPtr.Zero);
366 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
371 /// Changes Image Based Light as the input textures.
373 /// <param name="diffuseUrl">The path of Cube map image that can be used as a diffuse IBL source.</param>
374 /// <param name="specularUrl">The path of Cube map image that can be used as a specular IBL source.</param>
375 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f]. Default value is 1.0f.</param>
377 /// http://tizen.org/privilege/mediastorage for local files in media storage.
378 /// http://tizen.org/privilege/externalstorage for local files in external storage.
380 /// <since_tizen> 10 </since_tizen>
381 public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f)
383 Interop.SceneView.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor);
384 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387 internal void SetUseFramebuffer(bool useFramebuffer)
389 Interop.SceneView.UseFramebuffer(SwigCPtr, useFramebuffer);
390 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
393 internal bool IsUsingFramebuffer()
395 bool result = Interop.SceneView.IsUsingFramebuffer(SwigCPtr);
396 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401 /// Set the ImageBasedLight ScaleFactor.
403 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f].</param>
404 private void SetImageBasedLightScaleFactor(float scaleFactor)
406 Interop.SceneView.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor);
407 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
411 /// Get the ImageBasedLight ScaleFactor.
413 /// <returns>ImageBasedLightScaleFactor that controls light source intensity.</returns>
414 private float GetImageBasedLightScaleFactor()
416 float scaleFactor = Interop.SceneView.GetImageBasedLightScaleFactor(SwigCPtr);
417 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
421 private void CameraTransition(Camera sourceCamera, Camera destinationCamera, int durationMilliSeconds, AlphaFunction alphaFunction)
423 inCameraTransition = true;
425 Position sourcePosition = sourceCamera.Position;
426 Rotation sourceOrientation = sourceCamera.Orientation;
427 Radian sourceFieldOfView = sourceCamera.FieldOfView;
429 Position destinationPosition = destinationCamera.Position;
430 Rotation destinationOrientation = destinationCamera.Orientation;
431 Radian destinationFieldOfView = destinationCamera.FieldOfView;
433 // If ProjectionDirection is not equal, match the value.
434 if (sourceCamera.ProjectionDirection != destinationCamera.ProjectionDirection)
436 float aspect = destinationCamera.AspectRatio;
437 if (destinationCamera.ProjectionDirection == Camera.ProjectionDirectionType.Vertical)
439 sourceFieldOfView = Camera.ConvertFovFromHorizontalToVertical(aspect, sourceFieldOfView);
443 sourceFieldOfView = Camera.ConvertFovFromVerticalToHorizontal(aspect, sourceFieldOfView);
447 cameraTransition = new Animation(durationMilliSeconds);
449 KeyFrames positionKeyFrames = new KeyFrames();
450 positionKeyFrames.Add(0.0f, sourcePosition);
451 positionKeyFrames.Add(1.0f, destinationPosition);
453 KeyFrames orientationKeyFrames = new KeyFrames();
454 orientationKeyFrames.Add(0.0f, sourceOrientation);
455 orientationKeyFrames.Add(1.0f, destinationOrientation);
457 KeyFrames fieldOfViewKeyFrames = new KeyFrames();
458 fieldOfViewKeyFrames.Add(0.0f, sourceFieldOfView.ConvertToFloat());
459 fieldOfViewKeyFrames.Add(1.0f, destinationFieldOfView.ConvertToFloat());
461 cameraTransition.AnimateBetween(destinationCamera, "Position", positionKeyFrames, Animation.Interpolation.Linear, alphaFunction);
462 cameraTransition.AnimateBetween(destinationCamera, "Orientation", orientationKeyFrames, Animation.Interpolation.Linear, alphaFunction);
463 cameraTransition.AnimateBetween(destinationCamera, "FieldOfView", fieldOfViewKeyFrames, Animation.Interpolation.Linear, alphaFunction);
465 float destinationNearPlaneDistance = destinationCamera.NearPlaneDistance;
466 float destinationFarPlaneDistance = destinationCamera.FarPlaneDistance;
467 destinationCamera.NearPlaneDistance = Math.Min(sourceCamera.NearPlaneDistance, destinationCamera.NearPlaneDistance);
468 destinationCamera.FarPlaneDistance = Math.Max(sourceCamera.FarPlaneDistance, destinationCamera.FarPlaneDistance);
470 cameraTransition.Finished += (s, e) =>
472 this.GetSelectedCamera().NearPlaneDistance = destinationNearPlaneDistance;
473 this.GetSelectedCamera().FarPlaneDistance = destinationFarPlaneDistance;
474 inCameraTransition = false;
475 CameraTransitionFinished?.Invoke(this, EventArgs.Empty);
477 cameraTransition.Play();
479 sourceFieldOfView.Dispose();
480 positionKeyFrames.Dispose();
481 orientationKeyFrames.Dispose();
482 fieldOfViewKeyFrames.Dispose();
486 /// Release swigCPtr.
488 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
489 [EditorBrowsable(EditorBrowsableState.Never)]
490 protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
492 Interop.SceneView.DeleteScene(swigCPtr);