UnityMol  0.9.6-875
UnityMol viewer / In developement
VolumetricFields.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using UI;
69 using Molecule.Model;
70 
71 public class VolumetricFields : Volumetric {
72  private float maxCharge = - float.MaxValue;
73  private float minCharge = float.MaxValue;
74  private float chargeAmplitude;
75  private bool init = false;
76 
77  private ReadDX readDx;
78 
79  public override void Init() {
80  if(init == false){
81  // We get the ReadDX object and from it, the values we need.
83  density = readDx._grid;
84  delta = readDx.GetDelta();
85  origin = readDx.GetOrigin();
86 
87  // This is needed to correctly place the particles.
88  origin.x = -origin.x;
89 
90  // Getting the bounds and amplitude of the electrostatics field.
91  foreach(float f in density) {
92  if(f > maxCharge)
93  maxCharge = f;
94  if(f < minCharge)
95  minCharge = f;
96  }
97 
98  // ChargeAmplitude = largest absolute value
99  if ( (-minCharge) > maxCharge)
100  chargeAmplitude = -minCharge;
101  else
102  chargeAmplitude = maxCharge;
103 
104  Debug.Log("Amplitude: "+chargeAmplitude);
105 
106  // Creating the dynamic particle list, building the static particle array,
107  // setting it to the particle system, and enabling the renderer.
109 
110  }
111  } // End of Init
112 
113  public override void CreatePoints() {
114  // Getting the size of the density grid.
115  int dim0 = density.GetLength(0);
116  int dim1 = density.GetLength(1);
117  int dim2 = density.GetLength(2);
118 
119  Vector3 indices = Vector3.zero;
120 
121  for(int x=0; x<dim0; x++) {
122  for(int y=0; y<dim1; y++) {
123  for(int z=0; z<dim2; z++) {
124  indices.x = -x;
125  indices.y = y;
126  indices.z = z;
127 
128  float charge = density[x,y,z];
129  Color c;
130  if(charge > 0)
131  c = Color.blue;
132  else
133  c = Color.red;
134  c.a = Mathf.Abs((charge / chargeAmplitude)); // Somewhat arbitrary, but guarantees 0 <= c.a <= 1.
135  if(raise) {
136  c.a = c.a * ALPHA_FACTOR;
137  if (c.a > 1f)
138  c.a = 1f;
139  }
140 
141  // Here, we cull.
142  float size = 0.8f * particleScale / delta.x;
143  if (c.a > ALPHA_THRESHOLD) {
144  ParticleSystem.Particle particle = new ParticleSystem.Particle();
145  Vector3 p = new Vector3(0f,0f,0f);
146  p = origin + Vector3.Scale(indices, delta);
147  particle.position = p;
148  particle.startColor = c;
149  //Debug.Log(particle.color);
150  //pSystem.renderer.material.color = c;
151  //particle.size = particleScale;
152  particle.startSize = size;
153  dynPoints.Add(particle);
154  }
155  }
156  }
157  }
158  } // End of CreatePoints
159 
160 }
float ALPHA_THRESHOLD
Definition: Volumetric.cs:72
float[,,] _grid
Definition: ReadDX.cs:88
Vector3 GetDelta()
Definition: ReadDX.cs:103
Definition: ReadDX.cs:77
override void Init()
Initializes this instance.
override void CreatePoints()
Creates the points for this particle system.
List< ParticleSystem.Particle > dynPoints
Definition: Volumetric.cs:80
static float particleScale
Definition: Volumetric.cs:71
void SetParticleSystem()
Sets the particle system.
Definition: Volumetric.cs:140
Vector3 GetOrigin()
Definition: ReadDX.cs:108
static Vector3 delta
Definition: Volumetric.cs:82
float[,,] density
Definition: Volumetric.cs:77
float ALPHA_FACTOR
Definition: Volumetric.cs:75
Definition: GUIDisplay.cs:66
static Vector3 origin
Definition: Volumetric.cs:83