Initial commit: Client Doc docs Server Tools

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
ud18010
2026-07-10 10:24:29 +08:00
co-authored by Cursor
commit 7e35d8da31
3374 changed files with 680813 additions and 0 deletions
@@ -0,0 +1,322 @@
Shader "XWorld/XTerrain"
{
Properties
{
// 四层颜色贴图
_LayerTex0 ("Layer 0 (R)", 2D) = "white" {}
_LayerTex1 ("Layer 1 (G)", 2D) = "white" {}
_LayerTex2 ("Layer 2 (B)", 2D) = "white" {}
_LayerTex3 ("Layer 3 (A)", 2D) = "white" {}
// 混合贴图(RGBA 分别对应四层权重)
_Control ("Control (RGBA)", 2D) = "white" {}
_Tiling ("Tiling", Float) = 20
_BumpScale("BumpScale", Range(0.0, 10)) = 5
_Roughness("Roughness", Range(0.0, 2.0)) = 0.3
_Metallic("Metallic", Range(0.0, 2.0)) = 0.3
_AO("AO", Range(0.0, 3.0)) = 1.0
//_EmissiveIntensity("EmissiveIntensity", Range(0.0, 2.0)) = 0.3
}
SubShader
{
// Universal Pipeline tag is required. If Universal render pipeline is not set in the graphics settings
// this Subshader will fail. One can add a subshader below or fallback to Standard built-in to make this
// material work with both Universal Render Pipeline and Builtin Unity Pipeline
Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
Pass
{
// Lightmode matches the ShaderPassName set in UniversalRenderPipeline.cs. SRPDefaultUnlit and passes with
// no LightMode tag are also rendered by Universal Render Pipeline
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 _ _MIXTEX_ON
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH
#pragma vertex vert
#pragma fragment frag
#include "XTerrain_Base.hlsl"
// 声明纹理和采样器
struct AttData
{
float3 normal;
float ao;//获得夹角越狭窄越强
float roughness;//周围高度差越多,越粗糙;颜色越相同越低;
float metallic;//使用平均亮度值
};
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);
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
output.uv = TRANSFORM_TEX(input.texcoord, _LayerTex0);
// already normalized from normal transform to WS.
// half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
// output.normalWS = half4(normalInput.normalWS, viewDirWS.x);//normalInput.normalWS;//
//#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
// real sign = input.tangentOS.w * GetOddNegativeScale();
// half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
//#endif
//#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
// output.tangentWS = tangentWS;
//#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);
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
output.viewDirTS = viewDirTS;
#endif
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;
return output;
}
//生成灰度图(高度)
float GetGrayColor(half3 color)
{
//return color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;
//return color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
return 0.299f * color.r + 0.587f * color.g + 0.114f * color.b;
}
//生成法线 ao 粗糙度,金属度
AttData GetNormalByGray(float2 uv, half4 col4)
{
AttData ad;
half bumpscale = _BumpScale * _LayerTex0_TexelSize.x;
float2 deltaU = float2(_LayerTex0_TexelSize.x, 0);
float h0 = GetGrayColor(col4.xyz);
float h1_u = GetGrayColor(SAMPLE_TEXTURE2D(_LayerTex0, sampler_LayerTex0, uv - deltaU).rgb);
float h2_u = GetGrayColor(SAMPLE_TEXTURE2D(_LayerTex0, sampler_LayerTex0, uv + deltaU).rgb);
//float3 tangent_u = float3(deltaU.x, 0, (h2_u - h0) * bumpscale);
float3 tangent_u = float3(deltaU.x, 0, (h2_u - h1_u) * bumpscale);
float2 deltaV = float2(0, _LayerTex0_TexelSize.y);
float h1_v = GetGrayColor(SAMPLE_TEXTURE2D(_LayerTex0, sampler_LayerTex0, uv - deltaV).rgb);
float h2_v = GetGrayColor(SAMPLE_TEXTURE2D(_LayerTex0, sampler_LayerTex0, uv + deltaV).rgb);
//float3 tangent_v = float3(0, deltaV.y, (h2_v - h0) * bumpscale);
float3 tangent_v = float3(0, deltaV.y, (h2_v - h1_v) * bumpscale);
//算ao. 两个向量夹角《
float ao;// = 0;
//ad.ao = 0;
ad.normal = normalize(cross(tangent_v, tangent_u));
ad.normal.z *= -1;
float3 vVA = float3(0, deltaV.y, (h2_v - h0) * bumpscale);
float3 vVB = float3(0, deltaV.y, (h1_v - h0) * bumpscale);
float3 VecV = cross(vVA, vVB);
//if (VecV.y > 0)
{//锐角,判断根据锐角度得到夹角深度
ao = saturate(acos(dot(normalize(vVA), normalize(vVB))));
}
float3 vUA = float3(deltaU.x, 0, (h2_u - h0) * bumpscale);
float3 vUB = float3(deltaU.x, 0, (h1_u - h0) * bumpscale);
float3 VecU = cross(vUA, vUB);
//if (VecU.y > 0)
{//锐角,判断根据锐角度得到夹角深度
ad.ao = saturate(acos(dot(normalize(vUA), normalize(vUB))));
}
ad.ao = saturate(1.0f - max(ao, ad.ao) * _AO);
//
//粗糙度
ad.roughness = saturate((abs(h1_u - h0) + abs(h2_u - h0) + abs(h1_v - h0) + abs(h2_v - h0)) + _Roughness);
//金属度
ad.metallic = saturate((h0 + h1_u + h2_u + h1_v + h2_v) / 5 * _Metallic);
return ad;
}
half4 frag(Varyings input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
#if defined(_PARALLAXMAP)
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
half3 viewDirTS = input.viewDirTS;
#else
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, viewDirWS);
#endif
ApplyPerPixelDisplacement(viewDirTS, input.uv);
#endif
// 采样混合权重
half4 splat_control = SAMPLE_TEXTURE2D(_Control, sampler_Control, input.uv);
// 各层颜色采样,使用同一套 UV 并乘以 Tiling
float2 uv = input.uv * _Tiling;
half4 col0 = SAMPLE_TEXTURE2D(_LayerTex0, sampler_LayerTex0, uv);
half4 col1 = SAMPLE_TEXTURE2D(_LayerTex1, sampler_LayerTex1, uv);
half4 col2 = SAMPLE_TEXTURE2D(_LayerTex2, sampler_LayerTex2, uv);
half4 col3 = SAMPLE_TEXTURE2D(_LayerTex3, sampler_LayerTex3, uv);
// 按权重混合
float total = splat_control.r + splat_control.g + splat_control.b + splat_control.a ;
float a = 1 - max(max(splat_control.r, splat_control.g), splat_control.b);
half4 col = col0 * splat_control.r / total
+ col1 * splat_control.g / total
+ col2 * splat_control.b / total
+ col3 * splat_control.a / total ;
//得到法线
AttData ad = GetNormalByGray(uv, col);
half3 normalWS = TransformTangentToWorld(ad.normal,
half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
SurfaceData surfaceData;
surfaceData.specular = half3(0.0, 0.0, 0.0);
surfaceData.albedo = col.rgb;
surfaceData.metallic = ad.metallic;
surfaceData.smoothness = 1.0 - ad.roughness; // 新版URP中smoothness直接控制高光
surfaceData.alpha = col.a;
surfaceData.normalTS = ad.normal;//input.tangentWS;//
//surfaceData.specular = 0;
surfaceData.emission = half3(0.0, 0.0, 0.0);
surfaceData.occlusion = ad.ao;
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);
//color.rgb = ad.ao;//ad.normal;
return color;
}
ENDHLSL
}
Pass
{
Name "ShadowCaster"
Tags{ "LightMode" = "ShadowCaster" }
ZWrite On
ZTest LEqual
Cull[_Cull]
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 "XTex_Base.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(positionWS);
#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
{
return SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
//return 0;
}
ENDHLSL
}
//UsePass "Universal Render Pipeline/Lit/ShadowCaster"
}
FallBack Off
}
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: c62f5ca63149bac4c8bcb39cf97084a2
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,167 @@
#ifndef XTERRAIN_LIT_BASE
#define XTERRAIN_LIT_BASE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _LayerTex0_ST;
float4 _LayerTex1_ST;
float4 _LayerTex2_ST;
float4 _LayerTex3_ST;
//half4 _BaseColor;
float _Tiling;
half _BumpScale;
half _Roughness;
half _Metallic;
half _AO;
half _EmissiveIntensity;
half _useCustomGI;
half3 _CustomGIColor;
float4 _LayerTex0_TexelSize;
CBUFFER_END
half _XRuntimeAmbientEnabled;
half4 _XRuntimeAmbientSky;
half4 _XRuntimeAmbientEquator;
half4 _XRuntimeAmbientGround;
//TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
TEXTURE2D(_LayerTex0);
SAMPLER(sampler_LayerTex0);
TEXTURE2D(_LayerTex1);
SAMPLER(sampler_LayerTex1);
TEXTURE2D(_LayerTex2);
SAMPLER(sampler_LayerTex2);
TEXTURE2D(_LayerTex3);
SAMPLER(sampler_LayerTex3);
TEXTURE2D(_Control);
SAMPLER(sampler_Control);
// 声明变量
struct Attributes
{
float4 positionOS : POSITION;
half4 color : COLOR;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 texcoord : TEXCOORD0;
float2 staticLightmapUV : TEXCOORD1;
#ifdef DYNAMICLIGHTMAP_ON
float2 dynamicLightmapUV : TEXCOORD2;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 1);
float3 positionWS : TEXCOORD2;
half4 normalWS : TEXCOORD3; // xyz: normal, w: viewDir.x
half4 tangentWS : TEXCOORD4; // xyz: tangent, w: viewDir.y
half4 bitangentWS : TEXCOORD5; // xyz: bitangent, w: viewDir.z
#ifdef _ADDITIONAL_LIGHTS_VERTEX
half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
#else
half fogFactor : TEXCOORD6;
#endif
half4 lightDir : TEXCOORD8;
half4 viewDir : TEXCOORD9;
#ifdef DYNAMICLIGHTMAP_ON
float2 dynamicLightmapUV : TEXCOORD7;
#endif
float4 positionCS : SV_POSITION;
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD10;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
half3 SampleXRuntimeAmbient(half3 normalWS)
{
half y = normalize(normalWS).y;
half useGlobal = step(0.5h, _XRuntimeAmbientEnabled);
half3 sky = lerp(half3(1.353h, 1.452h, 1.568h), _XRuntimeAmbientSky.rgb, useGlobal);
half3 equator = lerp(half3(1.023h, 1.122h, 1.254h), _XRuntimeAmbientEquator.rgb, useGlobal);
half3 ground = lerp(half3(0.561h, 0.528h, 0.462h), _XRuntimeAmbientGround.rgb, useGlobal);
half3 upper = lerp(equator, sky, saturate(y));
half3 lower = lerp(equator, ground, saturate(-y));
return lerp(lower, upper, step(0.0h, y));
}
void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
{
inputData = (InputData)0;
inputData.positionWS = input.positionWS;
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
#if defined(_NORMALMAP) || defined(_DETAIL)
float sgn = input.tangentWS.w; // should be either +1 or -1
float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
half3x3 tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
#if defined(_NORMALMAP)
inputData.tangentToWorld = tangentToWorld;
#endif
inputData.normalWS = TransformTangentToWorld(normalTS, tangentToWorld);
#else
inputData.normalWS = input.normalWS.xyz;
#endif
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
inputData.viewDirectionWS = viewDirWS;
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
inputData.shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
#else
inputData.shadowCoord = float4(0, 0, 0, 0);
#endif
#ifdef _ADDITIONAL_LIGHTS_VERTEX
inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
#else
inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
#endif
#if defined(DYNAMICLIGHTMAP_ON)
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
#elif defined(LIGHTMAP_ON)
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
#else
inputData.bakedGI = SampleSH(inputData.normalWS);
#endif
inputData.bakedGI = SampleXRuntimeAmbient(inputData.normalWS);
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
#if defined(DEBUG_DISPLAY)
#if defined(DYNAMICLIGHTMAP_ON)
inputData.dynamicLightmapUV = input.dynamicLightmapUV;
#endif
#if defined(LIGHTMAP_ON)
inputData.staticLightmapUV = input.staticLightmapUV;
#else
inputData.vertexSH = input.vertexSH;
#endif
#endif
}
#endif
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 3538332e45c8b1a4f9d4964380ddc6a9
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,322 @@
Shader "Test/XTex"
{
Properties
{
[MainColor] _BaseColor("Color", Color) = (1,1,1,1)
[MainTexture] _MainTex("Albedo", 2D) = "white" {}
[Toggle(_MIXTEX_ON)] _UseMixTex("Use Mix Map", Float) = 0
[MixTexture] _MixTex("MixTex", 2D) = "white" {}
_BumpScale("BumpScale", Range(0.0, 10)) = 5
_Roughness("Roughness", Range(0.0, 2.0)) = 0.4
_Metallic("Metallic", Range(0.0, 2.0)) = 0.3
_AO("AO", Range(0.0, 3.0)) = 1.0
_EmissiveIntensity("EmissiveIntensity", Range(0.0, 2.0)) = 0.3
}
SubShader
{
// Universal Pipeline tag is required. If Universal render pipeline is not set in the graphics settings
// this Subshader will fail. One can add a subshader below or fallback to Standard built-in to make this
// material work with both Universal Render Pipeline and Builtin Unity Pipeline
Tags{ "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
Pass
{
// Lightmode matches the ShaderPassName set in UniversalRenderPipeline.cs. SRPDefaultUnlit and passes with
// no LightMode tag are also rendered by Universal Render Pipeline
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 _ _MIXTEX_ON
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH
#pragma vertex vert
#pragma fragment frag
#include "XTex_Base.hlsl"
struct AttData
{
float3 normal;
float ao;//获得夹角越狭窄越强
float roughness;//周围高度差越多,越粗糙;颜色越相同越低;
float metallic;//使用平均亮度值
};
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
// already normalized from normal transform to WS.
// half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
// output.normalWS = half4(normalInput.normalWS, viewDirWS.x);//normalInput.normalWS;//
//#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR) || defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
// real sign = input.tangentOS.w * GetOddNegativeScale();
// half4 tangentWS = half4(normalInput.tangentWS.xyz, sign);
//#endif
//#if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
// output.tangentWS = tangentWS;
//#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);
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
output.viewDirTS = viewDirTS;
#endif
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;
}
//生成灰度图(高度)
float GetGrayColor(half3 color)
{
//return color.r * 0.2126 + color.g * 0.7152 + color.b * 0.0722;
//return color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
return 0.299f * color.r + 0.587f * color.g + 0.114f * color.b;
}
//生成法线 ao 粗糙度,金属度
AttData GetNormalByGray(float2 uv, half4 col4)
{
AttData ad;
half bumpscale = _BumpScale * _MainTex_TexelSize.x;
float2 deltaU = float2(_MainTex_TexelSize.x, 0);
float h0 = GetGrayColor(col4.xyz);
float h1_u = GetGrayColor(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - deltaU).rgb);
float h2_u = GetGrayColor(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + deltaU).rgb);
//float3 tangent_u = float3(deltaU.x, 0, (h2_u - h0) * bumpscale);
float3 tangent_u = float3(deltaU.x, 0, (h2_u - h1_u) * bumpscale);
float2 deltaV = float2(0, _MainTex_TexelSize.y);
float h1_v = GetGrayColor(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv - deltaV).rgb);
float h2_v = GetGrayColor(SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + deltaV).rgb);
//float3 tangent_v = float3(0, deltaV.y, (h2_v - h0) * bumpscale);
float3 tangent_v = float3(0, deltaV.y, (h2_v - h1_v) * bumpscale);
//算ao. 两个向量夹角《
float ao;// = 0;
//ad.ao = 0;
ad.normal = normalize(cross(tangent_v, tangent_u));
ad.normal.z *= -1;
float3 vVA = float3(0, deltaV.y, (h2_v - h0) * bumpscale);
float3 vVB = float3(0, deltaV.y, (h1_v - h0) * bumpscale);
float3 VecV = cross(vVA, vVB);
//if (VecV.y > 0)
{//锐角,判断根据锐角度得到夹角深度
ao = saturate(acos(dot(normalize(vVA), normalize(vVB))));
}
float3 vUA = float3(deltaU.x, 0, (h2_u - h0) * bumpscale);
float3 vUB = float3(deltaU.x, 0, (h1_u - h0) * bumpscale);
float3 VecU = cross(vUA, vUB);
//if (VecU.y > 0)
{//锐角,判断根据锐角度得到夹角深度
ad.ao = saturate(acos(dot(normalize(vUA), normalize(vUB))));
}
ad.ao = saturate(1.0f - max(ao, ad.ao) * _AO);
//
//粗糙度
ad.roughness = saturate((abs(h1_u - h0) + abs(h2_u - h0) + abs(h1_v - h0) + abs(h2_v - h0)) + _Roughness);
//金属度
ad.metallic = saturate((h0 + h1_u + h2_u + h1_v + h2_v) / 5 * _Metallic);
return ad;
}
half4 frag(Varyings input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
#if defined(_PARALLAXMAP)
#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
half3 viewDirTS = input.viewDirTS;
#else
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
half3 viewDirTS = GetViewDirectionTangentSpace(input.tangentWS, input.normalWS, viewDirWS);
#endif
ApplyPerPixelDisplacement(viewDirTS, input.uv);
#endif
SurfaceData surfaceData;
//InitializeStandardLitSurfaceData(input.uv, surfaceData);
half4 albedoAlpha = SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
half3 albedo = albedoAlpha.rgb * _BaseColor.rgb;
//Light mainLight = GetMainLight();
#ifdef _MIXTEX_ON
half4 mixTex = SAMPLE_TEXTURE2D(_MixTex, sampler_MixTex, input.uv);
AttData ad;// = GetNormalByGray(input.uv, albedoAlpha);;
ad.normal.rgb = DecodeSphereMap(mixTex.rg);
ad.ao = saturate(1.0 - (1.0 - mixTex.a) * _AO);
ad.roughness = mixTex.a * _Roughness;
ad.metallic = mixTex.b * _Metallic;
#else
//得到法线
AttData ad = GetNormalByGray(input.uv, albedoAlpha);
//half gray = GetGrayColor(albedoAlpha.xyz);
//ad.normal.rgb = half3(gray,gray,gray);
#endif
//half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
half3 normalWS = TransformTangentToWorld(ad.normal,
half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
surfaceData.specular = half3(0.0, 0.0, 0.0);
surfaceData.albedo = albedo;
surfaceData.metallic = ad.metallic;
surfaceData.smoothness = 1.0 - ad.roughness; // 新版URP中smoothness直接控制高光
surfaceData.alpha = albedoAlpha.a;
surfaceData.normalTS = ad.normal;//input.tangentWS;//
//surfaceData.specular = 0;
surfaceData.emission = half3(0.0, 0.0, 0.0);
surfaceData.occlusion = ad.ao;
surfaceData.clearCoatMask = half(0.0);
surfaceData.clearCoatSmoothness = half(0.0);
InputData inputData;
input.normalWS.rgb = normalWS;//ad.normal;//法线需要传入
InitializeInputData(input, surfaceData.normalTS, inputData);
//SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
//#ifdef _DBUFFER
// ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
//#endif
half4 color = UniversalFragmentPBR(inputData, surfaceData);
color.rgb = MixFog(color.rgb, inputData.fogCoord);
//color.a = OutputAlpha(color.a, _Surface);
//color.rgb = input.tangentWS;//normalWS;//surfaceData.normalTS;//
return color;
}
ENDHLSL
}
Pass
{
Name "ShadowCaster"
Tags{ "LightMode" = "ShadowCaster" }
ZWrite On
ZTest LEqual
Cull[_Cull]
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 "XTex_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);
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
float4 positionCS = TransformWorldToHClip(positionWS);
#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
{
return SampleAlbedoAlpha(input.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)).a;
//return 0;
}
ENDHLSL
}
//UsePass "Universal Render Pipeline/Lit/ShadowCaster"
}
FallBack Off
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32be331071e4bc34895b26703115c3e6
ShaderImporter:
externalObjects: {}
defaultTextures:
- _MainTex: {instanceID: 0}
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,169 @@
#ifndef XTEX_LIT_BASE
#define XTEX_LIT_BASE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _BaseColor;
half _BumpScale;
half _Roughness;
half _Metallic;
half _AO;
half _EmissiveIntensity;
half _useCustomGI;
half3 _CustomGIColor;
float4 _MainTex_TexelSize;
#ifdef _MIXTEX_ON
float4 _MixTex_ST;
#endif
CBUFFER_END
half _XRuntimeAmbientEnabled;
half4 _XRuntimeAmbientSky;
half4 _XRuntimeAmbientEquator;
half4 _XRuntimeAmbientGround;
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
#ifdef _MIXTEX_ON
TEXTURE2D(_MixTex); SAMPLER(sampler_MixTex);
#endif
struct Attributes
{
float4 positionOS : POSITION;
half4 color : COLOR;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 texcoord : TEXCOORD0;
float2 staticLightmapUV : TEXCOORD1;
#ifdef DYNAMICLIGHTMAP_ON
float2 dynamicLightmapUV : TEXCOORD2;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 1);
float3 positionWS : TEXCOORD2;
half4 normalWS : TEXCOORD3; // xyz: normal, w: viewDir.x
half4 tangentWS : TEXCOORD4; // xyz: tangent, w: viewDir.y
half4 bitangentWS : TEXCOORD5; // xyz: bitangent, w: viewDir.z
#ifdef _ADDITIONAL_LIGHTS_VERTEX
half4 fogFactorAndVertexLight : TEXCOORD6; // x: fogFactor, yzw: vertex light
#else
half fogFactor : TEXCOORD6;
#endif
half4 lightDir : TEXCOORD8;
half4 viewDir : TEXCOORD9;
#ifdef DYNAMICLIGHTMAP_ON
float2 dynamicLightmapUV : TEXCOORD7;
#endif
float4 positionCS : SV_POSITION;
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD10;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
real3 SafeNormalize_Half3(real3 inVec)
{
real dp3 = max(REAL_MIN, dot(inVec, inVec));
return inVec * rsqrt(dp3);
}
float3 DecodeSphereMap(float2 encoded)
{
float4 nn = float4(encoded, 1, -1);
float l = dot(nn.xyz, -nn.xyw);
nn.z = l;
nn.xy *= sqrt(l);
return nn.xyz * 2 + float3(0, 0, -1);
}
half3 SampleXRuntimeAmbient(half3 normalWS)
{
half y = normalize(normalWS).y;
half useGlobal = step(0.5h, _XRuntimeAmbientEnabled);
half3 sky = lerp(half3(1.353h, 1.452h, 1.568h), _XRuntimeAmbientSky.rgb, useGlobal);
half3 equator = lerp(half3(1.023h, 1.122h, 1.254h), _XRuntimeAmbientEquator.rgb, useGlobal);
half3 ground = lerp(half3(0.561h, 0.528h, 0.462h), _XRuntimeAmbientGround.rgb, useGlobal);
half3 upper = lerp(equator, sky, saturate(y));
half3 lower = lerp(equator, ground, saturate(-y));
return lerp(lower, upper, step(0.0h, y));
}
void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
{
inputData = (InputData)0;
inputData.positionWS = input.positionWS;
half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
#if defined(_NORMALMAP) || defined(_DETAIL)
float sgn = input.tangentWS.w; // should be either +1 or -1
float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
half3x3 tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
#if defined(_NORMALMAP)
inputData.tangentToWorld = tangentToWorld;
#endif
inputData.normalWS = TransformTangentToWorld(normalTS, tangentToWorld);
#else
inputData.normalWS = input.normalWS.xyz;
#endif
inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
inputData.viewDirectionWS = viewDirWS;
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
inputData.shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
#else
inputData.shadowCoord = float4(0, 0, 0, 0);
#endif
#ifdef _ADDITIONAL_LIGHTS_VERTEX
inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
#else
inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
#endif
#if defined(DYNAMICLIGHTMAP_ON)
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
#elif defined(LIGHTMAP_ON)
inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
#else
inputData.bakedGI = SampleSH(inputData.normalWS);
#endif
inputData.bakedGI = SampleXRuntimeAmbient(inputData.normalWS);
inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
#if defined(DEBUG_DISPLAY)
#if defined(DYNAMICLIGHTMAP_ON)
inputData.dynamicLightmapUV = input.dynamicLightmapUV;
#endif
#if defined(LIGHTMAP_ON)
inputData.staticLightmapUV = input.staticLightmapUV;
#else
inputData.vertexSH = input.vertexSH;
#endif
#endif
}
#endif
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: a68f172b57b5e5e4f8842cd5a63f22a3
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant: