Unity-SRP之DrawCall

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchoop

有用的话请杯肥宅水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值