UnityMol  0.9.6-875
UnityMol viewer / In developement
Clip4RayCasting.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 
69 public class Clip4RayCasting : MonoBehaviour {
70 
71  public static GameObject CreateClip (){
72 
73 GameObject obj=new GameObject("Clip");
74 
75 obj.AddComponent<SkinnedMeshRenderer>();
76 
77 SkinnedMeshRenderer renderer_= obj.GetComponent<SkinnedMeshRenderer>();
78 
79 // Build basic mesh_
80 
81 Mesh _mesh= new Mesh();
82 
83 _mesh.vertices = new Vector3[] {new Vector3(-1f, -1f, 0), new Vector3(1f, -1f, 0), new Vector3(-1f, 1f, 0),new Vector3(1f, 1f, 0)};
84 //_mesh.vertices = new Vector3[] {new Vector3(-0.5f, -0.5f, 0), new Vector3(0.5f, -0.5f, 0), new Vector3(-0.5f, 0.5f, 0),new Vector3(0.5f, 0.5f, 0)};
85 
86 _mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)};
87 
88 _mesh.triangles = new int[] {0, 1, 2, 1, 3, 2};
89 
90 _mesh.RecalculateNormals();
91 
92 renderer_.material = new Material (Shader.Find(" Diffuse"));
93 
94 
95 
96 // Assign bone weights to mesh_
97 
98 // We use 2 bones. One for the lower vertices, one for the upper vertices.
99 
100 BoneWeight[] weights= new BoneWeight[4];
101 
102 
103 
104 weights[0].boneIndex0 = 0;
105 
106 weights[0].weight0 = 1;
107 
108 
109 
110 weights[1].boneIndex0 = 0;
111 
112 weights[1].weight0 = 1;
113 
114 
115 
116 weights[2].boneIndex0 = 1;
117 
118 weights[2].weight0 = 1;
119 
120 
121 
122 weights[3].boneIndex0 = 1;
123 
124 weights[3].weight0 = 1;
125 
126 
127 
128 _mesh.boneWeights = weights;
129 
130 // Create Bone Transforms and Bind poses
131 
132 // One bone at the bottom and one at the top
133 
134 Transform[] bones= new Transform[2];
135 
136 Matrix4x4[] bindPoses= new Matrix4x4[2];
137 
138 
139 
140 bones[0] = new GameObject ("Lower").transform;
141 
142 bones[0].parent = obj.transform;
143 
144 // Set the position relative to the parent
145 
146 bones[0].localRotation = Quaternion.identity;
147 
148 //bones[0].localPosition = new Vector3 (0, -0.5f, 0);
149 bones[0].localPosition = new Vector3 (0, -1f, 0);
150 
151 // The bind pose is bone's inverse transformation matrix
152 
153 // In this case the matrix we also make this matrix relative to the root
154 
155 // So that we can move the root game object around freely
156 
157 bindPoses[0] = bones[0].worldToLocalMatrix * obj.transform.localToWorldMatrix;
158 
159 bones[1] = new GameObject ("Upper").transform;
160 
161 bones[1].parent = obj.transform;
162 
163 // Set the position relative to the parent
164 
165 bones[1].localRotation = Quaternion.identity;
166 
167 //bones[1].localPosition = new Vector3 (0, 0.5f, 0);
168 bones[1].localPosition = new Vector3 (0, 1f, 0);
169 // The bind pose is bone's inverse transformation matrix
170 
171 // In this case the matrix we also make this matrix relative to the root
172 
173 // So that we can move the root game object around freely
174 
175 bindPoses[1] = bones[1].worldToLocalMatrix * obj.transform.localToWorldMatrix;
176 
177 
178 
179 _mesh.bindposes = bindPoses;
180 
181 // Assign bones and bind poses
182 
183 renderer_.bones = bones;
184 
185 renderer_.sharedMesh = _mesh;
186 
187 
188 return obj;
189 }
190 }
static GameObject CreateClip()