diff --git a/shaders/BSDF.shader b/shaders/BSDF.shader index be6beb8..aa64780 100644 --- a/shaders/BSDF.shader +++ b/shaders/BSDF.shader @@ -156,6 +156,64 @@ Shader "BSDF/URP" } ENDHLSL } - + Pass{ + Name "ShadowCaster" + Tags { + "LightMode" = "ShadowCaster"//阴影投射 + } + ZWrite On + ZTest LEqual + ColorMask 0 + Cull Off + HLSLPROGRAM //着色器 + #pragma multi_compile instancing + #pragma multi_compile DOTS INSTANCING ON + #pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW + + #pragma vertex ShadowVS + #pragma fragment ShadowFS + + float3 _LightDirection; + float3 _LightPosition; + + struct Attributes + { + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + }; + //将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置 + float4 GetShadowPositionHClip(Attributes input){ + float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); + float3 normalWS = TransformObjectToWorldDir(input.normalOS); + #if _CASTING_PUNCTUAL_LIGHT_SHADOW + float3 lightDirectionWS = normalize(_LightPosition - positionWS); + #else + float3 lightDirectionWS = _LightDirection; + #endif + + float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS)); + + #if UNITY_REVERSED_Z + positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #else + positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #endif + return positionCS; + } + Varyings ShadowVS(Attributes input){ + Varyings output; + output.positionCS = GetShadowPositionHClip(input); + return output; + } + half4 ShadowFS(Varyings input):SV_TARGET{ + return 0; + } + ENDHLSL + } } } diff --git a/shaders/BSDF手搓.shader b/shaders/BSDF手搓.shader index 5e2a48b..3d934e8 100644 --- a/shaders/BSDF手搓.shader +++ b/shaders/BSDF手搓.shader @@ -9,6 +9,8 @@ Shader "BSDF/StandardURP" _Metallic("金属度", Range(0, 1)) = 0.0 _Specular("镜面反射颜色", Color) = (1, 1, 1, 1) _Smoothness("光滑度", Range(0, 1)) = 0 + [Toggle]_UseSmoothnessMap("使用光滑度贴图", Float) = 0 + _SmoothnessMap("光滑度贴图", 2D) = "white" {} _EmissionColor("自发光颜色", Color) = (0, 0, 0, 1) _EmissionStrength("自发光强度", Float) = 0 _ClearCoatMask("清漆遮罩", Range(0, 1)) = 0 @@ -30,6 +32,8 @@ Shader "BSDF/StandardURP" #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS//附加光源 #pragma multi_compile _ _SHADOWS_SOFT_SHADOWS//软阴影 + #pragma shader_feature _NORMALMAP//法线贴图 + #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" @@ -42,6 +46,7 @@ Shader "BSDF/StandardURP" float4 _Specular; float _ClearCoatMask; float _ClearCoatSmoothness; + half _UseSmoothnessMap; CBUFFER_END TEXTURE2D(_BaseMap); @@ -50,6 +55,8 @@ Shader "BSDF/StandardURP" SAMPLER(sampler_LightMap); TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap); + TEXTURE2D(_SmoothnessMap); + SAMPLER(sampler_SmoothnessMap); ENDHLSL Pass{ Name "UniversalForward" @@ -65,14 +72,16 @@ Shader "BSDF/StandardURP" float4 positionOS : POSITION; float2 uv0 : TEXCOORD0; float3 normalOS : NORMAL; + float4 tangentOS : TANGENT; }; struct Varyings { float4 positionCS : SV_POSITION; - float3 normalWS : TEXCOORD2; - float2 uv0 : TEXCOORD0;//语义相同用于传送数据 float3 positionWS : TEXCOORD1; + float2 uv0 : TEXCOORD0;//语义相同用于传送数据 + float3 normalWS : TEXCOORD2; + float4 tangentWS : TEXCOORD3; }; /* struct SurfaceData @@ -100,6 +109,7 @@ Shader "BSDF/StandardURP" float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv0); float3 baseColor = _BaseColor.rgb; float4 normalMap = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, input.uv0); + float4 smoothnessMap = SAMPLE_TEXTURE2D(_SmoothnessMap, sampler_SmoothnessMap, input.uv0); SurfaceData surface; surface.albedo = baseMap.rgb * baseColor;//漫反射颜色 surface.specular =_Specular.rgb;//镜面反射颜色 @@ -114,12 +124,14 @@ Shader "BSDF/StandardURP" return surface; } - InputData InitializeInputData(Varyings input){ + InputData InitializeInputData(Varyings input, float3 normalTS){ InputData data; data.positionWS = input.positionWS; data.shadowCoord = TransformWorldToShadowCoord(input.positionWS);// 阴影坐标 data.fogCoord = ComputeFogFactor(input.positionWS);//雾坐标 - data.normalWS = input.normalWS;//世界空间法线 + float3x3 TBN = CreateTangentToWorld(normalize(input.normalWS), input.tangentWS.xyz, input.tangentWS.w); + float3 normalWS = normalize(mul(normalTS, TBN)); + data.normalWS = normalWS; data.viewDirectionWS = GetWorldSpaceNormalizeViewDir(input.positionWS);//视线方向 // 采样光照贴图 float2 lightmapUV = input.uv0 * unity_LightmapST.xy + unity_LightmapST.zw; @@ -143,17 +155,17 @@ Shader "BSDF/StandardURP" return bsdf; } - float3 BRDFToColor(BRDFData bsdf, InputData inputData,float3 positionWS){ + float3 BRDFToColor(BRDFData bsdf, InputData inputData){ float3 color = float3(0, 0, 0); //主光源 - float4 shadowCoord = TransformWorldToShadowCoord(positionWS);//阴影坐标 - Light mainLight = GetMainLight(shadowCoord); + float4 shadowCoord = inputData.shadowCoord;//阴影坐标 + Light mainLight = GetMainLight(); color += LightingPhysicallyBased(bsdf,mainLight, inputData.normalWS, inputData.viewDirectionWS); //附加光源 for (int i = 0; i < GetAdditionalLightsCount(); ++i) { half4 shadowMask = CalculateShadowMask(inputData); - Light additionalLight = GetAdditionalLight(i, positionWS,shadowMask); + Light additionalLight = GetAdditionalLight(i,inputData.positionWS,shadowMask); color += LightingPhysicallyBased(bsdf, additionalLight, inputData.normalWS, inputData.viewDirectionWS); } //环境光 @@ -164,24 +176,83 @@ Shader "BSDF/StandardURP" Varyings MainVertexShader(Attributes input){ Varyings output; VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz); - VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS); + VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS,input.tangentOS); output.positionCS = vertexInput.positionCS; output.uv0 = input.uv0; output.positionWS = vertexInput.positionWS; output.normalWS = vertexNormalInput.normalWS; + output.tangentWS = float4(vertexNormalInput.tangentWS, input.tangentOS.w); return output; } float4 MainFragmentShader(Varyings input):SV_TARGET{ - InputData inputData = InitializeInputData(input); SurfaceData surface = GetSurfaceData(input); + InputData inputData = InitializeInputData(input,surface.normalTS); BRDFData bsdf = SurfaceDataToBSDFData(surface, inputData); - float3 color = BRDFToColor(bsdf, inputData, inputData.positionWS); + float3 color = BRDFToColor(bsdf, inputData); color.rgb += surface.emission * _EmissionStrength;//自发光 color.rgb = MixFog(color.rgb, inputData.fogCoord);//混合雾效 return float4(color.rgb, 1); } ENDHLSL } - + Pass{ + Name "ShadowCaster" + Tags { + "LightMode" = "ShadowCaster"//阴影投射 + } + ZWrite On + ZTest LEqual + ColorMask 0 + Cull Off + HLSLPROGRAM //着色器 + #pragma multi_compile instancing + #pragma multi_compile DOTS INSTANCING ON + #pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW + + #pragma vertex ShadowVS + #pragma fragment ShadowFS + + float3 _LightDirection; + float3 _LightPosition; + + struct Attributes + { + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + }; + //将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置 + float4 GetShadowPositionHClip(Attributes input){ + float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); + float3 normalWS = TransformObjectToWorldDir(input.normalOS); + #if _CASTING_PUNCTUAL_LIGHT_SHADOW + float3 lightDirectionWS = normalize(_LightPosition - positionWS); + #else + float3 lightDirectionWS = _LightDirection; + #endif + + float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS)); + + #if UNITY_REVERSED_Z + positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #else + positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #endif + return positionCS; + } + Varyings ShadowVS(Attributes input){ + Varyings output; + output.positionCS = GetShadowPositionHClip(input); + return output; + } + half4 ShadowFS(Varyings input):SV_TARGET{ + return 0; + } + ENDHLSL + } } } diff --git a/shaders/BSDF手搓双面.shader b/shaders/BSDF手搓双面.shader index e0554c4..69171a2 100644 --- a/shaders/BSDF手搓双面.shader +++ b/shaders/BSDF手搓双面.shader @@ -183,6 +183,64 @@ Shader "BSDF/StandardURPDouble" } ENDHLSL } - + Pass{ + Name "ShadowCaster" + Tags { + "LightMode" = "ShadowCaster"//阴影投射 + } + ZWrite On + ZTest LEqual + ColorMask 0 + Cull Off + HLSLPROGRAM //着色器 + #pragma multi_compile instancing + #pragma multi_compile DOTS INSTANCING ON + #pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW + + #pragma vertex ShadowVS + #pragma fragment ShadowFS + + float3 _LightDirection; + float3 _LightPosition; + + struct Attributes + { + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + }; + //将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置 + float4 GetShadowPositionHClip(Attributes input){ + float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); + float3 normalWS = TransformObjectToWorldDir(input.normalOS); + #if _CASTING_PUNCTUAL_LIGHT_SHADOW + float3 lightDirectionWS = normalize(_LightPosition - positionWS); + #else + float3 lightDirectionWS = _LightDirection; + #endif + + float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS)); + + #if UNITY_REVERSED_Z + positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #else + positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #endif + return positionCS; + } + Varyings ShadowVS(Attributes input){ + Varyings output; + output.positionCS = GetShadowPositionHClip(input); + return output; + } + half4 ShadowFS(Varyings input):SV_TARGET{ + return 0; + } + ENDHLSL + } } } diff --git a/shaders/BSDF手搓噪波.shader b/shaders/BSDF手搓噪波.shader index 684e46f..865e811 100644 --- a/shaders/BSDF手搓噪波.shader +++ b/shaders/BSDF手搓噪波.shader @@ -225,7 +225,7 @@ Shader "BSDF/StandardURPNoise" VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz); VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS); float alpha = clamp((max(vertexInput.positionWS.y, _MinBoundsY) - _MinBoundsY) / (_MaxBoundsY - _MinBoundsY), 0, 1); //将y值归一化到0-1之间 - float3 distanceToMiddle =normalize(vertexInput.positionWS - float3(_MiddlePosition.x, vertexInput.positionWS.y, _MiddlePosition.z)); + float3 distanceToMiddle =normalize(vertexInput.positionWS - float3(_MiddlePosition.x+0.5f*ValueNoise3D(float3(_MiddlePosition.x, input.positionOS.y, _MiddlePosition.z) * 190.0 + _Time.y * 5), vertexInput.positionWS.y, _MiddlePosition.z+0.5f*ValueNoise3D(float3(_MiddlePosition.x, input.positionOS.y, _MiddlePosition.z) * 190.0 + _Time.y * 5))); output.uv0 = input.uv0; diff --git a/shaders/BSDF混合渐变.shader b/shaders/BSDF混合渐变.shader index 1c6aaaf..5d342fe 100644 --- a/shaders/BSDF混合渐变.shader +++ b/shaders/BSDF混合渐变.shader @@ -198,6 +198,64 @@ Shader "BSDF/MixedURPGradient" } ENDHLSL } - + Pass{ + Name "ShadowCaster" + Tags { + "LightMode" = "ShadowCaster"//阴影投射 + } + ZWrite On + ZTest LEqual + ColorMask 0 + Cull Off + HLSLPROGRAM //着色器 + #pragma multi_compile instancing + #pragma multi_compile DOTS INSTANCING ON + #pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW + + #pragma vertex ShadowVS + #pragma fragment ShadowFS + + float3 _LightDirection; + float3 _LightPosition; + + struct Attributes + { + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + }; + //将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置 + float4 GetShadowPositionHClip(Attributes input){ + float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); + float3 normalWS = TransformObjectToWorldDir(input.normalOS); + #if _CASTING_PUNCTUAL_LIGHT_SHADOW + float3 lightDirectionWS = normalize(_LightPosition - positionWS); + #else + float3 lightDirectionWS = _LightDirection; + #endif + + float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS)); + + #if UNITY_REVERSED_Z + positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #else + positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #endif + return positionCS; + } + Varyings ShadowVS(Attributes input){ + Varyings output; + output.positionCS = GetShadowPositionHClip(input); + return output; + } + half4 ShadowFS(Varyings input):SV_TARGET{ + return 0; + } + ENDHLSL + } } } diff --git a/shaders/BSDF渐变.shader b/shaders/BSDF渐变.shader index ddb89bf..86ee025 100644 --- a/shaders/BSDF渐变.shader +++ b/shaders/BSDF渐变.shader @@ -191,6 +191,64 @@ Shader "BSDF/URPGradient" } ENDHLSL } - + Pass{ + Name "ShadowCaster" + Tags { + "LightMode" = "ShadowCaster"//阴影投射 + } + ZWrite On + ZTest LEqual + ColorMask 0 + Cull Off + HLSLPROGRAM //着色器 + #pragma multi_compile instancing + #pragma multi_compile DOTS INSTANCING ON + #pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW + + #pragma vertex ShadowVS + #pragma fragment ShadowFS + + float3 _LightDirection; + float3 _LightPosition; + + struct Attributes + { + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + }; + + struct Varyings + { + float4 positionCS : SV_POSITION; + }; + //将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置 + float4 GetShadowPositionHClip(Attributes input){ + float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); + float3 normalWS = TransformObjectToWorldDir(input.normalOS); + #if _CASTING_PUNCTUAL_LIGHT_SHADOW + float3 lightDirectionWS = normalize(_LightPosition - positionWS); + #else + float3 lightDirectionWS = _LightDirection; + #endif + + float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS)); + + #if UNITY_REVERSED_Z + positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #else + positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE); + #endif + return positionCS; + } + Varyings ShadowVS(Attributes input){ + Varyings output; + output.positionCS = GetShadowPositionHClip(input); + return output; + } + half4 ShadowFS(Varyings input):SV_TARGET{ + return 0; + } + ENDHLSL + } } } diff --git a/材质/小狐狸材质/狐狸头.mat b/材质/小狐狸材质/狐狸头.mat index be9a57a..54b9860 100644 --- a/材质/小狐狸材质/狐狸头.mat +++ b/材质/小狐狸材质/狐狸头.mat @@ -88,6 +88,10 @@ Material: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} + - _SmoothnessMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} - _SpecGlossMap: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} @@ -139,6 +143,7 @@ Material: - _SrcBlendAlpha: 1 - _Surface: 0 - _UVSec: 0 + - _UseSmoothnessMap: 0 - _WorkflowMode: 1 - _ZWrite: 1 m_Colors: diff --git a/材质/树木3材质/树干.mat b/材质/树木3材质/树干.mat index 444b0e3..d7a8cd3 100644 --- a/材质/树木3材质/树干.mat +++ b/材质/树木3材质/树干.mat @@ -48,7 +48,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _LightMap: - m_Texture: {fileID: 2800000, guid: 0151a36925978414f9b7d96e9490ff05, type: 3} + m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MainTex: @@ -60,7 +60,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _NormalMap: - m_Texture: {fileID: 0} + m_Texture: {fileID: 2800000, guid: 24c0fd8c5a83fc24e92991d4858e4790, type: 3} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _OcclusionMap: @@ -71,6 +71,10 @@ Material: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} + - _SmoothnessMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} - _SpecGlossMap: m_Texture: {fileID: 0} m_Scale: {x: 1, y: 1} @@ -120,6 +124,7 @@ Material: - _SrcBlendAlpha: 1 - _Surface: 0 - _UVSec: 0 + - _UseSmoothnessMap: 0 - _WorkflowMode: 1 - _ZWrite: 1 m_Colors: