Shader是运行在GPU上的一种处理图像信息的程序。要在哪里绘制,如何绘制通常由Shader决定。
着色器有很多可编程的阶段,比如顶点着色器和片元着色器。这些着色器的可编程性在于我们可以使用一种特定的语言来编写程序,如同我们用C#来编写游戏逻辑一样。着色语言有三种,微软DirectX的HLSL(High Level Shading Language)、OpenGL的GLSL(OpenGL Shading Language)和NVIDIA的CG(C for Graphic)。这些语言会被编译成与机器无关的汇编语言,也被称为中间语言,这些中间语言再交给显卡驱动来翻译成真正的机器语言,即GPU可以理解的语言。
在Unity的内置渲染管线中,使用CG语言来编写着色器,但是CG语言停止更新很多年了,基本上被放弃,现在SRP的着色器代码库使用的是HLSL,接下来所有着色器代码都将使用HLSL着色语言编写。
UnitShader
Shader "CustomRP/Unlit"
{
Properties
{
}
SubShader
{
Pass
{
HLSLPROGRAM
#pragma vertex UnlitPassVertex
#pragma fragment UnlitPassFragment
#include "UnlitPass.hlsl"
ENDHLSL
}
}
}
#ifndef CUSTOM_UNLIT_PASS_INCLUDE
#define CUSTOM_UNLIT_PASS_INCLUDE
#include "../ShaderLibrary/Common.hlsl"
//顶点函数
float4 UnlitPassVertex(float3 positionOS: POSITION) :SV_POSITION
{
float3 positionWS = TransformObjectToWorld(positionOS.xyz);
return TransformWorldToHClip(positionWS);
}
//片元函数
void UnlitPassFragment() {}
#endif
//公共方法库
#ifndef CUSTOM_COMMON_INCLUDE
#define CUSTOM_COMMON_INCLUDE
//使用UnityInput里面的转换矩阵前先include进来
#include "UnityInput.hlsl"
//函数功能:顶点从模型空间转换到世界空间
float3 TransformObjectToWorld(float3 positionOS)
{
return mul(unity_ObjectToWorld, float4(positionOS, 1.0)).xyz;
}
//函数功能:顶点从世界坐标转换到裁剪空间
float4 TransformWorldToHClip(float3 positionWS)
{
return mul(unity_MatrixVP, float4(positionWS, 1.0));
}
#endif
//unity标准输入库
#ifndef CUSTOM_UNITY_INPUT_INCLUDE
#define CUSTOM_UNITY_INPUT_INCLUDE
//定义一个从模型空间转换到世界空间的转换矩阵
float4x4 unity_ObjectToWorld;
//定义一个从世界坐标空间转换到裁剪空间的矩阵
float4x4 unity_MatrixVP;
#endif
SRP源码库
Shader "CustomRP/Unlit"
{
Properties
{
_MainColor("Color", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader
{
Pass
{
HLSLPROGRAM
#pragma vertex UnlitPassVertex
#pragma fragment UnlitPassFragment
#include "UnlitPass.hlsl"
ENDHLSL
}
}
}
#ifndef CUSTOM_UNLIT_PASS_INCLUDE
#define CUSTOM_UNLIT_PASS_INCLUDE
#include "../ShaderLibrary/Common.hlsl"
//顶点函数
float4 UnlitPassVertex(float3 positionOS: POSITION):SV_POSITION
{
float3 positionWS = TransformObjectToWorld(positionOS.xyz);
return TransformWorldToHClip(positionWS);
}
float4 _MainColor;
//片元函数
float4 UnlitPassFragment():SV_Target
{
return _MainColor;
}
#endif
//公共方法库
#ifndef CUSTOM_COMMON_INCLUDE
#define CUSTOM_COMMON_INCLUDE
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
//使用UnityInput里面的转换矩阵前先include进来
#include "UnityInput.hlsl"
//定义一些宏取代常用的转换矩阵
#define UNITY_MATRIX_M unity_ObjectToWorld
#define UNITY_MATRIX_I_M unity_WorldToObject
#define UNITY_MATRIX_V unity_MatrixV
#define UNITY_MATRIX_VP unity_MatrixVP
#define UNITY_MATRIX_P glstate_matrix_projection
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
函数功能:顶点从模型空间转换到世界空间
//float3 TransformObjectToWorld(float3 positionOS)
//{
// return mul(unity_ObjectToWorld, float4(positionOS, 1.0)).xyz;
//}
//
函数功能:顶点从世界坐标转换到裁剪空间
//float4 TransformWorldToHClip(float3 positionWS)
//{
// return mul(unity_MatrixVP, float4(positionWS, 1.0));
//}
#endif
//unity标准输入库
#ifndef CUSTOM_UNITY_INPUT_INCLUDE
#define CUSTOM_UNITY_INPUT_INCLUDE
//定义一个从模型空间转换到世界空间的转换矩阵
float4x4 unity_ObjectToWorld;
//定义一个从世界坐标空间转换到裁剪空间的矩阵
float4x4 unity_WorldToObject;
real4 unity_WorldTransformParams;
float4x4 unity_MatrixVP;
float4x4 unity_MatrixV;
float4x4 glstate_matrix_projection;
#endif