[NUI] Apply missing comments for Scene3D demo
authorEunki Hong <eunkiki.hong@samsung.com>
Wed, 25 Jan 2023 15:07:10 +0000 (00:07 +0900)
committerJaehyun Cho <jaehyun0cho@gmail.com>
Wed, 1 Feb 2023 07:07:33 +0000 (16:07 +0900)
1. Collect all private values in upward of source code.
2. Formatting C#
3. MilliSeconds --> Milliseconds
4. Add some comments

Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
test/Tizen.NUI.Scene3D.Sample/Scene3DSample.cs

index f4123ee..a52fb7d 100644 (file)
@@ -31,13 +31,75 @@ class Scene3DSample : NUIApplication
     Vector2 mWindowSize;
 
     SceneView mSceneView;
-    Model     mModel;
-
-    uint currentCameraIndex = 0u;
-    const int cameraAnimationDurationMilliSeconds = 2000; // milliSeconds
+    Model mModel;
 
     Animation mModelRotateAnimation;
-    const int modelRotateAnimationDurationMilliSeconds = 10000; // milliSeconds
+    const int modelRotateAnimationDurationMilliseconds = 10000; // milliseconds
+
+    private bool mMutex = false; // Lock key event during some transition / Change informations
+
+    #region Model list define
+    private static readonly List<string> ModelUrlList = new List<string>()
+    {
+        // Model reference : https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4
+        // Get from KhronosGroup https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet
+        "DamagedHelmet/DamagedHelmet.gltf",
+        "BoxAnimated/BoxAnimated.gltf",
+    };
+    private int currentModelIndex = 0;
+    #endregion
+
+    #region Image based light list define
+    private static readonly List<(string, string)> IBLUrlList = new List<(string, string)>
+    {
+        ("forest_diffuse_cubemap.png", "forest_specular_cubemap.png"),
+        ("papermill_E_diffuse-64.ktx", "papermill_pmrem.ktx"),
+        ("Irradiance.ktx", "Radiance.ktx"),
+    };
+    private int currentIBLIndex = 0;
+    private float IBLFactor = 0.7f;
+    #endregion
+
+    #region Camera list define
+    private class CameraInfo
+    {
+        public Vector3 Position {get; set;} = Vector3.Zero;
+        public Rotation Orientation {get; set;} = null;
+        public Radian Fov {get; set;} = null;
+        public float Near {get; set;} = 0.5f;
+        public float Far {get; set;} = 50.0f;
+    };
+    private static readonly List<CameraInfo> CameraInfoList = new List<CameraInfo>()
+    {
+        // -Z front and -Y up.
+        new CameraInfo(){
+            Position = new Vector3(0.0f, 0.0f, 5.55f),
+            // Basic camera           : +Z front and -Y up
+            // After YAxis 180 rotate : -Z front and -Y up
+            // Note : Default camera has YAxis 180.0f rotation even we didn't setup.
+            Orientation = new Rotation(new Radian(new Degree(180.0f)), Vector3.YAxis),
+            Fov = new Radian(new Degree(45.0f)),
+            Near = 0.5f,
+            Far = 50.0f,
+        },
+
+        // +Y front and +X up.
+        new CameraInfo(){
+            Position = new Vector3(0.0f, -3.95f, 0.0f),
+            // Rotate by XAxis first, and then rotate by YAxis
+            // Basic camera           : +Z front and -Y up
+            // After XAxis -90 rotate : +Y front and +Z up
+            // After YAxis 90 rotate  : +Y front and +X up
+            Orientation = new Rotation(new Radian(new Degree(90.0f)), Vector3.YAxis) *
+                          new Rotation(new Radian(new Degree(-90.0f)), Vector3.XAxis),
+            Fov = new Radian(new Degree(70.0f)),
+            Near = 0.5f,
+            Far = 50.0f,
+        },
+    };
+    uint currentCameraIndex = 0u;
+    const int cameraAnimationDurationMilliseconds = 2000; // milliseconds
+    #endregion
 
     protected void CreateSceneView()
     {
@@ -52,46 +114,61 @@ class Scene3DSample : NUIApplication
             PositionUsesPivotPoint = true,
         };
 
