Skip to content

Commit 9a08f7f

Browse files
authored
Merge pull request #340 from Unity-Technologies/dev/light-layer-check
add light layers (#339)
2 parents 2f5d09a + cc9b476 commit 9a08f7f

File tree

5 files changed

+126
-26
lines changed

5 files changed

+126
-26
lines changed

com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ Shader "Toon" {
12181218
#pragma multi_compile _ _SHADOWS_SOFT
12191219
#pragma multi_compile _ _FORWARD_PLUS
12201220
#pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3
1221+
#pragma multi_compile_fragment _ _LIGHT_LAYERS
12211222

12221223
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
12231224
// -------------------------------------

com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,7 @@ Shader "Toon(Tessellation)" {
12791279
#pragma multi_compile _ _SHADOWS_SOFT
12801280
#pragma multi_compile _ _FORWARD_PLUS
12811281
#pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3
1282+
#pragma multi_compile_fragment _ _LIGHT_LAYERS
12821283

12831284
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
12841285
// -------------------------------------

com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBody.hlsl

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@
237237
float distanceAttenuation;
238238
float shadowAttenuation;
239239
int type;
240+
#ifdef _LIGHT_LAYERS
241+
uint layerMask;
242+
#endif
240243
};
241244

242245
///////////////////////////////////////////////////////////////////////////////
@@ -322,6 +325,9 @@
322325
light.shadowAttenuation = 1.0;
323326
light.color = _MainLightColor.rgb;
324327
light.type = _MainLightPosition.w;
328+
#ifdef _LIGHT_LAYERS
329+
light.layerMask = _MainLightLayerMask;
330+
#endif
325331
return light;
326332
}
327333

@@ -341,12 +347,18 @@
341347
half3 color = _AdditionalLightsBuffer[perObjectLightIndex].color.rgb;
342348
half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation;
343349
half4 spotDirection = _AdditionalLightsBuffer[perObjectLightIndex].spotDirection;
350+
#ifdef _LIGHT_LAYERS
351+
uint lightLayerMask = _AdditionalLightsBuffer[perObjectLightIndex].layerMask;
352+
#endif
344353
half4 lightOcclusionProbeInfo = _AdditionalLightsBuffer[perObjectLightIndex].occlusionProbeChannels;
345354
#else
346355
float4 lightPositionWS = _AdditionalLightsPosition[perObjectLightIndex];
347356
half3 color = _AdditionalLightsColor[perObjectLightIndex].rgb;
348357
half4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[perObjectLightIndex];
349358
half4 spotDirection = _AdditionalLightsSpotDir[perObjectLightIndex];
359+
#ifdef _LIGHT_LAYERS
360+
uint lightLayerMask = asuint(_AdditionalLightsLayerMasks[perObjectLightIndex]);
361+
#endif
350362
half4 lightOcclusionProbeInfo = _AdditionalLightsOcclusionProbes[perObjectLightIndex];
351363
#endif
352364

@@ -364,6 +376,9 @@
364376
light.shadowAttenuation = AdditionalLightRealtimeShadowUTS(perObjectLightIndex, positionWS, positionCS);
365377
light.color = color;
366378
light.type = lightPositionWS.w;
379+
#ifdef _LIGHT_LAYERS
380+
light.layerMask = lightLayerMask;
381+
#endif
367382

368383
// In case we're using light probes, we can sample the attenuation from the `unity_ProbesOcclusion`
369384
#if defined(LIGHTMAP_ON) || defined(_MIXED_LIGHTING_SUBTRACTIVE)
@@ -396,9 +411,21 @@
396411
return GetAdditionalPerObjectUtsLight(perObjectLightIndex, positionWS, positionCS);
397412
}
398413

399-
half3 GetLightColor(UtsLight light)
414+
half3 GetLightColor(
415+
UtsLight light
416+
#ifdef _LIGHT_LAYERS
417+
, uint meshRenderingLayers
418+
#endif
419+
)
420+
{
421+
half3 lightColor = 0;
422+
#ifdef _LIGHT_LAYERS
423+
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
424+
#endif
400425
{
401-
return light.color * light.distanceAttenuation;
426+
lightColor = light.color * light.distanceAttenuation;
427+
}
428+
return lightColor;
402429
}
403430

404431

