Browse Source

New visualization added

Furkan Karakocaoglu 1 year ago
parent
commit
7fb11833c7

+ 15 - 3
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/General/PlaybackController.cs

@@ -21,6 +21,9 @@ public class PlaybackController : MonoBehaviour
             year.GetComponent<WalkLerpPlayback>().rewind = false;
             year.GetComponent<WalkLerpPlayback>().pause = false;
             year.GetComponent<WalkLerpPlayback>().play = false;
+            year.GetComponent<SpatialTimeSimulation>().rewind = false;
+            year.GetComponent<SpatialTimeSimulation>().pause = false;
+            year.GetComponent<SpatialTimeSimulation>().play = false;
         }
     }
 
@@ -28,11 +31,14 @@ public class PlaybackController : MonoBehaviour
     {
         foreach (var year in years)
         {
-            if (!year.GetComponent<WalkLerpPlayback>().rewind)
+            if (!year.GetComponent<WalkLerpPlayback>().rewind || !year.GetComponent<SpatialTimeSimulation>().rewind)
             {
                 year.GetComponent<WalkLerpPlayback>().rewind = true;
                 year.GetComponent<WalkLerpPlayback>().pause = false;
                 year.GetComponent<WalkLerpPlayback>().play = false;
+                year.GetComponent<SpatialTimeSimulation>().rewind = true;
+                year.GetComponent<SpatialTimeSimulation>().pause = false;
+                year.GetComponent<SpatialTimeSimulation>().play = false;
             }
         }
     }
@@ -40,11 +46,14 @@ public class PlaybackController : MonoBehaviour
     {
         foreach (var year in years)
         {
-            if (!year.GetComponent<WalkLerpPlayback>().pause)
+            if (!year.GetComponent<WalkLerpPlayback>().pause || !year.GetComponent<SpatialTimeSimulation>().pause)
             {
                 year.GetComponent<WalkLerpPlayback>().rewind = false;
                 year.GetComponent<WalkLerpPlayback>().pause = true;
                 year.GetComponent<WalkLerpPlayback>().play = false;
+                year.GetComponent<SpatialTimeSimulation>().rewind = false;
+                year.GetComponent<SpatialTimeSimulation>().pause = true;
+                year.GetComponent<SpatialTimeSimulation>().play = false;
             }
         }
     }
@@ -53,11 +62,14 @@ public class PlaybackController : MonoBehaviour
     {
         foreach (var year in years)
         {
-            if (!year.GetComponent<WalkLerpPlayback>().play)
+            if (!year.GetComponent<WalkLerpPlayback>().play || !year.GetComponent<SpatialTimeSimulation>().play)
             {
                 year.GetComponent<WalkLerpPlayback>().rewind = false;
                 year.GetComponent<WalkLerpPlayback>().pause = false;
                 year.GetComponent<WalkLerpPlayback>().play = true;
+                year.GetComponent<SpatialTimeSimulation>().rewind = false;
+                year.GetComponent<SpatialTimeSimulation>().pause = false;
+                year.GetComponent<SpatialTimeSimulation>().play = true;
             }
         }
     }

+ 105 - 153
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/TestingFunctions/SpatialTimeSimulation.cs

@@ -5,7 +5,7 @@ using System.Collections.Generic;
 using System.IO;
 using System;
 
