UnityMol  0.9.6-875
UnityMol viewer / In developement
ElectrostaticManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using Molecule.Model;
5 
6 public class ElectrostaticManager {
7 
8  private List<GameObject> FieldLines = new List<GameObject>();
9  private GameObject FieldLineManager;
10  private List<GameObject> ElecIsoPos = new List<GameObject>();
11  private List<GameObject> ElecIsoNeg = new List<GameObject>();
12 
13  public static ParticleSystem particleSystem;
14  private static GameObject VolumetricManager;
15  public static VolumetricFields volumetricFields = null;
16  public bool volumetricFieldsInitialized = false;
17 
18  public static bool fieldLineColorGradient = true;
19  public static ColorObject EnergyGrayColor = new ColorObject(Color.white);
20 
21  public static ReadDX readdx = null;// = new ReadDX ();
22  public static bool dxRead = false; // true when dx read
23 
24  public static float fl_speed = 0.13333333f;
25  public static float fl_density = 3.4f;
26  public static float fl_linelength = 0.7f;
27  public static float fl_linewidth = 0.2f;
28 
29  public static string dxFilePath;
30 
31  //Electrostatic iso-surface parameters
32  public bool electroIsoPositiveInitialized = false;
33  public bool electroIsoNegativeInitialized = false;
34 
35 
36  //Electric field line symbol radius
37  public float adjustFieldLineCut = 40f;
38 
39 
40 
41  public void Init(){
42 
43  VolumetricManager = GameObject.Find("SurfaceManager");
44  particleSystem = VolumetricManager.GetComponent<ParticleSystem>();
45  /*
46  //Create a Particle System
47  particleSystem = VolumetricManager.AddComponent<ParticleSystem>();
48  particleSystem.loop = false;
49  ParticleSystem.EmissionModule em = particleSystem.emission;
50  em.enabled = false;
51  ParticleSystem.ShapeModule sh = particleSystem.shape;
52  sh.enabled = false;
53  particleSystem.maxParticles = 2000000;
54  particleSystem.scalingMode = ParticleSystemScalingMode.Shape;*/
55 
56  readdx = new ReadDX();
57 
58 
59  }
60  public static void ReadDxFile(){
61  if(dxRead == false){
62  readdx.ReadFile(dxFilePath,MoleculeModel.Offset);
63  dxRead = true;
64  volumetricFields = VolumetricManager.AddComponent<VolumetricFields>();
65  }
66  }
67  public void AddFieldLine(GameObject flgo){
68  FieldLines.Add(flgo);
69  }
70  public int nbFieldLine(){
71  return FieldLines.Count;
72  }
73 
74  public void DisplayFieldLines(){
76  }
77 
78  public void DestroyFieldLines() {
79  /* MVB: We had the following error for DestroyFieldLines. I think the reason is that even
80  * if we never use a particleSystem, we try to destroy it at the end! So check whether we have
81  * one first..
82  * ..need to verify whether it works when loading field line scenes; seems currently broken.
83 MissingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it.
84 Your script should either check if it is null or you should not destroy the object.
85 UnityEngine.Component.GetComponent[Renderer] ()
86 ElectrostaticManager.DestroyFieldLines () (at Assets/Scripts/Surface-Electrostatic/ElectrostaticManager.cs:91)
87 Molecule.View.DisplayMolecule.ClearMemory () (at Assets/Scripts/Molecule/View/DisplayMolecule.cs:293)
88 Molecule3D.clearScene () (at Assets/Scripts/Molecule3D.cs:218)
89 Molecule3D.OnDisable () (at Assets/Scripts/Molecule3D.cs:136)
90 */
91  if(particleSystem) {
92  Renderer ps_r = particleSystem.GetComponent<Renderer>();
93  ps_r.enabled = false;
95  for(int i=0;i<FieldLines.Count;i++){
96  Object.Destroy(FieldLines[i]);
97  }
98  FieldLines.Clear();
99  }
100  }
101  public void DestroyElectIso() {
102 
103  foreach (GameObject surf in ElecIsoPos)
104  GameObject.Destroy(surf);
105 
106  foreach (GameObject surf in ElecIsoNeg)
107  GameObject.Destroy(surf);
108 
109  ElecIsoPos.Clear();
110  ElecIsoNeg.Clear();
111  }
112  public void update(){
113  if(FieldLines.Count >0){
114 
115  for(int i=0;i<FieldLines.Count;i++) {
116 
117  LineRenderer curLineRenderer;
118  curLineRenderer = FieldLines[i].GetComponent<LineRenderer>();
119  curLineRenderer.material.SetFloat("_timeOff",Time.time);
120  }
121  }
122  }
123 
124  public void UpdateParameters(){
125  foreach (GameObject FieldLine in FieldLines) {
126  LineRenderer curLineRenderer;
127  curLineRenderer = FieldLine.GetComponent<LineRenderer>();
128 
129  // for benoist video comment next line
130  curLineRenderer.material.SetColor("_Color", EnergyGrayColor.color);
131  if (fieldLineColorGradient)
132  curLineRenderer.material.SetFloat("_colormode", 0f);
133  else
134  curLineRenderer.material.SetFloat("_colormode", 1f);
135 
136 
137  curLineRenderer.material.SetFloat("_Speed",fl_speed);
138  curLineRenderer.material.SetFloat("_Density",fl_density);
139  curLineRenderer.material.SetFloat("_Length", fl_linelength);
140  curLineRenderer.SetWidth(fl_linewidth,fl_linewidth);
141  curLineRenderer.material.SetFloat("_depthcut", (SurfaceManager.depthCut-maxCamera.currentDistance));
142  curLineRenderer.material.SetFloat("_adjust",(adjustFieldLineCut));
143  curLineRenderer.material.SetVector("_SurfacePos", FieldLine.transform.position);
144 
145  if (SurfaceManager.cutMode == 2)
146  curLineRenderer.material.SetFloat("_cut", 2f);
147  else if (SurfaceManager.cutMode == 1){
148  curLineRenderer.material.SetFloat("_cut", 1f);
149  curLineRenderer.material.SetVector("_cutplane",new Vector4(SurfaceManager.cutX,
153  }
154  }
155  }
156 
157 
158  public void HideFieldLines(){
159  for(int i=0;i<FieldLines.Count;i++)
160  FieldLines[i].SetActive(false);
161  }
162  public void ShowFieldLines(){
163  for(int i=0;i<FieldLines.Count;i++)
164  FieldLines[i].SetActive(true);
165  }
166  public void CreateVolumetricFields(){
167  if(!volumetricFieldsInitialized){
168  ReadDxFile();
169  volumetricFields.Init();
170  volumetricFieldsInitialized=true;
171  }
172  else
173  volumetricFields.ShowHide();
174 
175  }
176  public void RecenterFieldLines(Vector3 pos){
177  for(int i=0;i<FieldLines.Count;i++)
178  FieldLines[i].transform.position += pos + MoleculeModel.Offset;
179  }
180 
181 }
static float cutX
List< GameObject > ElecIsoNeg
void ReadFile(string file_name)
Definition: ReadDX.cs:122
static float cutZ
static ParticleSystem particleSystem
static float cutY
Definition: ReadDX.cs:77
void ShowHide()
Definition: Volumetric.cs:97
static Vector3 Offset
The offset for the molecule.
void RecenterFieldLines(Vector3 pos)
override void Init()
Initializes this instance.
List< GameObject > FieldLines
static float currentDistance
Definition: maxCamera.cs:128
static GameObject VolumetricManager
Color color
Definition: ColorObject.cs:72
static VolumetricFields volumetricFields
static void DisplayFieldLine()
static float depthCut
List< GameObject > ElecIsoPos
void AddFieldLine(GameObject flgo)
static bool fieldLineColorGradient
static int cutMode
static ColorObject EnergyGrayColor