起因
之前偶然项目中看到过 API Vector3.ProjectOnPlane API 的使用
然在这篇文章也看到:Unity3d那些你不常用但确有其用的方法—Vector3.ProjectOnPlane,该作者使用的是将 UI 的面向和相机的 forward 正向的反向,有点 BB(Billboard)的功能想过
好奇
然后我就好奇,这个函数的作用是啥,返回的 Vector3 是什么位置
所以我就使用 Gizmos 来绘制查看
绘制 Vector3.ProjectOnPlane 的参数,与返回结果
using UnityEngine;
public class DrawProjectOnPlaneGizmos : MonoBehaviour
{
public Transform transForward;
public Transform targetPos;
private void OnDrawGizmos()
{
var forwardStart = transForward.position;
var forwardEnd = forwardStart + transForward.forward;
var srcCol = Gizmos.color;
Gizmos.color = Color.blue;
Gizmos.DrawLine(forwardStart, forwardEnd);
var p2pStart = forwardStart;
var p2pEnd = targetPos.position;
Gizmos.color = Color.yellow;
Gizmos.DrawLine(p2pStart, p2pEnd);
Vector3 projPos = Vector3.ProjectOnPlane(p2pEnd - p2pStart, transForward.forward);
Gizmos.color = Color.green;
Gizmos.DrawLine(forwardEnd, projPos);
Gizmos.DrawLine(p2pEnd, projPos);
Gizmos.color = srcCol;
}
}
绘制结果
定位作用、优化
从上面的效果图可以看出来,Vector3.ProjectOnPlane(Vector3 vector, Vector3 planeNormal) 的作用就是 返回 vector 向量 相对 planeNormal 上的反向投影
先来看看 该 API Unity C# 的源码:
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
float num = Dot(planeNormal, planeNormal);
if (num < Mathf.Epsilon)
{
return vector;
}
float num2 = Dot(vector, planeNormal);
return new Vector3(vector.x - planeNormal.x * num2 / num, vector.y - planeNormal.y * num2 / num, vector.z - planeNormal.z * num2 / num);
}
我能吐槽的是,这运算量,和这可读性,真的不敢恭维~
那么,其实可以优化:
private static Vector3 MyProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
var dot = Vector3.Dot(vector, planeNormal);
return vector + -planeNormal * dot;
}
完整脚本
using UnityEngine;
public class DrawProjectOnPlaneGizmos : MonoBehaviour
{
public Transform transForward;
public Transform targetPos;
private float forwardAddLength = 10.0f;
public bool usingMyFunc = false;
private void OnDrawGizmos()
{
var forwardStart = transForward.position;
var forwardEnd = forwardStart + transForward.forward;
var srcCol = Gizmos.color;
Gizmos.color = Color.blue;
Gizmos.DrawLine(forwardStart, forwardEnd);
Gizmos.DrawLine(forwardEnd, forwardEnd + transForward.forward * forwardAddLength);
var p2pStart = forwardStart;
var p2pEnd = targetPos.position;
Gizmos.color = Color.yellow;
Gizmos.DrawLine(p2pStart, p2pEnd);
Vector3 projPos;
if (usingMyFunc)
{
projPos = MyProjectOnPlane(p2pEnd - p2pStart, transForward.forward);
}
else
{
projPos = Vector3.ProjectOnPlane(p2pEnd - p2pStart, transForward.forward);
}
Gizmos.color = Color.green;
Gizmos.DrawLine(forwardEnd, projPos);
Gizmos.color = Color.red;
Gizmos.DrawLine(p2pEnd, projPos);
Gizmos.color = srcCol;
}
private static Vector3 MyProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
var dot = Vector3.Dot(vector, planeNormal);
return vector + -planeNormal * dot;
}
}
Project
References
|