-[DefaultExecutionOrder(40)]
+[DefaultExecutionOrder(61)]
 [RequireComponent(typeof(InstantiatePrefab), typeof(WriteInCSV), typeof(ReadFromCSV))]
 public class SpatialTimeSimulation : MonoBehaviour
 {
@@ -18,6 +18,8 @@ public class SpatialTimeSimulation : MonoBehaviour
 
     // Global GameObjects
     private GameObject[][] humansGO;
+    private NavMeshAgent[][] humansNMA;
+    private Animator[][] humansA;
 
     // Playback variables
     private int[] maxIJ;
@@ -30,33 +32,66 @@ public class SpatialTimeSimulation : MonoBehaviour
     private float prevSliderValue;
 
     // Thief Settings
-    private Vector3 stealHere;
+    //private Vector3 stealHere;
 
-    // Testing
+    // Spatial time visualization
+    private List<Vector3>[][] positions;
     public float thickness = 0.1f;
+    public Material color;
 
     private void Start()
     {
         // Get information from InstatiatePrefab
         humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
-        NavMeshAgent[][] humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
-        stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
+        humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
+        humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
+        positions = new List<Vector3>[humansGO.Length][];
+        //stealHere = gameObject.GetComponent<ControllingThief>().stealHere;
 
         // Read from CSV file and save time, position, rotation in matrix
-        int index = gameObject.GetComponent<WriteInCSV>().index;
+        int index = gameObject.GetComponent<WriteInCSV>().index; 
         string dir = Directory.GetCurrentDirectory();
         string reference = @"\Assets\Data_position\Walk" + index + ".csv";
         timePosRotList = gameObject.GetComponent<ReadFromCSV>().ReadFromCSVFile(dir + reference);
 
-        // Set initial position and rotation
+        // Add 0.5f + time to y
+
+        // Disable components and add LineRenderer
         currentStartPoint = 0;
         for (int i = 0; i < humansGO.Length; ++i)
         {
+            positions[i] = new List<Vector3>[humansGO[i].Length];
             for (int j = 0; j < humansGO[i].Length; ++j)
             {
-                humansGO[i][j].SetActive(false);
-
+                // A shortcut to use the position data immediately for the LineRenderer
+                positions[i][j] = new List<Vector3>();
+                foreach (var position in timePosRotList[i][j].Item2)
+                {
+                    positions[i][j].Add(new Vector3(position.x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], position.z));
+                    currentStartPoint++;
+                }
+                currentStartPoint = 0;
+                
+                // Every component should be disabled except the humans game object, bc the LineRenderer operates there
+                humansGO[i][j].SetActive(true);
+                humansA[i][j].enabled = false;
                 humansNMA[i][j].enabled = false;
+
+                foreach(Transform child in humansGO[i][j].transform)
+                {
+                    child.gameObject.SetActive(false);
+                }
+
+                // Add the LineRenderer to the gameobject
+                LineRenderer drawPath = humansGO[i][j].AddComponent<LineRenderer>();
+                drawPath.material = new Material(Shader.Find("Sprites/Default"));
+                drawPath.startColor = color.color;
+                drawPath.endColor = color.color;
+                drawPath.startWidth = thickness;
+                drawPath.endWidth = thickness;
+
+                drawPath.positionCount = currentStartPoint + 1;
+                drawPath.SetPosition(currentStartPoint, positions[i][j][currentStartPoint]);
             }
         }
 
@@ -73,164 +108,81 @@ public class SpatialTimeSimulation : MonoBehaviour
         slider.wholeNumbers = true;
         prevSliderValue = slider.value;
 
-        //SetTransform(currentStartPoint);
-        //currentStartPoint++;
+        currentStartPoint++;
         slider.value = currentStartPoint;
         startTime = Time.time;
+    }
 
