217 lines
6.7 KiB
Plaintext
217 lines
6.7 KiB
Plaintext
Shader "Test/XPbr"
|
|
{
|
|
Properties
|
|
{
|
|
[MainColor] _BaseColor("Color", Color) = (1,1,1,1)
|
|
[MainTexture] _MainTex("Base (RGB:Albedo A:Alpha/AO)", 2D) = "white" {}
|
|
_MixTex("Mix (RG:Normal B:Metallic A:Roughness)", 2D) = "white" {}
|
|
|
|
// 勾选:A 通道当透明(AlphaTest 裁剪);不勾选:A 通道当 AO
|
|
[Toggle(_ALPHATEST_ON)] _AlphaTest("Alpha As Transparency (Clip)", Float) = 0
|
|
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
|
|
|
|
_Metallic("Metallic", Range(0.0, 1.0)) = 0.8
|
|
_Roughness("Roughness", Range(0.0, 1.0)) = 0.6
|
|
_AO("AO", Range(0.0, 1.0)) = 1.0
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
Tags{ "LightMode" = "UniversalForward" }
|
|
|
|
ZWrite on
|
|
|
|
HLSLPROGRAM
|
|
// Required to compile gles 2.0 with standard SRP library
|
|
// All shaders must be compiled with HLSLcc and currently only gles is not using HLSLcc by default
|
|
#pragma prefer_hlslcc gles
|
|
#pragma exclude_renderers d3d11_9x
|
|
#pragma target 2.0
|
|
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
|
|
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
|
|
#pragma multi_compile _ LIGHTMAP_ON
|
|
#pragma multi_compile _ DYNAMICLIGHTMAP_ON
|
|
|
|
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
|
|
#pragma multi_compile_fragment _ _SHADOWS_SOFT _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH
|
|
|
|
#pragma shader_feature_local_fragment _ALPHATEST_ON
|
|
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
|
|
#include "XPbr_Base.hlsl"
|
|
|
|
Varyings vert(Attributes input)
|
|
{
|
|
Varyings output = (Varyings)0;
|
|
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
|
|
// normalWS and tangentWS already normalize.
|
|
// this is required to avoid skewing the direction during interpolation
|
|
// also required for per-vertex lighting and SH evaluation
|
|
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
|
|
|
|
half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
|
|
|
|
half fogFactor = 0;
|
|
#if !defined(_FOG_FRAGMENT)
|
|
fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
|
|
#endif
|
|
|
|
half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
|
|
output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
|
|
output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
|
|
output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
|
|
|
|
OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
|
|
#ifdef DYNAMICLIGHTMAP_ON
|
|
output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
|
|
#endif
|
|
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
|
|
#ifdef _ADDITIONAL_LIGHTS_VERTEX
|
|
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
|
|
#else
|
|
output.fogFactor = fogFactor;
|
|
#endif
|
|
|
|
output.positionWS = vertexInput.positionWS;
|
|
|
|
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
|
|
output.shadowCoord = GetShadowCoord(vertexInput);
|
|
#endif
|
|
|
|
output.positionCS = vertexInput.positionCS;
|
|
output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
|
|
|
|
return output;
|
|
}
|
|
|
|
half4 frag(Varyings input) : SV_Target
|
|
{
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
|
|
|
|
// 基础贴图:rgb 漫反射,a 透明或 AO(由 _ALPHATEST_ON 决定语义)
|
|
half4 albedoAlpha = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
|
|
// 混合贴图:rg 压缩法线,b 金属度,a 粗糙度
|
|
half4 mixTex = SAMPLE_TEXTURE2D(_MixTex, sampler_MixTex, input.uv);
|
|
|
|
#if defined(_ALPHATEST_ON)
|
|
clip(albedoAlpha.a - _Cutoff);
|
|
half occlusion = 1.0;
|
|
half alpha = albedoAlpha.a;
|
|
#else
|
|
half occlusion = lerp(1.0, albedoAlpha.a, _AO);
|
|
half alpha = 1.0;
|
|
#endif
|
|
|
|
half3 normalTS = DecodeSphereMap(mixTex.rg);
|
|
half3 normalWS = TransformTangentToWorld(normalTS,
|
|
half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
|
|
|
|
SurfaceData surfaceData = (SurfaceData)0;
|
|
surfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
|
|
surfaceData.metallic = saturate(mixTex.b * _Metallic);
|
|
surfaceData.smoothness = 1.0 - saturate(mixTex.a * _Roughness);
|
|
surfaceData.alpha = alpha;
|
|
surfaceData.normalTS = normalTS;
|
|
surfaceData.specular = half3(0.0, 0.0, 0.0);
|
|
surfaceData.emission = half3(0.0, 0.0, 0.0);
|
|
surfaceData.occlusion = occlusion;
|
|
surfaceData.clearCoatMask = half(0.0);
|
|
surfaceData.clearCoatSmoothness = half(0.0);
|
|
|
|
InputData inputData;
|
|
input.normalWS.rgb = normalWS;//法线需要传入
|
|
InitializeInputData(input, surfaceData.normalTS, inputData);
|
|
|
|
half4 color = UniversalFragmentPBR(inputData, surfaceData);
|
|
|
|
color.rgb = MixFog(color.rgb, inputData.fogCoord);
|
|
return color;
|
|
}
|
|
|
|
ENDHLSL
|
|
}
|
|
|
|
Pass
|
|
{
|
|
Name "ShadowCaster"
|
|
Tags{ "LightMode" = "ShadowCaster" }
|
|
|
|
ZWrite On
|
|
ZTest LEqual
|
|
ColorMask 0
|
|
|
|
HLSLPROGRAM
|
|
// Required to compile gles 2.0 with standard srp library
|
|
#pragma prefer_hlslcc gles
|
|
#pragma exclude_renderers d3d11_9x
|
|
#pragma target 2.0
|
|
|
|
// -------------------------------------
|
|
// Material Keywords
|
|
#pragma shader_feature_local_fragment _ALPHATEST_ON
|
|
|
|
//--------------------------------------
|
|
// GPU Instancing
|
|
#pragma multi_compile_instancing
|
|
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
|
|
#include "XPbr_Base.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
|
|
|
|
float3 _LightDirection;
|
|
|
|
Varyings vert(Attributes input)
|
|
{
|
|
Varyings output = (Varyings)0;
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
|
|
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
|
|
|
|
float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
|
|
|
|
#if UNITY_REVERSED_Z
|
|
positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
|
|
#else
|
|
positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
|
|
#endif
|
|
|
|
output.positionCS = positionCS;
|
|
output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
|
|
|
|
return output;
|
|
}
|
|
|
|
half4 frag(Varyings input) : SV_TARGET
|
|
{
|
|
#if defined(_ALPHATEST_ON)
|
|
half alpha = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv).a;
|
|
clip(alpha - _Cutoff);
|
|
#endif
|
|
return 0;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
|
|
FallBack Off
|
|
}
|