URP shader与Built-in shader差异和URP shader模板

更新:从URP12开始,额外添加了一个DepthPrimingMode设置选项,在URP的render上进行设置,默认是禁用的
如果你使用下面代码写shader,请保持DepthPrimingMode禁用即可,如果不小心打开,物体会不显示:这个选项是
深度预处理的,你打开设置,但没有提供具体的深度预处理Pass,所以不显示(我猜测),详细请自行查找资料

差异:
1.Tags里需要添加“RenderPipeline”=“UniversalPipeline”
2.CGPROGRAM改为HLSLPROGRAM,ENDCG改为ENDHLSL,CGINCLUDE改为HLSLINCLUDE
3.fixed4已不支持,最低half4(测试版本2022.2.8)
4.URP内置的库文件路径:

可编程渲染管线的shader库
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/..."
通用渲染管线的shader库
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/..."

最常用的:
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

5.constant buffer:具体的都有哪些,包含哪些内容,详见UnityShaderVariables.cginc和UnityInstancing.cginc文件(编辑器安装目录),其他文件可能也有相关的内容

不要自己定义constant buffer,opengl不支持,使用Unity宏会自动处理差异
1.格式如下:
CBUFFER_START(...)
	//变量
CBUFFER_END

常见的buffer:UnityShaderVariables.cginc里都有
UnityPerMaterial:材质属性相关的数据
UnityPerDraw:Draw相关的数据
UnityPerFrame:frame相关的数据
UnityFog:fog相关的数据
UnityLighting:lighting相关的数据

例如:
CBUFFER_START(UnityPerMaterial)
	float _Speed;
CBUFFER_END

2.GPU实例化constant buffer的格式:在UnityInstancing.cginc文件
UNITY_INSTANCING_BUFFER_START(...)
	//变量
UNITY_INSTANCING_BUFFER_END(...)

定义变量宏:
UNITY_DEFINE_INSTANCED_PROP(float4 _BaseColor)
获取变量宏:
UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseColor)//常用
UNITY_ACCESS_MERGED_INSTANCED_PROP(UnityPerMaterial,_BaseColor)//没见用过,但源文件里有这个

如果觉得获取变量宏麻烦,可以自己进行自定义:
#define MYCUSTOM(name) UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,name)
这样我们就可以用自定义的宏获取变量:float4 color = MYCUSTOM(_BaseColor);

例如:
UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
	UNITY_DEFINE_INSTANCED_PROP(float4 _BaseColor)
	UNITY_DEFINE_INSTANCED_PROP(float4 _BaseColor_ST)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)

float4 color = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseColor);

3.贴图和采样器不放在constant buffer,因为它们时shader资源,必须放在全局作用域内
例如:
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);

模板
Unlit模板(SRP兼容):

Shader "CustomURP/Unlit"
{
	Properties
	{
		_BaseMap("Base Map",2D) = "white"{}
		_BaseColor("Base Color",Color) = (1,1,1,1)
	}
	SubShader
	{
		tags{"RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"}
		
		Pass
		{
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			
			struct Attributes
			{
				float3 positionOS:POSITION;
				half2 uv:TEXCOORD0;
			};
			
			struct Varyings
			{
				float4 positionHCS:SV_POSITION;
				half2 uv:TEXCOORD0;
			};			
			
			CBUFFER_START(UnityPerMaterial)				
				half4 _BaseColor;
				float4 _BaseMap_ST;
			CBUFFER_END
			
			TEXTURE2D(_BaseMap);
			SAMPLER(sampler_BaseMap);
			
			Varyings vert(Attributes IN)
			{
				Varyings o;				
				o.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
				o.uv = TRANSFORM_TEX(IN.uv,_BaseMap);				
				return o;
			}
			
			half4 frag(Varyings IN):SV_Target
			{
				half4 color = SAMPLE_TEXTURE2D(_BaseMap,sampler_BaseMap,IN.uv);
				return color ;
			}
			ENDHLSL
		}
	}
}

Unlit模板(GPU实例化):故意破坏SRP兼容性(否则会SRP批处理)

Shader "CustomURP/UnlitGPUInstancing"
{
	Properties
	{
		_BaseMap("Base Map",2D) = "white"{}
		_BaseColor("Base Color",Color) = (1,1,1,1)
		[HideInInspector]_GPUInstancing("GPUInstancing",float) = 1	//1.声明一个属性
	}
	SubShader
	{
		tags{"RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"}
		
		Pass
		{
			HLSLPROGRAM
			#pragma multi_compile_instancing
			#pragma vertex vert
			#pragma fragment frag			
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			
			struct Attributes
			{
				float3 positionOS:POSITION;
				half2 uv:TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};
			
			struct Varyings
			{
				float4 positionHCS:SV_POSITION;
				half2 uv:TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};			
			
			float _GPUInstancing;	//2.在constant buffer外定义变量
			
			UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)				
				UNITY_DEFINE_INSTANCED_PROP(half4,_BaseColor)		
				UNITY_DEFINE_INSTANCED_PROP(float4,_BaseMap_ST)
			UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
			
			TEXTURE2D(_BaseMap);
			SAMPLER(sampler_BaseMap);
			
			Varyings vert(Attributes IN)
			{
				Varyings o;				
				UNITY_SETUP_INSTANCE_ID(IN);
				UNITY_TRANSFER_INSTANCE_ID(IN,o);		
				o.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);		
				float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseMap_ST);
				
				//3.使用_GPUInstancing变量,不使用还是兼容SRP,可能是编译器的优化,因为该值是1,所以对结果没有影响
				//乘以哪个属性都可以
				o.uv = IN.uv * baseST.xy + baseST.zw * _GPUInstancing ;				
				return o;
			}
			
			half4 frag(Varyings IN):SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(IN);
				half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap,sampler_BaseMap,IN.uv);
				half4 baseColor = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial,_BaseColor);
				return baseColor;
			}
			ENDHLSL
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值