1.绕Y轴旋转
Ry(θ) = | cos(θ) 0 sin(θ) |
| 0 1 0 |
| -sin(θ) 0 cos(θ) |
P = | x |
| y |
| z |
P' = Ry(θ) * P
2.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mvp : MonoBehaviour
{
private MeshRenderer rend;
private Material mat;
void Start()
{
rend = GetComponent<MeshRenderer>();
mat = rend.material;
}
// Update is called once per frame
void Update()
{
Matrix4x4 m4 = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;
Matrix4x4 rotY = new Matrix4x4();
rotY[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);
rotY[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);
rotY[1, 1] = 1;
rotY[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);
rotY[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);
rotY[3, 3] = 1;
Matrix4x4 m = m4 * rotY;
mat.SetMatrix("mvp",m);
}
}
效果:
3.加入缩放
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mvp : MonoBehaviour
{
private MeshRenderer rend;
private Material mat;
void Start()
{
rend = GetComponent<MeshRenderer>();
mat = rend.material;
}
// Update is called once per frame
void Update()
{
Matrix4x4 m4 = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;
Matrix4x4 rotY = new Matrix4x4();
rotY[0, 0] = Mathf.Cos(Time.realtimeSinceStartup);
rotY[0, 2] = Mathf.Sin(Time.realtimeSinceStartup);
rotY[1, 1] = 1;
rotY[2, 0] = -Mathf.Sin(Time.realtimeSinceStartup);
rotY[2, 2] = Mathf.Cos(Time.realtimeSinceStartup);
rotY[3, 3] = 1;
Matrix4x4 s1 = new Matrix4x4();
float sc = Mathf.Abs(Mathf.Cos(Time.realtimeSinceStartup)) * 1.5f;
s1[0, 0] = sc;
s1[1, 1] = sc;
s1[2, 2] = sc;
s1[3, 3] = 1;
Matrix4x4 m = m4 * rotY * s1;
mat.SetMatrix("mvp",m);
}
}
效果: