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

差异:
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
		}
	}
}

你可能感兴趣的:(unity,着色器)