Skip to content

Instantly share code, notes, and snippets.

@nakamura001
Created August 25, 2012 07:53
Show Gist options
  • Save nakamura001/3462284 to your computer and use it in GitHub Desktop.
Save nakamura001/3462284 to your computer and use it in GitHub Desktop.
[Unity]meshデータを使ってGLクラスで描画を行う
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject targetGameObject;
private Mesh mesh;
private Vector3[] vertices;
private int[] indices;
void Start ()
{
if (targetGameObject != null) {
MeshFilter meshFilter = targetGameObject.GetComponent<MeshFilter> ();
if (meshFilter) {
mesh = meshFilter.mesh;
} else {
SkinnedMeshRenderer skinnedMeshRenderer = targetGameObject.GetComponent<SkinnedMeshRenderer> ();
if (skinnedMeshRenderer != null) {
mesh = skinnedMeshRenderer.sharedMesh;
}
}
vertices = mesh.vertices;
indices = mesh.GetIndices (0);
Debug.Log ("subMeshCount:" + mesh.subMeshCount);
}
Debug.Log ("vertices.Length:" + vertices.Length);
}
void drawLineTriangle (Vector3[] v)
{
for (int i=0; i<3; i++) {
int j = (i + 0) % 3;
int k = (i + 1) % 3;
GL.Vertex3 (v [j].x, v [j].y, v [j].z);
GL.Vertex3 (v [k].x, v [k].y, v [k].z);
}
}
void OnPostRender ()
{
GL.PushMatrix ();
GL.LoadOrtho ();
GL.Color (Color.red);
Quaternion q = Quaternion.Euler (-30f, 0, 0);
Vector3 offsetV = new Vector3 (.3f, .3f, 0f);
float s = 1.0f;
for (int subMeshCount=0; subMeshCount<mesh.subMeshCount; subMeshCount++) {
Debug.Log ("mesh.GetTopology (subMeshCount):" + mesh.GetTopology (subMeshCount));
switch (mesh.GetTopology (subMeshCount)) {
case MeshTopology.Lines:
GL.Begin (GL.LINES);
for (int i=0; i<indices.Length/3; i++) {
Vector3[] v = new Vector3[3];
v [0] = q * vertices [indices [i * 3 + 0]] + offsetV;
v [0] *= s;
v [1] = q * vertices [indices [i * 3 + 1]] + offsetV;
v [1] *= s;
v [2] = q * vertices [indices [i * 3 + 2]] + offsetV;
v [2] *= s;
drawLineTriangle (v);
}
break;
case MeshTopology.Triangles:
GL.Begin (GL.TRIANGLES);
break;
case MeshTopology.TriangleStrip:
GL.Begin (GL.TRIANGLE_STRIP);
Vector3 v;
for (int i=0; i<indices.Length; i++) {
v = vertices [indices [i]] * 1f;
v = q * v + offsetV;
GL.Vertex3 (v.x, v.y, v.z);
}
break;
}
}
GL.End ();
GL.PopMatrix ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment