#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