+        mSceneView.CameraTransitionFinished += (o, e) =>
+        {
+            if (mMutex)
+            {
+                mMutex = false;
+            }
+        };
+
+        mSceneView.ResourcesLoaded += (o, e) =>
+        {
+            Tizen.Log.Error("NUI", $"IBL image loaded done\n");
+            if (mMutex)
+            {
+                mMutex = false;
+            }
+        };
+
         SetupSceneViewCamera(mSceneView);
 
         mWindow.Add(mSceneView);
     }
     private void SetupSceneViewCamera(SceneView sceneView)
     {
-        // Default camera setting
-        // Note : SceneView always have 1 default camera.
-        Tizen.NUI.Scene3D.Camera defaultCamera = sceneView.GetCamera(0u);
-
-        defaultCamera.PositionX = 0.0f;
-        defaultCamera.PositionY = 0.0f;
-        defaultCamera.PositionZ = 5.55f;
-        defaultCamera.NearPlaneDistance = 0.5f;
-        defaultCamera.FarPlaneDistance = 50.0f;
-        //defaultCamera.Orientation = new Rotation(new Radian(new Degree(180.0f)), Vector3.YAxis);
-        defaultCamera.FieldOfView = new Radian(new Degree(45.0f));
-        defaultCamera.OrthographicSize = 2.7f;
-
-        // Additional camera setting (top view camera).
-        Tizen.NUI.Scene3D.Camera camera = new Tizen.NUI.Scene3D.Camera()
+        int cameraCount = CameraInfoList.Count;
+        for (int i = 0; i < cameraCount; ++i)
         {
-            PositionX = 0.0f,
-            PositionY = -3.95f,
-            PositionZ = 0.0f,
-            NearPlaneDistance = 0.5f,
-            FarPlaneDistance = 50.0f,
-            // Rotate by XAxis first, and then rotate by YAxis
-            Orientation = new Rotation(new Radian(new Degree(90.0f)), Vector3.YAxis) *
-                            new Rotation(new Radian(new Degree(-90.0f)), Vector3.XAxis),
-            FieldOfView = new Radian(new Degree(70.0f)),
-            OrthographicSize = 2.7f,
-        };
-        sceneView.AddCamera(camera);
+            var cameraInfo = CameraInfoList[i];
+            Tizen.NUI.Scene3D.Camera camera;
+            if (i == 0)
+            {
+                // Default camera setting
+                // Note : SceneView always have 1 default camera.
+                camera = sceneView.GetCamera(0u);
+            }
+            else
+            {
+                // Additional camera setting (top view camera).
+                camera = new Tizen.NUI.Scene3D.Camera();
+                sceneView.AddCamera(camera);
+            }
+            camera.PositionX = cameraInfo?.Position?.X ?? 0.0f;
+            camera.PositionY = cameraInfo?.Position?.Y ?? 0.0f;
+            camera.PositionZ = cameraInfo?.Position?.Z ?? 0.0f;
+            camera.Orientation = cameraInfo?.Orientation ?? new Rotation(new Radian(new Degree(180.0f)), Vector3.YAxis);
+
+            camera.NearPlaneDistance = cameraInfo?.Near ?? 0.5f;
+            camera.FarPlaneDistance = cameraInfo?.Far ?? 50.0f;
+            camera.FieldOfView = cameraInfo?.Fov ?? new Radian(new Degree(45.0f));
+        }
     }
 
     protected void CreateModel(string modelUrl)
     {
         // Release old one.
-        if(mModel != null)
+        if (mModel != null)
         {
             mModel.Unparent();
             mModel.Dispose();
@@ -105,36 +182,49 @@ class Scene3DSample : NUIApplication
         {
             Model model = s as Model;
 
-            // You can play animation if it has.
-            if(model.GetAnimationCount() > 0u)
+            // You can play animation if the animation exists.
+            if (model.GetAnimationCount() > 0u)
             {
                 model.GetAnimation(0u).Looping = true;
                 model.GetAnimation(0u).Play();
             }
             Tizen.Log.Error("NUI", $"{model.Name} size : {model.Size.Width}, {model.Size.Height}, {model.Size.Depth}\n");
+
+            if (mMutex)
+            {
+                mMutex = false;
+            }
         };
-        mModelRotateAnimation = new Animation(modelRotateAnimationDurationMilliSeconds);
+        mModelRotateAnimation = new Animation(modelRotateAnimationDurationMilliseconds);
         mModelRotateAnimation.AnimateBy(mModel, "Orientation", new Rotation(new Radian(new Degree(360.0f)), Vector3.YAxis));
 
         mModelRotateAnimation.Looping = true;
         mModelRotateAnimation.Play();
 
         mSceneView.Add(mModel);
+
+        mMutex = true;
     }
 
     void SetupIBLimage(string specularUrl, string diffuseUrl, float iblFactor)
     {
-        mSceneView.SetImageBasedLightSource(IMAGE_DIR + specularUrl, IMAGE_DIR + diffuseUrl,iblFactor);
+        mSceneView.SetImageBasedLightSource(IMAGE_DIR + specularUrl, IMAGE_DIR + diffuseUrl, iblFactor);
+
+        mMutex = true;
     }
 
     void OnKeyEvent(object source, Window.KeyEventArgs e)
     {
+        // Skip interaction when some resources are changing now.
+        if (mMutex)
+        {
+            return;
+        }
         if (e.Key.State == Key.StateType.Down)
         {
             FullGC();
-            //Streamline.AnnotateChannelEnd(0);
 
-            switch( e.Key.KeyPressedName )
+            switch (e.Key.KeyPressedName)
             {
                 case "Escape":
                 case "Back":
@@ -148,19 +238,21 @@ class Scene3DSample : NUIApplication
                 case "Select":
                 {
                     currentCameraIndex++;
-                    if(currentCameraIndex >= mSceneView.GetCameraCount())
+                    if (currentCameraIndex >= mSceneView.GetCameraCount())
                     {
                         currentCameraIndex = 0;
                     }
 
-                    mSceneView.CameraTransition(currentCameraIndex, cameraAnimationDurationMilliSeconds);
+                    mSceneView.CameraTransition(currentCameraIndex, cameraAnimationDurationMilliseconds);
+
+                    mMutex = true;
                     break;
                 }
 
                 case "1":
                 {
                     currentModelIndex++;
-                    if(currentModelIndex >= ModelUrlList.Count)
+                    if (currentModelIndex >= ModelUrlList.Count)
                     {
                         currentModelIndex = 0;
                     }
@@ -171,34 +263,18 @@ class Scene3DSample : NUIApplication
                 case "2":
                 {
                     currentIBLIndex++;
-                    if(currentIBLIndex >= IBLUrlList.Count)
+                    if (currentIBLIndex >= IBLUrlList.Count)
                     {
                         currentIBLIndex = 0;
                     }
 
-                    SetupIBLimage(IBLUrlList[currentIBLIndex].Item1, IBLUrlList[currentIBLIndex].Item2, 0.7f);
+                    SetupIBLimage(IBLUrlList[currentIBLIndex].Item1, IBLUrlList[currentIBLIndex].Item2, IBLFactor);
                     break;
                 }
             }
         }
     }
 
-    private static readonly List<string> ModelUrlList = new List<string>()
-    {
-        // Model reference : https://sketchfab.com/models/b81008d513954189a063ff901f7abfe4
-        // Get from KhronosGroup https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet
-        "DamagedHelmet/DamagedHelmet.gltf",
-        "BoxAnimated/BoxAnimated.gltf",
-    };
-    private int currentModelIndex = 0;
-
-    private static readonly List<(string, string)> IBLUrlList = new List<(string, string)>
-    {
-        ("forest_diffuse_cubemap.png", "forest_specular_cubemap.png"),
-        ("papermill_E_diffuse-64.ktx", "papermill_pmrem.ktx"),
-        ("Irradiance.ktx", "Radiance.ktx"),
-    };
-    private int currentIBLIndex = 0;
 
     public void Activate()
     {
@@ -209,7 +285,7 @@ class Scene3DSample : NUIApplication
         mWindow.KeyEvent += OnKeyEvent;
 
         CreateSceneView();
-        SetupIBLimage(IBLUrlList[currentIBLIndex].Item1, IBLUrlList[currentIBLIndex].Item2, 0.7f);
+        SetupIBLimage(IBLUrlList[currentIBLIndex].Item1, IBLUrlList[currentIBLIndex].Item2, IBLFactor);
         CreateModel(ModelUrlList[currentModelIndex]);
     }
     public void FullGC(){