Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ECSScatterPlotDisplayer.cs
Go to the documentation of this file.
1 #if ECS_EXISTS
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using UnityEngine;
6 
7 namespace droid.Neodroid.Prototyping.Displayers.ECS {
8  [ExecuteInEditMode]
9  [AddComponentMenu("Neodroid/Displayers/ECSScatterPlot")]
10  [RequireComponent(typeof(ParticleSystem))]
11  public class EcsScatterPlotDisplayer : EcsDisplayer {
12  ParticleSystem _particle_system;
13  ParticleSystemRenderer _particle_system_renderer;
14  [SerializeField]ParticleSystemSimulationSpace _particle_system_simulation_space =
15  ParticleSystemSimulationSpace.World;
16  ParticleSystem.MainModule _particle_system_main_module;
17  ParticleSystem.Particle[] _particles;
18  [SerializeField] float[] _values;
19  [SerializeField] Gradient _gradient;
20  [SerializeField] float _size = 0.6f;
21  [SerializeField] bool _plot_random_series = false;
22 
23  protected override void Setup() {
24  this._particle_system = this.GetComponent<ParticleSystem>();
25  var em = this._particle_system.emission;
26  em.enabled = false;
27  em.rateOverTime = 0;
28  var sh = this._particle_system.shape;
29  sh.enabled = false;
30 
31  this._particle_system_main_module = this._particle_system.main;
32  this._particle_system_main_module.loop = false;
33  this._particle_system_main_module.playOnAwake = false;
34  this._particle_system_main_module.simulationSpace = this._particle_system_simulation_space;
35  this._particle_system_main_module.simulationSpeed = 0;
36  this._particle_system_main_module.startSize = this._size;
37 
38  this._particle_system_renderer = this.GetComponent<ParticleSystemRenderer>();
39  //this._particle_system_renderer.renderMode = ParticleSystemRenderMode.Mesh;
40  this._particle_system_renderer.alignment = ParticleSystemRenderSpace.World;
41 
42 
43 
44  this._gradient = new Gradient {
45  colorKeys = new[] {
46  new GradientColorKey(new Color(1, 0, 0), 0f),
47  new GradientColorKey(new Color(0, 1, 0), 1f)
48  }
49  };
50  }
51 
52  protected override void Awake() {
53  base.Awake();
54  this.Setup();
55  }
56 
57  public struct ValuePoint {
58  public Vector3 _Pos;
59  public float _Size;
60  public float _Val;
61 
62  public ValuePoint(Vector3 pos, float val, float size) {
63  this._Pos = pos;
64  this._Val = val;
65  this._Size = size;
66  }
67  }
68 
69  public override void Display(Double value) {
70  #if NEODROID_DEBUG
71  if (this.Debugging)
72  print("Applying the double " + value + " To " + this.name);
73 
74  #endif
75  this._values = new[] {(float)value};
76  this.ScatterPlot(this._values);
77  }
78 
79  public override void Display(float[] values) {
80  #if NEODROID_DEBUG
81  if (this.Debugging) {
82  var s = "";
83  foreach (var value in values) {
84  s += $"{value},";
85  }
86 
87 
88  print("Applying the float array " + s + " To " + this.name);
89  }
90  #endif
91 
92  this._values = values;
93  this.ScatterPlot(values);
94  }
95 
96  public override void Display(String values) {
97  #if NEODROID_DEBUG
98  if (this.Debugging) {
99  Debug.Log("Applying the float array " + values + " To " + this.name);
100  }
101  #endif
102 
103  var vs = new List<float>();
104  foreach (var value in values.Split(',')) {
105  vs.Add(float.Parse(value));
106  }
107 
108  this._values = vs.ToArray();
109  this.ScatterPlot(this._values);
110  }
111 
112  public override void Display(float values) {
113  #if NEODROID_DEBUG
114  if (this.Debugging)
115  print("Applying the float " + values + " To " + this.name);
116 #endif
117  this._values = new[] {values};
118  this.ScatterPlot(this._values);
119  }
120 
121  public void ScatterPlot(float[] points) {
122  if(this._particles == null || this._particles.Length != points.Length)
123  this._particles = new ParticleSystem.Particle[points.Length];
124  var i = 0;
125  foreach (var point in points) {
126  this._particles[i].remainingLifetime = 100000;
127  this._particles[i].position = Vector3.one * i;
128  var clamped = Math.Min(Math.Max(0.0f, point), 1.0f);
129  this._particles[i].startColor = this._gradient.Evaluate(clamped);
130  this._particles[i].startSize = 1f;
131  i++;
132  }
133 
134  this._particle_system.SetParticles(this._particles, points.Length);
135  }
136 
137  #if UNITY_EDITOR
138  void OnDrawGizmos() { if (this.enabled) {
139  if (this.enabled) {
140  if(this._plot_random_series){
141  this.ScatterPlot(PlotFunctions.SampleRandomSeries(1));
142  }
143  }
144  }
145  #endif
146 
147  public void ScatterPlot(ValuePoint[] points) {
148  var alive = this._particle_system.GetParticles(this._particles);
149  if(alive < points.Length)
150  this._particles = new ParticleSystem.Particle[points.Length];
151  var i = 0;
152  foreach (var point in points) {
153  this._particles[i].remainingLifetime = 100000;
154  this._particles[i].position = point._Pos;
155  this._particles[i].startColor = this._gradient.Evaluate(point._Val);
156  this._particles[i].startSize = point._Size;
157  i++;
158  }
159 
160  this._particle_system.SetParticles(this._particles, points.Length);
161  }
162  }
163 }
164 #endif