Attention une mise à jour du service Gitlab va être effectuée le mardi 18 janvier (et non lundi 17 comme annoncé précédemment) entre 18h00 et 18h30. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes.

CamMvt.cs 17.9 KB
Newer Older
BRUNEAU Julien's avatar
BRUNEAU Julien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**
18
** Authors: Julien Bruneau, Tristan Le Bouffant, Alberto Jovane, Fabrice ATREVI
BRUNEAU Julien's avatar
BRUNEAU Julien committed
19
20
21
**
** Contact: crowd_group@inria.fr
*/
22

BRUNEAU Julien's avatar
BRUNEAU Julien committed
23
24
25
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
26
27
using UnityEngine.UI;
using System.IO;
BRUNEAU Julien's avatar
BRUNEAU Julien committed
28

JOVANE Alberto's avatar
JOVANE Alberto committed
29
30
public class CamMvt : MonoBehaviour
{
31
32
33
34
35
36
37
38
39
40
41
42

    #region camera modes name
    public const string DEFAULT = "Default";
    public const string FREE_MOVMENT = "Free_Movement";
    public const string FOLLOW = "Follow";
    public const string LOOK_AT = "Look_At";
    public const string FIRST_PERSON = "First_Person";
    public const string TORSUM = "Torsum";
    #endregion



JOVANE Alberto's avatar
JOVANE Alberto committed
43
    #region attributes
44
45
46
    // Parameters interface
    public GameObject panelMovCamera;            // panel with the camera parameters on the interface
    public Dropdown camSelect;                   // the downdrop of the camera parameters
JOVANE Alberto's avatar
JOVANE Alberto committed
47

48
49
    // Camera mode
    public string current_camera_mode = "empty"; // the current Mode 
JOVANE Alberto's avatar
JOVANE Alberto committed
50

51
52
    public Vector3 _default_position;            // default position defined by the config file
    public Vector3 _default_rotation;            // default rotation defined by the config file
53

JOVANE Alberto's avatar
JOVANE Alberto committed
54
55
    // Rotation - Looking at an agent
    public bool activateLookAt = false;          // if true: lock the view to look at the selected agent -  else : free movment
56
57
    public bool activateFollow = false;          // if true: follow the selected agent

JOVANE Alberto's avatar
JOVANE Alberto committed
58
59
60
61
62
63
64
65
66
    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
67
68
    public bool followOnX;                       // Lock axe X of the camera during camera's translation if false
    public bool followOnY;                       // Lock axe Z of the camera during camera's translation if false
JOVANE Alberto's avatar
JOVANE Alberto committed
69
70
71
72
73

    // 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 
74
    private string[] torsum_names = new string[] { "HIPS", "Hips", "hips", "Bip01 Spine1" }; //names of the torsum transform
JOVANE Alberto's avatar
JOVANE Alberto committed
75
76
77
    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)
78
79
80
81
    public bool smoothFirstPersonView = true;    // if true: the movment of the camera is smoothed on the y - else : the camera move with the head or torsum  
    private float smooth_y_head;                 // the y value to keep in first person view
    private float smooth_y_torsum;               // the y value to keep in torsum view

JOVANE Alberto's avatar
JOVANE Alberto committed
82
83
84
85
86
87
88

    // 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

89
90
91



JOVANE Alberto's avatar
JOVANE Alberto committed
92
    // Use this for initialization
BRUNEAU Julien's avatar
BRUNEAU Julien committed
93
94
    void Start()
    {
95
96
        panelMovCamera = GameObject.Find("/controlCanvas/cameraMovPanel");
        camSelect = panelMovCamera.transform.Find("cameraType").GetComponent<Dropdown>();
BRUNEAU Julien's avatar
BRUNEAU Julien committed
97
98
    }

JOVANE Alberto's avatar
JOVANE Alberto committed
99
    /// <summary>
JOVANE Alberto's avatar
JOVANE Alberto committed
100
    /// Initialize the targets of the camera movement with parameters of config file
BRUNEAU Julien's avatar
Master    
BRUNEAU Julien committed
101
    /// </summary>
