-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRemoteTransformations.cs
127 lines (113 loc) · 4.6 KB
/
RemoteTransformations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**************************************************************************
* Copyright (C) echoAR, Inc. 2018-2020. *
* echoAR, Inc. proprietary and confidential. *
* *
* Use subject to the terms of the Terms of Service available at *
* https://www.echoar.xyz/terms, or another agreement *
* between echoAR, Inc. and you, your company or other organization. *
***************************************************************************/
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using SimpleJSON;
using System.Globalization;
using UnityEngine.Video;
public class RemoteTransformations : MonoBehaviour
{
[HideInInspector]
public Entry entry;
/// <summary>
/// EXAMPLE BEHAVIOUR
/// Queries the database and transforms the object based on the result.
/// </summary>
// Initial transformation
private Vector3 initialWorldSpacePosition;
private Quaternion initialWorldSpaceRotation;
private Vector3 initialScale;
// Use this for initialization
void Start()
{
try
{
// Set initial transfomation
initialWorldSpacePosition = (this.gameObject.transform.parent) ? this.gameObject.transform.parent.transform.position : this.gameObject.transform.position;
initialWorldSpaceRotation = this.gameObject.transform.rotation;
initialScale = this.gameObject.transform.localScale;
}
catch (System.Exception e)
{
Debug.Log(e);
}
string value = "";
// Handle spinning
float speed = 150;
if (entry.getAdditionalData().TryGetValue("speed", out value))
{
speed *= float.Parse(value, CultureInfo.InvariantCulture);
}
float offset = 0;
if (entry.getAdditionalData().TryGetValue("direction", out value))
{
if (value.Equals("right"))
offset += Time.time % 360 * speed;
else
offset -= Time.time % 360 * speed;
}
// Handle rotation
Quaternion targetQuaternion = initialWorldSpaceRotation;
float x = 0, y = 0, z = 0;
if (entry.getAdditionalData().TryGetValue("xAngle", out value))
{
x = float.Parse(value, CultureInfo.InvariantCulture);
}
if (entry.getAdditionalData().TryGetValue("yAngle", out value))
{
y = float.Parse(value, CultureInfo.InvariantCulture);
}
if (entry.getAdditionalData().TryGetValue("zAngle", out value))
{
z = float.Parse(value, CultureInfo.InvariantCulture);
}
this.gameObject.transform.rotation = Quaternion.Euler(x, y + offset, z);
}
public void Update()
{
string value = "";
float positionFactor = 1f;
// Attach to parent
if (this.gameObject.transform.parent) {
initialWorldSpacePosition = this.gameObject.transform.parent.transform.position;
positionFactor = this.gameObject.transform.parent.transform.localScale.magnitude;
}
// Handle translation
Vector3 positionOffest = Vector3.zero;
if (entry.getAdditionalData().TryGetValue("x", out value))
{
positionOffest.x = float.Parse(value, CultureInfo.InvariantCulture);
}
if (entry.getAdditionalData().TryGetValue("y", out value))
{
positionOffest.y = float.Parse(value, CultureInfo.InvariantCulture);
}
if (entry.getAdditionalData().TryGetValue("z", out value))
{
positionOffest.z = float.Parse(value, CultureInfo.InvariantCulture);
}
this.gameObject.transform.position = initialWorldSpacePosition + positionOffest * positionFactor;
// Handle Scale
float scaleFactor = 1f;
if (entry.getAdditionalData().TryGetValue("scale", out value))
{
scaleFactor = float.Parse(value, CultureInfo.InvariantCulture);
}
this.gameObject.transform.localScale = initialScale * scaleFactor;
// Mute
bool mute = false;
if (entry.getAdditionalData() != null && entry.getAdditionalData().TryGetValue("mute", out value)) {
mute = value.Equals("true") ? true : false;
}
VideoPlayer videoPlayer = this.GetComponent<VideoPlayer>();
for (ushort i = 0; videoPlayer != null && i < videoPlayer.controlledAudioTrackCount; ++i)
videoPlayer.SetDirectAudioMute(i, mute);
}
}