Created
February 2, 2018 17:54
-
-
Save knasa21/236453c41f15b39a0341bd587b7f9d8f to your computer and use it in GitHub Desktop.
頂点法線の可視化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class ShowVerticesNormals : MonoBehaviour | |
{ | |
MeshFilter meshFilter; | |
Mesh mesh; | |
Matrix4x4 matrix; | |
Vector3[] normals; | |
Vector3[] vertices; | |
public bool draw = false; | |
[Range(0.01f, 1.0f)] | |
public float handleSize = 1; | |
// Use this for initialization | |
void Start() | |
{ | |
meshFilter = new MeshFilter(); | |
mesh = new Mesh(); | |
matrix = new Matrix4x4(); | |
normals = new Vector3[0]; | |
vertices = new Vector3[0]; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
void GetNormals() | |
{ | |
meshFilter = GetComponent<MeshFilter>(); | |
mesh = meshFilter.mesh; | |
normals = mesh.normals; | |
vertices = mesh.vertices; | |
matrix = meshFilter.transform.localToWorldMatrix; | |
} | |
#if UNITY_EDITOR | |
private void OnDrawGizmos() | |
{ | |
if (!draw) return; | |
if (normals == null || vertices == null) return; | |
if (normals.Length != 0 && vertices.Length != 0) { | |
for (int index = 0; index < normals.Length; index++) { | |
Vector3 normal = normals[index]; | |
Vector3 vertex = vertices[index]; | |
Handles.color = new Color(normal.x, normal.y, normal.z); | |
Handles.Slider(matrix.MultiplyPoint(vertices[index]), normals[index], handleSize, Handles.ArrowHandleCap, 1f); | |
} | |
} | |
} | |
[CustomEditor(typeof(ShowVerticesNormals))] | |
public class ShowVerticesNormalsEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
base.OnInspectorGUI(); | |
var showVerticesNormals = (ShowVerticesNormals)target; | |
if(GUILayout.Button("GetNormals")) { | |
showVerticesNormals.GetNormals(); | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment