当前位置:网站首页>Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
2022-07-02 05:56:00 【Hey, skin potatoes】
Screenshot of material panel :

Function realization (URP Under the rendering pipeline ):
PBR texture of material 、 Cast and receive shadows .
Code display :
Shader "XingWo/Object_PBR_Simplelify"
{
Properties
{
_MainColor("Main Color", Color) = (1,1,1,0)
_MainTeture("Main Teture", 2D) = "white" {
}
_RoughnessMap("RoughnessMap", 2D) = "white" {
}
_Metallic("Metallic", Range( 0 , 1)) = 0.2
_Gloss("Gloss", Range( 0 , 1)) = 0.3
_NormalIntensity("NormalIntensity", Range( 0.001 , 2)) = 1
_NormalMap("Normal Map", 2D) = "bump" {
}
[HDR]_Emission("Emission", Color) = (0,0,0,0)
}
SubShader
{
LOD 100
Tags {
"RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" }
Cull Off
AlphaToMask Off
Pass
{
Name "Forward"
Tags {
"LightMode"="UniversalForward" }
Blend One Zero, One Zero
ZWrite On
ZTest LEqual
HLSLPROGRAM
#pragma multi_compile _ _SCREEN_SPACE_OCCLUSION
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS _ADDITIONAL_OFF
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile _ _SHADOWS_SOFT
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
#pragma multi_compile _ SHADOWS_SHADOWMASK
#pragma multi_compile _ DIRLIGHTMAP_COMBINED
#pragma multi_compile _ LIGHTMAP_ON
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
struct VertexInput
{
float4 vertex : POSITION;
float3 ase_normal : NORMAL;
float4 ase_tangent : TANGENT;
float4 texcoord1 : TEXCOORD1;
float4 texcoord : TEXCOORD0;
};
struct VertexOutput
{
float4 clipPos : SV_POSITION;
float4 lightmapUVOrVertexSH : TEXCOORD0;
half4 fogFactorAndVertexLight : TEXCOORD1;
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD2;
#endif
float4 tSpace0 : TEXCOORD3;
float4 tSpace1 : TEXCOORD4;
float4 tSpace2 : TEXCOORD5;
#if defined(ASE_NEEDS_FRAG_SCREEN_POSITION)
float4 screenPos : TEXCOORD6;
#endif
float4 ase_texcoord7 : TEXCOORD7;
};
CBUFFER_START(UnityPerMaterial)
float4 _NormalMap_ST;
float4 _MainColor;
float4 _MainTeture_ST;
float4 _Emission;
float4 _RoughnessMap_ST;
float _NormalIntensity;
float _Metallic;
float _Gloss;
CBUFFER_END
sampler2D _NormalMap;
sampler2D _MainTeture;
sampler2D _RoughnessMap;
VertexOutput vert( VertexInput v )
{
VertexOutput o = (VertexOutput)0;
o.ase_texcoord7.xy = v.texcoord.xy;//UV
o.ase_texcoord7.zw = 0;
float3 positionWS = TransformObjectToWorld( v.vertex.xyz );
float3 positionVS = TransformWorldToView( positionWS );
float4 positionCS = TransformWorldToHClip( positionWS );
VertexNormalInputs normalInput = GetVertexNormalInputs(v.ase_normal, v.ase_tangent);
o.tSpace0 = float4(normalInput.normalWS, positionWS.x);
o.tSpace1 = float4(normalInput.tangentWS, positionWS.y);
o.tSpace2 = float4(normalInput.bitangentWS, positionWS.z);
half3 vertexLight = VertexLighting( positionWS, normalInput.normalWS );
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
VertexPositionInputs vertexInput = (VertexPositionInputs)0;
vertexInput.positionWS = positionWS;
vertexInput.positionCS = positionCS;
o.shadowCoord = GetShadowCoord( vertexInput );
#endif
o.clipPos = positionCS;
return o;
}
half4 frag ( VertexOutput IN ) : SV_Target
{
float3 WorldNormal = normalize(IN.tSpace0.xyz);
float3 WorldTangent = IN.tSpace1.xyz;
float3 WorldBiTangent = IN.tSpace2.xyz;
float3 WorldPosition = float3(IN.tSpace0.w,IN.tSpace1.w,IN.tSpace2.w);
float3 WorldViewDirection = _WorldSpaceCameraPos.xyz - WorldPosition;
float4 ShadowCoords = float4(0, 0, 0, 0);
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
ShadowCoords = IN.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
ShadowCoords = TransformWorldToShadowCoord( WorldPosition );
#endif
WorldViewDirection = SafeNormalize( WorldViewDirection );
float2 uv_NormalMap = IN.ase_texcoord7.xy * _NormalMap_ST.xy + _NormalMap_ST.zw;
float3 tex2DNode13 = UnpackNormalScale( tex2D( _NormalMap, uv_NormalMap ), 1.0f );
float2 appendResult30 = (float2(tex2DNode13.r , tex2DNode13.g));
float dotResult36 = dot( appendResult30 , appendResult30 );
float3 appendResult40 = (float3(( _NormalIntensity * appendResult30 ) , sqrt( ( 1.0 - saturate( dotResult36 ) ) )));
float3 NormalMap47 = appendResult40;
float3 normalizeResult147 = normalize( BlendNormal( WorldNormal , NormalMap47 ) );
float3 normalizeResult145 = normalize( _MainLightPosition.xyz );
float dotResult146 = dot( normalizeResult147 , normalizeResult145 );
float halfLambert95 = ( ( dotResult146 * 0.5 ) + 0.5 );
float2 uv_MainTeture = IN.ase_texcoord7.xy * _MainTeture_ST.xy + _MainTeture_ST.zw;
float4 MainColor50 = ( _MainColor * tex2D( _MainTeture, uv_MainTeture ) );
float2 uv_RoughnessMap = IN.ase_texcoord7.xy * _RoughnessMap_ST.xy + _RoughnessMap_ST.zw;
float3 Albedo = MainColor50.rgb;
float3 Normal = NormalMap47;
float3 Emission = _Emission.rgb;
float3 Specular = 0.5;
float Metallic = _Metallic;
float Smoothness = ( _Gloss * tex2D( _RoughnessMap, uv_RoughnessMap ).g );
float Occlusion = 1;
float Alpha = 1;
float AlphaClipThreshold = 0.5;
float AlphaClipThresholdShadow = 0.5;
float3 BakedGI = 0;
float3 RefractionColor = 1;
float RefractionIndex = 1;
float3 Transmission = 1;
float3 Translucency = 1;
InputData inputData;
inputData.positionWS = WorldPosition;
inputData.viewDirectionWS = WorldViewDirection;
inputData.shadowCoord = ShadowCoords;
inputData.normalWS = TransformTangentToWorld(Normal, half3x3( WorldTangent, WorldBiTangent, WorldNormal ));
float3 SH = SampleSH(inputData.normalWS.xyz);
inputData.bakedGI = SAMPLE_GI( IN.lightmapUVOrVertexSH.xy, SH, inputData.normalWS );
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(IN.clipPos);
inputData.shadowMask = SAMPLE_SHADOWMASK(IN.lightmapUVOrVertexSH.xy);
half4 color = UniversalFragmentPBR(
inputData,
Albedo,
Metallic,
Specular,
Smoothness,
Occlusion,
Emission,
Alpha);
return color;
}
ENDHLSL
}
Pass
{
Name "ShadowCaster"
Tags {
"LightMode"="ShadowCaster" }
ZWrite On
ZTest LEqual
AlphaToMask Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
struct VertexInput
{
float4 vertex : POSITION;
float3 ase_normal : NORMAL;
};
struct VertexOutput
{
float4 clipPos : SV_POSITION;
};
CBUFFER_START(UnityPerMaterial)
float4 _NormalMap_ST;
float4 _MainColor;
float4 _MainTeture_ST;
float4 _Emission;
float4 _RoughnessMap_ST;
float _NormalIntensity;
float _Metallic;
float _Gloss;
CBUFFER_END
float3 _LightDirection;
VertexOutput vert( VertexInput v )
{
VertexOutput o;
float3 positionWS = TransformObjectToWorld( v.vertex.xyz );
float3 normalWS = TransformObjectToWorldDir(v.ase_normal);
float4 clipPos = TransformWorldToHClip( ApplyShadowBias( positionWS, normalWS, _LightDirection ) );
o.clipPos = clipPos;
return o;
}
half4 frag(VertexOutput IN) : SV_TARGET
{
float4 ShadowCoords = float4( 0, 0, 0, 0 );
#if defined(ASE_NEEDS_FRAG_SHADOWCOORDS)
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
ShadowCoords = IN.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
ShadowCoords = TransformWorldToShadowCoord( WorldPosition );
#endif
#endif
float Alpha = 1;
float AlphaClipThreshold = 0.5;
float AlphaClipThresholdShadow = 0.5;
return 0;
}
ENDHLSL
}
}
}
At the end :
Some contents may not be optimized in place , But this basic can already be used , You can add some customized effects , Welcome to use ;
If you encounter something in the process of use bug And questions , Please leave a message , I will optimize it at the first time ;
边栏推荐
- Mathematical statistics and machine learning
- PHP read file (read JSON file, convert to array)
- I want to understand the swift code before I learn it. I understand it
- Redis Key-Value数据库 【高级】
- memcached安装
- TI毫米波雷达学习(一)
- 深度学习分类网络 -- AlexNet
- Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
- Technologists talk about open source: This is not just using love to generate electricity
- Yyds dry inventory what is test driven development
猜你喜欢

神机百炼3.53-Kruskal

在uni-app中引入uView

15 C language advanced dynamic memory management

PHP development and testing WebService (soap) -win

Balsamiq wireframes free installation
![[paper translation] gcnet: non local networks meet squeeze exception networks and beyond](/img/7a/718162d08796f70251511101b3a61b.png)
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond

Stick to the big screen UI, finereport development diary

Matplotlib double Y axis + adjust legend position

Vscode paste image plugin saves image path settings

Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
随机推荐
Summary of MySQL constraints
3D 打印机 G 代码命令:完整列表和教程
1035 Password
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
Zzuli:1068 binary number
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
Zzuli:1064 encrypted characters
Addchild() and addattribute() functions in PHP
PHP gets CPU usage, hard disk usage, and memory usage
mock-用mockjs模拟后台返回数据
492.构造矩形
Yyds dry inventory what is test driven development
JS determines whether the mobile terminal or the PC terminal
【C语言】筛选法求素数
Technologists talk about open source: This is not just using love to generate electricity
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
GRBL 软件:简单解释的基础知识
Cube magique infini "simple"
测试 - 用例篇
Redis Key-Value数据库【初级】