UnityMol  0.9.6-875
UnityMol viewer / In developement
SurfaceManager.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 using UI;
6 
7 public class SurfaceManager {
8  /**********************
9  * Static variables *
10  **********************/
11  public static float brightness = 1f;
12  public static float colorWeight = 0.5f;
13 
14  public static bool pdbGen = false;// bool of density grid. true when density was calculated
15  public static GameObject pdb2den;
16  //
17  // Cut parameters
18  //
19  public static float cutX = 1f;
20  public static float cutY = 0f;
21  public static float cutZ = 0f;
22  public static float depthCut = 40f;
23  public static float depthCutMin = 0;
24  public static float depthCutMax = 0;
25 
26  // If true, a strictly correct algorithm will be used instead of the AtomTree
27  // heuristics. I don't think there's much point in doing that, since AtomTree
28  // seems to produce the same results in most cases.
29  private static bool slowColoring = false;
30  private static List<Vector3> atomLocations;
31  private static List<Color> atomColors;
32 
33  private List<GameObject> surfaceList = new List<GameObject>();
34  private List<GameObject> surfacePos = new List<GameObject>();
35  private List<GameObject> surfaceNeg = new List<GameObject>();
36 
37  private static GameObject VolumetricDensityObj = null;
39 
40  private AtomTree atomTree;
41  private float bTime;
42 
43  private float valmin = 0;
44  private float valmax = 0;
45  private string bftype = "";
46 
47  public static bool surfaceInitialized = false;
48 
49  public static ColorObject SurfaceColor = new ColorObject(Color.white); // color of surface
50  public static ColorObject SurfaceInsideColor = new ColorObject(Color.gray); // color of the inside of the surface
51 
52  public static float surfaceThreshold = 0.5f;
53  public static int cutMode = 0;//0 = no cut | 1 = static cut | 2 = mobile cut
54 
55  public static bool externalSurfaceTexture = false;
56  public static string surfaceTextureName = null;
57 
58  public static string m_last_extSurf_Path = null;
59  public static Texture2D extSurf = null;
60  public static bool volumetricDensityActivated = false;
61 
62 
63 
64  public static bool useHetatmForSurface = false;
65 
66  public static bool useSugarForSurface = false;
67 
68  private void InitTree() {
69 
70  bTime = Time.realtimeSinceStartup;
71  atomTree = AtomTree.Build();
72  }
73  public void ResetColorVertices(){
74  GameObject[] surfaceObjs = getSurfaces();
75  foreach(GameObject surfaceObj in surfaceObjs) {
76  ResetColorVertices(surfaceObj.GetComponent<MeshFilter>().mesh);
77  }
78  }
79  public void ResetColorVertices(Mesh mesh){
80  int nbVertices = mesh.vertices.Length;
81  Color32[] meshColors = new Color32[nbVertices];
82  for(int i=0; i<nbVertices; i++) {
83  meshColors[i] = Color.white;
84  }
85  mesh.colors32 = meshColors;
86  }
87  private void ColorVertices(Mesh mesh) {
88  int nbVertices = mesh.vertices.Length;
89  float valtype;
90  List<float> BFactorList = MoleculeModel.BFactorList;
91  Vector3[] vertices = mesh.vertices;
92  Color32[] meshColors = new Color32[nbVertices];
93 
96  List<float[]> atomLocs = MoleculeModel.atomsLocationlist;
97  SurfaceManager.atomLocations = new List<Vector3>();
98  for(int i=0; i<atomLocs.Count; i++) {
99  SurfaceManager.atomLocations.Add(new Vector3(atomLocs[i][0], atomLocs[i][1], atomLocs[i][2]));
100  }
102  }
103 
104  string type;
105  for(int i=0; i<nbVertices; i++) {
106  type = atomTree.GetClosestAtomType(vertices[i]);
107  if(UI.UIData.surfColChain){
108  Ribbons.InitCol();
109  meshColors[i] = Ribbons.GetColorChain(type);
110  }
111  else if(UI.UIData.surfColHydroKD){
113  meshColors[i] = HydrophobicScales.GetColorHydro(type);
114  }
115  else if(UI.UIData.surfColHydroEng){
117  meshColors[i] = HydrophobicScales.GetColorHydro(type);
118  }
119 
120  else if(UI.UIData.surfColHydroEis){
122  meshColors[i] = HydrophobicScales.GetColorHydro(type);
123  }
124  else if(UI.UIData.surfColPChim){
126  meshColors[i] = HydrophobicScales.GetColorHydro(type);
127  }
128  else if(UI.UIData.surfColHydroWO){
130  meshColors[i] = HydrophobicScales.GetColorHydro(type);
131  }
132  else if(UI.UIData.surfColBF){
133  valtype = float.Parse (type);
134  if(valmax == 0){
135  valmin = BFactorRep.GetMin(BFactorList);
136  valmax = BFactorRep.GetMax(BFactorList);
137  }
138  valtype = (valtype - valmin) / (valmax -valmin);
139  bftype = BFactorRep.GetBFStyle(valtype);
140  meshColors[i] = AtomModel.GetModel (bftype).baseColor;
141  }
142  else
143  meshColors[i] = MoleculeModel.GetAtomColor(type);
144  }
145  mesh.colors32 = meshColors;
146  }
147 
148  private Color GetClosestAtomColor(Vector3 pos) {
149  float minDist = float.MaxValue;
150  Color minColor = Color.magenta;
151  float dist;
152  for(int i=0; i<SurfaceManager.atomLocations.Count; i++) {
153  dist = Vector3.SqrMagnitude(pos - SurfaceManager.atomLocations[i]);
154  Debug.Log ("dist " + dist);
155  if(dist < minDist) {
156  minDist = dist;
157  minColor = SurfaceManager.atomColors[i];
158  }
159  }
160  return minColor;
161  }
162 
163  /*
164  * Surface Manager API
165  */
166 
167  public GameObject getParentGameObject ()
168  {
169  return GameObject.Find("SurfaceManager");
170  }
171 
172  public void ColorVertices() {
173  GameObject[] surfaceObjs = getSurfaces();
174  foreach(GameObject surfaceObj in surfaceObjs) {
175  ColorVertices(surfaceObj.GetComponent<MeshFilter>().mesh);
176  }
177  float elapsed = 100f * (Time.realtimeSinceStartup - bTime);
178  Debug.Log("SurfaceManager::Update Atom Color total processing time: " + elapsed.ToString());
179  }
180 
181  public void ResetBrightness() {
182  GameObject[] surfaceObjs = getSurfaces();
183  for(int i=0; i<surfaceObjs.Length; i++)
184  surfaceObjs[i].GetComponent<Renderer>().material.SetFloat("_Brightness", SurfaceManager.brightness);
185  }
186 
187  public void ResetColorWeight() {
188  GameObject[] surfaceObjs = getSurfaces();
189  for(int i=0; i<surfaceObjs.Length; i++)
190  surfaceObjs[i].GetComponent<Renderer>().material.SetFloat("_ColorWeight", SurfaceManager.colorWeight);
191  }
192 
193  public static void InitCutParameters()
194  {
195  SurfaceManager.depthCutMin = -5 + Mathf.Min(new float[]{MoleculeModel.MinValue.x,
198  SurfaceManager.depthCutMax = 5 + Mathf.Max(new float[]{MoleculeModel.MaxValue.x,
202  }
203 
204  public void addSurface(GameObject surface)
205  {
206  surfaceList.Add(surface);
207  }
208  public void addSurfacePos(GameObject surface){
209  surfacePos.Add(surface);
210  }
211  public void addSurfaceNeg(GameObject surface){
212  surfaceNeg.Add(surface);
213  }
214  public GameObject[] getSurfaces()
215  {
216  return surfaceList.ToArray();
217  }
218  public GameObject[] getSurfacesNeg(){
219  return surfaceNeg.ToArray();
220  }
221  public GameObject[] getSurfacesPos(){
222  return surfacePos.ToArray();
223  }
224 
225 
226  public void DestroySurfaces() {
227  GameObject [] SurfaceManager = getSurfaces();
228  foreach (GameObject Surface in SurfaceManager)
229  {
230  surfaceList.Remove(Surface);
231  Object.Destroy(Surface);
232  }
233  surfaceInitialized = false;
234  }
235  public void DestroyPos(){
236  for(int i=0;i<surfacePos.Count;i++){
237  Object.Destroy(surfacePos[i]);
238  surfacePos.Remove(surfacePos[i]);
239  }
240 
241  }
242  public void DestroyNeg(){
243  for(int i=0;i<surfaceNeg.Count;i++){
244  Object.Destroy(surfaceNeg[i]);
245  surfaceNeg.Remove(surfaceNeg[i]);
246  }
247 
248  }
249 
250  public static void GenerateSurface() {
251  if (UIData.toggleBfac || surfaceInitialized) {
252  UIData.toggleBfac = false;
253  pdbGen = false;
255 
256  }
257 
258  if (!pdbGen) {
260  if(pdb2den == null){
261  pdb2den = new GameObject ("pdb2den OBJ");
262  pdb2den.AddComponent<PDBtoDEN> ();
263  }
264 
265  PDBtoDEN generatedensity = pdb2den.GetComponent<PDBtoDEN> ();
266 
267  generatedensity.TranPDBtoDEN ();
268  pdbGen = true;
269  }
270 
271  if (!surfaceInitialized) {
272  PDBtoDEN.ProSurface (surfaceThreshold);
273  surfaceInitialized = true;
274  }
275  }
276 
277  public static void GenerateSurfaceBfact(){
278  if(surfaceInitialized){
279  pdbGen = false;
281  }
282 
283  if (!pdbGen) {
285  UIData.toggleBfac = true;
286 
287  if(pdb2den == null){
288  pdb2den = new GameObject ("pdb2den OBJ");
289  pdb2den.AddComponent<PDBtoDEN>();
290  }
291 
292 
293  PDBtoDEN generatedensity = pdb2den.GetComponent<PDBtoDEN>();
294  generatedensity.TranPDBtoDEN ();
295  pdbGen = true;
296  }
297  if(!surfaceInitialized) {
298  PDBtoDEN.ProSurface (surfaceThreshold);
299  surfaceInitialized = true;
300  }
301  }
302  public static void ShowHideVolumetricDensity(){
303  volumetricDensityActivated = !volumetricDensityActivated;
304 
305  if(VolumetricDensityObj == null){
306  VolumetricDensityObj = new GameObject("VolumetricDensity");
307  volumetricDensityscript = VolumetricDensityObj.AddComponent<VolumetricDensity>();
308  }
309 
310  if (volumetricDensityActivated)
311  volumetricDensityscript.Init ();
312  else
313  volumetricDensityscript.Clear ();
314 
315  }
316 
317  public static void ShowHideSurfaces(){
319  foreach (GameObject Surface in SurfaceManager) {
320  Surface.GetComponent<Renderer>().enabled = !Surface.GetComponent<Renderer>().enabled;
321  }
322 
323  }
324 
325 
326  public void ShowHideSurfaceNeg(){
327  GameObject[] SurfacesNeg = UnityMolMain.getSurfaceManager().getSurfacesNeg();
328  foreach (GameObject Surface in SurfacesNeg)
329  Surface.GetComponent<Renderer>().enabled = !Surface.GetComponent<Renderer>().enabled;
330  }
331 
332  public void ShowHideSurfacePos(){
334  foreach (GameObject Surface in getSurfacesPos)
335  Surface.GetComponent<Renderer>().enabled = !Surface.GetComponent<Renderer>().enabled;
336  }
337 
338  public static void SurfaceRenderUp (){
339  SurfaceManager surfaceManager = UnityMolMain.getSurfaceManager();
340  GameObject[] surfaceObjs = surfaceManager.getSurfaces();
341  Shader vertexcolored = Shader.Find ("Vertex Colored");
342  Shader matcapcut = Shader.Find ("Mat Cap Cut");
343  foreach (GameObject surfaceObj in surfaceObjs) {
344  if (surfaceObj.GetComponent<Renderer>().material.shader == vertexcolored) {
345  surfaceObj.GetComponent<Renderer>().material.shader = matcapcut;
346  surfaceObj.GetComponent<Renderer>().material.SetTexture ("_MatCap", (Texture)Resources.Load ("lit_spheres/divers/daphz1"));
347  } else {
348  surfaceManager.InitTree ();
349  surfaceManager.ColorVertices ();
350  // Init and ColorVertice already change the shader of all the SurfaceOBJ. So if you continue the foreach
351  // you'll find "VertexColored" for the next shaders and it will replace them by MatCapCut
352  break; // So you must break...
353  }
354  }
355  }
356 
357  public void resetSurfaceColors(){
358  GameObject[] Surfaces = UnityMolMain.getSurfaceManager().getSurfaces();
359  foreach (GameObject Surface in Surfaces) {
360  Surface.GetComponent<Renderer>().material.SetColor("_Color", Color.white);
361  }
362  }
363  public void updateColors(){
365  GameObject[] Surfaces = UnityMolMain.getSurfaceManager().getSurfaces();
366  foreach (GameObject Surface in Surfaces) {
367  Surface.GetComponent<Renderer>().material.SetColor("_Color", SurfaceColor.color);
368  Surface.GetComponent<Renderer>().material.SetColor("_ColorIN", SurfaceInsideColor.color);
369  }
370  }
371  public void updateCut(){
372  GameObject[] Surfaces = UnityMolMain.getSurfaceManager().getSurfaces();
373  foreach (GameObject Surface in Surfaces) {
374  Surface.GetComponent<Renderer>().material.SetVector("_SurfacePos", Surface.transform.position);
375  Surface.GetComponent<Renderer>().material.SetFloat("_depthcut", SurfaceManager.depthCut);
376  Surface.GetComponent<Renderer>().material.SetFloat("_cutX", SurfaceManager.cutX);
377  Surface.GetComponent<Renderer>().material.SetFloat("_cutY", SurfaceManager.cutY);
378  Surface.GetComponent<Renderer>().material.SetFloat("_cutZ", SurfaceManager.cutZ);
379  Surface.GetComponent<Renderer>().material.SetFloat("_cut", (float)cutMode);
380  }
381  }
382  public void updateTexture(){
383  GameObject[] Surfaces = UnityMolMain.getSurfaceManager().getSurfaces();
384  foreach (GameObject Surface in Surfaces) {
385  if(extSurf!=null || surfaceTextureName!=null){
386  if(externalSurfaceTexture){
387  if(!UIData.grayscalemode)
388  Surface.GetComponent<Renderer>().material.SetTexture("_MatCap",extSurf);
389  else{
390  Surface.GetComponent<Renderer>().material.SetTexture("_MatCap",ToGray(extSurf));
391  }
392  }
393  else{
394  if(!UIData.grayscalemode)
395  Surface.GetComponent<Renderer>().material.SetTexture("_MatCap",(Texture)Resources.Load(surfaceTextureName)); // do not do that every frame!
396  else{
397  Surface.GetComponent<Renderer>().material.SetTexture("_MatCap",ToGray((Texture)Resources.Load(surfaceTextureName)));
398  }
399  }
400  }
401  else
402  Surface.GetComponent<Renderer>().material.SetTexture("_MatCap",(Texture)Resources.Load("lit_spheres/divers/daphz1"));
403  }
404 
405  }
406 
407  public static void usePropertiesColor() {
409  // SurfaceManager.SurfaceRenderUp();
412  UIData.surfColPChim = false;
413  }
414 
415  public static void useBFactorColor() {
417  // SurfaceManager.SurfaceRenderUp();
420  UIData.surfColBF = false;
421  }
422 
423  public static void useChainColor() {
425  // SurfaceManager.SurfaceRenderUp();
428  UIData.surfColChain = false;
429  }
430 
431  public static void useKyteAndDoolittleColor() {
433  // SurfaceManager.SurfaceRenderUp();
436  UIData.surfColHydroKD = false;
437  }
438 
439  public static void useEnglemanColor() {
441  // SurfaceManager.SurfaceRenderUp();
444  UIData.surfColHydroEng = false;
445  }
446 
447  public static void useEinsenbergColor() {
449  // SurfaceManager.SurfaceRenderUp();
452  UIData.surfColHydroEis = false;
453  }
454 
455  public static void useWhiteOctanolColor() {
457  // SurfaceManager.SurfaceRenderUp();
460  UIData.surfColHydroWO = false;
461  }
462 
463  public static void setBrightness(float value) {
464  SurfaceManager.brightness = value;
466  }
467 
468  public static void setColorWeight(float value) {
469  SurfaceManager.colorWeight = value;
471  }
472 
473  public void RecenterSurfaces(Vector3 pos){
474  for(int i=0;i<surfaceList.Count;i++)
475  surfaceList[i].transform.position += pos+MoleculeModel.Offset;
477  }
478 
479  //From HBallManager
489  public Texture2D ToGray(Texture texture){ // Should be moved out of HBallManager since it's also used for Surface Textures
490  Texture2D tex2D = (Texture2D)texture;
491  Texture2D grayTex = new Texture2D(tex2D.width, tex2D.height);
492  float grayScale;
493  float alpha;
494  for (int y = 0; y < tex2D.height; ++y) {
495  for (int x = 0; x < tex2D.width; ++x) {
496  grayScale = tex2D.GetPixel(x, y).r * 0.21f + tex2D.GetPixel(x, y).g * 0.71f + tex2D.GetPixel(x, y).b * 0.07f;
497  alpha = tex2D.GetPixel(x, y).a;
498  grayTex.SetPixel (x, y, new Color(grayScale, grayScale, grayScale, alpha));
499  }
500  }
501  grayTex.Apply();
502  return grayTex;
503  }
504 
505  public void SetTransparentSurface(bool transparent){
506  for(int i=0;i<surfaceList.Count;i++){
507  if(transparent)
508  surfaceList[i].GetComponent<MeshRenderer>().material = (Material) Resources.Load("Materials/transSurface");
509  else
510  surfaceList[i].GetComponent<MeshRenderer>().material = (Material) Resources.Load("Materials/matcapSurface");
511  }
512  }
513 
514  public void SetTransparentSurfaceColor(Color newcolor){
515  for(int i=0;i<surfaceList.Count;i++){
516  surfaceList[i].GetComponent<MeshRenderer>().material.color = newcolor;
517  }
518  }
519 
520 }
static bool volumetricDensityActivated
static float cutX
static void useChainColor()
static ColorObject SurfaceColor
static GameObject VolumetricDensityObj
List< GameObject > surfaceNeg
static float depthCutMax
static bool useHetatmForSurface
static float depthCutMin
static float cutZ
static bool surfColBF
Definition: UIData.cs:188
static bool surfColChain
Definition: UIData.cs:181
void SetTransparentSurface(bool transparent)
static float cutY
static void useBFactorColor()
static void useEnglemanColor()
void DestroySurfaces()
static void SurfaceRenderUp()
void addSurfaceNeg(GameObject surface)
void TranPDBtoDEN(float resolution=DEFAULT_RESOLUTION, bool cap=true)
Definition: PDBtoDEN.cs:141
static void useEinsenbergColor()
static AtomModel GetModel(string type)
Definition: AtomModel.cs:257
static void ShowHideSurfaces()
static void GenerateSurfaceBfact()
static bool externalSurfaceTexture
static bool pdbGen
GameObject[] getSurfaces()
static Vector3 Offset
The offset for the molecule.
static AtomTree Build()
Definition: AtomTree.cs:231
void RecenterFieldLines(Vector3 pos)
GameObject[] getSurfacesPos()
Color GetClosestAtomColor(Vector3 pos)
static void ProSurface(float seuil)
Definition: PDBtoDEN.cs:340
GameObject[] getSurfacesNeg()
void RecenterSurfaces(Vector3 pos)
static GameObject pdb2den
static bool slowColoring
void ShowHideSurfacePos()
static void InitEisenberg()
static bool surfColPChim
Definition: UIData.cs:187
static void InitCutParameters()
static void ShowHideVolumetricDensity()
Color color
Definition: ColorObject.cs:72
void ShowHideSurfaceNeg()
void resetSurfaceColors()
static void setBrightness(float value)
static Texture2D extSurf
static bool surfColHydroKD
Definition: UIData.cs:182
static List< float > BFactorList
Bfactor of each atom.
static bool surfColHydroEng
Definition: UIData.cs:183
static bool toggleBfac
Definition: UIData.cs:195
string GetClosestAtomType(Vector3 pos)
Definition: AtomTree.cs:151
static Color GetAtomColor(string atomType)
static float GetMax(List< float > ListValues)
Find the max value of a list of float.
Definition: BFactorRep.cs:179
static VolumetricDensity volumetricDensityscript
static void useWhiteOctanolColor()
static void usePropertiesColor()
void ResetBrightness()
static float depthCut
static string surfaceTextureName
static ElectrostaticManager getElectrostaticManager()
Definition: UnityMolMain.cs:42
static string m_last_extSurf_Path
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
List< GameObject > surfaceList
AtomTree atomTree
static List< Vector3 > atomLocations
static Color32 GetColorChain(string type)
Definition: Ribbons.cs:1226
static ColorObject SurfaceInsideColor
static string GetBFStyle(float BFValue)
Definition: BFactorRep.cs:136
static Vector3 MaxValue
The "biggest" corner of the bounding box that encloses the molecule.
static float colorWeight
override void Init()
Initializes this instance.
static void useKyteAndDoolittleColor()
static List< Color > atomsColorList
The color of each atom.
GameObject getParentGameObject()
static bool useSugarForSurface
void ColorVertices(Mesh mesh)
static void InitCol()
Definition: Ribbons.cs:1208
static Vector3 MinValue
The "smallest" corner of the bounding box that encloses the molecule.
static List< Color > atomColors
static void InitWhiteOct()
void SetTransparentSurfaceColor(Color newcolor)
static SurfaceManager getSurfaceManager()
Definition: UnityMolMain.cs:24
void ResetColorWeight()
void addSurfacePos(GameObject surface)
static float GetMin(List< float > ListValues)
Find the min value of a list of float.
Definition: BFactorRep.cs:166
static bool grayscalemode
Definition: UIData.cs:159
static float brightness
static void InitKyteDoo()
void addSurface(GameObject surface)
static bool surfaceInitialized
void ResetColorVertices(Mesh mesh)
static void GenerateSurface()
void Clear()
Clears this instance.
Definition: Volumetric.cs:105
Texture2D ToGray(Texture texture)
Return the grayscale version of a texture
static void InitEngleman()
static int cutMode
static float surfaceThreshold
static bool surfColHydroWO
Definition: UIData.cs:184
void ResetColorVertices()
static void InitPhysChim()
Definition: GUIDisplay.cs:66
static void setColorWeight(float value)
List< GameObject > surfacePos
static Color32 GetColorHydro(string type)
static bool surfColHydroEis
Definition: UIData.cs:185
static List< float[]> atomsLocationlist
The coordinates of each atom.