返回介绍

Matrix4x4.MultiplyPoint3x4 变换位置

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

JavaScript => public function MultiplyPoint3x4(v: Vector3): Vector3;
C# => public Vector3 MultiplyPoint3x4(Vector3 v);

Parameters 参数

Description 描述

Transforms a position by this matrix (fast).

通过这个矩阵变换位置(快)。

Returns a position v transformed by the current transformation matrix. This function is a faster version of MultiplyPoint; but it can only handle regular 3D transformations. MultiplyPoint is slower, but can handle projective transformations as well.

返回由当前变换矩阵变换得到的位置v。这个函数是MultiplyPoint的快速版,但是它只能处理常规的3D变换。Multiplypoint较慢,但是能处理投影变换。

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 和您的相关数据。
    原文