Skip to content

Commit

Permalink
Implement environment lighting [closes #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed Apr 1, 2023
1 parent 11f6724 commit e930a39
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 7 deletions.
64 changes: 64 additions & 0 deletions Assets/Samples/Lighting Settings.lighting
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!850595691 &4890085278179872738
LightingSettings:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Lighting Settings
serializedVersion: 4
m_GIWorkflowMode: 1
m_EnableBakedLightmaps: 0
m_EnableRealtimeLightmaps: 0
m_RealtimeEnvironmentLighting: 1
m_BounceScale: 1
m_AlbedoBoost: 1
m_IndirectOutputScale: 1
m_UsingShadowmask: 1
m_BakeBackend: 1
m_LightmapMaxSize: 1024
m_BakeResolution: 40
m_Padding: 2
m_LightmapCompression: 3
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_ExtractAO: 0
m_MixedBakeMode: 2
m_LightmapsBakeMode: 1
m_FilterMode: 1
m_LightmapParameters: {fileID: 15204, guid: 0000000000000000f000000000000000, type: 0}
m_ExportTrainingData: 0
m_TrainingDataDestination: TrainingData
m_RealtimeResolution: 2
m_ForceWhiteAlbedo: 0
m_ForceUpdates: 0
m_FinalGather: 0
m_FinalGatherRayCount: 256
m_FinalGatherFiltering: 1
m_PVRCulling: 1
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 512
m_PVREnvironmentSampleCount: 256
m_PVREnvironmentReferencePointCount: 2048
m_LightProbeSampleCountMultiplier: 4
m_PVRBounces: 2
m_PVRMinBounces: 1
m_PVREnvironmentMIS: 1
m_PVRFilteringMode: 1
m_PVRDenoiserTypeDirect: 1
m_PVRDenoiserTypeIndirect: 1
m_PVRDenoiserTypeAO: 1
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
m_PVRFilteringGaussRadiusAO: 2
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_PVRTiledBaking: 0
8 changes: 8 additions & 0 deletions Assets/Samples/Lighting Settings.lighting.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/Samples/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ RenderSettings:
m_FogDensity: 0.18
m_LinearFogStart: 7
m_LinearFogEnd: 9
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientSkyColor: {r: 0, g: 0.2613725, b: 0.5849056, a: 1}
m_AmbientEquatorColor: {r: 0.26415092, g: 0.26415092, b: 0.26415092, a: 1}
m_AmbientGroundColor: {r: 3.523293, g: 0.53831804, b: 0.016619347, a: 1}
m_AmbientIntensity: 0
m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
Expand Down Expand Up @@ -98,7 +98,7 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_LightingSettings: {fileID: 0}
m_LightingSettings: {fileID: 4890085278179872738, guid: 51e8cbb876391d7408f6f73230dd2732, type: 2}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
Expand Down
1 change: 1 addition & 0 deletions Assets/ToonRP/Runtime/ToonCameraRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private void DrawOpaqueGeometry()
var drawingSettings = new DrawingSettings(ForwardShaderTagId, sortingSettings)
{
enableDynamicBatching = _settings.UseDynamicBatching,
perObjectData = PerObjectData.LightProbe,
};
var filteringSettings = new FilteringSettings(RenderQueueRange.opaque);

Expand Down
20 changes: 19 additions & 1 deletion Assets/ToonRP/ShaderLibrary/Lighting.hlsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef TOON_RP_LIGHTING
#define TOON_RP_LIGHTING

#include "Shadows.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ImageBasedLighting.hlsl"

#include "Shadows.hlsl"
#include "UnityInput.hlsl"

CBUFFER_START(_ToonRPLight)
float3 _DirectionalLightColor;
Expand Down Expand Up @@ -34,4 +37,19 @@ Light GetMainLight(const float3 shadowCoords)
return light;
}

// Samples SH L0, L1 and L2 terms
float3 SampleSH(const float3 normalWs)
{
real4 shCoefficients[7];
shCoefficients[0] = unity_SHAr;
shCoefficients[1] = unity_SHAg;
shCoefficients[2] = unity_SHAb;
shCoefficients[3] = unity_SHBr;
shCoefficients[4] = unity_SHBg;
shCoefficients[5] = unity_SHBb;
shCoefficients[6] = unity_SHC;

return max(float3(0, 0, 0), SampleSH9(shCoefficients, normalWs));
}

#endif // TOON_RP_LIGHTING
10 changes: 10 additions & 0 deletions Assets/ToonRP/ShaderLibrary/UnityInput.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ real4 unity_WorldTransformParams;
// Velocity
float4x4 unity_MatrixPreviousM;
float4x4 unity_MatrixPreviousMI;

// SH block feature
real4 unity_SHAr;
real4 unity_SHAg;
real4 unity_SHAb;
real4 unity_SHBr;
real4 unity_SHBg;
real4 unity_SHBb;
real4 unity_SHC;

CBUFFER_END

float4x4 unity_MatrixVP;
Expand Down
5 changes: 4 additions & 1 deletion Assets/ToonRP/Shaders/ToonRPDefaultForwardPass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ float4 PS(const v2f IN) : SV_TARGET
specularRamp = min(specularRamp * shadowAttenuation, shadowAttenuation);
const float3 specular = light.color * _SpecularColor * specularRamp;

float3 outputColor = diffuse + specular;
const float3 ambient = SampleSH(normalWs) * albedo;

float3 outputColor = diffuse + specular + ambient;
TOON_RP_FOG_MIX(IN, outputColor);

return float4(outputColor, 1.0f);
}

Expand Down

0 comments on commit e930a39

Please sign in to comment.