Unity Matrix.TRS 大概源码

private Matrix4x4 TRS(Vector3 position, Quaternion rotation, Vector3 scale)
    {
        float num = rotation.x * 2f;
        float num2 = rotation.y * 2f;
        float num3 = rotation.z * 2f;
        float num4 = rotation.x * num;
        float num5 = rotation.y * num2;
        float num6 = rotation.z * num3;
        float num7 = rotation.x * num2;
        float num8 = rotation.x * num3;
        float num9 = rotation.y * num3;
        float num10 = rotation.w * num;
        float num11 = rotation.w * num2;
        float num12 = rotation.w * num3;

        Matrix4x4 result = default(Matrix4x4);

        result.m00 = scale.x * (1f - (num5 + num6));
        result.m01 = scale.y * (num7 - num12);
        result.m02 = scale.z * (num8 + num11);
        result.m03 = position.x;

        result.m10 = scale.x * (num7 + num12);
        result.m11 = scale.y * (1f - (num4 + num6));
        result.m12 = scale.z * (num9 - num10);
        result.m13 = position.y;

        result.m20 = scale.x * (num8 - num11);
        result.m21 = scale.y * (num9 + num10);
        result.m22 = scale.z * (1f - (num4 + num5));
        result.m23 = position.z;

        result.m30 = 0f;
        result.m31 = 0f;
        result.m32 = 0f;
        result.m33 = 1f;

        return result;
    }

使用和对比:

    var obj = GameObject.Find("Cube (1)").transform;
    Debug.Log(TRS(obj.position, obj.rotation, obj.localScale));
    Debug.Log(Matrix4x4.TRS(obj.position, obj.rotation, obj.localScale));

运行效果:一模一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值