UnityMol  0.9.6-875
UnityMol viewer / In developement
QuadAtoms.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 
6 public class QuadAtoms : MonoBehaviour {
7  private List<float[]> atoms; // could be static at this point, though not for long
8  private List<AtomModel> typeList;
9  private static float radius = 0.2f;
10  public Material mat;
11  public Camera mCamera;
12 // private bool init = false;
13 // private GameObject[] planes;
14  private GameObject[] spheres;
15  Vector3 cX, cY;
16 
17  // Use this for initialization
18  public void Init () {
19  // Debug.Log("Hi! I'm a Quad Renderer!");
21  typeList = MoleculeModel.atomsTypelist;
22  mat = new Material(Shader.Find("Transparent/Cutout/Diffuse"));
23  mat.color = Color.blue;
24  mCamera = Camera.main;
25  cX = radius * (mCamera.transform.rotation * Vector3.right);
26  cY = radius * (mCamera.transform.rotation * Vector3.up);
27 // transform.LookAt(transform.position + mCamera.transform.rotation * Vector3.back, mCamera.transform.rotation * Vector3.up);
28 // planes = new GameObject[atoms.Count];
29  spheres = new GameObject[atoms.Count];
30  for(int i=0; i<atoms.Count; i++)
31  {
32 // DrawSphere(i);
33  DrawPlane(i);
34  }
35 // init = true;
36  }
37 
38  private void DrawPlane(int i)
39  {
40  float[] atom = atoms[i];
41  Color col = typeList[i].baseColor;
42  float x = atom[0];
43  float y = atom[1];
44  float z = atom[2];
45 
46  Mesh mesh = new Mesh();
47  GetComponent<MeshFilter>().mesh = mesh;
48 
49  Vector3 pos = new Vector3(x,y,z);
50  Vector3[] vertices = new Vector3[4];
51 
52 
53  vertices[0] = pos - cX + cY;
54  vertices[1] = pos + cX + cY;
55  vertices[2] = pos - cX - cY;
56  vertices[3] = pos + cX - cY;
57 
58  mesh.vertices = vertices;
59 
60  int[] triangles = new int[6];
61 
62  // Top left
63  triangles[0] = 0;
64  triangles[1] = 1;
65  triangles[2] = 2;
66 
67  // Bottom right
68  triangles[3] = 1;
69  triangles[4] = 2;
70  triangles[5] = 3;
71 
72  mesh.triangles = triangles;
73 
74  Vector3[] normals = new Vector3[4];
75 
76  normals[0] = -Vector3.forward;
77  normals[1] = -Vector3.forward;
78  normals[2] = -Vector3.forward;
79  normals[3] = -Vector3.forward;
80 
81  mesh.normals = normals;
82 
83  Vector2[] uv = new Vector2[4];
84 
85  uv[0] = new Vector2(0, 0);
86  uv[1] = new Vector2(1, 0);
87  uv[2] = new Vector2(0, 1);
88  uv[3] = new Vector2(1, 1);
89 
90  mesh.uv = uv;
91 
92  Color[] colors = new Color[4];
93  for(int j=0; i<4; i++)
94  colors[j] = col;
95 
96  mesh.colors = colors;
97 
98 /*
99  GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
100  plane.transform.Translate(new Vector3(x,y,z));
101  plane.transform.localScale = new Vector3(radius, radius, radius);
102  //plane.transform.Rotate(new Vector3(-90,0,0));
103  plane.transform.parent = this.transform;
104  plane.renderer.material.color = col;
105  planes[i] = plane;
106 */
107  }
108 
109 
110  private void DrawSphere(int i)
111  {
112  float[] atom = atoms[i];
113  Color col = typeList[i].baseColor;
114  float x = atom[0];
115  float y = atom[1];
116  float z = atom[2];
117  GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
118  sphere.transform.Translate(new Vector3(x,y,z));
119  sphere.transform.parent = this.transform;
120  sphere.GetComponent<Renderer>().material.color = col;
121  spheres[i] = sphere;
122  }
123 
124 }
Camera mCamera
Definition: QuadAtoms.cs:11
static List< AtomModel > atomsTypelist
The type of each atom.
List< float[]> atoms
Definition: QuadAtoms.cs:7
void DrawPlane(int i)
Definition: QuadAtoms.cs:38
Material mat
Definition: QuadAtoms.cs:10
Vector3 cX
Definition: QuadAtoms.cs:15
Vector3 cY
Definition: QuadAtoms.cs:15
void Init()
Definition: QuadAtoms.cs:18
GameObject[] spheres
Definition: QuadAtoms.cs:14
static float radius
Definition: QuadAtoms.cs:9
List< AtomModel > typeList
Definition: QuadAtoms.cs:8
void DrawSphere(int i)
Definition: QuadAtoms.cs:110
static List< float[]> atomsLocationlist
The coordinates of each atom.