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