UnityMol  0.9.6-875
UnityMol viewer / In developement
FieldLineStyle.cs
Go to the documentation of this file.
1 
66 using System;
67 using UnityEngine;
68 using UnityEngine.Rendering;
69 using System.Collections;
70 using System.Collections.Generic;
71 using Molecule.Model;
72 using UI;
73 using Molecule.Control;
74 using System.Globalization;
75 
76 public class FieldLineStyle
77 {
78  public FieldLineStyle()
79  {
80  }
81 
82  public static void DisplayFieldLine()
83  {
84 
86  Color c1 = Color.white;
87  Debug.Log("Entering :: DisplayFieldLine");
88 
89  float disttot = 0.0f;
90  float xdif = 0.0f;
91  float ydif = 0.0f;
92  float zdif = 0.0f;
93  Vector3 locationbegin = new Vector3(0,0,0);
94  Vector3 curent = new Vector3(0,0,0);
95  Transform FiledLineManager = new GameObject("FieldLineManager").transform;//GameObject.Find("FieldLineManager").transform;
96  for(int i=0;i<MoleculeModel.FieldLineList.Count;i++)
97 // for(int i=7;i<8;i++)
98  {
99  GameObject FieldLineF = new GameObject();
100  FieldLineF.name="FieldLineF"+i;
101  FieldLineF.transform.parent = FiledLineManager;
103  LineRenderer lineRenderer = FieldLineF.AddComponent<LineRenderer>();
104  lineRenderer.useWorldSpace = false;
105  lineRenderer.material = new Material (Shader.Find("Custom/FieldLineCg"));
106  lineRenderer.SetColors(c1, c1);
107  lineRenderer.SetWidth(0.2f,0.2f);
108 // lineRenderer.SetVertexCount(((ArrayList)MoleculeModel.FieldLineList[i]).Count);
109  lineRenderer.SetVertexCount(5);
110  lineRenderer.shadowCastingMode = ShadowCastingMode.Off;
111  lineRenderer.receiveShadows = false;
112 
113  //distance
114 // float[] leng= new float[((ArrayList)MoleculeModel.FieldLineList[i]).Count];
115 // leng[0] = UnityEngine.Random.value * 4.0f;
116 
117  //For the static cut we need to store the real world position of each vertex.
118  //luMin will store the min values in x,y,z for the line's vertices.
119  //luWidth will store the max - min
120  Vector3 luMin = new Vector3(float.MaxValue,float.MaxValue,float.MaxValue);
121  Vector3 luWidth = new Vector3(float.MinValue,float.MinValue,float.MinValue);
122  ArrayList posLookup = new ArrayList();
123 
124 
125  int j;
126  int nbPoint =0;
127  int nbsegment=0;
128  for(j=0;j<((List<Vector3>)MoleculeModel.FieldLineList[i]).Count;j++)
129 // for(j=0;j<1;j++)
130  {
131 // Vector3 location=new Vector3(((Vector3)((ArrayList)MoleculeModel.FieldLineList[i])[j]).x,((Vector3)((ArrayList)MoleculeModel.FieldLineList[i])[j]).y,((Vector3)((ArrayList)MoleculeModel.FieldLineList[i])[j]).z);
132  Vector3 location=new Vector3( ((MoleculeModel.FieldLineList[i])[j]).x,
133  ((MoleculeModel.FieldLineList[i])[j]).y,
134  ((MoleculeModel.FieldLineList[i])[j]).z);
135 
136  if (j==0){
137  lineRenderer.SetPosition(j, location);
138  nbPoint +=1;
139  lineRenderer.SetVertexCount(nbPoint+1);
140  locationbegin = location;
141 
142  posLookup.Add(location);
143  luMin = location;
144  //let store the max values in luWidth for the moment
145  luWidth = location;
146 
147  }
148 
149  if(j>0){
150 // float dist = Vector3.Distance (((Vector3)((ArrayList)MoleculeModel.FieldLineList[i])[j]), ((Vector3)((ArrayList)MoleculeModel.FieldLineList[i])[j-1]));
151  float dist = Vector3.Distance ( ((MoleculeModel.FieldLineList[i])[j]),
152  ((MoleculeModel.FieldLineList[i])[j-1]) );
153  disttot += dist;
154 // Debug.Log("dist: " + dist + " disttot:" + disttot);
155 // leng[j] = leng[j-1] + dist;
156  if(disttot>1){
157  xdif = location[0] - locationbegin[0]; //Debug.Log("xdiff: " + xdif);
158  ydif = location[1] - locationbegin[1]; //Debug.Log("ydiff: " + ydif);
159  zdif = location[2] - locationbegin[2]; //Debug.Log("zdiff: " + zdif);
160 // Debug.Log("longueur "+disttot +" " + (int)disttot);
161 
162  if(disttot-(int)disttot <0.5)
163  nbsegment = 2*(int)disttot;
164  else
165  nbsegment = 2 *(int)disttot+1;
166 
167  int k;
168 // Debug.Log("Begin "+locationbegin);
169  curent = locationbegin;
170  for (k=0; k<nbsegment;k++){
171 // Debug.Log("curent" + curent);
172  curent[0]+=(xdif/nbsegment);
173  curent[1]+=(ydif/nbsegment);
174  curent[2]+=(zdif/nbsegment);
175 // Debug.Log("curent" + curent);
176  lineRenderer.SetPosition(nbPoint, curent);
177 // Debug.Log(" "+new Vector3((locationbegin[0]+xdif)/nbsegment,(locationbegin[1]+ydif)/nbsegment,(locationbegin[2]+ydif)/nbsegment));
178  nbPoint +=1;
179  lineRenderer.SetVertexCount(nbPoint+1);
180 
181  posLookup.Add(curent);
182  for(int ind=0; ind<3; ind++)
183  {
184  luMin[ind] = Mathf.Min(luMin[ind],curent[ind]);
185  //let store the max values in luWidth for the moment
186  luWidth[ind] = Mathf.Max(luWidth[ind],curent[ind]);
187  }
188 
189  }
190  disttot = 0;
191  locationbegin = curent;
192  }
193 
194  }
195  }
196 
197  //The real positions are passed to the shader in a texture
198  //The values have to be converted to color, hence between 0 and 1. 0 matches the minVal, 1 matches the maxVal
199  //luMin and luWidth are used to compute the color value and then retrieve the real value in the shader
200  //colVal = (realVal - minVal) / widthVal
201  //realVal = (colVal * widthVal) + minVal
202  //And thus for x, y and z
203  Texture2D tex_wPos = new Texture2D(nbPoint,1,TextureFormat.ARGB32, false);
204  tex_wPos.filterMode = FilterMode.Point; //Automatic interpolation
205  tex_wPos.wrapMode = TextureWrapMode.Clamp; //Clamp to avoid periodic interpolation
206  luWidth = luWidth - luMin;
207  //Computing and storing the colValues in the texture.
208  for(j=0; j<nbPoint; ++j)
209  {
210  Vector3 p = ((Vector3)posLookup[j])-luMin;
211  tex_wPos.SetPixel(j,0,new Color(p.x/luWidth.x, p.y/luWidth.y, p.z/luWidth.z, 1.0f));
212  }
213  tex_wPos.Apply(); //Apply the new colors to the texture
214  //Binding the variables to the shader
215  lineRenderer.material.SetTexture("_PosLookup",tex_wPos);
216  lineRenderer.material.SetVector("_MinPosLookup",luMin);
217  lineRenderer.material.SetVector("_WidthPosLookup",luWidth);
218 
219  lineRenderer.SetVertexCount(nbPoint);
220 // MoleculeModel.FieldLineDist.Add(leng);
221  lineRenderer.material.SetFloat("_Unsynchronize",UnityEngine.Random.value * 10.0f);
222  lineRenderer.material.SetFloat("_timeOff",Time.time);
223  lineRenderer.material.SetColor("_Color",Color.white);
224 
225 
226  }
227  }
228 
229  }
230 }
static void DisplayFieldLine()
static ElectrostaticManager getElectrostaticManager()
Definition: UnityMolMain.cs:42
static List< List< Vector3 > > FieldLineList
void AddFieldLine(GameObject flgo)
Definition: GUIDisplay.cs:66