#ifndef CHARACTER_BASE #define CHARACTER_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" TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap); CBUFFER_START(UnityPerMaterial) float4 _MainTex_ST; float4 _NormalMap_ST; half _NormalMapScale; half _Strength; half _Saturation; half _Contrast; half _ShLight; half4 _ColorTint; half3 _SpecularColor; half _SpecularFactor; half _Smoothness; half3 _EmissionColor; half _EmissionFactor; half _CameraLightIntensity; half4 _CameraLightColor; half4 _Light2Dir; CBUFFER_END struct a2v { half4 vertex : POSITION; half4 normal : NORMAL; half4 tangent: TANGENT; half2 texcoord : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f { float4 pos : SV_POSITION; float4 uvTex0 : TEXCOORD0; float3 worldPos : TEXCOORD1; #if defined(_USENORMALMAP_OFF) || !defined(SGAME_QUALITY_PERFECT) float3 worldNormal : TEXCOORD2; half3 worldViewDir : TEXCOORD3; #else half4 worldTangentDir : TEXCOORD2; half4 worldBitangentDir : TEXCOORD3; half4 worldNormalDir : TEXCOORD4; #endif float4 shadowCoord : TEXCOORD5; #if defined(_SG_EXPFOG) half4 _fogCoord : TEXCOORD6; #endif UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO }; inline half3 GetAmbientColor_Custom(half3 mainColor, half3 shLight) { half3 ambient = lerp(mainColor, mainColor * UNITY_LIGHTMODEL_AMBIENT.xyz, shLight); ambient *= 1; ambient *= 1; return ambient; } inline half3 CalculateColorAlter(half3 finalColor, half ligh, half saturation, half contrast) { half gray = 0.2125 * finalColor.r + 0.7154 * finalColor.g + 0.0721 * finalColor.b; half3 grayColor = real3(gray, gray, gray); finalColor = lerp(0.5, finalColor, contrast); finalColor = lerp(grayColor, finalColor, saturation) * ligh; return finalColor; } inline half3 BlinnPhoneLighting(half3 mainColor, half3 worldPos, half3 worldNormal, half3 worldViewDir, half3 specularColor, half smoothness, half lightAtten, half lightClamp = 20, half dotResLerp = 0, half3 customLightDir = half3(0, 0, 0), half useCustomDir = 0) { #if defined(SGAME_WET) && defined(SGAME_QUALITY_PERFECT) || defined(SGAME_QUALITY_HIGH) mainColor = lerp(mainColor, mainColor * _SG_WetColor, _SG_WetFactor); specularColor = lerp(specularColor, _SG_WetSpecularColor * _SG_WetSpecularStrength, _SG_WetFactor); smoothness = lerp(lerp(smoothness, _SG_WetSmoothness, _SG_WetFactor), smoothness, smoothness > _SG_WetSmoothness); #endif //Diffuse Light mainLight = GetMainLight(); half3 worldLightDir = normalize(mainLight.direction) * (1 - useCustomDir) + useCustomDir * customLightDir; half H = normalize(worldViewDir + worldLightDir); half NDotL = max(0, saturate(dot(worldNormal, worldLightDir))); half ndotH = max(0, dot(worldNormal, H)); half dotLerp = lerp(NDotL, ndotH, dotResLerp); half3 lightColor = mainLight.color.rgb; specularColor = mainLight.color.rgb * specularColor; half3 diffuse = mainColor * lightColor; #if defined(SGAME_WET) && defined(SGAME_QUALITY_PERFECT) || defined(SGAME_QUALITY_HIGH) half thunderNDotL = saturate(dot(worldNormal, _SG_ThunderDirection)); half3 thunderDiffuse = mainColor * lerp(half3(0, 0, 0), _SG_ThunderColor, max(lightAtten, 0.1)) * thunderNDotL * _SG_ThunderStrength; diffuse += thunderDiffuse; #endif #if defined(SGAME_QUALITY_PERFECT) || defined(SGAME_QUALITY_HIGH) worldViewDir = normalize(worldViewDir); half3 halfDir = normalize(worldLightDir + worldViewDir); half NDotH = saturate(dot(worldNormal, halfDir)); half specularPow = SGamePow(NDotH, smoothness * 128.0); half3 specular = specularColor.rgb * specularPow; half3 finalColor = diffuse + specular; #else half3 finalColor = diffuse; #endif return finalColor; } inline half3 BlinnPhoneLightingFwdBase_CustomAmbient(half3 mainColor, half3 worldPos, half3 worldNormal, half3 worldViewDir, half3 shLight, half lightAtten, half3 specularColor, half smoothness) { half3 ambient = GetAmbientColor_Custom(mainColor, shLight); half3 baseLighting = BlinnPhoneLighting(mainColor, worldPos, worldNormal, worldViewDir, specularColor, smoothness, lightAtten, 0.9); half3 finalColor = clamp(ambient, 0, 0.65) + baseLighting; return finalColor; } inline v2f VertexMain_SceneLight(a2v v) { v2f o; UNITY_SETUP_INSTANCE_ID(v); UNITY_TRANSFER_INSTANCE_ID(v, o); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz); VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent); o.pos = vertexInput.positionCS; o.uvTex0.xy = TRANSFORM_TEX(v.texcoord, _MainTex); half3 worldPos = vertexInput.positionWS; half3 worldNormal = normalInput.normalWS; half3 worldViewDir = GetCameraPositionWS() - worldPos; #if defined(_USENORMALMAP_OFF) || !defined(SGAME_QUALITY_PERFECT) o.worldViewDir = worldViewDir; o.worldNormal = normalize(worldNormal); #else o.worldTangentDir = half4(normalInput.tangentWS, worldViewDir.x); o.worldBitangentDir = half4(normalInput.bitangentWS, worldViewDir.y); o.worldNormalDir = half4(normalInput.normalWS, worldViewDir.z); #endif o.shadowCoord = GetShadowCoord(vertexInput); o.worldPos.xyz = worldPos; return o; } inline half4 FragMain_SceneLight(v2f i) : SV_TARGET { float3 worldPos = i.worldPos; half4 normalCol = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uvTex0); half4 mainColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uvTex0) * _ColorTint; #if defined(_USENORMALMAP_OFF) || !defined(SGAME_QUALITY_PERFECT) float3 worldNormal = normalize(i.worldNormal); half3 worldViewDir = normalize(i.worldViewDir); #else half3 worldViewDir = normalize(half3(i.worldTangentDir.w, i.worldBitangentDir.w, i.worldNormalDir.w)); // float3 tangentNormal = UnSampleNormalRG(normalCol.rg, i.uvTex0, _NormalMapScale); float3 tangentNormal = normalCol.rgb * 2 - 1; tangentNormal = lerp(half3(0, 0, 1), tangentNormal, _NormalMapScale); float3 worldNormal = normalize(mul(tangentNormal, float3x3(i.worldTangentDir.xyz, i.worldBitangentDir.xyz, i.worldNormalDir.xyz))); #endif half smoothness = _Smoothness; half3 specularColor = mainColor.a * _SpecularFactor * _SpecularColor; #if defined(_USECUTOFF_ON) clip(0.9 - step(normalCol.a, 1 / 255)); #endif mainColor.rgb = CalculateColorAlter(mainColor.rgb, _Strength, _Saturation, _Contrast); half3 shLight = _ShLight; half atten = 1; Light mainLight = GetMainLight(i.shadowCoord, worldPos, 0); half3 worldLightDir = mainLight.direction; atten = 1; half3 finalColor = BlinnPhoneLightingFwdBase_CustomAmbient(mainColor, worldPos, worldNormal, worldViewDir, shLight, atten, specularColor, smoothness); #if defined(_USECAMERALIGHT_ON) half3 N = worldNormal; half3 L = normalize(_Light2Dir.xyz); half3 V = normalize(worldViewDir); half3 H = normalize(L + V); half NoL = max(dot(L, N), 0.175); finalColor += _CameraLightColor * _CameraLightIntensity * mainColor.rgb * NoL; #endif #if defined(_USEEMISSION_ON) finalColor += finalColor * _EmissionFactor * _EmissionColor.rgb * max(normalCol.a / 180 * 255 - 1, 0); #endif half alpha = min(1, (normalCol.a * 315 / 159 - 1 / 160)) * _ColorTint.a + 1 / 160; return half4(finalColor, alpha); } #endif