Unterschied zwischen URP-Shader und integriertem Shader und URP-Shader-Vorlage

Unterschiede:
1. „RenderPipeline“ = „UniversalPipeline“ muss in Tags hinzugefügt werden
2. CGPROGRAM in HLSLPROGRAM, ENDCG in ENDHLSL, CGINCLUDE in HLSLINCLUDE ändern
3. Fixed4 wird nicht mehr unterstützt, mindestens half4 (Testversion 2022.2.8)
4. URP Pfad der integrierten Bibliotheksdatei:

可编程渲染管线的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. konstanter Puffer: Was sind die Besonderheiten und was sind sie enthalten?Weitere Informationen finden Sie in den Dateien UnityShaderVariables.cginc und UnityInstancing.cginc (Editor-Installationsverzeichnis), und andere Dateien können auch ähnlichen Inhalt haben

不要自己定义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);

Vorlage
Unbeleuchtete Vorlage (SRP-kompatibel):

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
		}
	}
}

Unbeleuchtete Vorlage (GPU-Instanziierung): Unterbricht absichtlich die SRP-Kompatibilität (sonst SRP-Batches)

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
		}
	}
}

Supongo que te gusta

Origin blog.csdn.net/weixin_41155760/article/details/129380831
Recomendado
Clasificación