UnityMol  0.9.6-875
UnityMol viewer / In developement
HStickMeshManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using UI;
6 using UnityEngine.Rendering;
7 
8 
10 
11  public GameObject[] hstick_meshes = new GameObject[0];
12  public List<int> texturesToUpdate;
13  public Material[] materialsSticks;
14  public Texture2D[] texturesSticks;
15  public Dictionary<int,KeyValuePair<int,int> > coordStickTexture;
16  public static Texture texture;
17  public static bool resetBrightness = false;
18  public static bool resetShrink = false;
19  public static bool resetScale = false;
20  public static float scaleSticks = 1.0f;
21  public static bool resetRadii = false;
22  public static bool resetColors = false;
23  public static bool resetTexture = false;
24 
25  public static float shininess = 0.0f;
26  public static bool resetShininess = false;
27 
29  hstick_meshes = new GameObject[0];
30  }
31 
35  public override void Init () {
36  enabled = true;
37  texturesToUpdate = new List<int>();
38  materialsSticks = new Material[hstick_meshes.Length];
39  texturesSticks = new Texture2D[hstick_meshes.Length];
40  for(int i=0;i<hstick_meshes.Length;i++){
41  materialsSticks[i] = hstick_meshes[i].GetComponent<Renderer>().material;
42  texturesSticks[i] = (Texture2D)materialsSticks[i].mainTexture;
43  }
44  isInit = true;
46  }
47 
51  public override void DestroyAll() {
52  for(int i=0;i<hstick_meshes.Length;i++)
53  GameObject.Destroy(hstick_meshes[i]);
54  hstick_meshes = new GameObject[0];
55  isInit = false;
56 
57  }
58 
59  public override void ToggleDistanceCueing(bool enabling) {
60  float attenuation;
61  attenuation = enabling? 1f : 0f;
62  for (int i=0; i<hstick_meshes.Length; i++)
63  hstick_meshes[i].GetComponent<Renderer>().material.SetFloat("_Attenuation", attenuation);
64  }
65 
81  public override void SetColor(Color col, List<string> atom, string residue = "All", string chain = "All"){
82  }
83 
84 
94  public override void SetColor(Color col, int atomNum) {
95  }
96 
109  public override void SetRadii(List<string> atom, string residue = "All", string chain = "All"){
110 
111  }
118  public override void SetRadii(int id){
119 
120  }
121 
131  public override GameObject GetBall(int id){
132  return null;
133  }
134 
138  public override void ResetPositions(){
139 
140  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
141  float[] atomOne;
142  float[] atomTwo;
143 
144 
145  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
146  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
147  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
148  if(coordStickTexture.TryGetValue(i,out res)){
149  atomOne = Molecule.Model.MoleculeModel.atomsLocationlist[idAtom1];
150  atomTwo = Molecule.Model.MoleculeModel.atomsLocationlist[idAtom2];
151 
152  texturesSticks[res.Key].SetPixel(4,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne[0]));
153  texturesSticks[res.Key].SetPixel(5,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne[1]));
154  texturesSticks[res.Key].SetPixel(6,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne[2]));
155 
156  texturesSticks[res.Key].SetPixel(7,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo[0]));
157  texturesSticks[res.Key].SetPixel(8,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo[1]));
158  texturesSticks[res.Key].SetPixel(9,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo[2]));
159  texturesToUpdate.Add(res.Key);
160  }
161  }
162  ApplyTextures();
163 
164  }
165 
169  public override void ResetIMDSimulationPositions(){
170 
171  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
172  Vector3 atomOne = Vector3.zero;
173  Vector3 atomTwo = Vector3.zero;
174 
175 
176  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
177  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
178  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
179  if(coordStickTexture.TryGetValue(i,out res)){
182 
183  texturesSticks[res.Key].SetPixel(4,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne.x));
184  texturesSticks[res.Key].SetPixel(5,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne.y));
185  texturesSticks[res.Key].SetPixel(6,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomOne.z));
186 
187  texturesSticks[res.Key].SetPixel(7,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo.x));
188  texturesSticks[res.Key].SetPixel(8,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo.y));
189  texturesSticks[res.Key].SetPixel(9,res.Value,EncodeFloatToColor.EncodeFloatRGBA(atomTwo.z));
190  texturesToUpdate.Add(res.Key);
191  }
192  }
193  ApplyTextures();
194 
195  }
196 
197 
198 
202  public override void DisableRenderers() {
203  for (int i=0; i< hstick_meshes.Length; i++)
204  hstick_meshes[i].GetComponent<Renderer>().enabled = false;
205  }
206 
207 
211  public override void EnableRenderers(){
213  for (int i=0; i< hstick_meshes.Length; i++)
214  hstick_meshes[i].GetComponent<Renderer>().enabled = true;
215 
216  }
217 
218  public override void EnableShadows(){
219  for (int i=0; i< hstick_meshes.Length; i++){
220  hstick_meshes[i].GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.On;
221  hstick_meshes[i].GetComponent<Renderer>().receiveShadows = true;
222  hstick_meshes[i].GetComponent<Renderer>().material.shader = Shader.Find("FvNano/Sticks HyperBalls Shadow Merged");
223  }
224  }
225  public override void DisableShadows(){
226  for (int i=0; i< hstick_meshes.Length; i++){
227  hstick_meshes[i].GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.Off;
228  hstick_meshes[i].GetComponent<Renderer>().receiveShadows = false;
229  hstick_meshes[i].GetComponent<Renderer>().material.shader = Shader.Find("FvNano/Sticks HyperBalls Merged");
230  }
231  }
232  public override void showHydrogens(bool hide){
233 
234 
235  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
236 
237 
238  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
239  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
240  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
241  if(Molecule.Model.MoleculeModel.atomsTypelist[idAtom1].type == "H" ||
242  Molecule.Model.MoleculeModel.atomsTypelist[idAtom2].type == "H") {
243  if(coordStickTexture.TryGetValue(i,out res)){
244  if(hide)
245  texturesSticks[res.Key].SetPixel(10,res.Value,Vector4.zero);
246  else
247  texturesSticks[res.Key].SetPixel(10,res.Value,Vector4.one);
248 
249  texturesToUpdate.Add(res.Key);
250  }
251  }
252  }
253  ApplyTextures();
254 
255  }
256 
257  public void ResetBrightness(){
258  for(int i=0;i<materialsSticks.Length;i++)
259  materialsSticks[i].SetFloat("_Brightness",HBallManager.brightness);
260  resetBrightness = false;
261  }
262  public void ResetTexture(){
263  for(int i=0;i<materialsSticks.Length;i++){
264  materialsSticks[i].SetTexture("_MatCap",texture);
265  materialsSticks[i].SetTexture("_MatCap2",texture);
266 
267  }
268  resetTexture = false;
269  }
270  public void ResetShrink(){
271  for(int i=0;i<materialsSticks.Length;i++)
272  materialsSticks[i].SetFloat("_Shrink",RepresentationOldGUI.shrink);
273  resetShrink = false;
274  }
275 
276  public void ResetScale(){
277  for(int i=0;i<materialsSticks.Length;i++)
278  materialsSticks[i].SetFloat("_Scale",RepresentationOldGUI.linkScale);
279  resetScale = false;
280  }
281 
282  public override void ResetRadii(){
283  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
284 
285 
286  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
287  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
288  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
289  if(coordStickTexture.TryGetValue(i,out res)){
290  float oriRadius = Molecule.Model.MoleculeModel.atomsTypelist[idAtom1].radius;
291  texturesSticks[res.Key].SetPixel(0,res.Value,EncodeFloatToColor.EncodeFloatRGBA(oriRadius
294 
295  oriRadius = Molecule.Model.MoleculeModel.atomsTypelist[idAtom2].radius;
296  texturesSticks[res.Key].SetPixel(1,res.Value,EncodeFloatToColor.EncodeFloatRGBA(oriRadius
299  texturesToUpdate.Add(res.Key);
300  }
301  }
302 
303  ApplyTextures();
304  resetRadii = false;
305  }
306 
307  public void ResetColors(){
308  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
309 
310 
311  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
312  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
313  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
314  Color colAtom1 = Molecule.Model.MoleculeModel.atomsColorList[idAtom1];
315  Color colAtom2 = Molecule.Model.MoleculeModel.atomsColorList[idAtom2];
316  if(coordStickTexture.TryGetValue(i,out res)){
317  texturesSticks[res.Key].SetPixel(2,res.Value,colAtom1);
318  texturesSticks[res.Key].SetPixel(3,res.Value,colAtom2);
319  texturesToUpdate.Add(res.Key);
320  }
321  }
322  resetColors = false;
323  ApplyTextures();
324  }
325 
326  public void ResetShininess(){
327  for(int i=0;i<materialsSticks.Length;i++)
328  materialsSticks[i].SetFloat("_Shininess",shininess);
329  resetShininess = false;
330  }
331 
332 
333  public void ApplyTextures(){
334  List<int> idtex = texturesToUpdate.Distinct().ToList();
335  foreach(int id in idtex){
336  if(id != -1)
337  texturesSticks[id].Apply(false,false);
338  }
339 
340  texturesToUpdate.Clear();
341  }
342 
343  public void hideBondForAtom(int idAtom,bool show){
344  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
345 
346 
347  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
348  int idAtom1 = Molecule.Model.MoleculeModel.bondEPList[i][0];
349  int idAtom2 = Molecule.Model.MoleculeModel.bondEPList[i][1];
350  if(idAtom1 == idAtom || idAtom2 == idAtom) {
351  if(coordStickTexture.TryGetValue(i,out res)){
352  if(show)
353  texturesSticks[res.Key].SetPixel(10,res.Value,Vector4.one);
354  else
355  texturesSticks[res.Key].SetPixel(10,res.Value,Vector4.zero);
356 
357  texturesToUpdate.Add(res.Key);
358  }
359  }
360  }
361  }
362  public void ResetVisibility(){
363  KeyValuePair<int,int> res = new KeyValuePair<int,int>();
364  for(int i=0;i<Molecule.Model.MoleculeModel.bondEPList.Count;i++){
365  if(coordStickTexture.TryGetValue(i,out res)){
366  texturesSticks[res.Key].SetPixel(10,res.Value,Vector4.one);
367  texturesToUpdate.Add(res.Key);
368  }
369  }
370  ApplyTextures();
371 
372 
373  }
374 
375  //Dirty but mandatory
376  void Update(){
377  // if(UIData.bondtype != UIData.BondType.optihs) {
378  // enabled = false;
379  // return;
380  // }
381  if(resetRadii)
382  ResetRadii();
383  if(resetBrightness)
384  ResetBrightness();
385  if(resetShrink)
386  ResetShrink();
387  if(resetScale)
388  ResetScale();
389  if(resetColors)
390  ResetColors();
391  if(resetTexture)
392  ResetTexture();
393  if(resetShininess)
394  ResetShininess();
395  }
396 }
override void DisableRenderers()
Disables the renderers for the entire set of balls and sticks.
static bool resetShininess
override GameObject GetBall(int id)
Gets the GameObject from hballs at the position "id".
static float shininess
override void showHydrogens(bool hide)
override void ResetPositions()
Resets the positions of all balls.
override void SetColor(Color col, int atomNum)
Sets the color of a specific atom.
override void ResetRadii()
GameObject[] hstick_meshes
override void Init()
Initalizes this instance.
override void SetRadii(List< string > atom, string residue="All", string chain="All")
Changes the scale of the atoms.
Dictionary< int, KeyValuePair< int, int > > coordStickTexture
override void ToggleDistanceCueing(bool enabling)
override void DisableShadows()
static List< AtomModel > atomsTypelist
The type of each atom.
Texture2D[] texturesSticks
static List< Vector3 > atomsIMDSimulationLocationlist
The coordinates of each atom, simulated through an IMD simulation.
static bool resetScale
override void ResetIMDSimulationPositions()
Resets the positions of all balls.
static Vector4 EncodeFloatRGBA(float v)
static float scaleSticks
static bool resetTexture
override void DestroyAll()
Disable and destroys all the Sticks.
override void SetColor(Color col, List< string > atom, string residue="All", string chain="All")
Sets the color of atoms.
void hideBondForAtom(int idAtom, bool show)
override void EnableRenderers()
Enables the renderers for the entire set of balls and sticks.
static bool resetBrightness
static void setCurrentBondManager(GenericManager manager)
Definition: UnityMolMain.cs:68
static Texture texture
static bool resetRadii
Material[] materialsSticks
static List< Color > atomsColorList
The color of each atom.
List< int > texturesToUpdate
override void SetRadii(int id)
Changes the scale for Atom selection.
static List< int[]> bondEPList
The bonds between atoms.
override void EnableShadows()
static bool resetColors
static float brightness
Definition: HBallManager.cs:82
static bool resetShrink
Definition: GUIDisplay.cs:66
static List< float > atomsLocalScaleList
static List< float[]> atomsLocationlist
The coordinates of each atom.