Unity的Shader学习笔记(06)[20/12/21_周一][19-23]

目录

课时19:3D数学基础5-矩阵的逆

课时20:3D数学基础6-矩阵和变换

课时21:3D数学基础实例-C#矩阵变换与三维渲染1

课时22:3D数学基础实例-C#矩阵变换与三维渲染2

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


Unity打不开,Import Asset 好久了,关键是上周五也这样,最后打开了的。起因是我在一个Editor工具卡住时强制关闭Unity。Library文件夹(48.5G)里面有个Artifacts文件夹(27.9G)。

计划打开后,备份Library,并删除,然后重新打开Unity试试,那要到下班时弄了。

发现下班后抱娃时,认真一点,不要刷抖音,理论教程能看一些的。

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

课时19:3D数学基础5-矩阵的逆

没有练习,缩放因子

绕x轴旋转,y轴,z轴

后面实现在vs的3d场景的旋转

缩放因子是什么,缩放比例吗?

旋转、缩放、平移、镜像?模型的镜像操作在Unity中能做吗?切变

投影(平行投影),投影(透视投影)

性质:可逆(撤销),等角,正交,刚体(物理组件)

线性变换,仿射变换。`

3D数学基础:图形与游戏开发

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

课时20:3D数学基础6-矩阵和变换

平移+线性变换

dx,dy,平移的量

旋转+平移

小孔成像

d:摄像机到投影平面的距离

屏幕只能是二维,只有x,y

摄像机在物体和投影平面中间

物体->世界->相机->投影

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

课时21:3D数学基础实例-C#矩阵变换与三维渲染1

写了几个类

    class Vector4
    {
        public double x, y, z, w;
        public Vector4() { }
        public Vector4(double x,double y,double z,double w)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.w = w;
        }

        public Vector4(Vector4 v)
        {
            this.x = v.x;
            this.y = v.y;
            this.z = v.z;
            this.w = v.w;
        }
    }
   class Matrix4x4
    {
        private double[,] pts;
        public Matrix4x4()
        {
            pts = new double[4, 4];
        }

        public double this[int i,int j]
        {
            get
            {
                return pts[i - 1, j - 1];
            }
            set
            {
                pts[i - 1, j - 1] = value;
            }
        }

        public Matrix4x4 Mul(Matrix4x4 m)
        {
            Matrix4x4 newM = new Matrix4x4();
            for(int w = 1; w <= 4; w++)
            {
                for(int h = 1; h <= 4; h++)
                {
                    for(int n = 1; n <= 4; n++)
                    {
                        newM[w, h] += this[w, n] * m[n, h];
                    }
                }
            }
            return newM;
        }

        public Vector4 Mul(Vector4 v)
        {
            Vector4 newV = new Vector4();
            newV.x = v.x * this[1, 1] + v.y * this[2, 1] + v.z * this[3, 1] + v.w * this[4, 1];
            newV.y = v.x * this[1, 2] + v.y * this[2, 2] + v.z * this[3, 2] + v.w * this[4, 2];
            newV.z = v.x * this[1, 3] + v.y * this[2, 3] + v.z * this[3, 3] + v.w * this[4, 3];
            newV.w = v.x * this[1, 4] + v.y * this[2, 4] + v.z * this[3, 4] + v.w * this[4, 4];
            return newV;
        }
    }
   class Triangle3D
    {
        public Vector4 A, B, C;
        public Triangle3D() { }

        public Triangle3D(Vector4 a,Vector4 b,Vector4 c)
        {
            this.A = new Vector4(a);
            this.B = new Vector4(b);
            this.C = new Vector4(c);
        }
    }

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

课时22:3D数学基础实例-C#矩阵变换与三维渲染2

class Triangle3D
    {
        private Vector4 a, b, c;//变换后数据
        public Vector4 A, B, C;//原始数据
        public Triangle3D() { }

        public Triangle3D(Vector4 a, Vector4 b, Vector4 c)
        {
            this.A = this.a = new Vector4(a);
            this.B = this.b = new Vector4(b);
            this.C = this.c = new Vector4(c);
        }

        public void Transform(Matrix4x4 m)
        {
            this.a = m.Mul(this.A);
            this.b = m.Mul(this.B);
            this.c = m.Mul(this.C);
        }

        public void Draw(Graphics g)
        {
            g.DrawLines(new Pen(Color.Red, 2), this.Get2DPointFArr());
        }

        private PointF[] Get2DPointFArr()
        {
            PointF[] arr = new PointF[4];
            arr[0] = Get2DPointF(this.a);
            arr[1] = Get2DPointF(this.b);
            arr[2] = Get2DPointF(this.c);
            arr[3] = arr[0];
            return arr;
        }

        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;
        }
    }
        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);
        }

继续打基础,能画出图了,中间的小点,下节课学比例变换,放大它。

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

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

缩放,旋转,摄像机,投影;

摄像机和投影要一起

   public partial class Form1 : Form
    {
        Matrix4x4 m_scale;//比例变换
        Matrix4x4 m_rotation;
        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_rotation = 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;
            m_rotation[1, 1] = Math.Cos(angle);
            m_rotation[1, 3] = Math.Sin(angle);
            m_rotation[2, 2] = 1;
            m_rotation[3, 1] = -Math.Sin(angle);
            m_rotation[3, 3] = Math.Cos(angle);
            m_rotation[4, 4] = 1;

            Matrix4x4 m = m_scale.Mul(m_rotation);
            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;
        }
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值