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 sreen.
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 the 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: ProjectionMode, AspectRatio, LeftPlaneDistance, RightPlaneDistance, TopPlaneDistance, and BottomPlaneDistance.
50 /// Position, Near/FarPlaneDistance, and FieldOfView are maintained even if the size of the SceneView is changed.
51 /// The Camera's FieldOfView is vertical fov. The horizontal fov is updated internally according to the SceneView size.
53 /// The <see cref="SetImageBasedLightSource(string, string, float)"/> method sets the same IBL to all Model objects added to the SceneView.
54 /// For the IBL, two cube map textures(diffuse and specular) are required.
55 /// SceneView supports 4 types layout for Cube Map: Vertical/Horizontal Cross layouts, and Vertical/Horizontal Array layouts.
56 /// And also, ktx format with cube map is supported.
57 /// If a model already has an IBL, it is batch overridden with the IBL of the SceneView.
58 /// If the SceneView has IBL, the IBL of newly added models is also overridden.
60 /// The IBL textures start to be loaded asynchronously when <see cref="SetImageBasedLightSource(string, string, float)"/> method is called.
61 /// ResourcesLoaded signal notifies that the loading of the IBL resources have been completed.
63 /// SceneView provides an option to use FBO for rendering result with <see cref="UseFramebuffer"/> property.
64 /// If it is false, SceneView is always drawn in the form of a rectangle on the default window surface directly.
65 /// It improves performance, but the SceneView is always drawn on top of other 2D objects regardless of rendering order.
67 /// If FBO is used, the rendering result of SceneView is drawn on the FBO and it is mapped on the plane of the SceneView.
68 /// It could decreases performance slightly, but it is useful to show SceneView according to the rendering order with other Views.
70 /// And since SceneView is a View, it can be placed together with other 2D UI components in the NUI window.
74 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public class SceneView : View
78 private bool inCameraTransition = false;
79 private Animation cameraTransition;
81 internal SceneView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
86 /// Create an initialized SceneView.
88 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public SceneView() : this(Interop.SceneView.SceneNew(), true)
92 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
98 /// <param name="sceneView">Handle to an object.</param>
99 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 public SceneView(SceneView sceneView) : this(Interop.SceneView.NewScene(SceneView.getCPtr(sceneView)), true)
103 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107 /// Assignment operator.
109 /// <param name="sceneView">Handle to an object.</param>
110 /// <returns>Reference to this.</returns>
111 internal SceneView Assign(SceneView sceneView)
113 SceneView ret = new SceneView(Interop.SceneView.SceneAssign(SwigCPtr, SceneView.getCPtr(sceneView)), false);
114 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
119 /// Set/Get the ImageBasedLight ScaleFactor.
120 /// Scale factor controls light source intensity in [0.0f, 1.0f]
122 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public float ImageBasedLightScaleFactor
128 SetImageBasedLightScaleFactor(value);
132 return GetImageBasedLightScaleFactor();
137 /// Set/Get the UseFramebuffer.
138 /// If this property is true, rendering result of SceneView is drawn on FBO and it is mapping on this SceneView plane.
139 /// If this property is false, each item in SceneView is rendered on window directly.
140 /// Default is false.
143 /// If useFramebuffer is true, it could decrease performance but entire rendering order is satisfied.
144 /// If useFramebuffer is false, the performance becomes better but SceneView is rendered on the top of the other 2D components regardless tree order.
146 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
147 [EditorBrowsable(EditorBrowsableState.Never)]
148 public bool UseFramebuffer
152 SetUseFramebuffer(value);
156 return IsUsingFramebuffer();
161 /// Adds a Camera to the SceneView
162 /// The Camera can be used as a selected camera to render the scene by using <see cref="SelectCamera(uint)"/> or <see cref="SelectCamera(string)"/>
164 /// <param name="camera">Camera added on this SceneView.</param>
166 /// Some properties of the Camera will be change depending on the Size of this SceneView.
167 /// Those properties are as follows:
168 /// aspectRatio, nearPlaneDistance, farPlaneDistance, leftPlaneDistance, rightPlaneDistance, topPlaneDistance, and bottomPlaneDistance.
170 /// The FieldOfView of Camera is for vertical fov.
171 /// When the size of the SceneView is changed, the vertical fov is maintained
172 /// and the horizontal fov is automatically calculated according to the SceneView's aspect ratio.
174 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
175 [EditorBrowsable(EditorBrowsableState.Never)]
176 public void AddCamera(Camera camera)
180 Interop.SceneView.AddCamera(SwigCPtr, camera.SwigCPtr);
181 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186 /// Removes a Camera from this SceneView.
187 /// If removed Camera is selected Camera,
188 /// first camera in the list is set to selected Camera.
190 /// <param name="camera"> camera Camera to be removed from this Camera.</param>
192 /// Camera.Dispose() don't automatically release memory. We should call this API if we want to release memory.
193 /// We cannot remove default camera. If we try to remove default camera, ignored.
195 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 public void RemoveCamera(Camera camera)
201 Interop.SceneView.RemoveCamera(SwigCPtr, camera.SwigCPtr);
202 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207 /// Retrieves the number of cameras.
209 /// <returns>The number of Cameras.</returns>
210 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
211 [EditorBrowsable(EditorBrowsableState.Never)]
212 public uint GetCameraCount()
214 uint count = Interop.SceneView.GetCameraCount(SwigCPtr);
215 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220 /// Retrieves a Camera of the index.
222 /// <param name="index"> Index of Camera to be retrieved.</param>
223 /// <returns>Camera of the index.</returns>
224 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 public Camera GetCamera(uint index)
228 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, index);
229 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
232 // Register new camera into Registry.
233 camera = new Camera(cPtr, true);
237 // We found matched NUI camera. Reduce cPtr reference count.
238 HandleRef handle = new HandleRef(this, cPtr);
239 Interop.Camera.DeleteCameraProperty(handle);
240 handle = new HandleRef(null, IntPtr.Zero);
242 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
247 /// Retrieves a Camera of the index.
249 /// <param name="name"> string keyword of Camera to be retrieved.</param>
250 /// <returns>Camera that has the name as a View.Name property</returns>
251 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
252 [EditorBrowsable(EditorBrowsableState.Never)]
253 public Camera GetCamera(string name)
255 global::System.IntPtr cPtr = Interop.SceneView.GetCamera(SwigCPtr, name);
256 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
259 // Register new camera into Registry.
260 camera = new Camera(cPtr, true);
264 // We found matched NUI camera. Reduce cPtr reference count.
265 HandleRef handle = new HandleRef(this, cPtr);
266 Interop.Camera.DeleteCameraProperty(handle);
267 handle = new HandleRef(null, IntPtr.Zero);
269 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274 /// Makes SceneView use a Camera of index as a selected camera.
276 /// <param name="index"> Index of Camera to be used as a selected camera.</param>
277 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
278 [EditorBrowsable(EditorBrowsableState.Never)]
279 public void SelectCamera(uint index)
281 if(inCameraTransition)
285 Interop.SceneView.SelectCamera(SwigCPtr, index);
286 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
290 /// Makes SceneView use a Camera of a name as a selected camera.
292 /// <param name="name"> string keyword of Camera to be used as a selected camera.</param>
293 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
294 [EditorBrowsable(EditorBrowsableState.Never)]
295 public void SelectCamera(string name)
297 if(inCameraTransition)
301 Interop.SceneView.SelectCamera(SwigCPtr, name);
302 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
306 /// Start camera transition from currently selected camera to a camera of index.
307 /// Camera Position and Orientation is smoothly animated.
310 /// The selected camera is switched when the transition is started.
311 /// During camera transition, Selected Camera cannot be changed by using SelectCamera() or CameraTransition() method.
313 /// <param name="index"> Index of destination Camera of Camera transition.</param>
314 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
315 /// <param name="alphaFunction">The alpha function to apply.</param>
316 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
317 [EditorBrowsable(EditorBrowsableState.Never)]
318 public void CameraTransition(uint index, int durationMilliSeconds, AlphaFunction alphaFunction = null)
320 if(inCameraTransition)
324 Camera source = GetSelectedCamera();
326 Camera destination = GetSelectedCamera();
327 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
331 /// Start camera transition from currently selected camera to a camera of input name.
332 /// Camera Position and Orientation is smoothly animated.
335 /// The selected camera is switched when the transition is started.
336 /// During camera transition, Selected Camera cannot be changed by using SelectCamera() or CameraTransition() method.
338 /// <param name="name"> string keyword of destination Camera of Camera transition.</param>
339 /// <param name="durationMilliSeconds">The duration in milliseconds.</param>
340 /// <param name="alphaFunction">The alpha function to apply.</param>
341 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
342 [EditorBrowsable(EditorBrowsableState.Never)]
343 public void CameraTransition(string name, int durationMilliSeconds, AlphaFunction alphaFunction = null)
345 if(inCameraTransition)
349 Camera source = GetSelectedCamera();
351 Camera destination = GetSelectedCamera();
352 CameraTransition(source, destination, durationMilliSeconds, alphaFunction);
356 /// Retrieves selected Camera.
358 /// <returns> Camera currently used in SceneView as a selected Camera.</returns>
359 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
360 [EditorBrowsable(EditorBrowsableState.Never)]
361 public Camera GetSelectedCamera()
363 global::System.IntPtr cPtr = Interop.SceneView.GetSelectedCamera(SwigCPtr);
364 Camera camera = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Camera;
367 // Register new camera into Registry.
368 camera = new Camera(cPtr, true);
372 // We found matched NUI camera. Reduce cPtr reference count.
373 HandleRef handle = new HandleRef(this, cPtr);
374 Interop.Camera.DeleteCameraProperty(handle);
375 handle = new HandleRef(null, IntPtr.Zero);
377 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
382 /// Changes Image Based Light as the input textures.
384 /// <param name="diffuseUrl">The path of Cube map image that can be used as a diffuse IBL source.</param>
385 /// <param name="specularUrl">The path of Cube map image that can be used as a specular IBL source.</param>
386 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f]. Default value is 1.0f.</param>
387 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
388 [EditorBrowsable(EditorBrowsableState.Never)]
389 public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f)
391 Interop.SceneView.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor);
392 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
395 internal void SetUseFramebuffer(bool useFramebuffer)
397 Interop.SceneView.UseFramebuffer(SwigCPtr, useFramebuffer);
398 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401 internal bool IsUsingFramebuffer()
403 bool result = Interop.SceneView.IsUsingFramebuffer(SwigCPtr);
404 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
409 /// Set the ImageBasedLight ScaleFactor.
411 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f].</param>
412 private void SetImageBasedLightScaleFactor(float scaleFactor)
414 Interop.SceneView.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor);
415 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419 /// Get the ImageBasedLight ScaleFactor.
421 /// <returns>ImageBasedLightScaleFactor that controls light source intensity.</returns>
422 private float GetImageBasedLightScaleFactor()
424 float scaleFactor = Interop.SceneView.GetImageBasedLightScaleFactor(SwigCPtr);
425 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429 private void CameraTransition(Camera sourceCamera, Camera destinationCamera, int durationMilliSeconds, AlphaFunction alphaFunction)
431 inCameraTransition = true;
433 Position sourcePosition = sourceCamera.Position;
434 Rotation sourceOrientation = sourceCamera.Orientation;
436 Position destinationPosition = destinationCamera.Position;
437 Rotation destinationOrientation = destinationCamera.Orientation;
439 cameraTransition = new Animation(durationMilliSeconds);
440 KeyFrames positionKeyFrames = new KeyFrames();
441 positionKeyFrames.Add(0.0f, sourcePosition);
442 positionKeyFrames.Add(1.0f, destinationPosition);
443 KeyFrames orientationKeyFrames = new KeyFrames();
444 orientationKeyFrames.Add(0.0f, sourceOrientation);
445 orientationKeyFrames.Add(1.0f, destinationOrientation);
446 cameraTransition.AnimateBetween(destinationCamera, "Position", positionKeyFrames, Animation.Interpolation.Linear, alphaFunction);
447 cameraTransition.AnimateBetween(destinationCamera, "Orientation", orientationKeyFrames, Animation.Interpolation.Linear, alphaFunction);
449 cameraTransition.Finished += (s, e) =>
451 inCameraTransition = false;
453 cameraTransition.Play();
457 /// Release swigCPtr.
459 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
460 [EditorBrowsable(EditorBrowsableState.Never)]
461 protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
463 Interop.SceneView.DeleteScene(swigCPtr);