Unity的Shader学习笔记(07)[20/12/22_周二][24-26]

目录

课时24:3D数学基础实例-C#矩阵变换和三维渲染4-矩阵和行列式(理论课)

课时25:3D数学基础实例-C#矩阵变换和三维渲染5-矩阵的逆

课时26:3D数学基础实例-C#矩阵变换和三维渲染6


课时24:3D数学基础实例-C#矩阵变换和三维渲染4-矩阵和行列式(理论课)

视频地址:https://www.bilibili.com/video/BV1YK41157AC?p=24

前面课时18的标题名称错了吧! 特意去蛮牛看了一下,嗯,它的也是这样的

课时18应该是《3D数学基础实例-C#矩阵变换和三维渲染1》,然后依次下来,到上节课的《3D数学基础实例-C#矩阵变换和三维渲染3》应该变成了《3D数学基础实例-C#矩阵变换和三维渲染6》,其实上节课以及结束c#编程实例了。

-------------------------------

行列式,方阵才有行列式,行列式是一个方阵的代数运行的结果,标量的值

-------------------------------------------------------------------

课时25:3D数学基础实例-C#矩阵变换和三维渲染5-矩阵的逆

这个和课时19的标题重复了

行列式为0的矩阵没有逆

奇异矩阵:行列式为0

这个“若....当且仅当”从以前上学就让人听着挺不舒服的.....

每一行都是单位向量,平方和为1

旋转矩阵的转置,撤销旋转。

---------------------------------------------------------------------

课时26:3D数学基础实例-C#矩阵变换和三维渲染6

这一集讲具体如何应用矩阵的逆撤销变换。

一些小改动,在课时23的基础上。

Triangle3D:Y坐标要颠倒一下

        private static PointF Get2DPointF(Vector4 v)
        {
            PointF p = new PointF();
            p.X = (float)(v.x / v.w);
            p.Y = -(float)(v.y / v.w);
            return p;
        }

Matrix4x4:加了个转置变换

        public Matrix4x4 Transpose()
        {
            Matrix4x4 t = new Matrix4x4();
            for(int i = 1; i <= 4; i++)
            {
                for(int j = 1; j <= 4; j++)
                {
                    t[i, j] = this[j, i];
                }
            }
            return t;
        }

Form1:增加了X、Y、Z旋转变换,和是否用矩阵的逆撤销变换。

   public partial class Form1 : Form
    {
        Matrix4x4 m_scale;//比例变换
        Matrix4x4 m_rotationX;
        Matrix4x4 m_rotationY;
        Matrix4x4 m_rotationZ;
        int a = 0;
        Matrix4x4 m_view;
        Matrix4x4 m_projection;

        public Form1()
        {
            InitializeComponent();

            m_scale = new Matrix4x4();
            m_scale[1, 1] = 250;
            m_scale[2, 2] = 250;
            m_scale[3, 3] = 250;
            m_scale[4, 4] = 1;

            m_view = new Matrix4x4();
            m_view[1, 1] = 1;
            m_view[2, 2] = 1;
            m_view[3, 3] = 1;
            m_view[4, 1] = 0;//x平移
            m_view[4, 2] = 0;//y平移
            m_view[4, 3] = 250;//z平移250
            m_view[4, 4] = 1;

            m_projection = new Matrix4x4();
            m_projection[1, 1] = 1;
            m_projection[2, 2] = 1;
            m_projection[3, 3] = 1;
            m_projection[3, 4] = 1.0 / 250;


            m_rotationX = new Matrix4x4();
            m_rotationY = new Matrix4x4();
            m_rotationZ = new Matrix4x4();
        }

        Triangle3D t;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.TranslateTransform(300, 300);
            t.Draw(e.Graphics);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Vector4 a = new Vector4(0, 0.5, 0, 1);
            Vector4 b = new Vector4(0.5, -0.5, 0, 1);
            Vector4 c = new Vector4(-0.5, -0.5,0, 1);
            t = new Triangle3D(a, b, c);
            //t.Transform(m_scale);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.Invalidate();
            a += 2;
            double angle = a / 360.0 * Math.PI;
            //X
            Matrix4x4 rotation = m_rotationX;
            rotation[1, 1] = 1;
            rotation[2, 2] = Math.Cos(angle);
            rotation[2, 3] = Math.Sin(angle);
            rotation[3, 2] = -Math.Sin(angle);
            rotation[3, 3] = Math.Cos(angle);
            rotation[4, 4] = 1;
            //Y
            rotation = m_rotationY;
            rotation[1, 1] = Math.Cos(angle);
            rotation[1, 3] = Math.Sin(angle);
            rotation[2, 2] = 1;
            rotation[3, 1] = -Math.Sin(angle);
            rotation[3, 3] = Math.Cos(angle);
            rotation[4, 4] = 1;
            //Z
            rotation = m_rotationZ;
            rotation[1, 1] = Math.Cos(angle);
            rotation[1, 2] = Math.Sin(angle);
            rotation[2, 1] = -Math.Sin(angle);
            rotation[2, 2] = Math.Cos(angle);
            rotation[3, 3] = 1;
            rotation[4, 4] = 1;

            if (cbX.Checked)
            {
                Matrix4x4 tx = m_rotationX.Transpose();
                m_rotationX = m_rotationX.Mul(tx);
            }
            if (cbY.Checked)
            {
                Matrix4x4 tx = m_rotationY.Transpose();
                m_rotationY = m_rotationY.Mul(tx);
            }
            if (cbZ.Checked)
            {
                Matrix4x4 tx = m_rotationZ.Transpose();
                m_rotationZ = m_rotationZ.Mul(tx);
            }

            Matrix4x4 mall = m_rotationX.Mul(m_rotationY.Mul(m_rotationZ));

            Matrix4x4 m = m_scale.Mul(mall);
            Matrix4x4 mv = m.Mul(m_view);
            Matrix4x4 mvp = mv.Mul(m_projection);
            t.Transform(mvp);
            this.Invalidate();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            m_view[4, 3] = (sender as TrackBar).Value;//视角距离
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            m_projection[3, 4] = 1.0 / ((sender as TrackBar).Value);//视角开口角度
        }
    }

注意:Form1_Load里面的初始坐标变了,和前面的Y轴颠倒相配合,才能得到正确的三角形朝上。

基础结束了,接下来回到Shader上了。

-----------------------------------------------------------------------------------------------------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值