Unity shader基础学习(一)------ Unity
2019-10-09 本文已影响0人
千喜Ya
本系列文章是学习siki学院Unity Shader入门(Unity2018.3)笔记
一.shader三大编程语言

Cg是一个可以被OpenGL和Direct3D广泛支持的图形处理器编程语言。Cg语言和OpenGL、Direct3D并不是同一层次的语言,而是OpenGL和DirectX的上层,即Cg程序是运行在OpenGL和DirectX标准顶点和像素着色的基础上的。Cg由NVIDIA公司和微软公司相互协作在标准硬件光照语言的语法和语义上达成了一致开发。所以,HLSL和Cg其实是同一种语言。
二.渲染流水线

1.CPU应用阶段
1).将数据加载到显存中
这是因为大多数显卡对于RAM没有直接的访问权限,而且对于显存的访问速度更快

2). 设置渲染状态

3).调用Draw Call
动态合并与静态合并都是减少调用Draw Call的次数,静态合并就是将相同材质球合起来调用一次Draw Call

2.几何阶段(Gpu 流水线)


1).顶点着色器

模型坐标(每一个顶点相对于模型中心点的坐标)转换成齐次裁剪空间坐标:
1.从模型空间转变成世界坐标
2.世界坐标转换成观察坐标(以摄像机为原点的坐标)
3.投影变换到其次裁剪空间(长宽高为1的正方体)

2).裁剪

3).屏幕映射

3.光栅化阶段
1).三角形设置/遍历

2).片元着色器

3).逐片元操作

模板测试 : 一般用来限制渲染区域或者渲染阴影什么的
某一个shader中的模板测试部分:


深度测试 :
之前屏幕映射中窗口坐标系中有个Z值,也就是深度


4).合并混合 : 处理此次颜色是覆盖上次颜色缓冲还是其他处理

附加知识点 :


Unity Shader != 真正的Shader :
