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 /// Model is a Class to show 3D mesh objects.
29 /// Model supports glTF 2.0 and DLI model formats.
30 /// Physically Based Rendering with Image Based Lighting is also supported.
34 /// Since NUI uses a left-handed coordinate system, loaded models are transformed into a left-handed coordinate system with Y pointing down.
35 /// The Animations defined in the glTF or DLI are also loaded and can be retrieved by using <see cref="GetAnimation(uint)"/> and <see cref="GetAnimation(string)"/> methods.
36 /// The number of animation is also retrieved by GetAnimationCount() method.
38 /// Model also supports Physically Based Rendering(PBR) with Image Based Lighting(IBL).
39 /// For the IBL, two cube map textures(diffuse and specular) are required.
40 /// Model supports 4 types layout for Cube Map: Vertical/Horizontal Cross layouts, and Vertical/Horizontal Array layouts.
41 /// And also, ktx format with cube map is supported.
43 /// The model and IBL textures start to be loaded asynchronously when the Model object is on Window.
44 /// ResourcesLoaded signal notifies that the loading of the model and IBL resources have been completed.
45 /// 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.
46 /// <see cref="GetAnimation(uint)"/> and <see cref="GetAnimation(string)"/> methods can be used after the model loading is finished.
48 /// By default, the loaded mesh has its own size and <see cref="PivotPoint"/> inferred from position of vertices.
49 /// The <see cref="PivotPoint"/> can be modified after model loading is finished.
50 /// If user set size property, the mesh will be scaled to the input size.
51 /// Default value of <see cref="ParentOrigin"/> of the Model is Center.
56 /// Model model = new Model(modelUrl)
58 /// Size = new Size(width, height),
60 /// model.ResourcesLoaded += (s, e) =>
62 /// model.PivotPoint = new Vector3(0.5f, 0.5f, 0.5f); // Use center as a Pivot.
64 /// int animationCount = model.GetAnimationCount();
65 /// if(animationCount > 0)
67 /// // Play an Animation of index 0.
68 /// model.GetAnimation(0).Play();
71 /// model.SetImageBasedLightSource(diffuseUrl, specularUrl, scaleFactor);
72 /// window.Add(model);
76 /// <since_tizen> 10 </since_tizen>
77 public class Model : View
79 internal Model(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
84 /// Create an initialized Model.
86 /// <param name="modelUrl">model file url.(e.g. glTF, and DLI).</param>
87 /// <param name="resourceDirectoryUrl"> The url to derectory containing resources: binary, image etc.</param>
89 /// If resourceDirectoryUrl is empty, the parent directory url of modelUrl is used for resource url.
91 /// http://tizen.org/privilege/mediastorage for local files in media storage.
92 /// http://tizen.org/privilege/externalstorage for local files in external storage.
94 /// <since_tizen> 10 </since_tizen>
95 public Model(string modelUrl, string resourceDirectoryUrl = "") : this(Interop.Model.ModelNew(modelUrl, resourceDirectoryUrl), true)
97 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
98 this.PositionUsesAnchorPoint = true;
102 /// Copy constructor.
104 /// <param name="model">Source object to copy.</param>
105 /// <since_tizen> 10 </since_tizen>
106 public Model(Model model) : this(Interop.Model.NewModel(Model.getCPtr(model)), true)
108 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
112 /// Assignment operator.
114 /// <param name="model">Source object to be assigned.</param>
115 /// <returns>Reference to this.</returns>
116 internal Model Assign(Model model)
118 Model ret = new Model(Interop.Model.ModelAssign(SwigCPtr, Model.getCPtr(model)), false);
119 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
124 /// Set/Get the ImageBasedLight ScaleFactor.
125 /// Scale factor controls light source intensity in [0.0f, 1.0f]
127 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
128 [EditorBrowsable(EditorBrowsableState.Never)]
129 public float ImageBasedLightScaleFactor
133 SetImageBasedLightScaleFactor(value);
137 return GetImageBasedLightScaleFactor();
142 /// Changes Image Based Light according to the given input textures.
144 /// <param name="diffuseUrl">The path of Cube map image that will be used as a diffuse IBL source.</param>
145 /// <param name="specularUrl">The path of Cube map image that will be used as a specular IBL source.</param>
146 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f]. Default value is 1.0f.</param>
148 /// http://tizen.org/privilege/mediastorage for local files in media storage.
149 /// http://tizen.org/privilege/externalstorage for local files in external storage.
151 /// <since_tizen> 10 </since_tizen>
152 public void SetImageBasedLightSource(string diffuseUrl, string specularUrl, float scaleFactor = 1.0f)
154 Interop.Model.SetImageBasedLightSource(SwigCPtr, diffuseUrl, specularUrl, scaleFactor);
155 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 /// Gets number of animations that has been loaded from model file.
162 /// This method should be called after Model load has been finished.
164 /// <returns>The number of loaded animations.</returns>
165 /// <since_tizen> 10 </since_tizen>
166 public uint GetAnimationCount()
168 uint ret = Interop.Model.GetAnimationCount(SwigCPtr);
169 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174 /// Gets animation at the index.
177 /// This method should be called after Model load has been finished.
179 /// <param name="index">Index of animation to be retrieved.</param>
180 /// <returns>Animation at the index.</returns>
181 /// <since_tizen> 10 </since_tizen>
182 public Animation GetAnimation(uint index)
184 Animation ret = new Animation(Interop.Model.GetAnimation(SwigCPtr, index), false);
185 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190 /// Retrieves animation with the given name.
191 /// Note: This method should be called after Model load finished.
193 /// <param name="name">String name of animation to be retrieved.</param>
194 /// <returns>Animation that has the given name.</returns>
195 /// <since_tizen> 10 </since_tizen>
196 public Animation GetAnimation(string name)
198 Animation ret = new Animation(Interop.Model.GetAnimation(SwigCPtr, name), false);
199 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204 /// Retrieves model root Actor.
206 /// <returns>Root View of the model.</returns>
207 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
208 [EditorBrowsable(EditorBrowsableState.Never)]
209 private View GetModelRoot()
211 View ret = new View(Interop.Model.GetModelRoot(SwigCPtr), false);
212 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217 /// Set the ImageBasedLight ScaleFactor.
219 /// <param name="scaleFactor">Scale factor that controls light source intensity in [0.0f, 1.0f].</param>
220 private void SetImageBasedLightScaleFactor(float scaleFactor)
222 Interop.Model.SetImageBasedLightScaleFactor(SwigCPtr, scaleFactor);
223 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227 /// Get the ImageBasedLight ScaleFactor.
229 /// <returns>ImageBasedLightScaleFactor that controls light source intensity.</returns>
230 private float GetImageBasedLightScaleFactor()
232 float scaleFactor = Interop.Model.GetImageBasedLightScaleFactor(SwigCPtr);
233 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
238 /// Release swigCPtr.
240 // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
241 [EditorBrowsable(EditorBrowsableState.Never)]
242 protected override void ReleaseSwigCPtr(global::System.Runtime.InteropServices.HandleRef swigCPtr)
244 Interop.Model.DeleteModel(swigCPtr);