URP シェーダーとビルトイン シェーダーおよび URP シェーダー テンプレートの違い

相違点:
1. タグに "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 Template (GPU Instancing): 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
		}
	}
}

おすすめ

転載: blog.csdn.net/weixin_41155760/article/details/129380831