AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LineObjectSwarm.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.UX
8 {
9  [ExecuteInEditMode]
10  public class LineObjectSwarm : MonoBehaviour
11  {
12  const int RandomValueResolution = 1024;
13 
14  public List<Transform> Objects = new List<Transform>();
15 
16  [Range(0, 100)]
17  public int Seed = 0;
18 
19  [SerializeField]
20  private LineBase source;
21 
22  [Header("Noise Settings")]
23  public float ScaleMultiplier = 10f;
24  public float SpeedMultiplier = 1f;
25  public float StrengthMultiplier = 0.5f;
26  public Vector3 AxisStrength = Vector3.one;
27  public Vector3 AxisSpeed = Vector3.one;
28  public Vector3 AxisOffset = Vector3.zero;
29 
30  private Vector3[] prevPoints;
31  private System.Random randomPosition;
32  private System.Random randomRotation;
33  private FastSimplexNoise noise = new FastSimplexNoise();
34 
35  [Header("Swarm Settings")]
36  [Range(0f, 1f)]
37  public float NormalizedDistance = 0f;
38 
39  public Vector3 SwarmScale = Vector3.one;
40 
41  public AnimationCurve ObjectScale = AnimationCurve.Linear(0f, 1f, 1f, 1f);
42 
43  public AnimationCurve ObjectOffset = AnimationCurve.Linear(0f, 0f, 1f, 0f);
44 
45  public RotationTypeEnum RotationTypeOverride = RotationTypeEnum.None;
46 
47  public bool SwarmVelocities = true;
48 
49  public float VelocityBlend = 0.5f;
50 
51  public Vector3 RotationOffset = Vector3.zero;
52 
53  public Vector3 AxisScale = Vector3.one;
54 
55  public virtual LineBase Source
56  {
57  get
58  {
59  if (source == null)
60  {
61  source = GetComponent<LineBase>();
62  }
63  return source;
64  }
65  set
66  {
67  source = value;
68  if (source == null)
69  {
70  enabled = false;
71  }
72  }
73  }
74 
75  public Vector3 GetRandomPoint()
76  {
77  Vector3 randomPoint = Vector3.one;
78  randomPoint.x = (float)randomPosition.Next(-RandomValueResolution, RandomValueResolution) / (RandomValueResolution * 2);
79  randomPoint.y = (float)randomPosition.Next(-RandomValueResolution, RandomValueResolution) / (RandomValueResolution * 2);
80  randomPoint.z = (float)randomPosition.Next(-RandomValueResolution, RandomValueResolution) / (RandomValueResolution * 2);
81 
82  return Vector3.Scale(randomPoint, SwarmScale);
83  }
84 
85  public void Update()
86  {
87  UpdateCollection();
88 
89 #if UNITY_EDITOR
90  UnityEditor.EditorUtility.SetDirty(gameObject);
91 #endif
92  }
93 
94  public void UpdateCollection()
95  {
96  if (Source == null)
97  {
98  return;
99  }
100 
101  if (prevPoints == null || prevPoints.Length != Objects.Count)
102  {
103  prevPoints = new Vector3[Objects.Count];
104  }
105 
106  randomPosition = new System.Random(Seed);
107  Vector3 linePoint = source.GetPoint(NormalizedDistance);
108  Quaternion lineRotation = source.GetRotation(NormalizedDistance, RotationTypeOverride);
109 
110  for (int i = 0; i < Objects.Count; i++)
111  {
112  if (Objects[i] == null)
113  {
114  continue;
115  }
116 
117  Vector3 point = source.transform.TransformVector(GetRandomPoint());
118  point.x = (float)(point.x + (noise.Evaluate((point.x + AxisOffset.x) * ScaleMultiplier, Time.unscaledTime * AxisSpeed.x * SpeedMultiplier)) * AxisStrength.x * StrengthMultiplier);
119  point.y = (float)(point.y + (noise.Evaluate((point.y + AxisOffset.y) * ScaleMultiplier, Time.unscaledTime * AxisSpeed.y * SpeedMultiplier)) * AxisStrength.y * StrengthMultiplier);
120  point.z = (float)(point.z + (noise.Evaluate((point.z + AxisOffset.z) * ScaleMultiplier, Time.unscaledTime * AxisSpeed.z * SpeedMultiplier)) * AxisStrength.z * StrengthMultiplier);
121 
122  Objects[i].position = point + linePoint;
123  if (SwarmVelocities)
124  {
125  Vector3 velocity = prevPoints[i] - point;
126  Objects[i].rotation = Quaternion.Lerp(lineRotation, Quaternion.LookRotation(velocity, Vector3.up), VelocityBlend);
127  }
128  else
129  {
130  Objects[i].rotation = lineRotation;
131  }
132  Objects[i].Rotate(RotationOffset);
133 
134  prevPoints[i] = point;
135  }
136  }
137  }
138 }
RotationTypeEnum
Default options for getting a rotation along a line
Definition: LineUtility.cs:20
A conglomeration of open-source simplex libraries in C# with an emphasis on performance ...
double Evaluate(double x, double y)
Quaternion GetRotation(float normalizedLength, RotationTypeEnum rotationType=RotationTypeEnum.None)
Gets the rotation of a point along the line at the specified length
Definition: LineBase.cs:232
Vector3 GetPoint(float normalizedLength)
Gets a point along the line at the specified length
Definition: LineBase.cs:291