JOVANE Alberto's avatar
JOVANE Alberto committed
102
103
    public void initializeTargetObjects()
    {
104
105
106
107
        //initial positions
        _default_position = transform.position;
        _default_rotation = transform.eulerAngles;

JOVANE Alberto's avatar
JOVANE Alberto committed
108
        // initialize the look at and follow target
JOVANE Alberto's avatar
JOVANE Alberto committed
109
110
        update_look_at_target();
        update_follow_target();
JOVANE Alberto's avatar
JOVANE Alberto committed
111
112
113

        // initialize the follow position with the starting camera position
        _position_follow = transform.position;
JOVANE Alberto's avatar
JOVANE Alberto committed
114
115
116
117
118
119
120
    }
    /// <summary>
    /// Update the target of look at camera
    /// </summary>
    public void update_look_at_target()
    {
        GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
121

122
        if (activateLookAt && lookAt_Id >= 0 && list.Length > 0)
JOVANE Alberto's avatar
JOVANE Alberto committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        {
            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");
141
        
142
        if (activateFollow && follow_Id >= 0 && list.Length > 0)
JOVANE Alberto's avatar
JOVANE Alberto committed
143
144
145
146
147
        {
            if (follow_Id > list.Length - 1)
                follow_Id = list.Length - 1;
            _previous_follow_Id = follow_Id;
            follow_Agent = list[follow_Id];
ATREVI Dieudonne's avatar
-    
ATREVI Dieudonne committed
148
            
JOVANE Alberto's avatar
JOVANE Alberto committed
149
150
151
152
153
154
155
156
157
158
159
            //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;
        }
BRUNEAU Julien's avatar
Master    
BRUNEAU Julien committed
160
    }
161
162
    public void readCameraMovParamOnScene()
    {
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
        activateLookAt        = panelMovCamera.transform.Find("Lookat").GetComponent<Toggle>().isOn;          
        activateFollow        = panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn;          
        lookAt_Id             = int.Parse(panelMovCamera.transform.Find("LookAgentID").GetComponent<InputField>().text);
        follow_Id             = int.Parse(panelMovCamera.transform.Find("FollowAgentID").GetComponent<InputField>().text);                 
        followOnX             = panelMovCamera.transform.Find("FollowX").GetComponent<Toggle>().isOn; 
        followOnY             = panelMovCamera.transform.Find("FollowY").GetComponent<Toggle>().isOn;
        lockFirstPersonView   = panelMovCamera.transform.Find("LockPerson").GetComponent<Toggle>().isOn;
        //smoothFirstPersonView = panelMovCamera.transform.Find("smoothPerson").GetComponent<Toggle>().isOn;
    }


    //It detects changes in the Current Camera Mode and intialize the parameters accordingly 
    private void check_mode_changes_and_initialize()
    {
        if(current_camera_mode != camSelect.options[camSelect.value].text)
        {
            //PREVIOUS CAMERA deinitialization
            if (current_camera_mode == DEFAULT)
            {
                //
            }
            else if (current_camera_mode == FREE_MOVMENT)
            {
                //
            }
            else if (current_camera_mode == FOLLOW)
            {
                activateFollow = false;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = false;
            }
            else if (current_camera_mode == LOOK_AT)
            {
                activateLookAt = false;
                panelMovCamera.transform.Find("Lookat").GetComponent<Toggle>().isOn = false;
            }
            else if (current_camera_mode == FIRST_PERSON)
            {
                activateFollow = false;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = false;
            }
            else if (current_camera_mode == TORSUM)
            {
                activateFollow = false;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = false;
            }


            current_camera_mode = camSelect.options[camSelect.value].text;


            //NEW CAMERA initialization
            if (current_camera_mode == DEFAULT)
            {
                //
            }
            else if(current_camera_mode == FREE_MOVMENT)
            {
                //
            }
            else if (current_camera_mode == FOLLOW)
            {
                //activate and set 0 id in case or not predefined id
                GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
                if (follow_Id < 0 && list.Length > 0)
                {
                    follow_Id = 0;
                    panelMovCamera.transform.Find("FollowAgentID").GetComponent<InputField>().text = follow_Id.ToString();
                }
                activateFollow = true;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = true;

                //set default rotation
                transform.eulerAngles = _default_rotation;
            }
            else if (current_camera_mode == LOOK_AT)
            {
                //activate and set 0 id in case or not predefined id
                GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
                if (lookAt_Id < 0 && list.Length > 0)
                {
                    lookAt_Id = 0;
                    panelMovCamera.transform.Find("LookAgentID").GetComponent<InputField>().text = lookAt_Id.ToString();
                }
                activateLookAt = true;
                panelMovCamera.transform.Find("Lookat").GetComponent<Toggle>().isOn = true;

                //set default position and rotation
                transform.eulerAngles = _default_rotation;
                transform.position = _default_position;

            }
            else if (current_camera_mode == FIRST_PERSON)
            {
                //activate and set 0 id in case or not predefined id
                GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
                if (follow_Id < 0 && list.Length > 0)
                {
                    follow_Id = 0;
                    panelMovCamera.transform.Find("FollowAgentID").GetComponent<InputField>().text = follow_Id.ToString();
                }
                activateFollow = true;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = true;

                //set default rotation
                update_follow_target();
                transform.forward = agent_reference_transform.forward;

                //initialize smooth value
                smooth_y_head = agent_head_transform.position.y;
            }
            else if (current_camera_mode == TORSUM)
            {
                //activate and set 0 id in case or not predefined id
                GameObject[] list = GameObject.FindGameObjectsWithTag("Player");
                if (follow_Id < 0 && list.Length > 0)
                {
                    follow_Id = 0;
                    panelMovCamera.transform.Find("FollowAgentID").GetComponent<InputField>().text = follow_Id.ToString();
                }
                activateFollow = true;
                panelMovCamera.transform.Find("Followat").GetComponent<Toggle>().isOn = true;
                
                //set default rotation
                update_follow_target();
                transform.forward = agent_reference_transform.forward;

                //initalize smooth value
                smooth_y_torsum = agent_torsum_transform.position.y;
            }
        }
293
    }
294
295
   
   // Update is called once per frame
BRUNEAU Julien's avatar
BRUNEAU Julien committed
296
    void Update()
JOVANE Alberto's avatar
JOVANE Alberto committed
297
298
299
300
301
    {
        #region UPDATE VALUES
        // ----------------------------
        // UPDATE VALUES
        // update follow id
302
        readCameraMovParamOnScene();
303
304
        check_mode_changes_and_initialize();

305
        if (follow_Id != _previous_follow_Id && activateFollow)
JOVANE Alberto's avatar
JOVANE Alberto committed
306
307
308
309
310
311
312
313
314
315
316
317
318
        {
            update_follow_target();
        }
        if (lookAt_Id != _previous_lookAt_Id && activateLookAt)
        {
            update_look_at_target();
        }
        #endregion


        #region ROTATION
        // ------------------------------
        // ROTATION - LOOKING AT AN AGENT
319
        if(current_camera_mode == DEFAULT)
320
321
322
        {
            transform.eulerAngles = _default_rotation;
        }
323
        if(current_camera_mode == LOOK_AT)
JOVANE Alberto's avatar
JOVANE Alberto committed
324
        {
325
            if (lookAt_Agent != null && activateLookAt)
326
                transform.LookAt(lookAt_Agent.transform);
JOVANE Alberto's avatar
JOVANE Alberto committed
327
        }
328
329
330
331
332
333
334
335
336
337
        if (current_camera_mode == FIRST_PERSON)
        {
            if (lockFirstPersonView)
                transform.forward = agent_reference_transform.forward;
        }
        else if (current_camera_mode == TORSUM)
        {
            if (lockFirstPersonView)
                transform.forward = agent_reference_transform.forward;
        }
JOVANE Alberto's avatar
JOVANE Alberto committed
338
339
340
341

        #endregion

        #region TRANSLATION
342
        if (current_camera_mode == DEFAULT)
343
344
345
346
        {
            transform.position = _default_position;
        }
        if (follow_Agent != null && activateFollow)
JOVANE Alberto's avatar
JOVANE Alberto committed
347
        {
BRUNEAU Julien's avatar
Master    
BRUNEAU Julien committed
348
349
            Vector3 delta = follow_Agent.transform.position - follow_LastPosition;
            follow_LastPosition = follow_Agent.transform.position;
350
            if (!followOnX){
351
                delta.x = 0;
352
353
354
355
            }
            if (!followOnY){
                delta.z = 0;
            }
JOVANE Alberto's avatar
JOVANE Alberto committed
356
            _position_follow = _position_follow + delta;
JOVANE Alberto's avatar
JOVANE Alberto committed
357
358
359
360
361
362
363

            _position_first_person = agent_head_transform.position;

            _position_torsum = agent_torsum_transform.position;

            // --------------------------------
            // TRANSLATION - FOLLOWING AN AGENT
364

365
            if (current_camera_mode == FOLLOW)
366
367
368
            {
                transform.position = _position_follow;
            }
369
            else if (current_camera_mode == FIRST_PERSON)
370
            {
371
372
373
374
                if (smoothFirstPersonView)
                    transform.position = new Vector3(_position_first_person.x, smooth_y_head, _position_first_person.z);
                else
                    transform.position = _position_first_person;
375
            }
376
            else if (current_camera_mode == TORSUM)
JOVANE Alberto's avatar
JOVANE Alberto committed
377
            {
378
379
380
381
                if (smoothFirstPersonView)
                    transform.position = new Vector3(_position_torsum.x, smooth_y_torsum, _position_torsum.z);
                else
                    transform.position = _position_torsum;
JOVANE Alberto's avatar
JOVANE Alberto committed
382
383
384
            }
        }
        #endregion
385
386
    }

JOVANE Alberto's avatar
JOVANE Alberto committed
387
    #region Additional Functions
388
389
    //additional functions

JOVANE Alberto's avatar
JOVANE Alberto committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
    /// <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;
BRUNEAU Julien's avatar
BRUNEAU Julien committed
432
    }
JOVANE Alberto's avatar
JOVANE Alberto committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

    /// <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;
        }
464
    }
JOVANE Alberto's avatar
JOVANE Alberto committed
465
    #endregion
466
}