返回介绍

Vector3.OrthoNormalize 直角归一化

发布于 2019-12-18 15:38:44 字数 7512 浏览 1942 评论 0 收藏 0

JavaScript => public static function OrthoNormalize(ref normal: Vector3, ref tangent: Vector3): void;
C# => public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);

Description 描述

Makes vectors normalized and orthogonal to each other.

使向量归一化并且彼此相互垂直。

Normalizes normal. Normalizes tangent and makes sure it is orthogonal to normal (that is, angle between them is 90 degrees).

归一化normal,规范化tangent并且确保它垂直于normal(就是两者之间的角度为90度)。

参见: Normalize 函数


JavaScript => public static function OrthoNormalize(ref normal: Vector3, ref tangent: Vector3, ref binormal: Vector3): void;
C# => public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal);

Description 描述

Makes vectors normalized and orthogonal to each other.

使向量归一化并且彼此相互垂直。

Normalizes normal. Normalizes tangent and makes sure it is orthogonal to normal. Normalizes binormal and makes sure it is orthogonal to both normal and tangent.

归一化normal,归一化tangent并且确保它垂直于normal。归一化binormal并确保它到normal和tangent两者之间相互垂直。

Points in space are usually specified with coordinates in the standard XYZ axis system. However, you can interpret any three vectors as “axes” if they are normalized (ie, have a magnitude of 1) and are orthogonal (ie, perpendicular to each other).

在标准的 XYZ 轴系统中通常指定了坐标空间中的点。然而,作为“轴”解释任何三个向量,如果他们都归一化 (即,大小是 1) 和正交 (即,相互垂直的)。

Creating your own coordinate axes is useful, say, if you want to scale a mesh in arbitrary directions rather than just along the XYZ axes - you can transform the vertices to your own coordinate system, scale them and then transform back. Often, a transformation like this will be carried out along only one axis while the other two are either left as they are or treated equally. For example, a stretching effect can be applied to a mesh by scaling up on one axis while scaling down proportionally on the other two. This means that once the first axis vector is specified, it doesn't greatly matter what the other two are as long as they are normalized and orthogonal. OrthoNormalize can be used to ensure the first vector is normal and then generate two normalized, orthogonal vectors for the other two axes.

创建您自己的坐标轴是很有用的,如果你想要缩放在任意方向的网格,而不是只是沿 XYZ 轴-你可以将顶点转换到你自己的坐标系统,规范它们的尺寸,然后变换回来。通常情况下,像这样的转化将会进行只沿着一个轴改变,而其他两个都要么离开他们要么同样处理。例如,拉伸的效果可以应用到网格通过对其他两个按比例缩小的同时扩大在一个轴上。这意味着一旦指定的第一个轴向量,则外两个没多大关系,只要他们规范化和正交。出可以用于确保第一个向量是规范的然后生成两个归一化,正交向量中的其他两个坐标轴。

参见: Normalize 函数

JavaScript:

	// Mesh "stretch" effect along a chosen axis.
 
	// The axis and amount of scaling.
	var stretchAxis: Vector3;
	var stretchFactor = 1.0;
 
	// MeshFilter component and arrays for the original and transformed vertices.
	private var mf: MeshFilter;
	private var origVerts: Vector3[];
	private var newVerts: Vector3[];
 
	// Our new basis vectors.
	private var basisA: Vector3;
	private var basisB: Vector3;
	private var basisC: Vector3;
 
 
	function Start() {
		// Get the Mesh Filter, then make a copy of the original vertices
		// and a new array to calculate the transformed vertices.
		mf = GetComponent.<MeshFilter>();
		origVerts = mf.mesh.vertices;
		newVerts = new Vector3[origVerts.Length];
	}
 
 
	function Update() {
		// BasisA is just the specified axis for stretching - the
		// other two are just arbitrary axes generated by OrthoNormalize.
		basisA = stretchAxis;
		Vector3.OrthoNormalize(basisA, basisB, basisC);
 
		// Copy the three new basis vectors into the rows of a matrix
		// (since it is actually a 4x4 matrix, the bottom right corner
		// should also be set to 1).
		var toNewSpace: Matrix4x4 = new Matrix4x4();
		toNewSpace.SetRow(0, basisA);
		toNewSpace.SetRow(1, basisB);
		toNewSpace.SetRow(2, basisC);
		toNewSpace[3, 3] = 1.0;
 
		// The scale values are just the diagonal entries of the scale
		// matrix. The vertices should be stretched along the first axis
		// and squashed proportionally along the other two.
		var scale: Matrix4x4 = new Matrix4x4();
		scale[0, 0] = stretchFactor;
		scale[1, 1] = 1.0 / stretchFactor;
		scale[2, 2] = 1.0 / stretchFactor;
		scale[3, 3] = 1.0;
 
		// The inverse of the first matrix transforms the vertices back to
		// the original XYZ coordinate space(transpose is the same as inverse
		// for an orthogonal matrix, which this is).
		var fromNewSpace: Matrix4x4 = toNewSpace.transpose;
 
		// The three matrices can now be combined into a single symmetric matrix.
		var trans: Matrix4x4 = toNewSpace * scale * fromNewSpace;
 
		// Transform each of the mesh's vertices by the symmetric matrix.
		for (var i = 0; i < origVerts.Length; i++) {
			newVerts[i] = trans.MultiplyPoint3x4(origVerts[i]);
		}
 
		// ...and finally, update the mesh with the new vertex array.
		mf.mesh.vertices = newVerts;
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Vector3 stretchAxis;
    public float stretchFactor = 1.0F;
    private MeshFilter mf;
    private Vector3[] origVerts;
    private Vector3[] newVerts;
    private Vector3 basisA;
    private Vector3 basisB;
    private Vector3 basisC;
    void Start() {
        mf = GetComponent<MeshFilter>();
        origVerts = mf.mesh.vertices;
        newVerts = new Vector3[origVerts.Length];
    }
    void Update() {
        basisA = stretchAxis;
        Vector3.OrthoNormalize(ref basisA, ref basisB, ref basisC);
        Matrix4x4 toNewSpace = new Matrix4x4();
        toNewSpace.SetRow(0, basisA);
        toNewSpace.SetRow(1, basisB);
        toNewSpace.SetRow(2, basisC);
        toNewSpace[3, 3] = 1.0F;
        Matrix4x4 scale = new Matrix4x4();
        scale[0, 0] = stretchFactor;
        scale[1, 1] = 1.0F / stretchFactor;
        scale[2, 2] = 1.0F / stretchFactor;
        scale[3, 3] = 1.0F;
        Matrix4x4 fromNewSpace = toNewSpace.transpose;
        Matrix4x4 trans = toNewSpace * scale * fromNewSpace;
        int i = 0;
        while (i < origVerts.Length) {
            newVerts[i] = trans.MultiplyPoint3x4(origVerts[i]);
            i++;
        }
        mf.mesh.vertices = newVerts;
    }
}

Vector3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文