/* * Copyright(c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Runtime.InteropServices; using System.ComponentModel; using Tizen.NUI; using Tizen.NUI.Binding; using Tizen.NUI.BaseComponents; namespace Tizen.NUI.Scene3D { /// /// Model is a Class to show 3D mesh objects. /// Model supports glTF 2.0 and DLI model formats. /// Physically Based Rendering with Image Based Lighting is also supported. /// /// /// /// Since NUI uses a left-handed coordinate system, loaded models are transformed into a left-handed coordinate system with Y pointing down. /// The Animations defined in the glTF or DLI are also loaded and can be retrieved by using and methods. /// The number of animation is also retrieved by GetAnimationCount() method. /// /// Model also supports Physically Based Rendering(PBR) with Image Based Lighting(IBL). /// For the IBL, two cube map textures(diffuse and specular) are required. /// Model supports 4 types layout for Cube Map: Vertical/Horizontal Cross layouts, and Vertical/Horizontal Array layouts. /// And also, ktx format with cube map is supported. /// /// The model and IBL textures start to be loaded asynchronously when the Model object is on Window. /// ResourcesLoaded signal notifies that the loading of the model and IBL resources have been completed. /// If Model or IBL is requested to be loaded before the other loading is completed, the ResourcesLoaded signal is called after all resources are loaded. /// and methods can be used after the model loading is finished. /// /// By default, the loaded mesh has its own size and inferred from position of vertices. /// The can be modified after model loading is finished. /// If user set size property, the mesh will be scaled to the input size. /// Default value of of the Model is Center. /// /// /// /// /// Model model = new Model(modelUrl) /// { /// Size = new Size(width, height), /// }; /// model.ResourcesLoaded += (s, e) => /// { /// model.PivotPoint = new Vector3(0.5f, 0.5f, 0.5f); // Use center as a Pivot. /// /// int animationCount = model.GetAnimationCount(); /// if(animationCount > 0) /// { /// // Play an Animation of index 0. /// model.GetAnimation(0).Play(); /// } /// }; /// model.SetImageBasedLightSource(diffuseUrl, specularUrl, scaleFactor); /// window.Add(model); /// /// /// /// 10 public partial class Model : View { internal Model(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn) { } /// /// Create an initialized Model. /// /// model file url.(e.g. glTF, and DLI). /// The url to derectory containing resources: binary, image etc. /// /// If resourceDirectoryUrl is empty, the parent directory url of modelUrl is used for resource url. /// /// http://tizen.org/privilege/mediastorage for local files in media storage. /// http://tizen.org/privilege/externalstorage for local files in external storage. /// /// 10 public Model(string modelUrl, string resourceDirectoryUrl = "") : this(Interop.Model.ModelNew(modelUrl, resourceDirectoryUrl), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); this.PositionUsesAnchorPoint = true; } /// /// Create an initialized Model. /// // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Model() : this(Interop.Model.ModelNew(), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); this.PositionUsesAnchorPoint = true; } /// /// Copy constructor. /// /// Source object to copy. /// 10 public Model(Model model) : this(Interop.Model.NewModel(Model.getCPtr(model)), true) { if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Assignment operator. /// /// Source object to be assigned. /// Reference to this. internal Model Assign(Model model) { Model ret = new Model(Interop.Model.ModelAssign(SwigCPtr, Model.getCPtr(model)), false); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Set/Get the ImageBasedLight ScaleFactor. /// Scale factor controls light source intensity in [0.0f, 1.0f] /// // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public float ImageBasedLightScaleFactor { set { SetImageBasedLightScaleFactor(value); } get { return GetImageBasedLightScaleFactor(); } } /// /// Adds modelNode to this Model. /// /// Root of a ModelNode tree // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public void AddModelNode(ModelNode modelRoot) { Interop.Model.AddModelNode(SwigCPtr, ModelNode.getCPtr(modelRoot)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Removes modelNode from this Model. /// /// Root of a ModelNode tree to be removed // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public void RemoveModelNode(ModelNode modelRoot) { Interop.Model.RemoveModelNode(SwigCPtr, ModelNode.getCPtr(modelRoot)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Removes Returns a child ModelNode object with a name that matches nodeName. /// /// The name of the child ModelNode object you want to find. /// Child ModelNode that has nodeName as name. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public ModelNode FindChildModelNodeByName(string nodeName) { global::System.IntPtr cPtr = Interop.Model.FindChildModelNodeByName(SwigCPtr, nodeName); ModelNode ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as ModelNode; if (ret == null) { // Register new animatable into Registry. ret = new ModelNode(cPtr, true); } else { // We found matched NUI animatable. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.BaseHandle.DeleteBaseHandle(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Changes Image Based Light according to the given input textures. /// /// The path of Cube map image that will be used as a diffuse IBL source. /// The path of Cube map image that will be used as a specular IBL source. /// Scale factor that controls light source intensity in [0.0f, 1.0f]. Default value is 1.0f. /// /// http://tizen.org/privilege/mediastorage for local files in media storage. /// http://tizen.org/privilege/externalstorage for local files in external storage. /// /// 10 public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f) { Interop.Model.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Gets number of animations that has been loaded from model file. /// /// /// This method should be called after Model load has been finished. /// /// The number of loaded animations. /// 10 public uint GetAnimationCount() { uint ret = Interop.Model.GetAnimationCount(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Gets animation at the index. /// /// /// This method should be called after Model load has been finished. /// /// Index of animation to be retrieved. /// Animation at the index. /// 10 public Animation GetAnimation(uint index) { global::System.IntPtr cPtr = Interop.Model.GetAnimation(SwigCPtr, index); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if(ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Retrieves animation with the given name. /// Note: This method should be called after Model load finished. /// /// String name of animation to be retrieved. /// Animation that has the given name. /// 10 public Animation GetAnimation(string name) { global::System.IntPtr cPtr = Interop.Model.GetAnimation(SwigCPtr, name); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if(ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Gets number of camera parameters that has been loaded from model file. /// /// /// This method should be called after Model load has been finished. /// /// The number of loaded camera parameters. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public uint GetCameraCount() { uint ret = Interop.Model.GetCameraCount(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Generate Camera using camera parameters at the index. /// If camera parameter is valid, create new Camera. /// Else, return empty Handle. /// /// /// This method should be called after Model load has been finished. /// /// Generated Camera by the index, or empty Handle if generation failed. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Camera GenerateCamera(uint index) { global::System.IntPtr cPtr = Interop.Model.GenerateCamera(SwigCPtr, index); Camera ret = new Camera(cPtr, true); // Always create new camera. if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Apply camera parameters at the index to inputed Camera. /// If camera parameter is valid and camera is not empty, apply parameters. /// It will change camera's transform and near / far / fov or orthographic size / aspect ratio (if defined) /// /// /// This method should be called after Model load has been finished. /// /// Index of camera to be retrieved. /// Camera to be applied parameter. /// True if Apply successed. False otherwise. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public bool ApplyCamera(uint index, Camera camera) { bool ret = false; if(camera?.HasBody() == true) { ret = Interop.Model.ApplyCamera(SwigCPtr, index, Camera.getCPtr(camera)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } return ret; } /// /// Load bvh animation and assign to model. /// Scale is additional scale factor of bvh animation. It is possible that /// Model's scale may not matched with bvh animation scale. /// If scale is null, default use as Vector3.ONE /// /// Name of bvh format file. /// Scale value of bvh animation match with model. /// Animaion of bvh // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Animation LoadBvhAnimation(string bvhFilename, Vector3 scale = null, bool translateRootFromModelNode = true) { global::System.IntPtr cPtr = Interop.Model.LoadBvhAnimation(SwigCPtr, bvhFilename, Vector3.getCPtr(scale), translateRootFromModelNode); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if(ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Load bvh animation and assign to model. /// Scale is additional scale factor of bvh animation. It is possible that /// Model's scale may not matched with bvh animation scale. /// If scale is null, default use as Vector3.ONE /// /// Contents of bvh format file. /// Scale value of bvh animation match with model. /// Animaion of bvh // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Animation LoadBvhAnimationFromBuffer(string bvhBuffer, Vector3 scale = null, bool translateRootFromModelNode = true) { global::System.IntPtr cPtr = Interop.Model.LoadBvhAnimationFromBuffer(SwigCPtr, bvhBuffer, bvhBuffer.Length, Vector3.getCPtr(scale), translateRootFromModelNode); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if (ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Load facial animation and assign to model. /// /// Name of json format file what we predefined. /// Animaion of facial // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Animation LoadFacialAnimation(string facialFilename) { global::System.IntPtr cPtr = Interop.Model.LoadFacialAnimation(SwigCPtr, facialFilename); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if (ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Load facial animation and assign to model. /// /// Contents of json format file what we predefined. /// Animaion of facial // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Animation LoadFacialAnimationFromBuffer(string facialBuffer) { global::System.IntPtr cPtr = Interop.Model.LoadFacialAnimationFromBuffer(SwigCPtr, facialBuffer, facialBuffer.Length); Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation; if (ret == null) { // Register new animation into Registry. ret = new Animation(cPtr, true); } else { // We found matched NUI animation. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.Animation.DeleteAnimation(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Prototype of animation generate by MotionData /// /// Inputed list of pair of MotionIndex and MotionValue. /// The duration in milliseconds. /// Generated animation by input motion data // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public Animation GenerateMotionDataAnimation(MotionData motionData, int durationMilliSeconds) { return InternalGenerateMotionDataAnimation(motionData, durationMilliSeconds); } /// /// Prototype of MotionData setter. /// Note that this API didn't apply KeyFrames animation. /// If you want to apply the animation, please use and play the result. /// /// Inputed list of pair of MotionIndex and MotionValue. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] public void SetMotionData(MotionData motionData) { InternalSetMotionData(motionData); } /// /// Retrieves model root Actor. /// /// Root View of the model. // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] private ModelNode GetModelRoot() { global::System.IntPtr cPtr = Interop.Model.GetModelRoot(SwigCPtr); ModelNode ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as ModelNode; if (ret == null) { // Register new animatable into Registry. ret = new ModelNode(cPtr, true); } else { // We found matched NUI animatable. Reduce cPtr reference count. HandleRef handle = new HandleRef(this, cPtr); Tizen.NUI.Interop.BaseHandle.DeleteBaseHandle(handle); handle = new HandleRef(null, IntPtr.Zero); } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return ret; } /// /// Set the ImageBasedLight ScaleFactor. /// /// Scale factor that controls light source intensity in [0.0f, 1.0f]. private void SetImageBasedLightScaleFactor(float scaleFactor) { Interop.Model.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Get the ImageBasedLight ScaleFactor. /// /// ImageBasedLightScaleFactor that controls light source intensity. private float GetImageBasedLightScaleFactor() { float scaleFactor = Interop.Model.GetImageBasedLightScaleFactor(SwigCPtr); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return scaleFactor; } /// /// Release swigCPtr. /// // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API) [EditorBrowsable(EditorBrowsableState.Never)] protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr) { Interop.Model.DeleteModel(swigCPtr); } } }