-        int count = 0;
-        // Create all cubes for every human and disable/enable them with time.
-        GameObject[][] cubes = new GameObject[humansGO.Length][];
-        for (int i = 0; i < cubes.Length; ++i)
+    private void FixedUpdate()
+    {
+        if (pause)
+            indexChangeRate = 0;
+        if (play)
+            indexChangeRate = 1;
+        if (rewind)
+            indexChangeRate = -1;
+
+        if (play || rewind)
         {
-            cubes[i] = new GameObject[humansGO[i].Length];
+            if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
+            {
+                if (slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
+                {
+                    prevSliderValue = slider.value;
+                    currentStartPoint += indexChangeRate;
+                    slider.value = currentStartPoint;
+                }
+                else
+                {
+                    prevSliderValue = slider.value;
+                    currentStartPoint = (int)slider.value;
+                }
+                SetTransform(currentStartPoint);
+            }
+            else if (slider.value - prevSliderValue != -1 || slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
+            {
+                currentStartPoint = (int)slider.value;
+            }
+        }
+        else
+        {
+            currentStartPoint = (int)slider.value;
+            SetTransform(currentStartPoint);
+        }
+    }
 
-            for (int j = 0; j < cubes[i].Length; ++j)
+    private void SetTransform(int currentStartPoint)
+    {
+        for (int i = 0; i < humansGO.Length; ++i)
+        {
+            for (int j = 0; j < humansGO[i].Length; ++j)
             {
-                currentStartPoint = 0;
-                while (currentStartPoint < timePosRotList[i][j].Item2.Count)
+                LineRenderer drawPath = humansGO[i][j].GetComponent<LineRenderer>();
+                
+                // if so ArgumentOutOfRangeException
+                if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
                 {
-                    //if(currentStartPoint == 0 || currentStartPoint >= timePosRotList[i][j].Item2.Count - 1)
-                    //{
-                    //    cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube);
-                    //    cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z);
-                    //    cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
-                    //    cubes[i][j].GetComponent<MeshRenderer>().material.color = Color.green;
-                    //    count++;
-                    //}
-                    //else if(currentStartPoint < timePosRotList[i][j].Item2.Count - 1)
-                    //{
-                    //    if (Vector3.Distance(timePosRotList[i][j].Item2[currentStartPoint - 1], timePosRotList[i][j].Item2[currentStartPoint]) <= 0.2f)
-                    //    {
-                    //        cubes[i][j] = GameObject.CreatePrimitive(PrimitiveType.Cube);
-                    //        cubes[i][j].transform.position = new Vector3(timePosRotList[i][j].Item2[currentStartPoint].x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], timePosRotList[i][j].Item2[currentStartPoint].z);
-                    //        cubes[i][j].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
-                    //        cubes[i][j].GetComponent<MeshRenderer>().material.color = Color.green;
-                    //        count++;
-                    //    }
-                    //}
-                    //currentStartPoint++;
-
-                    GameObject newPath = new GameObject();
-                    LineRenderer drawPath = newPath.AddComponent<LineRenderer>();
-                    drawPath.material = new Material(Shader.Find("Sprites/Default"));
-                    drawPath.startColor = Color.green;
-                    drawPath.endColor = Color.green;
-                    drawPath.startWidth = thickness;
-                    drawPath.endWidth = thickness;
-
-                    drawPath.positionCount = timePosRotList[i][j].Item2.Count;
-
-                    List<Vector3> newList = new List<Vector3>(); 
-                    foreach(var vec in timePosRotList[i][j].Item2)
+                    if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
                     {
-                        var z = new Vector3(vec.x, 0.5f + timePosRotList[i][j].Item1[currentStartPoint], vec.z);
-                        newList.Add(z);
-                        currentStartPoint++;
+                        drawPath.positionCount = positions[i][j].Count;
+                        drawPath.SetPositions(positions[i][j].ToArray());
                     }
-
-                    drawPath.SetPositions(newList.ToArray());
-
+                    continue;
+                }
+                
+                if (currentStartPoint < timePosRotList[i][j].Item2.Count)
+                {
+                    // We need to specify the Length of the positionCount variable
+                    drawPath.positionCount = currentStartPoint + 1;
+                    // We copy the positions list and remove every element after the currentStartPoint from the copy
+                    List<Vector3> tmpLst = new List<Vector3>(positions[i][j]);
+                    tmpLst.RemoveRange(currentStartPoint + 1, positions[i][j].Count - currentStartPoint - 1);
+                    drawPath.SetPositions(tmpLst.ToArray());
                 }
             }
         }
-        Debug.Log("Count: " + count);
-
     }
 
-    //private void FixedUpdate()
-    //{
-    //    if (pause)
-    //        indexChangeRate = 0;
-    //    if (play)
-    //        indexChangeRate = 1;
-    //    if (rewind)
-    //        indexChangeRate = -1;
-
-    //    if (play || rewind)
-    //    {
-    //        if ((currentStartPoint + indexChangeRate) < timePosRotList[maxIJ[0]][maxIJ[1]].Item2.Count && (currentStartPoint + indexChangeRate) >= 0)
-    //        {
-    //            if (slider.value - prevSliderValue == -1 || slider.value - prevSliderValue == 0 || slider.value - prevSliderValue == 1) // rewind || play
-    //            {
-    //                prevSliderValue = slider.value;
-    //                currentStartPoint += indexChangeRate;
-    //                slider.value = currentStartPoint;
-    //            }
-    //            else
-    //            {
-    //                prevSliderValue = slider.value;
-    //                currentStartPoint = (int)slider.value;
-    //            }
-    //            SetTransform(currentStartPoint);
-    //        }
-    //        else if (slider.value - prevSliderValue != -1 || slider.value - prevSliderValue != 0 || slider.value - prevSliderValue != 1)
-    //        {
-    //            currentStartPoint = (int)slider.value;
-    //        }
-    //    }
-    //    else
-    //    {
-    //        // upper else-Statement only for pause
-    //        if (slider.value - prevSliderValue != 0)
-    //        {
-    //            //prevSliderValue = slider.value;
-    //            currentStartPoint = (int)slider.value;
-    //        }
-
-    //        for (int i = 0; i < humansGO.Length; ++i)
-    //        {
-    //            for (int j = 0; j < humansGO[i].Length; ++j)
-    //            {
-    //                if (currentStartPoint < timePosRotList[i][j].Item2.Count)
-    //                {
-    //                    humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
-    //                    humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
-    //                }
-    //            }
-    //        }
-    //    }
-    //}
-
-    //private void SetTransform(int currentStartPoint)
-    //{
-    //    for (int i = 0; i < humansGO.Length; ++i)
-    //    {
-    //        for (int j = 0; j < humansGO[i].Length; ++j)
-    //        {
-    //            // if so ArgumentOutOfRangeException
-    //            if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
-    //            {
-    //                continue;
-    //            }
-
-    //            Vector3 startPos = timePosRotList[i][j].Item2[currentStartPoint];
-    //            Vector3 endPos = timePosRotList[i][j].Item2[currentStartPoint + indexChangeRate];
-    //            Quaternion startRot = timePosRotList[i][j].Item3[currentStartPoint];
-    //            Quaternion endRot = timePosRotList[i][j].Item3[currentStartPoint + indexChangeRate];
-
-    //            float distCovered = (Time.time - startTime) * timePosRotList[i][j].Item4[currentStartPoint];
-    //            float fracJourney = 1f;
-    //            float journeyLength = Vector3.Distance(startPos, endPos);
-    //            if (journeyLength != 0 && distCovered != 0) fracJourney = distCovered / journeyLength;
-
-    //            // Animation Idle, if startPoint == endPoint and startRot == endRot
-    //            //if (journeyLength <= 0 && startRot.Equals(endRot) || currentStartPoint == 0 || currentStartPoint == timePosRotList[i][j].Item2.Count)
-    //            //{
-    //            //    humansA[i][j].SetBool("isWalking", false);
-    //            //}
-    //            //else if (journeyLength > 0)
-    //            //{
-    //            //    humansA[i][j].SetBool("isWalking", true);
-    //            //}
-    //            //humansA[i][j].speed = 2*(journeyLength / 0.2f);
-
-    //            humansGO[i][j].transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
-    //            humansGO[i][j].transform.rotation = Quaternion.Lerp(startRot, endRot, fracJourney);
-    //        }
-    //    }
-    //}
-
     private int[] GetHumanWithLongestScreenTime()
     {
         int[] maxIJ = new int[2] { 0, 0 };

+ 19 - 1
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/WanderingAI/WalkLerpPlayback.cs

@@ -129,7 +129,19 @@ public class WalkLerpPlayback : MonoBehaviour
             {
                 for (int j = 0; j < humansGO[i].Length; ++j)
                 {
-                    if(currentStartPoint < timePosRotList[i][j].Item2.Count)
+                    if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
+                    {
+                        if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
+                        {
+                            humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
+                            humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
+                        }
+
+                        humansA[i][j].SetBool(isWalking, false);
+                        continue;
+                    }
+
+                    if (currentStartPoint < timePosRotList[i][j].Item2.Count)
                     {
                         humansGO[i][j].transform.position = timePosRotList[i][j].Item2[currentStartPoint]; // First entry of Position
                         humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[currentStartPoint]; // First entry of Rotation
@@ -151,6 +163,12 @@ public class WalkLerpPlayback : MonoBehaviour
                 // if so ArgumentOutOfRangeException
                 if ((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1) || (currentStartPoint + indexChangeRate) < 0)
                 {
+                    if((currentStartPoint + indexChangeRate) >= (timePosRotList[i][j].Item2.Count - 1))
+                    {
+                        humansGO[i][j].transform.position = timePosRotList[i][j].Item2[timePosRotList[i][j].Item2.Count - 1];
+                        humansGO[i][j].transform.rotation = timePosRotList[i][j].Item3[timePosRotList[i][j].Item3.Count - 1];
+                    }
+
                     humansA[i][j].SetBool(isWalking, false);
                     continue;
                 }

+ 40 - 1
testumgebung/CrowdModelling/Assets/Scenes/Origin_OnlyYears.unity

@@ -3884,6 +3884,7 @@ GameObject:
   - component: {fileID: 421502120}
   - component: {fileID: 421502119}
   - component: {fileID: 421502121}
+  - component: {fileID: 421502124}
   m_Layer: 0
   m_Name: 2020
   m_TagString: 2020
@@ -4049,6 +4050,24 @@ MonoBehaviour:
   targetScattering: 2
   leaveTimer: 20
   leavePosition: {x: 11, y: 0.5, z: 15}
+--- !u!114 &421502124
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 421502114}
+  m_Enabled: 0
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 3d37e36cd8c5bcf4fb9ba88f0470112b, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  rewind: 0
+  pause: 0
+  play: 0
+  slider: {fileID: 1594303656}
+  thickness: 0.1
+  color: {fileID: 2100000, guid: 3b8cef3dac064aa42b654ad713c8d001, type: 2}
 --- !u!1 &422840833
 GameObject:
   m_ObjectHideFlags: 0
@@ -11129,6 +11148,7 @@ GameObject:
   - component: {fileID: 946692990}
   - component: {fileID: 946692989}
   - component: {fileID: 946692991}
+  - component: {fileID: 946692994}
   m_Layer: 0
   m_Name: 2021
   m_TagString: 2021
@@ -11294,6 +11314,24 @@ MonoBehaviour:
   targetScattering: 2
   leaveTimer: 20
   leavePosition: {x: 11, y: 0.5, z: 15}
+--- !u!114 &946692994
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 946692984}
+  m_Enabled: 0
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 3d37e36cd8c5bcf4fb9ba88f0470112b, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  rewind: 0
+  pause: 0
+  play: 0
+  slider: {fileID: 1594303656}
+  thickness: 0.1
+  color: {fileID: 2100000, guid: 882a0d364bc4eb344a7f4c354a348be8, type: 2}
 --- !u!1 &954172724
 GameObject:
   m_ObjectHideFlags: 0
@@ -15046,7 +15084,7 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 4fc82bce38e12654e8a9b827cccab15f, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  interactable: 0
+  interactable: 1
   legend: {fileID: 1981258667}
 --- !u!114 &1295373008
 MonoBehaviour:
@@ -22881,6 +22919,7 @@ MonoBehaviour:
   play: 0
   slider: {fileID: 1594303656}
   thickness: 0.1
+  color: {fileID: 2100000, guid: f69cbde494105384fb66b1aa7d1fb814, type: 2}
 --- !u!1 &2071883079 stripped
 GameObject:
   m_CorrespondingSourceObject: {fileID: 8716330276743338538, guid: 968d356209aa0774eaf076909046744e,