Commit 76e4ddb8 authored by JOVANE Alberto's avatar JOVANE Alberto
Browse files

Camera Updates

parent 65627464
......@@ -163,7 +163,7 @@ public class LoadEnv : MonoBehaviour
i++;
}
cam_Movement.updateTargetedObject();
cam_Movement.initializeTargetObjects();
cam_fc.OnSceneChange();
}
......@@ -460,7 +460,7 @@ public class LoadEnv : MonoBehaviour
Camera.main.GetComponent<CamRecorder>().Init();
InitDeepRecorders();
cam_Movement.updateTargetedObject();
cam_Movement.initializeTargetObjects();
cam_fc.OnSceneChange();
}
......
......@@ -15,7 +15,7 @@
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Authors: Julien Bruneau, Tristan Le Bouffant
** Authors: Julien Bruneau, Tristan Le Bouffant, Alberto Jovane
**
** Contact: crowd_group@inria.fr
*/
......@@ -24,74 +24,276 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMvt : MonoBehaviour
{
#region attributes
// Rotation - Looking at an agent
public int lookAt_Id; // Id of the agent to look at
private GameObject lookAt_Agent = null; // Agent to look at
// Translation - Following an agent
public int follow_Id; // Id of the agent to follow
private GameObject follow_Agent; // Agent to follow
private Vector3 follow_LastPosition; // Last position of the agent to follow
public bool follow_LockX; // Lock axe X of the camera during camera's translation
public bool follow_LockZ; // Lock axe Z of the camera during camera's translation
#endregion
// Use this for initialization
public class CamMvt : MonoBehaviour
{
#region attributes
// different camera types
public enum CameraType
{
Default, //the option defined by the scenario
Follow, // follow the agent if defined by the scenario
First_Person, // First Person View
Torsum // Torsum view
}
[SerializeField()]
private CameraType _camera_type;
// Rotation - Looking at an agent
public bool activateLookAt = false; // if true: lock the view to look at the selected agent - else : free movment
private int _previous_lookAt_Id; // Id of the previous agent to look at
public int lookAt_Id; // Id of the agent to look at
private GameObject lookAt_Agent = null; // Agent to look at
// Translation - Following an agent
public int follow_Id; // Id of the agent to follow or to
private int _previous_follow_Id; // Id of the previous agent to follow
private GameObject follow_Agent; // Agent to follow
private Vector3 follow_LastPosition; // Last position of the agent to follow
public bool follow_LockX; // Lock axe X of the camera during camera's translation
public bool follow_LockZ; // Lock axe Z of the camera during camera's translation
// Head and Torsum references
private Transform agent_head_transform; // transform reference of the head of the current followID agent
private string[] head_names = new string[] { "HEAD", "Head", "head" }; //names of the head transform
private Transform agent_torsum_transform; // transform reference of the head of the torsum followID agent
private string[] torsum_names = new string[] { "HIPS", "Hips", "hips" }; //names of the torsum transform
private Transform agent_reference_transform; // transform reference of the head of the torsum followID agent
public bool lockFirstPersonView = false; // if true: lock the view - else (default) : the user can move the view (mouse right click + movment)
// values holding the updates at run time
private Vector3 _position_follow; // the current follow position
private Vector3 _position_first_person; // the current first person position
private Vector3 _position_torsum; // the current torsum position
#endregion
// Use this for initialization
void Start()
{
_camera_type = CameraType.Default;
}
/// <summary>
/// Update the target of the camera movement
/// <summary>
/// Initialize the targets of the camera movement
/// </summary>
public void updateTargetedObject()
{
GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
if (lookAt_Id >= 0 && list.Length > 0)
{
if (lookAt_Id > list.Length - 1)
lookAt_Id = list.Length - 1;
lookAt_Agent = list[lookAt_Id];
}
if (follow_Id >= 0 && list.Length > 0)
{
if (follow_Id > list.Length - 1)
follow_Id = list.Length - 1;
follow_Agent = list[follow_Id];
follow_LastPosition = follow_Agent.transform.position;
}
public void initializeTargetObjects()
{
//default setting
if (lookAt_Id >= 0)
activateLookAt = true;
else
{
activateLookAt = false;
lookAt_Id = 0;
}
update_look_at_target();
update_follow_target();
}
/// <summary>
/// Update the target of look at camera
/// </summary>
public void update_look_at_target()
{
GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
if (activateLookAt && lookAt_Id >= 0)
{
if (lookAt_Id > list.Length - 1)
lookAt_Id = list.Length - 1;
_previous_lookAt_Id = lookAt_Id;
lookAt_Agent = list[lookAt_Id];
}
else
{
lookAt_Id = -1;
_previous_lookAt_Id = lookAt_Id;
}
}
/// <summary>
/// Update the target of the follow camera and of the first_person/torsum camera
/// </summary>
public void update_follow_target()
{
GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
if (follow_Id >= 0 && list.Length > 0)
{
if (follow_Id > list.Length - 1)
follow_Id = list.Length - 1;
_previous_follow_Id = follow_Id;
follow_Agent = list[follow_Id];
//initialize the positions
follow_LastPosition = follow_Agent.transform.position;
agent_head_transform = get_transform_reference_of_head(follow_Agent);
agent_torsum_transform = get_transform_reference_of_hips(follow_Agent);
agent_reference_transform = follow_Agent.transform.GetChild(0);
}
else
{
follow_Id = -1;
_previous_follow_Id = follow_Id;
}
}
// Update is called once per frame
void Update()
{
#region ROTATION
// ------------------------------
// ROTATION - LOOKING AT AN AGENT
if (lookAt_Agent != null)
{
#region UPDATE VALUES
// ----------------------------
// UPDATE VALUES
// update follow id
if (follow_Id != _previous_follow_Id)
{
update_follow_target();
}
if (lookAt_Id != _previous_lookAt_Id && activateLookAt)
{
update_look_at_target();
}
#endregion
#region ROTATION
// ------------------------------
// ROTATION - LOOKING AT AN AGENT
if (lookAt_Agent != null && activateLookAt)
{
transform.LookAt(lookAt_Agent.transform);
#endregion
#region TRANSLATION
// --------------------------------
// TRANSLATION - FOLLOWING AN AGENT
if (follow_Agent != null)
{
}
if (lockFirstPersonView)
{
if (_camera_type == CameraType.First_Person)
{
transform.forward = agent_reference_transform.forward;
}
else if (_camera_type == CameraType.Torsum)
{
transform.forward = agent_reference_transform.forward;
}
}
#endregion
#region TRANSLATION
if (follow_Agent != null)
{
Vector3 delta = follow_Agent.transform.position - follow_LastPosition;
follow_LastPosition = follow_Agent.transform.position;
if (follow_LockX)
delta.x = 0;
if (follow_LockZ)
delta.z = 0;
transform.position = transform.position + delta;
}
#endregion
_position_follow = transform.position + delta;
_position_first_person = agent_head_transform.position;
_position_torsum = agent_torsum_transform.position;
// --------------------------------
// TRANSLATION - FOLLOWING AN AGENT
if (_camera_type == CameraType.Follow && _camera_type == CameraType.Default)
{
transform.position = _position_follow;
}
else if (_camera_type == CameraType.First_Person)
{
transform.position = _position_first_person;
}
else if (_camera_type == CameraType.Torsum)
{
transform.position = _position_torsum;
}
}
#endregion
}
#region Additional Functions
//additional functions
/// <summary>
/// Find the head transform and return it
/// </summary>
/// <param name="agent">the agent gameObject</param>
/// <returns> The reference of the trasform of the head if found, otherwise null </returns>
private Transform get_transform_reference_of_head(GameObject agent, bool general_approach = true)
{
Transform result;
if (general_approach)
{
//general approach work with all the
result = recursive_find_names(agent.transform, head_names);
}
else
{
//specific approach if the structure is different crash
result = agent.transform.GetChild(0).GetChild(0).GetChild(2).GetChild(0).GetChild(0).GetChild(0).GetChild(1).GetChild(0);
}
return result;
}
/// <summary>
/// Find the head transform and return it
/// </summary>
/// <param name="agent">the agent gameObject</param>
/// <returns> The reference of the trasform of the head if found, otherwise null </returns>
private Transform get_transform_reference_of_hips(GameObject agent, bool general_approach = true)
{
Transform result;
if (general_approach)
{
//general approach work with all the
result = recursive_find_names(agent.transform, torsum_names);
}
else
{
//specific approach if the structure is different crash
result = agent.transform.GetChild(0).GetChild(0);
}
return result;
}
/// <summary>
/// recursive look for "names" in child hierrarchy
/// </summary>
/// <param name="parent"> the transform, starting point of the look for head child </param>
/// <param name="names"> the list of names to look for</param>
/// <returns>The reference of the trasform of the head if found, otherwise null</returns>
private Transform recursive_find_names(Transform parent, string[] names)
{
if (parent.childCount == 0)
return null;
else
{
foreach (Transform child in parent.transform)
{
bool contains = false;
foreach (string name in names)
{
contains = contains || child.name.Contains(name);
}
if (contains)
return child;
}
foreach (Transform child in parent.transform)
{
Transform result = recursive_find_names(child, names);
if (result != null)
return result;
}
return null;
}
}
#endregion
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment