ライトの取得をURPに対応

This commit is contained in:
notargs 2023-04-26 11:07:27 +09:00
parent 89431f6451
commit 0f0c8bba94
3 changed files with 49 additions and 6 deletions

View File

@ -30,12 +30,19 @@ struct Varyings
#endif
float3 viewDirWS : TEXCOORD4;
#ifdef MTOON_URP
#ifdef MTOON_URP
half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
#else
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD6;
#endif
DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 7);
#else
UNITY_FOG_COORDS(5)
UNITY_LIGHTING_COORDS(6,7)
#endif
#endif
float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS"
UNITY_VERTEX_INPUT_INSTANCE_ID

View File

@ -57,14 +57,24 @@ Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name.
output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign);
#endif
#ifdef MTOON_URP
#ifdef MTOON_URP
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
OUTPUT_LIGHTMAP_UV(input.texcoord1, unity_LightmapST, output.lightmapUV);
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
half3 vertexLight = VertexLighting(output.positionWS, output.normalWS);
half fogFactor = ComputeFogFactor(output.pos.z);
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
#else
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = GetShadowCoord(vertexInput);
#endif
#else
UNITY_TRANSFER_FOG(output, output.pos);
UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy);
#endif
#endif
return output;
}

View File

@ -3,6 +3,7 @@
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#else
#include <UnityCG.cginc>
#include <AutoLight.cginc>
@ -24,16 +25,41 @@ struct UnityLighting
UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS)
{
#ifdef MTOON_URP
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
float4 shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
#else
float4 shadowCoord = float4(0, 0, 0, 0);
#endif
float shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
Light mainLight = GetMainLight(shadowCoord, input.positionWS, shadowMask);
const half3 lightDir = mainLight.direction;
const half3 lightColor = mainLight.color.rgb;
float atten = mainLight.shadowAttenuation;
#else
UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS);
const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS));
const half3 lightColor = _LightColor0.rgb;
#endif
if (MToon_IsForwardBasePass())
{
UnityLighting output;
#ifdef MTOON_URP
output.indirectLight = SampleSH(normalWS);
output.indirectLightEqualized = (SampleSH(half3(0, 1, 0)) + SampleSH(half3(0, -1, 0))) * 0.5;
#else
output.indirectLight = ShadeSH9(half4(normalWS, 1));
output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5;
#endif
output.directLightColor = lightColor;
output.directLightDirection = lightDir;
output.directLightAttenuation = atten;