Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Normal Gizmo

Discussion in 'Scripting' started by Harry Tuttle, May 6, 2008.

  1. Harry Tuttle

    Harry Tuttle

    Joined:
    Jan 3, 2006
    Posts:
    122
    Does anyone have any script to show mesh normals using Gizmo.DrawLine or Debug.DrawLine?

    I'm sure I've found this before on the forum/Wiki but I've been searching on and off all day and can't find a thing.
     
  2. ausiemick

    ausiemick

    Joined:
    Oct 8, 2012
    Posts:
    20
    This was posted almost 9 years ago, but still encase sombody googles it like I did, heres the solution

    Code (CSharp):
    1. var vert = _mesh.vertices[i];
    2. var normal = _mesh.normals[i];
    3. Gizmos.DrawSphere(vert, 0.1f);
    4. Gizmos.DrawLine(vert, vert + normal);

    upload_2017-1-28_16-41-22.png
     
    AndreasScholl, Welpx and Kiwasi like this.
  3. AndreasScholl

    AndreasScholl

    Joined:
    Oct 16, 2015
    Posts:
    12
    Here is a complete script that you can add to a gameobject with a meshfilter / meshrenderer.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class MeshNormalsGizmo : MonoBehaviour
    5. {
    6.     private Mesh _mesh = null;
    7.  
    8.     void Start()
    9.     {
    10.         MeshFilter filter = GetComponent<MeshFilter>();
    11.        
    12.         if (filter != null)
    13.         {
    14.             _mesh = filter.sharedMesh;
    15.         }
    16.     }
    17.  
    18.     private void OnDrawGizmos()
    19.     {
    20.         if (_mesh == null)
    21.         {
    22.             return;
    23.         }
    24.  
    25.         for (int count = 0; count < _mesh.vertexCount; count++)
    26.         {
    27.             var vert = transform.TransformPoint(_mesh.vertices[count]);
    28.             var normal = transform.TransformDirection(_mesh.normals[count]);
    29.             Gizmos.color = Color.green;
    30.             Gizmos.DrawSphere(vert, 0.05f);
    31.             Gizmos.color = Color.blue;
    32.             Gizmos.DrawLine(vert, vert + normal);
    33.         }
    34.     }
    35. }
    36.  
     
    QuixStars and Suchakree like this.