Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
GameObjectScatterPlotDisplayer.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
5 using UnityEngine;
6 
7 namespace droid.Runtime.Prototyping.Displayers.ScatterPlots {
8  [ExecuteInEditMode]
9  [AddComponentMenu(DisplayerComponentMenuPath._ComponentMenuPath
10  + "GameObjectScatterPlotDisplayer"
11  + DisplayerComponentMenuPath._Postfix)]
13  [SerializeField] Gradient _gradient;
14  ParticleSystem _particle_system;
15 
16  ParticleSystem.MainModule _particle_system_main_module;
17  ParticleSystemRenderer _particle_system_renderer;
18 
19  [SerializeField]
20  ParticleSystemSimulationSpace _particle_system_simulation_space = ParticleSystemSimulationSpace.World;
21 
22  ParticleSystem.Particle[] _particles;
23  [SerializeField] float _size = 0.6f;
24 
25  List<float> _vs = new List<float>();
26 
27  protected override void Setup() {
28  this._particle_system = this.GetComponent<ParticleSystem>();
29  var em = this._particle_system.emission;
30  em.enabled = false;
31  em.rateOverTime = 0;
32  var sh = this._particle_system.shape;
33  sh.enabled = false;
34 
35  this._particle_system_main_module = this._particle_system.main;
36  this._particle_system_main_module.loop = false;
37  this._particle_system_main_module.playOnAwake = false;
38  this._particle_system_main_module.simulationSpace = this._particle_system_simulation_space;
39  this._particle_system_main_module.simulationSpeed = 0;
40  this._particle_system_main_module.startSize = this._size;
41 
42  this._particle_system_renderer = this.GetComponent<ParticleSystemRenderer>();
43  //this._particle_system_renderer.renderMode = ParticleSystemRenderMode.Mesh;
44  this._particle_system_renderer.alignment = ParticleSystemRenderSpace.World;
45 
46  if (this._gradient == null) {
47  this._gradient = new Gradient {
48  colorKeys = new[] {
49  new GradientColorKey(new Color(1, 0, 0), 0f),
50  new GradientColorKey(new Color(0, 1, 0), 1f)
51  }
52  };
53  }
54  }
55 
56  public override void Display(Double value) {
57  #if NEODROID_DEBUG
58  if (this.Debugging) {
59  Debug.Log("Applying the double " + value + " To " + this.name);
60  }
61  #endif
62 
63  this._Values = new[] {(float)value};
64  this.PlotSeries(this._Values);
65  }
66 
67  public override void Display(float[] values) {
68  #if NEODROID_DEBUG
69  if (this.Debugging) {
70  var s = "";
71  foreach (var value in values) {
72  s += $"{value},";
73  }
74 
75  Debug.Log("Applying the float array " + s + " To " + this.name);
76  }
77  #endif
78  this._Values = values;
79  this.PlotSeries(values);
80  }
81 
82  public override void Display(String values) {
83  #if NEODROID_DEBUG
84  if (this.Debugging) {
85  Debug.Log("Applying the float array " + values + " To " + this.name);
86  }
87  #endif
88 
89  this._vs.Clear();
90  foreach (var value in values.Split(',')) {
91  this._vs.Add(float.Parse(value));
92  }
93 
94  this._Values = this._vs.ToArray();
95  this.PlotSeries(this._Values);
96  }
97 
98  public override void Display(Vector3 value) { throw new NotImplementedException(); }
99  public override void Display(Vector3[] value) { this.ScatterPlot(value); }
100 
101  public override void Display(Points.ValuePoint points) { this.PlotSeries(new[] {points}); }
102 
103  public override void Display(Points.ValuePoint[] points) {
104  if (this._particles == null || this._particles.Length != points.Length) {
105  this._particles = new ParticleSystem.Particle[points.Length];
106  }
107 
108  #if NEODROID_DEBUG
109  if (this.Debugging) {
110  var points_str = points.Aggregate("",
111  (current, point) =>
112  current
113  + $"({point._Pos.ToString()}, {point._Val},{point._Size})"
114  + ", ");
115  Debug.Log("Applying the points " + points_str + " to " + this.name);
116  }
117  #endif
118 
119  var i = 0;
120  foreach (var point in points) {
121  this._particles[i].remainingLifetime = 100000;
122  this._particles[i].position = point._Pos;
123  var clamped = Math.Min(Math.Max(0.0f, point._Val), 1.0f);
124  this._particles[i].startColor = this._gradient.Evaluate(clamped);
125  this._particles[i].startSize = point._Size;
126  i++;
127  }
128 
129  this._particle_system.SetParticles(this._particles, points.Length);
130  }
131 
132  public override void Display(Points.StringPoint point) { throw new NotImplementedException(); }
133  public override void Display(Points.StringPoint[] points) { throw new NotImplementedException(); }
134 
135  //public override void Display(Object o) { throw new NotImplementedException(); }
136 
137  public override void Display(float values) {
138  #if NEODROID_DEBUG
139  if (this.Debugging) {
140  Debug.Log("Applying the float " + values + " To " + this.name);
141  }
142  #endif
143 
144  this._Values = new[] {values};
145  this.PlotSeries(this._Values);
146  }
147 
151  public void ScatterPlot(Vector3[] points) {
152  if (this._particles == null || this._particles.Length != points.Length) {
153  this._particles = new ParticleSystem.Particle[points.Length];
154  }
155 
156  #if NEODROID_DEBUG
157  if (this.Debugging) {
158  var points_str = points.Aggregate("", (current, point) => current + point.ToString() + ", ");
159  Debug.Log("Applying the points " + points_str + " To " + this.name);
160  }
161  #endif
162 
163  var i = 0;
164  var l = (float)points.Length;
165  foreach (var point in points) {
166  this._particles[i].remainingLifetime = 100000;
167  this._particles[i].position = point;
168  var clamped = Math.Min(Math.Max(0.0f, i / l), 1.0f);
169  this._particles[i].startColor = this._gradient.Evaluate(clamped);
170  this._particles[i].startSize = this._size;
171  i++;
172  }
173 
174  this._particle_system.SetParticles(this._particles, points.Length);
175  }
176 
177  public void PlotSeries(float[] points) {
178  if (this._particles == null || this._particles.Length != points.Length) {
179  this._particles = new ParticleSystem.Particle[points.Length];
180  }
181 
182  #if NEODROID_DEBUG
183  if (this.Debugging) {
184  Debug.Log("Applying the series " + points + " To " + this.name);
185  }
186  #endif
187 
188  var i = 0;
189  foreach (var point in points) {
190  this._particles[i].remainingLifetime = 100000;
191  this._particles[i].position = Vector3.one * i;
192  var clamped = Math.Min(Math.Max(0.0f, point), 1.0f);
193  this._particles[i].startColor = this._gradient.Evaluate(clamped);
194  this._particles[i].startSize = this._size;
195  i++;
196  }
197 
198  this._particle_system.SetParticles(this._particles, points.Length);
199  }
200 
204  public override void PlotSeries(Points.ValuePoint[] points) {
205  var alive = this._particle_system.GetParticles(this._particles);
206  if (alive < points.Length) {
207  this._particles = new ParticleSystem.Particle[points.Length];
208  }
209 
210  var i = 0;
211  foreach (var point in points) {
212  this._particles[i].remainingLifetime = 100000;
213  this._particles[i].position = point._Pos;
214  this._particles[i].startColor = this._gradient.Evaluate(point._Val);
215  this._particles[i].startSize = point._Size;
216  i++;
217  }
218 
219  this._particle_system.SetParticles(this._particles, points.Length);
220  }
221  }
222 }