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.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
21 namespace Tizen.NUI.Scene3D
24 /// Camera class controls a camera in 3D space.
26 /// Camera can be added on the SceneView and displays SceneView's virtual 3D world to the screen.
27 /// Camera can be translated and rotated in the space.
30 /// Transform inheritance cannot be guaranteed when adding children to a camera.
32 /// <since_tizen> 10 </since_tizen>
33 public partial class Camera : View
35 internal Camera(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
40 /// Creates an uninitialized Camera.
42 /// <since_tizen> 10 </since_tizen>
43 public Camera() : this(Interop.Camera.New(), true)
45 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
51 /// <param name="camera">The Camera object to be copied.</param>
52 /// <since_tizen> 10 </since_tizen>
53 public Camera(Camera camera) : this(Interop.Camera.NewCamera(Camera.getCPtr(camera)), true)
55 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
61 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
62 [EditorBrowsable(EditorBrowsableState.Never)]
63 public Camera Assign(Camera rhs)
65 Camera ret = new Camera(Interop.Camera.Assign(SwigCPtr, Camera.getCPtr(rhs)), false);
66 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
71 /// Creates a CameraActor object.
72 /// Sets the default camera perspective projection for the given canvas size..
74 internal Camera(Vector2 size) : this(Interop.Camera.New(Vector2.getCPtr(size)), true)
76 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
79 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 internal static Camera DownCast(BaseHandle handle)
85 throw new global::System.ArgumentNullException(nameof(handle));
87 Camera ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Camera;
88 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93 /// Enumeration for the projectionMode.
94 /// ProjectionMode defines how the camera shows 3D objects or scene on a 2D plane with projection.
96 /// <since_tizen> 10 </since_tizen>
97 public enum ProjectionModeType
100 /// Distance causes foreshortening; objects further from the camera appear smaller.
104 /// Relative distance from the camera does not affect the size of objects.
110 /// Enumeration for the projectionDirection.
112 /// This will be released at Tizen.NET API Level 10, so currently this would be used as inhouse API.
113 [EditorBrowsable(EditorBrowsableState.Never)]
114 public enum ProjectionDirectionType
117 /// Distance causes foreshortening; objects further from the camera appear smaller.
121 /// Relative distance from the camera does not affect the size of objects.
127 /// Sets/Gets the projection mode.
128 /// The default is Perspective
130 /// <since_tizen> 10 </since_tizen>
131 public ProjectionModeType ProjectionMode
135 return (ProjectionModeType)GetValue(ProjectionProperty);
139 SetValue(ProjectionProperty, value);
140 NotifyPropertyChanged();
146 /// Sets/Gets the projection direction.
147 /// Projection direction determine basic direction of projection relative properties.
148 /// It will be used when we need to calculate some values relative with aspect ratio.
149 /// <see cref="FieldOfView"/>, and <see cref="OrthographicSize"/>
152 /// For example, if aspect ratio is 4:3 and set fieldOfView as 60 degree.
153 /// If ProjectionDirectionType.Vertical, basic direction is vertical. so, FoV of horizontal direction become ~75.2 degree
154 /// If ProjectionDirectionType.Horizontal, basic direction is horizontal. so, FoV of vertical direction become ~46.8 degree
157 /// This property doesn't change <see cref="FieldOfView"/> and <see cref="OrthographicSize"/> value automatically.
158 /// So result scene might be changed.
160 /// <para>The default is Vertical.</para>
162 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public ProjectionDirectionType ProjectionDirection
168 return (ProjectionDirectionType)GetValue(ProjectionDirectionProperty);
172 SetValue(ProjectionDirectionProperty, value);
173 NotifyPropertyChanged();
178 /// Sets/Gets the field of view in Radians.
179 /// FieldOfView depends on <see cref="ProjectionDirection"/> value.
180 /// The default field of view is 45 degrees.
182 /// <since_tizen> 10 </since_tizen>
183 public Radian FieldOfView
187 return new Radian((float)GetValue(FieldOfViewProperty));
191 SetValue(FieldOfViewProperty, value?.ConvertToFloat() ?? 0.0f);
192 NotifyPropertyChanged();
197 /// Sets/Gets Orthographic Size of this camera.
198 /// OrthographicSize depends on <see cref="ProjectionDirection"/> value.
199 /// If ProjectoinDirection is Vertical, OrthographicSize is height/2 of viewing cube of Orthographic projection.
200 /// If ProjectoinDirection is Horizontal, OrthographicSize is width/2 of viewing cube of Orthographic projection.
201 /// Remained Width or Height of viewing cube is internally computed by using aspect ratio of Viewport.
203 /// <since_tizen> 10 </since_tizen>
204 public float OrthographicSize
208 return InternalProjectionDirection == ProjectionDirectionType.Vertical ? TopPlaneDistance : RightPlaneDistance;
214 if(InternalProjectionDirection == ProjectionDirectionType.Vertical)
217 halfWidth = AspectRatio * value;
221 halfHeight = value / AspectRatio;
224 SetValue(TopPlaneDistanceProperty, halfHeight);
225 SetValue(BottomPlaneDistanceProperty, -halfHeight);
226 SetValue(LeftPlaneDistanceProperty, -halfWidth);
227 SetValue(RightPlaneDistanceProperty, halfWidth);
228 NotifyPropertyChanged();
233 /// Gets the aspect ratio of the camera.
235 /// <since_tizen> 10 </since_tizen>
236 public float AspectRatio
240 return (float)GetValue(AspectRatioProperty);
245 /// Sets/Gets the near clipping plane distance.
247 /// <since_tizen> 10 </since_tizen>
248 public float NearPlaneDistance
252 return (float)GetValue(NearPlaneDistanceProperty);
256 SetValue(NearPlaneDistanceProperty, value);
257 NotifyPropertyChanged();
262 /// Sets/Gets the far clipping plane distance.
264 /// <since_tizen> 10 </since_tizen>
265 public float FarPlaneDistance
269 return (float)GetValue(FarPlaneDistanceProperty);
273 SetValue(FarPlaneDistanceProperty, value);
274 NotifyPropertyChanged();
279 /// Gets the left clipping plane distance.
281 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
282 [EditorBrowsable(EditorBrowsableState.Never)]
283 public float LeftPlaneDistance
287 return (float)GetValue(LeftPlaneDistanceProperty);
292 /// Gets the right clipping plane distance.
294 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
295 [EditorBrowsable(EditorBrowsableState.Never)]
296 public float RightPlaneDistance
300 return (float)GetValue(RightPlaneDistanceProperty);
305 /// Gets the top clipping plane distance.
307 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
308 [EditorBrowsable(EditorBrowsableState.Never)]
309 public float TopPlaneDistance
313 return (float)GetValue(TopPlaneDistanceProperty);
318 /// Gets the bottom clipping plane distance.
320 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
321 [EditorBrowsable(EditorBrowsableState.Never)]
322 public float BottomPlaneDistance
326 return (float)GetValue(BottomPlaneDistanceProperty);
331 /// Requests for an inversion on the Y axis on the projection calculation.
332 /// or gets whether the Y axis is inverted.
334 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
335 [EditorBrowsable(EditorBrowsableState.Never)]
336 public bool InvertYAxis
340 return (bool)GetValue(InvertYAxisProperty);
344 SetValue(InvertYAxisProperty, value);
345 NotifyPropertyChanged();
350 /// Gets ProjectionMatrix of this Camera
351 /// TODO : Open Matrix
353 internal Matrix ProjectionMatrix
357 Matrix returnValue = new Matrix();
358 PropertyValue projectionMatrix = GetProperty(Interop.Camera.ProjectionMatrixGet());
359 projectionMatrix?.Get(returnValue);
360 projectionMatrix?.Dispose();
366 /// Gets ViewMatrix of this Camera
367 /// TODO : Open Matrix
369 internal Matrix ViewMatrix
373 Matrix returnValue = new Matrix();
374 PropertyValue viewMatrix = GetProperty(Interop.Camera.ViewMatrixGet());
375 viewMatrix.Get(returnValue);
376 viewMatrix.Dispose();
381 private ProjectionModeType InternalProjectionMode
385 int returnValue = (int)ProjectionModeType.Perspective;
386 PropertyValue projectionMode = GetProperty(Interop.Camera.ProjectionModeGet());
387 projectionMode?.Get(out returnValue);
388 projectionMode?.Dispose();
389 return (ProjectionModeType)returnValue;
393 PropertyValue setValue = new Tizen.NUI.PropertyValue((int)value);
394 SetProperty(Interop.Camera.ProjectionModeGet(), setValue);
399 private ProjectionDirectionType InternalProjectionDirection
403 int returnValue = (int)ProjectionDirectionType.Vertical;
404 PropertyValue projectionDirection = GetProperty(Interop.Camera.ProjectionDirectionGet());
405 projectionDirection?.Get(out returnValue);
406 projectionDirection?.Dispose();
407 return (ProjectionDirectionType)returnValue;
411 PropertyValue setValue = new Tizen.NUI.PropertyValue((int)value);
412 SetProperty(Interop.Camera.ProjectionDirectionGet(), setValue);
417 private float InternalFieldOfView
421 float returnValue = 0.0f;
422 PropertyValue fieldView = GetProperty(Interop.Camera.FieldOfViewGet());
423 fieldView?.Get(out returnValue);
424 fieldView?.Dispose();
429 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
430 SetProperty(Interop.Camera.FieldOfViewGet(), setValue);
435 private float InternalAspectRatio
439 float returnValue = 0.0f;
440 PropertyValue aspectRatio = GetProperty(Interop.Camera.AspectRatioGet());
441 aspectRatio?.Get(out returnValue);
442 aspectRatio?.Dispose();
447 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
448 SetProperty(Interop.Camera.AspectRatioGet(), setValue);
453 private float InternalNearPlaneDistance
457 float returnValue = 0.0f;
458 PropertyValue nearPlaneDistance = GetProperty(Interop.Camera.NearPlaneDistanceGet());
459 nearPlaneDistance?.Get(out returnValue);
460 nearPlaneDistance?.Dispose();
465 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
466 SetProperty(Interop.Camera.NearPlaneDistanceGet(), setValue);
471 private float InternalFarPlaneDistance
475 float returnValue = 0.0f;
476 PropertyValue farPlaneDistance = GetProperty(Interop.Camera.FarPlaneDistanceGet());
477 farPlaneDistance?.Get(out returnValue);
478 farPlaneDistance?.Dispose();
483 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
484 SetProperty(Interop.Camera.FarPlaneDistanceGet(), setValue);
489 private float InternalLeftPlaneDistance
493 float returnValue = 0.0f;
494 PropertyValue leftPlaneDistance = GetProperty(Interop.Camera.LeftPlaneDistanceGet());
495 leftPlaneDistance?.Get(out returnValue);
496 leftPlaneDistance?.Dispose();
501 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
502 SetProperty(Interop.Camera.LeftPlaneDistanceGet(), setValue);
507 private float InternalRightPlaneDistance
511 float returnValue = 0.0f;
512 PropertyValue rightPlaneDistance = GetProperty(Interop.Camera.RightPlaneDistanceGet());
513 rightPlaneDistance?.Get(out returnValue);
514 rightPlaneDistance?.Dispose();
519 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
520 SetProperty(Interop.Camera.RightPlaneDistanceGet(), setValue);
525 private float InternalTopPlaneDistance
529 float returnValue = 0.0f;
530 PropertyValue topPlaneDistance = GetProperty(Interop.Camera.TopPlaneDistanceGet());
531 topPlaneDistance?.Get(out returnValue);
532 topPlaneDistance?.Dispose();
537 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
538 SetProperty(Interop.Camera.TopPlaneDistanceGet(), setValue);
543 private float InternalBottomPlaneDistance
547 float returnValue = 0.0f;
548 PropertyValue bottomPlaneDistance = GetProperty(Interop.Camera.BottomPlaneDistanceGet());
549 bottomPlaneDistance?.Get(out returnValue);
550 bottomPlaneDistance?.Dispose();
555 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
556 SetProperty(Interop.Camera.BottomPlaneDistanceGet(), setValue);
561 private bool InternalInvertYAxis
565 bool returnValue = false;
566 PropertyValue invertYAxis = GetProperty(Interop.Camera.InvertYAxisGet());
567 invertYAxis?.Get(out returnValue);
568 invertYAxis?.Dispose();
573 PropertyValue setValue = new Tizen.NUI.PropertyValue(value);
574 SetProperty(Interop.Camera.InvertYAxisGet(), setValue);
579 internal void SetProjection(ProjectionModeType mode)
581 Interop.Camera.SetProjectionMode(SwigCPtr, (int)mode);
582 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
585 internal ProjectionModeType GetProjection()
587 ProjectionModeType ret = (ProjectionModeType)Interop.Camera.GetProjectionMode(SwigCPtr);
588 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
592 internal void SetFieldOfView(float fieldOfView)
594 Interop.Camera.SetFieldOfView(SwigCPtr, fieldOfView);
595 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
598 internal float GetFieldOfView()
600 float ret = Interop.Camera.GetFieldOfView(SwigCPtr);
601 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
605 internal void SetAspectRatio(float aspectRatio)
607 Interop.Camera.SetAspectRatio(SwigCPtr, aspectRatio);
608 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
611 internal float GetAspectRatio()
613 float ret = Interop.Camera.GetAspectRatio(SwigCPtr);
614 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
618 internal void SetNearClippingPlane(float nearClippingPlane)
620 Interop.Camera.SetNearClippingPlane(SwigCPtr, nearClippingPlane);
621 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
624 internal float GetNearClippingPlane()
626 float ret = Interop.Camera.GetNearClippingPlane(SwigCPtr);
627 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
631 internal void SetFarClippingPlane(float farClippingPlane)
633 Interop.Camera.SetFarClippingPlane(SwigCPtr, farClippingPlane);
634 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
637 internal float GetFarClippingPlane()
639 float ret = Interop.Camera.GetFarClippingPlane(SwigCPtr);
640 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
644 internal void SetInvertYAxis(bool invertYAxis)
646 Interop.Camera.SetInvertYAxis(SwigCPtr, invertYAxis);
647 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
650 internal bool GetInvertYAxis()
652 bool ret = Interop.Camera.GetInvertYAxis(SwigCPtr);
653 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
658 /// Convert from vertical fov to horizontal fov consider with camera's AspectRatio.
660 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
661 [EditorBrowsable(EditorBrowsableState.Never)]
662 public static Radian ConvertFovFromVerticalToHorizontal(float aspect, Radian verticalFov)
664 if(verticalFov == null)
668 return new Radian(2.0f * (float)Math.Atan(Math.Tan(verticalFov.ConvertToFloat() * 0.5f) * aspect));
672 /// Convert from horizontal fov to vertical fov consider with camera's AspectRatio.
674 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
675 [EditorBrowsable(EditorBrowsableState.Never)]
676 public static Radian ConvertFovFromHorizontalToVertical(float aspect, Radian horizontalFov)
678 if(horizontalFov == null)
682 return new Radian(2.0f * (float)Math.Atan(Math.Tan(horizontalFov.ConvertToFloat() * 0.5f) / aspect));
686 /// Release swigCPtr.
688 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
689 [EditorBrowsable(EditorBrowsableState.Never)]
690 protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
692 Interop.Camera.DeleteCamera(swigCPtr);