UnityMol  0.9.6-875
UnityMol viewer / In developement
HStickManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Rendering;
3 
4 using System.Collections;
5 using System.Collections.Generic;
6 using UI;
7 using Molecule.Model;
8 
9 public class HStickManager : GenericManager {
10  public static StickUpdate[] sticks;
11 
12  public static bool xgmml = false;
13  public static float depthFactor = 1.0f;
14  public static bool resetBrightness = false;
15  public static bool adjustRadii = false;
16  private static float oldDepthFactor = 1.0f;
17  public static float shininess = 0.0f;
18  public static bool resetShininess = false;
19  private static bool wasInteractive = false;
20  private static bool wasInteractiveColored = false;
21 
22 
23  private Material[] stickMaterials;
24 
25  // Use this for initialization
26  public override void Init () {
27  if(UI.GUIDisplay.file_extension=="xgmml")
28  xgmml = true;
29 
30  sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
32  enabled = true;
33 
34  stickMaterials = new Material[sticks.Length];
35 
36  for (int i=0; i< sticks.Length; i++){
37  sticks[i].GetComponent<Renderer>().enabled = true;
38  sticks[i].GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.Off;
39  sticks[i].GetComponent<Renderer>().receiveShadows = false;
40  stickMaterials[i] = sticks[i].GetComponent<Renderer>().material;
41  }
42  isInit = true;
43 
44  }
45 
46  public override void DestroyAll() {
47  sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
48  for(int i=0;i<sticks.Length;i++)
49  GameObject.Destroy(sticks[i].gameObject);
50  isInit = false;
51  }
52 
53  public override void SetColor(Color col, List<string> atoms, string residue = "All", string chain = "All"){}
54  public override void SetColor(Color col, int atomNum){}
55  public override void SetRadii(List<string> atoms, string residue = "All", string chain = "All"){}
56  public override void SetRadii(int atomNum){}
57 
58  public override GameObject GetBall(int id){
59  return null;
60  }
61 
62  public override void ToggleDistanceCueing(bool enabling) {
63  if(UIData.bondtype != UIData.BondType.hyperstick)
64  return;
65  float attenuation;
66  attenuation = enabling? 1f : 0f;
67 
68  //for (int i=0; i< sticks.Length; i++)
69  for (int i=0; i< sticks.Length; i++)
70  stickMaterials[i].SetFloat("_Attenuation", attenuation);
71  }
72 
73 
74  private void ResetColors() {
75  if(UIData.bondtype == UIData.BondType.hyperstick){
76  // sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
77 
78  for (int i=0; i< sticks.Length; i++) {
79  Color atom1Color = MoleculeModel.atomsColorList[sticks[i].atomnumber1];
80  Color atom2Color = MoleculeModel.atomsColorList[sticks[i].atomnumber2];
81 
82  stickMaterials[i].SetColor("_Color", atom1Color);
83  stickMaterials[i].SetColor("_Color2",atom2Color);
84  }
85 
87  }
88  }
89 
90  private void ResetTextures() {
91  if(UIData.bondtype == UIData.BondType.hyperstick){
92  sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
93  if(UIData.atomtype == UIData.AtomType.hyperball){
94 
95  for (int i=0; i< sticks.Length; i++) {
96  stickMaterials[i].SetTexture("_MatCap", (MoleculeModel.atoms[sticks[i].atomnumber1] as GameObject).GetComponent<Renderer>().material.GetTexture("_MatCap"));
97  stickMaterials[i].SetTexture("_MatCap2", (MoleculeModel.atoms[sticks[i].atomnumber2] as GameObject).GetComponent<Renderer>().material.GetTexture("_MatCap"));
98  }
99  }
100  else{
101  for (int i=0; i< sticks.Length; i++) {
102  stickMaterials[i].SetTexture("_MatCap", (Texture)Resources.Load("lit_spheres/divers/daphz05"));
103  stickMaterials[i].SetTexture("_MatCap2", (Texture)Resources.Load("lit_spheres/divers/daphz05"));
104  }
105  }
107  }
108  }
109 
114  public override void ResetPositions() {
115 
116  sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
117  for (int i=0; i< sticks.Length; i++) {
118  float[] fposAtom1 = MoleculeModel.atomsLocationlist[sticks[i].atomnumber1];
119  float[] fposAtom2 = MoleculeModel.atomsLocationlist[sticks[i].atomnumber2];
120 
121  Vector3 posAtom1 = new Vector3(fposAtom1[0],fposAtom1[1],fposAtom1[2]);
122  Vector3 posAtom2 = new Vector3(fposAtom2[0],fposAtom2[1],fposAtom2[2]);
123 
124  stickMaterials[i].SetVector("_TexPos1", posAtom1);
125  sticks[i].transform.position = posAtom1;
126  stickMaterials[i].SetVector("_TexPos2", posAtom2);
127  }
128  }
129 
131 
132  // sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
133  for (int i=0; i< sticks.Length; i++) {
134 
135  Vector3 posAtom1 = ((GameObject)MoleculeModel.atoms[sticks[i].atomnumber1]).transform.position;
136  Vector3 posAtom2 = ((GameObject)MoleculeModel.atoms[sticks[i].atomnumber2]).transform.position;
137 
138  stickMaterials[i].SetVector("_TexPos1", posAtom1);
139  sticks[i].transform.position = posAtom1;
140  stickMaterials[i].SetVector("_TexPos2", posAtom2);
141  }
142  }
143 
144  public void ResetColorsInteractive() {
145  for (int i=0; i< sticks.Length; i++) {
146 
147  Color colAtom1 = ((GameObject)MoleculeModel.atoms[sticks[i].atomnumber1]).GetComponent<Renderer>().sharedMaterial.color;
148  Color colAtom2 = ((GameObject)MoleculeModel.atoms[sticks[i].atomnumber2]).GetComponent<Renderer>().sharedMaterial.color;
149 
150  stickMaterials[i].SetColor("_Color", colAtom1);
151  stickMaterials[i].SetColor("_Color2",colAtom2);
152  }
153  }
154  public override void ResetIMDSimulationPositions() {
155  Vector3 atomOne = Vector3.zero;
156  Vector3 atomTwo = Vector3.zero;
157 
158  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
159  Vector3 posAtom1 = MoleculeModel.atomsIMDSimulationLocationlist[sticks[i].atomnumber1];
160  Vector3 posAtom2 = MoleculeModel.atomsIMDSimulationLocationlist[sticks[i].atomnumber2];
161 
162  stickMaterials[i].SetVector("_TexPos1", posAtom1);
163  sticks[i].transform.position = posAtom1;
164  stickMaterials[i].SetVector("_TexPos2", posAtom2);
165  }
166  }
170  public override void ResetRadii() {
171  adjustRadii = false;
172  // sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
173  // if(UIData.atomtype == UIData.AtomType.hyperball) {
174  for(int i=0;i<sticks.Length;i++){
175 
176 
177  int idAtom1 = sticks[i].atomnumber1;
178  int idAtom2 = sticks[i].atomnumber2;
179  float oriRadius1 = Molecule.Model.MoleculeModel.atomsTypelist[idAtom1].radius;
180  float oriRadius2 = Molecule.Model.MoleculeModel.atomsTypelist[idAtom2].radius;
181 
182  float radiusAtom1 = oriRadius1 * RepresentationOldGUI.globalRadius * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom1]/100;
183  float radiusAtom2 = oriRadius2 * RepresentationOldGUI.globalRadius * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom2]/100;
184 
185 
186  //if it's not a network
187  if(!xgmml){
188 
189 
190  stickMaterials[i].SetFloat("_Rayon1", radiusAtom1);
191  stickMaterials[i].SetFloat("_Rayon2", radiusAtom2);
192  //if it's a network, we had to reduce de size of stick (otherwise it looks like a licornice representation)
193  }else{
194  // Debug.Log(sticks[i].atompointer1.renderer.material.GetFloat("_Rayon")/2);
195  stickMaterials[i].SetFloat("_Rayon1", radiusAtom1/2);
196  stickMaterials[i].SetFloat("_Rayon2", radiusAtom2/2);
197  }
198  // Might not be necessary anymore.
199  sticks[i].oldrayon1 = radiusAtom1;
200  sticks[i].oldrayon2 = radiusAtom2;
201  }
202  // }
203  // else if(UIData.atomtype == UIData.AtomType.optihb){
204 
205  // for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
206  // int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
207  // int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
208  // float oriRadius1 = Molecule.Model.MoleculeModel.atomsTypelist[idAtom1].radius;
209  // float oriRadius2 = Molecule.Model.MoleculeModel.atomsTypelist[idAtom2].radius;
210 
211  // if(!xgmml){
212  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon1",oriRadius1
213  // * RepresentationOldGUI.globalRadius
214  // * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom1]/100);
215 
216  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon2",oriRadius2
217  // * RepresentationOldGUI.globalRadius
218  // * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom2]/100);
219  // }
220  // else{
221  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon1",(oriRadius1
222  // * RepresentationOldGUI.globalRadius
223  // * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom1]/100)/2);
224 
225  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon2",(oriRadius2
226  // * RepresentationOldGUI.globalRadius
227  // * Molecule.Model.MoleculeModel.atomsLocalScaleList[idAtom2]/100)/2);
228  // }
229  // }
230 
231  // }
232  // else {
233  // for (int i=0; i< sticks.Length; i++) {
234  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon1", sticks[i].atompointer1.transform.lossyScale.x/2);
235  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Rayon2", sticks[i].atompointer2.transform.lossyScale.x/2);
236 
237  // sticks[i].oldrayon1 = sticks[i].atompointer1.transform.lossyScale.x/2;
238  // sticks[i].oldrayon2 = sticks[i].atompointer2.transform.lossyScale.x/2;
239  // }
240  // }
241  }
242 
243  public override void EnableRenderers() {
245  for (int i=0; i< sticks.Length; i++)
246  sticks[i].GetComponent<Renderer>().enabled = true;
247  enabled = true;
248  }
249 
250  public override void DisableRenderers() {
251  sticks = GameObject.FindObjectsOfType(typeof(StickUpdate)) as StickUpdate[];
252  Debug.Log("StickManager: DisableRenderers()" + sticks.Length);
253  for (int i=0; i< sticks.Length; i++)
254  sticks[i].GetComponent<Renderer>().enabled = false;
255  enabled = false;
256  }
257 
258  public override void EnableShadows(){
259  for(int i=0; i<sticks.Length; i++){
260  sticks[i].GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.On;
261  sticks[i].GetComponent<Renderer>().receiveShadows = true;
262  sticks[i].GetComponent<Renderer>().material.shader = Shader.Find("FvNano/HyperSticks Shadow GL_D3D");
263  }
264  }
265 
266  public override void DisableShadows(){
267  for(int i=0; i<sticks.Length; i++){
268  sticks[i].GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.Off;
269  sticks[i].GetComponent<Renderer>().receiveShadows = false;
270  sticks[i].GetComponent<Renderer>().material.shader = Shader.Find("FvNano/HyperSticks GL_D3D");
271 
272  }
273  }
274  public override void showHydrogens(bool hide){
275  for(int i=0; i<sticks.Length; i++){
276  int idAtom1 = sticks[i].atomnumber1;
277  int idAtom2 = sticks[i].atomnumber2;
278  if( Molecule.Model.MoleculeModel.atomsTypelist[idAtom1].type == "H" ||
279  Molecule.Model.MoleculeModel.atomsTypelist[idAtom2].type == "H")
280  sticks[i].GetComponent<Renderer>().enabled = ! hide;
281  }
282  }
283 
284  private void ResetBrightness() {
285  for(int i=0; i<sticks.Length; i++)
286  sticks[i].GetComponent<Renderer>().material.SetFloat("_Brightness", HBallManager.brightness);
287 
288  resetBrightness = false;
289  }
290  public void ResetShininess(){
291  for(int i=0; i<sticks.Length; i++)
292  sticks[i].GetComponent<Renderer>().material.SetFloat("_Shininess", shininess);
293  }
294 
295  // Update is called once per frame
296  void Update () {
297  if(UIData.bondtype != UIData.BondType.hyperstick) {
298  enabled = false;
299  return;
300  }
301 
303  ResetColors();
304  ResetTextures();
305  }
306 
307  if(resetBrightness)
308  ResetBrightness();
309 
310  if(wasInteractive && !GUIMoleculeController.toggle_NA_INTERACTIVE){
311  ResetPositions();
312  ResetColors();
313  }
315  if(UIData.toggleGray){
317  wasInteractiveColored = true;
318  }
319  else if(wasInteractiveColored){
320  ResetColors();
321  wasInteractiveColored = false;
322  }
324  wasInteractive = true;
325  }
326 
327  if (xgmml && (oldDepthFactor!=depthFactor)) {
328 // ResetBondVectors();
329  ResetPositions();
330  oldDepthFactor=depthFactor;
331  }
332 
333 
335  // for (int i=0; i< sticks.Length; i++)
336  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Shrink", StickUpdate.shrink);
337  for(int i=0;i<sticks.Length;i++)
338  stickMaterials[i].SetFloat("_Shrink",StickUpdate.shrink);
340  }
342  // for (int i=0; i< sticks.Length; i++)
343  // sticks[i].GetComponent<Renderer>().material.SetFloat("_Scale",StickUpdate.scale);
344  for(int i=0;i<sticks.Length;i++)
345  stickMaterials[i].SetFloat("_Scale",StickUpdate.scale);
347  }
348 
349  if(adjustRadii)
350  ResetRadii();
351 
352  if(resetShininess)
353  ResetShininess();
354 
355  }
356 
357 }
void ResetColorsInteractive()
void ResetShininess()
static StickUpdate[] sticks
static float scale
Definition: StickUpdate.cs:79
Material[] stickMaterials
override void Init()
Initializes this instance of the manager.
void ResetTextures()
static float shininess
static float oldshrink
Definition: StickUpdate.cs:77
override GameObject GetBall(int id)
static bool resetBrightness
override void DisableRenderers()
Disables the renderers for all objects managed by the instance of the manager.
override void ResetIMDSimulationPositions()
static float oldscale
Definition: StickUpdate.cs:80
override void ResetRadii()
Adjusts the stick radii, which is needed to match the size of the balls when their radii are modified...
float oldrayon1
Definition: StickUpdate.cs:90
override void SetColor(Color col, List< string > atoms, string residue="All", string chain="All")
override void SetColor(Color col, int atomNum)
float oldrayon2
Definition: StickUpdate.cs:91
static bool xgmml
static List< AtomModel > atomsTypelist
The type of each atom.
void ResetColors()
override void DisableShadows()
static List< Vector3 > atomsIMDSimulationLocationlist
The coordinates of each atom, simulated through an IMD simulation.
int atomnumber1
Definition: StickUpdate.cs:85
override void ResetPositions()
Resets the bond vectors, which is necessary when the balls move.
override void EnableRenderers()
Enables the renderers for all objects managed by the instance of the manager.
int atomnumber2
Definition: StickUpdate.cs:86
override void showHydrogens(bool hide)
static void setCurrentBondManager(GenericManager manager)
Definition: UnityMolMain.cs:68
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
override void EnableShadows()
static bool resetShininess
static BondType bondtype
Definition: UIData.cs:141
!WiP manage GUI, and provide static strings for the GUI.
Definition: GUIDisplay.cs:94
static List< Color > atomsColorList
The color of each atom.
static string file_extension
Definition: GUIDisplay.cs:121
override void DestroyAll()
static List< int[]> bondEPList
The bonds between atoms.
static bool bondsReadyToBeReset
Definition: BallUpdate.cs:75
static bool wasInteractiveColored
static bool toggleGray
Definition: UIData.cs:125
static float oldDepthFactor
static AtomType atomtype
Definition: UIData.cs:139
static bool wasInteractive
override void SetRadii(int atomNum)
override void SetRadii(List< string > atoms, string residue="All", string chain="All")
static bool adjustRadii
void ResetBrightness()
static float depthFactor
static float brightness
Definition: HBallManager.cs:82
static float shrink
Definition: StickUpdate.cs:76
Definition: GUIDisplay.cs:66
static bool resetRadii
Definition: BallUpdate.cs:76
static List< float > atomsLocalScaleList
override void ToggleDistanceCueing(bool enabling)
static List< float[]> atomsLocationlist
The coordinates of each atom.
void ResetPositionsInteractive()