UnityMol  0.9.6-875
UnityMol viewer / In developement
BondCubeUpdate.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 BondCubeUpdate : MonoBehaviour {
72 
73  public static float scale = 1.0f;
74  public static float radiusFactor = 1.0f;
75  public int atomnumber1 = -1;
76  public int atomnumber2 = -1;
77 
78  public static float oldscale = 1.0f;
79  public static float width = RepresentationOldGUI.bondWidth ;
80  public static float oldWidth = RepresentationOldGUI.bondWidth ;
81 // private float oldrayon1 = 2.0f;
82 // private float oldrayon2 = 2.0f;
83 
84 
85 
86  //Only check for d3d once
87 // private bool d3d= false;
88  void Start () {
89 // d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
90 // Debug.Log("BondCubeUpdate: Start()");
92 
93  if(atomnumber1 == -1 || atomnumber2 == -1){
94  Debug.LogError("Problem when initializing the bond as a cube");
95  return;
96  }
97 
98 
99 
100 
101  float[] fposAtom1 = MoleculeModel.atomsLocationlist[atomnumber1];
102  float[] fposAtom2 = MoleculeModel.atomsLocationlist[atomnumber2];
103 
104  Vector3 posAtom1 = new Vector3(fposAtom1[0],fposAtom1[1],fposAtom1[2]);
105  Vector3 posAtom2 = new Vector3(fposAtom2[0],fposAtom2[1],fposAtom2[2]);
106 
107  Color colorAtom1 = MoleculeModel.atomsColorList[atomnumber1];
108  Color colorAtom2 = MoleculeModel.atomsColorList[atomnumber2];
109 
110 
111  Vector3 v_dist = posAtom2 - posAtom1 ;
112  float length = v_dist.magnitude ;
113  Vector3 lscale = new Vector3(transform.localScale.x, transform.localScale.y, length);
114  this.transform.localScale = lscale ;
115 
116 
117  transform.position = (posAtom1 + posAtom2)/2.0f;
118  transform.LookAt(posAtom2);
119 
120  Renderer rd = GetComponent<Renderer>();
121  rd.material.SetVector("_Pos1", posAtom1);
122  rd.material.SetVector("_Pos2", posAtom2);
123 
124 
125 
126  //Debug.Log(color1.ToString());
127  rd.material.SetColor("_Color1", colorAtom1);
128 
129  //Debug.Log(color2.ToString());
130  rd.material.SetColor("_Color2", colorAtom2);
131 
132  Mesh mesh = GetComponent<MeshFilter>().mesh;
133  Vector3[] vertices = mesh.vertices;
134  Color32[] colors = new Color32[vertices.Length];
135  float dist1, dist2;
136  Matrix4x4 localToWorld = transform.localToWorldMatrix;
137 
138  Vector3 pos;
139  for(int i=0; i<vertices.Length; i++) {
140  pos = localToWorld.MultiplyPoint3x4(vertices[i]);
141  dist1 = Vector3.Distance(posAtom1, pos);
142  dist2 = Vector3.Distance(posAtom2, pos);
143  if( dist1 < dist2 )
144  colors[i] = colorAtom1;
145  else
146  colors[i] = colorAtom2;
147  }
148 
149  mesh.colors32 = colors;
150  GetComponent<MeshFilter>().mesh = mesh;
151  rd.material.shader = Shader.Find("Custom/Ribbons");
152 
153  }
154 
155 }
static float width
static float scale
static float oldscale
static float radiusFactor
static List< Color > atomsColorList
The color of each atom.
static float oldWidth
Definition: GUIDisplay.cs:66
static List< float[]> atomsLocationlist
The coordinates of each atom.