UnityMol  0.9.6-875
UnityMol viewer / In developement
CubeBondManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Rendering;
3 using System.Collections;
4 using System.Collections.Generic;
5 using UI;
6 using Molecule.Model;
7 
9  private BondCubeUpdate[] bonds;
10 
11  // Use this for initialization
12  public override void Init () {
13  bonds = GameObject.FindObjectsOfType(typeof(BondCubeUpdate)) as BondCubeUpdate[];
15  enabled = true;
16  foreach(BondCubeUpdate bu in bonds)
17  bu.GetComponent<Collider>().enabled = false;
18  isInit = true;
19 
20  }
21 
22  public override void DestroyAll() {
23  isInit = false;
24 
25  }
26 
27  public override void SetColor(Color col, List<string> atoms, string residue = "All", string chain = "All"){}
28  public override void SetColor(Color col, int atomNum){}
29  public override void SetRadii(List<string> atoms, string residue = "All", string chain = "All"){}
30  public override void SetRadii(int atomNum){}
31 
32  public override GameObject GetBall(int id){
33  return null;
34  }
35 
36  public override void ToggleDistanceCueing(bool enabling) {
37 
38  }
39 
40  private void AdjustWidths() {
41  float width = BondCubeUpdate.width;
42  foreach(BondCubeUpdate bcu in bonds) {
43  Vector3 lscale = new Vector3(width, width, bcu.transform.localScale.z);
44  bcu.transform.localScale = lscale ;
45  }
46  BondCubeUpdate.oldWidth = width;
47  }
48 
49  private void AdjustScales() {
50  foreach(BondCubeUpdate bcu in bonds)
51  bcu.GetComponent<Renderer>().material.SetFloat("_Scale", BondCubeUpdate.scale);
53  }
54 
55  private void ResetColors() {
56  if(UIData.bondtype == UIData.BondType.cube){
57  bonds = GameObject.FindObjectsOfType(typeof(BondCubeUpdate)) as BondCubeUpdate[];
58  foreach(BondCubeUpdate bcu in bonds) {
59  //bcu.renderer.material.SetColor("_Color1", bcu.atompointer1.renderer.material.GetColor("_Color"));
60  //bcu.renderer.material.SetColor("_Color2", bcu.atompointer2.renderer.material.GetColor("_Color"));
61 
62  Mesh mesh = bcu.GetComponent<MeshFilter>().mesh;
63  Vector3[] vertices = mesh.vertices;
64  Color32[] colors = new Color32[vertices.Length];
65  float dist1, dist2;
66  Matrix4x4 localToWorld = bcu.transform.localToWorldMatrix;
67 
68  Color colorAtom1 = MoleculeModel.atomsColorList[bcu.atomnumber1];
69  Color colorAtom2 = MoleculeModel.atomsColorList[bcu.atomnumber2];
70 
71 
72  float[] fposAtom1 = MoleculeModel.atomsLocationlist[bcu.atomnumber1];
73  float[] fposAtom2 = MoleculeModel.atomsLocationlist[bcu.atomnumber2];
74 
75  Vector3 posAtom1 = new Vector3(fposAtom1[0],fposAtom1[1],fposAtom1[2]);
76  Vector3 posAtom2 = new Vector3(fposAtom2[0],fposAtom2[1],fposAtom2[2]);
77 
78 
79  Vector3 pos;
80 
81 
82  for(int i=0; i<vertices.Length; i++) {
83  pos = localToWorld.MultiplyPoint3x4(vertices[i]);
84  dist1 = Vector3.Distance(posAtom1, pos);
85  dist2 = Vector3.Distance(posAtom2, pos);
86  if( dist1 < dist2 )
87  colors[i] = colorAtom1;
88  else
89  colors[i] = colorAtom2;
90  }
91 
92  mesh.colors32 = colors;
93  bcu.GetComponent<MeshFilter>().mesh = mesh;
94  }
96  }
97  }
98 
99  public override void EnableRenderers() {
101  foreach(BondCubeUpdate bcu in bonds)
102  bcu.GetComponent<Renderer>().enabled = true;
103  enabled = true;
104  }
105 
106  public override void DisableRenderers() {
107  Debug.Log("CubeBondManager: DisableRenderers()");
108  foreach(BondCubeUpdate bcu in bonds)
109  bcu.GetComponent<Renderer>().enabled = false;
110  enabled = false;
111  }
112 
113 
114  public override void EnableShadows(){
115  foreach(BondCubeUpdate bcu in bonds){
116  bcu.GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.On;
117  bcu.GetComponent<Renderer>().receiveShadows = true;
118  }
119  }
120  public override void DisableShadows(){
121  foreach(BondCubeUpdate bcu in bonds){
122  bcu.GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.Off;
123  bcu.GetComponent<Renderer>().receiveShadows = false;
124  }
125  }
126  public override void ResetRadii(){
127 
128  }
129 
130 
131  // Update is called once per frame
132  void Update () {
133  if(UIData.bondtype != UIData.BondType.cube) {
134  enabled = false;
135  return;
136  }
137 
139  AdjustWidths();
140 
142  AdjustScales();
143 
145  ResetColors();
146  }
147 
148 
149  // TODO ?
150 /*
151  if(UIData.EnableUpdate)
152  {
153  transform.position = (atompointer1.transform.position + atompointer2.transform.position)/2.0f;
154  transform.LookAt(atompointer2.transform.position);
155 
156  renderer.material.SetVector("_Pos1", atompointer1.transform.position);
157  renderer.material.SetVector("_Pos2", atompointer2.transform.position);
158  renderer.material.SetColor("_Color1", atompointer1.renderer.material.GetColor("_Color"));
159  renderer.material.SetColor("_Color2", atompointer2.renderer.material.GetColor("_Color"));
160  }
161 */
162  public override void ResetPositions() {
163  Vector3 atomOne = Vector3.zero;
164  bonds = GameObject.FindObjectsOfType(typeof(BondCubeUpdate)) as BondCubeUpdate[];
165  for (int i=0; i< bonds.Length; i++) {
166 
167  float[] fposAtom1 = MoleculeModel.atomsLocationlist[bonds[i].atomnumber1];
168  float[] fposAtom2 = MoleculeModel.atomsLocationlist[bonds[i].atomnumber2];
169 
170  Vector3 posAtom1 = new Vector3(fposAtom1[0],fposAtom1[1],fposAtom1[2]);
171  Vector3 posAtom2 = new Vector3(fposAtom2[0],fposAtom2[1],fposAtom2[2]);
172 
173  bonds[i].GetComponent<Renderer>().material.SetVector("_Pos1", posAtom1);
174  bonds[i].transform.position = posAtom1;
175  bonds[i].GetComponent<Renderer>().material.SetVector("_Pos2", posAtom2);
176  }
177  }
178 
179  public override void ResetIMDSimulationPositions() {
180 
181  }
182  public override void showHydrogens(bool hide){
183 
184  for (int i=0; i< bonds.Length; i++) {
185 
186  if(MoleculeModel.atomsTypelist[bonds[i].atomnumber1].type == "H" || MoleculeModel.atomsTypelist[bonds[i].atomnumber2].type == "H" ){
187  bonds[i].GetComponent<Renderer>().enabled = !hide;
188  }
189  }
190 
191  }
192 
193 }
override void DisableShadows()
override void ResetIMDSimulationPositions()
override void DestroyAll()
static float width
override void DisableRenderers()
Disables the renderers for all objects managed by the instance of the manager.
static float scale
override void EnableRenderers()
Enables the renderers for all objects managed by the instance of the manager.
override void SetRadii(int atomNum)
static List< AtomModel > atomsTypelist
The type of each atom.
override void Init()
Initializes this instance of the manager.
override void ToggleDistanceCueing(bool enabling)
static float oldscale
override GameObject GetBall(int id)
override void EnableShadows()
static void setCurrentBondManager(GenericManager manager)
Definition: UnityMolMain.cs:68
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static BondType bondtype
Definition: UIData.cs:141
override void ResetPositions()
override void SetRadii(List< string > atoms, string residue="All", string chain="All")
static List< Color > atomsColorList
The color of each atom.
override void showHydrogens(bool hide)
static float oldWidth
static bool bondsReadyToBeReset
Definition: BallUpdate.cs:75
BondCubeUpdate[] bonds
override void SetColor(Color col, int atomNum)
override void ResetRadii()
override void SetColor(Color col, List< string > atoms, string residue="All", string chain="All")
Definition: GUIDisplay.cs:66
static List< float[]> atomsLocationlist
The coordinates of each atom.