@@ -524,12 +551,27 @@
524551

525552
#endif //#if defined(_SHADINGGRADEMAP)
526553

527-
float4 frag(VertexOutput i, fixed facing : VFACE) : SV_TARGET
554+
void frag(
555+
VertexOutput i
556+
, fixed facing : VFACE
557+
, out float4 finalRGBA : SV_Target0
558+
#ifdef _WRITE_RENDERING_LAYERS
559+
, out float4 outRenderingLayers : SV_Target1
560+
#endif
561+
)
528562
{
529563
#if defined(_SHADINGGRADEMAP)
530-
return fragShadingGradeMap(i, facing);
564+
fragShadingGradeMap(i, facing, finalRGBA
565+
#ifdef _WRITE_RENDERING_LAYERS
566+
,outRenderingLayers
567+
#endif
568+
);
531569
#else
532-
return fragDoubleShadeFeather(i, facing);
570+
fragDoubleShadeFeather(i, facing, finalRGBA
571+
#ifdef _WRITE_RENDERING_LAYERS
572+
,outRenderingLayers
573+
#endif
574+
);
533575
#endif
534576

535577
}

com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyDoubleShadeWithFeather.hlsl

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77

88

9-
float4 fragDoubleShadeFeather(VertexOutput i, fixed facing : VFACE) : SV_TARGET
9+
void fragDoubleShadeFeather(
10+
VertexOutput i
11+
, fixed facing : VFACE
12+
, out float4 finalRGBA : SV_Target0
13+
#ifdef _WRITE_RENDERING_LAYERS
14+
, out float4 outRenderingLayers : SV_Target1
15+
#endif
16+
)
1017
{
1118

1219

@@ -81,7 +88,17 @@
8188

8289
UtsLight mainLight = GetMainUtsLightByID(i.mainLightID, i.posWorld.xyz, inputData.shadowCoord, i.positionCS);
8390

84-
half3 mainLightColor = GetLightColor(mainLight);
91+
#if defined(_LIGHT_LAYERS)
92+
uint meshRenderingLayers = GetMeshRenderingLayer();
93+
#endif
94+
95+
half3 mainLightColor = GetLightColor(
96+
mainLight
97+
#ifdef _LIGHT_LAYERS
98+
, meshRenderingLayers
99+
#endif
100+
);
101+
85102
float4 _MainTex_var = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, TRANSFORM_TEX(Set_UV0, _MainTex));
86103

87104
#ifdef _DBUFFER
@@ -268,8 +285,13 @@
268285

269286
UtsLight additionalLight = GetUrpMainUtsLight(0,0);
270287
additionalLight = GetAdditionalUtsLight(iLight, inputData.positionWS,i.positionCS);
271-
half3 additionalLightColor = GetLightColor(additionalLight);
272-
// attenuation = light.distanceAttenuation;
288+
half3 additionalLightColor = GetLightColor(
289+
additionalLight
290+
#ifdef _LIGHT_LAYERS
291+
, meshRenderingLayers
292+
#endif
293+
);
294+
// attenuation = light.distanceAttenuation;
273295

274296

275297
float3 lightDirection = additionalLight.direction;
@@ -347,8 +369,13 @@
347369
{
348370
additionalLight = GetAdditionalUtsLight(iLight, inputData.positionWS,i.positionCS);
349371
}
350-
half3 additionalLightColor = GetLightColor(additionalLight);
351-
// attenuation = light.distanceAttenuation;
372+
half3 additionalLightColor = GetLightColor(
373+
additionalLight
374+
#ifdef _LIGHT_LAYERS
375+
, meshRenderingLayers
376+
#endif
377+
);
378+
// attenuation = light.distanceAttenuation;
352379

353380

354381
float3 lightDirection = additionalLight.direction;
@@ -459,18 +486,22 @@
459486
#ifdef _IS_CLIPPING_OFF
460487
//DoubleShadeWithFeather
461488

462-
fixed4 finalRGBA = fixed4(finalColor,1);
489+
finalRGBA = fixed4(finalColor,1);
463490

464491
#elif _IS_CLIPPING_MODE
465492
//DoubleShadeWithFeather_Clipping
466493

467-
fixed4 finalRGBA = fixed4(finalColor,1);
494+
finalRGBA = fixed4(finalColor,1);
468495

