找回密码
 立即注册
查看: 739|回复: 5

[简易教程] 可查看法线的脚本normals line script in unity

[复制链接]
发表于 2020-4-14 13:38 | 显示全部楼层 |阅读模式
  1. using System;
  2. using UnityEngine;

  3. [ExecuteInEditMode]
  4. public class ShowNormals : MonoBehaviour {

  5.     public float length = 1;

  6.     public Vector3 bias;
  7.     public Boolean isShowDebugPoint;

  8.     // Update is called once per frame
  9.     private void OnValidate()
  10.     {
  11.         
  12.     }  
  13.     private void OnDrawGizmos()
  14.     {
  15.         if (isShowDebugPoint)
  16.         {
  17.             // 获取网格法线
  18.             Mesh mesh = GetComponent<MeshFilter>().sharedMesh;

  19.             Vector3[] vertices = mesh.vertices;
  20.             Vector3[] normals = mesh.normals;

  21.             for (var i = 0; i < normals.Length; i++)
  22.             {
  23.                 Vector3 pos = vertices[i];
  24.                 pos.x *= transform.localScale.x;
  25.                 pos.y *= transform.localScale.y;
  26.                 pos.z *= transform.localScale.z;
  27.                 pos += transform.position + bias;

  28.                 Gizmos.DrawLine
  29.                 (
  30.                     pos,
  31.                     pos + normals[i] * length);
  32.             }
  33.         }
  34.     }

  35. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2020-5-8 07:09 | 显示全部楼层
楼主是超人
发表于 2020-5-8 07:47 | 显示全部楼层
好帖就是要顶
发表于 2020-5-8 07:34 | 显示全部楼层
真心顶
发表于 2020-5-8 07:53 | 显示全部楼层
很好哦
发表于 2020-5-8 07:43 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-6 05:02 , Processed in 0.092660 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表