返回介绍

Matrix4x4.inverse 逆矩阵

发布于 2019-12-18 15:38:00 字数 3273 浏览 1247 评论 0 收藏 0

JavaScript => public var inverse: Matrix4x4;
C# => public Matrix4x4 inverse;;

Description 描述

The inverse of this matrix (Read Only).

相反的矩阵(只读)。

Inverted matrix is such that if multiplied by the original would result in identity matrix.

逆矩阵是指如果乘以原有的矩阵将会得到单位矩阵。

If some matrix transforms vectors in a particular way, then the inverse matrix can transform them back. For example, Transform's worldToLocalMatrix and localToWorldMatrix are inverses of each other.

如果某些矩阵变换向量在特别的方法中,那么逆矩阵可以变换到他们的背后。例如,对象的世界坐标转本地坐标矩阵和自身转世界坐标矩阵是彼此相反的。

JavaScript:

 // Stretch a mesh at an arbitrary angle around the X axis.
 
	// Angle and amount of stretching.
	var rotAngle: float;
	var stretch: float;
 
 
	private var mf: MeshFilter;
	private var origVerts: Vector3[];
	private var newVerts: Vector3[];
 
 
	function Start () {
		// Get the Mesh Filter component, save its original vertices
		// and make a new vertex array for processing.
		mf = GetComponent.<MeshFilter>();
		origVerts = mf.mesh.vertices;
		newVerts = new Vector3[origVerts.Length];
	}
 
 
	function Update () {
		// Create a rotation matrix from a Quaternion.
		var rot = Quaternion.Euler(rotAngle, 0, 0);
		var m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
 
		// Get the inverse of the matrix (ie, to undo the rotation).
		var inv = m.inverse;
 
		// For each vertex...
		for (var i = 0; i < origVerts.Length; i++) {
			// Rotate the vertex and scale it along its new Y axis.
			var pt = m.MultiplyPoint3x4(origVerts[i]);
			pt.y *= stretch;
 
			// Return the vertex to its original rotation (but with the
			// scaling still applied).
			newVerts[i] = inv.MultiplyPoint3x4(pt);
		}
 
		// Copy the transformed vertices back to the mesh.
		mf.mesh.vertices = newVerts;
	}    

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float rotAngle;
    public float stretch;
    private MeshFilter mf;
    private Vector3[] origVerts;
    private Vector3[] newVerts;
    void Start() {
        mf = GetComponent<MeshFilter>();
        origVerts = mf.mesh.vertices;
        newVerts = new Vector3[origVerts.Length];
    }
    void Update() {
        Quaternion rot = Quaternion.Euler(rotAngle, 0, 0);
        Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);
        Matrix4x4 inv = m.inverse;
        int i = 0;
        while (i < origVerts.Length) {
            Vector3 pt = m.MultiplyPoint3x4(origVerts[i]);
            pt.y *= stretch;
            newVerts[i] = inv.MultiplyPoint3x4(pt);
            i++;
        }
        mf.mesh.vertices = newVerts;
    }
}

matrix4x4

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

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

发布评论

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