UnityMol  0.9.6-875
UnityMol viewer / In developement
TransparentFocus.cs
Go to the documentation of this file.
1  // ------------------------------------------------------------------------------
2 // <autogenerated>
3 // This code was generated by a tool.
4 // Mono Runtime Version: 4.0.30319.1
5 //
6 // Changes to this file may cause incorrect behavior and will be lost if
7 // the code is regenerated.
8 // </autogenerated>
9 // ------------------------------------------------------------------------------
10 using System;
11 
12 using UnityEngine;
13 using System.Collections;
14 using System.Collections.Generic;
15 using UI;
16 using Molecule.Model;
17 using System.Diagnostics;
18 using GuidedNav;
19 
20 public class TransparentFocus : MonoBehaviour {
21 
22  public Vector3 target;
23  public Shader shaderDifuse;
24  public Shader shaderTransparent;
25  public GameObject o;
26  public GameObject parentObj;
27  private RaycastHit[] backupHits;
28  private int count = 0;
29  private ArrayList AtomsHit = new ArrayList();
30  private ArrayList AtomsHitCurrent = new ArrayList();
31  private ArrayList TransparentAtoms = new ArrayList();
32  private Vector3 sphere_start = Vector3.zero;
33 
34  // Use this for initialization
35  void Start ()
36  {
37  shaderDifuse = Shader.Find("Diffuse");
38  shaderTransparent = Shader.Find("Transparent/Diffuse");
39  }
40 
44  void MakeTransparent(int c)
45  {
46  UnityEngine.Debug.LogWarning("Make transparent");
47  for(int i=0; i<c; i++)
48  {
49  //Color k = Color.white;
50  o = (GameObject) AtomsHit[i];
51  o.GetComponent<Renderer>().material.shader = shaderTransparent;
52  Color k = o.GetComponent<Renderer>().material.color;
53  k.a = 0.1f;
54  o.GetComponent<Renderer>().material.color = k;
55  if(i == 0){
56  UnityEngine.Debug.LogWarning("Position: "+ o.transform.position);
57  }
58  if(!TransparentAtoms.Contains(o))
59  {
60  TransparentAtoms.Add(o);
61  }
62  }
63  }
64 
68  void MakeOpaque()
69  {
70  //UnityEngine.Debug.LogWarning("Make opaque");
71  for(int i=0; i<TransparentAtoms.Count; i++)
72  {
73  if(!AtomsHitCurrent.Contains(TransparentAtoms[i]))
74  {
75  UnityEngine.Debug.LogWarning("Atom opacification");
76  o = (GameObject) TransparentAtoms[i];
77  o.GetComponent<Renderer>().material.shader = shaderDifuse;
78  Color k = o.GetComponent<Renderer>().material.color;
79  k.a = 1.0f;
80  o.GetComponent<Renderer>().material.color = k;
81  o.layer = 0;
82  TransparentAtoms.Remove(o);
83  AtomsHit.Remove(o);
84  }
85  }
86  AtomsHitCurrent.Clear();
87  }
88 
89  // Update is called once per frame
90  void Update ()
91  {
92  // We apply the transparent focus only when the optimal view mode is activated by the user
93  if((UIData.atomtype == UIData.AtomType.sphere) && (UIData.optim_view))
94  {
95  RaycastHit[] hits;
96  target = GNParameters.optim_target;
97  double distance = Vector3.Distance(transform.position, target) * 0.50;
98  // UnityEngine.Debug.Log ("Distance: "+distance*2);
99  sphere_start = transform.position - target;
100  float radius = (float) distance / 4;
101  //float radius = 15.0f;
102  // UnityEngine.Debug.LogWarning("Radius: "+radius);
103  sphere_start = sphere_start.normalized * radius;
104  sphere_start = transform.position + sphere_start;
105  // UnityEngine.Debug.LogWarning("Sphere start: "+sphere_start);
106  // UnityEngine.Debug.LogWarning("Ray length: "+(Vector3.Distance(sphere_start, target) - radius) *0.9f);
107  // A sphere is thrown from behind the camera to the target and each atoms on the way is stored in an array
108  hits = Physics.SphereCastAll(sphere_start, radius, target - transform.position, (Vector3.Distance(sphere_start, target) - radius) *0.98f, 1);
109  // The maximum number of colliders stored by SphereCastAll is 128, we need to iterate in order to get the complete list of atoms
110  if(hits.Length == 128)
111  {
112  while (hits.Length > 0)
113  {
114  for(int j = 0; j<hits.Length; j++)
115  {
116  o = hits[j].collider.gameObject;
117  if(o.GetComponent<Renderer>().material.color.a != 0.1f)
118  {
119  if(o.GetComponent<Renderer>().material.color.a != 0.1f)
120  {
121  o.layer = 1;
122  AtomsHit.Add(o);
123  count = count +1;
124  }
125  }
126  }
127  hits = Physics.SphereCastAll(sphere_start, radius, target - transform.position, (Vector3.Distance(sphere_start, target) - radius) *0.98f, 1);
128  }
129 
130  UnityEngine.Debug.LogWarning("Number of hits: "+count);
131  if(count > 0){
132  MakeTransparent(count);
133  }
134  count = 0;
135  }
136  else if(hits.Length > 0)
137  {
138  for(int j = 0; j<hits.Length; j++)
139  {
140  o = hits[j].collider.gameObject;
141  if(o.GetComponent<Renderer>().material.color.a != 0.1f)
142  {
143 
144  o.layer = 1;
145  AtomsHit.Add(o);
146  count = count +1;
147  }
148  }
149  UnityEngine.Debug.Log("Number of hits: "+count);
150  if(count > 0){
151  MakeTransparent(count);
152  }
153  count = 0;
154  }
155  else
156  {
157  if(count > 0){
158  MakeTransparent(count);
159  }
160  count = 0;
161  }
162 
163  // Get current atoms between camera and target
164  hits = Physics.SphereCastAll(sphere_start, radius, target - transform.position, (Vector3.Distance(sphere_start, target) - radius) *0.98f, 2);
165  while(hits.Length > 0){
166  for(int j = 0; j<hits.Length; j++)
167  {
168  o = hits[j].collider.gameObject;
169  o.layer = 2;
170  AtomsHitCurrent.Add(o);
171  }
172  hits = Physics.SphereCastAll(sphere_start, radius, target - transform.position, (Vector3.Distance(sphere_start, target) - radius) *0.98f, 2);
173  }
174  for(int k=0; k < AtomsHitCurrent.Count; k++)
175  {
176  o = (GameObject) AtomsHitCurrent[k];
177  o.layer = 1;
178  }
179  MakeOpaque();
180  }
181  }
182 
183  void SetDifuseShading(GameObject f)
184  {
185  if(f.GetComponent<Renderer>().material.color.a == 1)
186  {
187  f.GetComponent<Renderer>().material.shader = shaderDifuse;
188  }
189  }
190 
192  {
193  Gizmos.color = Color.red;
194  Gizmos.DrawRay(sphere_start, target - sphere_start);
195  }
196 
197 }
198 
GameObject parentObj
void SetDifuseShading(GameObject f)
static bool optim_view
Definition: UIData.cs:203
ArrayList TransparentAtoms
static Vector3 optim_target
3D coordinates of the target.
RaycastHit[] backupHits
ArrayList AtomsHitCurrent
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
void MakeOpaque()
Apply opacity on atoms previously transparent but not in the focus anymore
The GNParameters class regroups settings used for Guided Navigation and GLIC spreading.
static AtomType atomtype
Definition: UIData.cs:139
void MakeTransparent(int c)
Apply transparence on atoms located between the camera and the target
Definition: GUIDisplay.cs:66