3 using System.Collections.Generic;
7 namespace droid.Neodroid.Prototyping.Displayers.ECS {
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;
23 protected override void Setup() {
24 this._particle_system = this.GetComponent<ParticleSystem>();
25 var em = this._particle_system.emission;
28 var sh = this._particle_system.shape;
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;
38 this._particle_system_renderer = this.GetComponent<ParticleSystemRenderer>();
40 this._particle_system_renderer.alignment = ParticleSystemRenderSpace.World;
44 this._gradient =
new Gradient {
46 new GradientColorKey(
new Color(1, 0, 0), 0f),
47 new GradientColorKey(
new Color(0, 1, 0), 1f)
52 protected override void Awake() {
57 public struct ValuePoint {
62 public ValuePoint(Vector3 pos,
float val,
float size) {
69 public override void Display(Double value) {
72 print(
"Applying the double " + value +
" To " + this.name);
75 this._values =
new[] {(float)value};
76 this.ScatterPlot(this._values);
79 public override void Display(
float[] values) {
83 foreach (var value
in values) {
88 print(
"Applying the float array " + s +
" To " + this.name);
92 this._values = values;
93 this.ScatterPlot(values);
96 public override void Display(String values) {
99 Debug.Log(
"Applying the float array " + values +
" To " + this.name);
103 var vs =
new List<float>();
104 foreach (var value
in values.Split(
',')) {
105 vs.Add(
float.Parse(value));
108 this._values = vs.ToArray();
109 this.ScatterPlot(this._values);
112 public override void Display(
float values) {
115 print(
"Applying the float " + values +
" To " + this.name);
117 this._values =
new[] {values};
118 this.ScatterPlot(this._values);
121 public void ScatterPlot(
float[] points) {
122 if(this._particles == null || this._particles.Length != points.Length)
123 this._particles =
new ParticleSystem.Particle[points.Length];
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;
134 this._particle_system.SetParticles(this._particles, points.Length);
138 void OnDrawGizmos() {
if (this.enabled) {
140 if(this._plot_random_series){
141 this.ScatterPlot(PlotFunctions.SampleRandomSeries(1));
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];
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;
160 this._particle_system.SetParticles(this._particles, points.Length);