469496
#elif _IS_CLIPPING_TRANSMODE
470497
//DoubleShadeWithFeather_TransClipping
471498
float Set_Opacity = SATURATE_IF_SDR((_Inverse_Clipping_var+_Tweak_transparency));
472-
fixed4 finalRGBA = fixed4(finalColor,Set_Opacity);
499+
finalRGBA = fixed4(finalColor,Set_Opacity);
500+
501+
#endif
473502

503+
#ifdef _WRITE_RENDERING_LAYERS
504+
uint renderingLayers = GetMeshRenderingLayer();
505+
outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
474506
#endif
475-
return finalRGBA;
476507
}

com.unity.toonshader/Runtime/UniversalRP/Shaders/UniversalToonBodyShadingGradeMap.hlsl

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55

66

7-
float4 fragShadingGradeMap(VertexOutput i, fixed facing : VFACE) : SV_TARGET
7+
void fragShadingGradeMap(
8+
VertexOutput i
9+
, fixed facing : VFACE
10+
, out float4 finalRGBA : SV_Target0
11+
#ifdef _WRITE_RENDERING_LAYERS
12+
, out float4 outRenderingLayers : SV_Target1
13+
#endif
14+
)
815
{
916

1017
i.normalDir = normalize(i.normalDir);
@@ -76,8 +83,17 @@
7683
envColor *= 1.8f;
7784

7885
UtsLight mainLight = GetMainUtsLightByID(i.mainLightID, i.posWorld.xyz, inputData.shadowCoord, i.positionCS);
79-
half3 mainLightColor = GetLightColor(mainLight);
8086

87+
#if defined(_LIGHT_LAYERS)
88+
uint meshRenderingLayers = GetMeshRenderingLayer();
89+
#endif
90+
91+
half3 mainLightColor = GetLightColor(
92+
mainLight
93+
#ifdef _LIGHT_LAYERS
94+
, meshRenderingLayers
95+
#endif
96+
);
8197

8298
float4 _MainTex_var = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, TRANSFORM_TEX(Set_UV0, _MainTex));
8399

@@ -331,7 +347,12 @@
331347
float notDirectional = 1.0f; //_WorldSpaceLightPos0.w of the legacy code.
332348
UtsLight additionalLight = GetUrpMainUtsLight(0,0);
333349
additionalLight = GetAdditionalUtsLight(loopCounter, inputData.positionWS, i.positionCS);
334-
half3 additionalLightColor = GetLightColor(additionalLight);
350+
half3 additionalLightColor = GetLightColor(
351+
additionalLight
352+
#ifdef _LIGHT_LAYERS
353+
, meshRenderingLayers
354+
#endif
355+
);
335356

336357
float3 lightDirection = additionalLight.direction;
337358
//v.2.0.5:
@@ -428,8 +449,12 @@
428449
{
429450
additionalLight = GetAdditionalUtsLight(iLight, inputData.positionWS, i.positionCS);
430451
}
431-
half3 additionalLightColor = GetLightColor(additionalLight);
432-
452+
half3 additionalLightColor = GetLightColor(
453+
additionalLight
454+
#ifdef _LIGHT_LAYERS
455+
, meshRenderingLayers
456+
#endif
457+
);
433458

434459

435460
float3 lightDirection = additionalLight.direction;
@@ -547,19 +572,19 @@
547572
//v.2.0.4
548573
#ifdef _IS_TRANSCLIPPING_OFF
549574

550-
fixed4 finalRGBA = fixed4(finalColor,1);
575+
finalRGBA = fixed4(finalColor,1);
551576

552577
#elif _IS_TRANSCLIPPING_ON
553578
float Set_Opacity = SATURATE_IF_SDR((_Inverse_Clipping_var+_Tweak_transparency));
554579

555-
fixed4 finalRGBA = fixed4(finalColor,Set_Opacity);
580+
finalRGBA = fixed4(finalColor,Set_Opacity);
556581

557582
#endif
558583

559-
560-
return finalRGBA;
561-
562-
584+
#ifdef _WRITE_RENDERING_LAYERS
585+
uint renderingLayers = GetMeshRenderingLayer();
586+
outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
587+
#endif
563588
}
564589

565590

0 commit comments

Comments
 (0)