浏览代码

Made build-ready, fixed serialization issues, stripped unused constants, fixed bias issue with transmission

/projects-TheLastStand
John 6 年前
当前提交
ec9b2e04
共有 9 个文件被更改,包括 71 次插入61 次删除
  1. 6
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs
  2. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset
  3. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs
  4. 41
      ScriptableRenderPipeline/LightweightPipeline/LWRP/DiffusionProfile/DiffusionProfileSettings.cs
  5. 1
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/DiffusionProfile/DiffusionProfileSettingsEditor.Styles.cs
  6. 4
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/DiffusionProfile/DiffusionProfileSettingsEditor.cs
  7. 23
      ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs
  8. 2
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Subsurface/DiffusionProfile/PreintegratedScatter.shader
  9. 52
      ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Subsurface/Lighting.hlsl

6
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineAsset.cs


{
get { return resources != null ? resources.CopyDepthShader : null; }
}
}
public Shader PreintegratedScatterShader
{
get { return resources != null ? resources.PreintegratedScatterShader : null; }
}
}
}

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.asset


CopyDepthShader: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
ScreenSpaceShadowShader: {fileID: 4800000, guid: 0f854b35a0cf61a429bd5dcfea30eddd,
type: 3}
PreintegratedScatterShader: {fileID: 4800000, guid: c509c4fbe7201384f9bfea1d062070a6,
type: 3}

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/Data/LightweightPipelineResources.cs


public Shader BlitShader;
public Shader CopyDepthShader;
public Shader ScreenSpaceShadowShader;
public Shader PreintegratedScatterShader;
}

41
ScriptableRenderPipeline/LightweightPipeline/LWRP/DiffusionProfile/DiffusionProfileSettings.cs


public static readonly int _HalfRcpVarianceAndWeight2 = Shader.PropertyToID("_HalfRcpVarianceAndWeight2");
public static readonly int _TransmissionTint = Shader.PropertyToID("_TransmissionTint");
public static readonly int _ThicknessRemap = Shader.PropertyToID("_ThicknessRemap");
//Addition Runtime Constants
public static readonly int _PreintegratedDiffuseScatteringTextures = Shader.PropertyToID("_PreintegratedDiffuseScatteringTextures");
public static readonly int _HalfRcpVariancesAndWeights = Shader.PropertyToID("_HalfRcpVariancesAndWeights");
}
[Serializable]

[Range(0f, 1f)] public float lerpWeight;
[ColorUsage(false)] public Color transmissionTint;
public Vector2 thicknessRemap;
public float worldScale;
public Vector4[] filterKernel { get; private set; }
public Vector4 halfRcpWeightedVariances { get; private set; }

transmissionTint = Color.white;
thicknessRemap = new Vector2(0f, 5f);
worldScale = 1f;
}

thicknessRemap.x = Mathf.Clamp(thicknessRemap.x, 0f, thicknessRemap.y);
worldScale = Mathf.Max(worldScale, 0.001f);
scatterDistance1 = new Color
{

[NonSerialized] public Vector4[] halfRcpVariancesAndWeights;
[NonSerialized] public Vector4[] filterKernels;
private Material preintegration;
[NonSerialized] public Texture2DArray preintegratedScatterLUTs;
[NonSerialized] private Material preintegration;
[NonSerialized] public TextureCache2D preintegratedScatterLUTs;
public DiffusionProfile this[int index]
{

profiles[i].Validate();
}
ValidateArray(ref filterKernels, DiffusionProfileConstants.DIFFUSION_PROFILE_COUNT * DiffusionProfileConstants.SSS_N_SAMPLES);
ValidateArray(ref filterKernels, DiffusionProfileConstants.DIFFUSION_PROFILE_COUNT * DiffusionProfileConstants.SSS_N_SAMPLES);
//TLS: Will this work in build? May need to do extra work to be found in resources. If broken, SSS won't work in build.
//NOTE: We can't get reference to the render pipeline assets from scriptable object, but we add it to the assets anyways so they get included in build.
// Searching here is safe because we linked already in the LW resources, forcing it to be included in build.
preintegratedScatterLUTs = new Texture2DArray(128, 128, DiffusionProfileConstants.DIFFUSION_PROFILE_COUNT, TextureFormat.ARGB32, false, true)
{
hideFlags = HideFlags.HideAndDontSave,
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Point
};
preintegratedScatterLUTs = new TextureCache2D();
preintegratedScatterLUTs.AllocTextureArray(DiffusionProfileConstants.DIFFUSION_PROFILE_COUNT, 128, 128, TextureFormat.ARGB32, false);
UpdateCache();
}

// Fill the neutral profile.
int neutralId = DiffusionProfileConstants.DIFFUSION_PROFILE_NEUTRAL_ID;
worldScales[neutralId] = Vector4.one;
halfRcpWeightedVariances[neutralId] = Vector4.one;
for (int j = 0, n = DiffusionProfileConstants.SSS_N_SAMPLES; j < n; j++)
{

//TODO: Remember to apply
//Graphics.CopyTexture(Texture2D.blackTexture, 0, preintegratedScatterLUTs, neutralId);
preintegratedScatterLUTs.Apply();
//Apply changes
//preintegratedScatterLUTs.();
}
public void UpdateCache(int p)

// Erase previous value (This need to be done here individually as in the SSS editor we edit individual component)
thicknessRemaps[i] = new Vector4(profiles[p].thicknessRemap.x, profiles[p].thicknessRemap.y - profiles[p].thicknessRemap.x, 0f, 0f);
worldScales[i] = new Vector4(profiles[p].worldScale, 1.0f / profiles[p].worldScale, 0f, 0f);
// Convert ior to fresnel0
transmissionTints[i] = new Vector4(profiles[p].transmissionTint.r * 0.25f, profiles[p].transmissionTint.g * 0.25f, profiles[p].transmissionTint.b * 0.25f, 0f); // Premultiplied
//disabledTransmissionTintsAndFresnel0[i] = new Vector4(0.0f, 0.0f, 0.0f, fresnel0);

filterKernels[n * i + j] = profiles[p].filterKernel[j];
}
//Pribobly keep this too?
Graphics.Blit(null, preintegratedScatterRT, preintegration);
Graphics.CopyTexture(preintegratedScatterRT, 0, preintegratedScatterLUTs, i);
CommandBuffer cmd = new CommandBuffer() { name = "BuildSSSLUT_" + p };
cmd.Blit(null, preintegratedScatterRT, preintegration);
preintegratedScatterLUTs.TransferToSlice(cmd, i, preintegratedScatterRT);
Graphics.ExecuteCommandBuffer(cmd);
}
}
}

1
ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/DiffusionProfile/DiffusionProfileSettingsEditor.Styles.cs


};
public readonly GUIContent profileMinMaxThickness = new GUIContent("Min-Max Thickness (mm)", "Shows the values of the thickness remap below (in millimeters).");
public readonly GUIContent profileThicknessRemap = new GUIContent("Thickness Remap (mm)", "Remaps the thickness parameter from [0, 1] to the desired range (in millimeters).");
public readonly GUIContent profileWorldScale = new GUIContent("World Scale", "Size of the world unit in meters.");
public readonly GUIContent profileIor = new GUIContent("Index of Refraction", "Index of refraction. 1.4 for skin. Between 1.3-1.5 for most other material.");
// Jimenez SSS Model
public readonly GUIContent profileScatterDistance1 = new GUIContent("Scattering Distance #1", "The radius (in centimeters) of the 1st Gaussian filter, one per color channel. Alpha is ignored. The blur is energy-preserving, so a wide filter results in a large area with small contributions of individual samples. Smaller values increase the sharpness.");

4
ScriptableRenderPipeline/LightweightPipeline/LWRP/Editor/DiffusionProfile/DiffusionProfileSettingsEditor.cs


internal SerializedProperty name;
internal SerializedProperty transmissionTint;
internal SerializedProperty thicknessRemap;
internal SerializedProperty worldScale;
internal SerializedProperty scatterDistance1;
internal SerializedProperty scatterDistance2;
internal SerializedProperty lerpWeight;

transmissionTint = rp.Find(x => x.transmissionTint),
thicknessRemap = rp.Find(x => x.thicknessRemap),
worldScale = rp.Find(x => x.worldScale),
scatterDistance1 = rp.Find(x => x.scatterDistance1),
scatterDistance2 = rp.Find(x => x.scatterDistance2),
lerpWeight = rp.Find(x => x.lerpWeight)

EditorGUILayout.PropertyField(profile.scatterDistance1, s_Styles.profileScatterDistance1);
EditorGUILayout.PropertyField(profile.scatterDistance2, s_Styles.profileScatterDistance2);
EditorGUILayout.PropertyField(profile.lerpWeight, s_Styles.profileLerpWeight);
EditorGUILayout.PropertyField(profile.worldScale, s_Styles.profileWorldScale);
EditorGUILayout.Space();
EditorGUILayout.LabelField(s_Styles.TransmissionLabel, EditorStyles.boldLabel);

23
ScriptableRenderPipeline/LightweightPipeline/LWRP/LightweightPipeline.cs


// and per-object light lists.
private List<int> m_SortedLightIndexMap = new List<int>();
private Vector4[] m_CascadeBiases = new Vector4[kMaxCascades];
private Dictionary<VisibleLight, int> m_VisibleLightsIDMap = new Dictionary<VisibleLight, int>(new LightEqualityComparer());
private Mesh m_BlitQuad;

private Material m_ScreenSpaceShadowsMaterial;
private Material m_PreintegratedScatterMaterial;
private int m_BlitTexID = Shader.PropertyToID("_BlitTex");
private CopyTextureSupport m_CopyTextureSupport;

m_BlitMaterial = CoreUtils.CreateEngineMaterial(m_Asset.BlitShader);
m_CopyDepthMaterial = CoreUtils.CreateEngineMaterial(m_Asset.CopyDepthShader);
m_ScreenSpaceShadowsMaterial = CoreUtils.CreateEngineMaterial(m_Asset.ScreenSpaceShadowShader);
m_PreintegratedScatterMaterial = CoreUtils.CreateEngineMaterial(m_Asset.PreintegratedScatterShader);
m_ErrorMaterial = CoreUtils.CreateEngineMaterial("Hidden/InternalErrorShader");
}

private void SetupShaderSubsurfaceConstants(CommandBuffer cmd)
{
cmd.SetGlobalTexture("_PreintegratedDiffuseScatteringTextures", DiffusionProfileSettings.preintegratedScatterLUTs);
cmd.SetGlobalVectorArray("_TransmissionTints", DiffusionProfileSettings.transmissionTints);
cmd.SetGlobalVectorArray("_ThicknessRemaps", DiffusionProfileSettings.thicknessRemaps);
cmd.SetGlobalVectorArray("_HalfRcpVariancesAndWeights", DiffusionProfileSettings.halfRcpVariancesAndWeights);
cmd.SetGlobalTexture (DiffusionProfileShaderIDs._PreintegratedDiffuseScatteringTextures, DiffusionProfileSettings.preintegratedScatterLUTs.GetTexCache());
cmd.SetGlobalVectorArray(DiffusionProfileShaderIDs._TransmissionTint, DiffusionProfileSettings.transmissionTints);
cmd.SetGlobalVectorArray(DiffusionProfileShaderIDs._ThicknessRemap, DiffusionProfileSettings.thicknessRemaps);
cmd.SetGlobalVectorArray(DiffusionProfileShaderIDs._HalfRcpVariancesAndWeights, DiffusionProfileSettings.halfRcpVariancesAndWeights); //Still need for transmission.
cmd.SetGlobalVectorArray ("_CascadeBiases", m_CascadeBiases);
}
private void SetupMainLightConstants(CommandBuffer cmd, List<VisibleLight> lights, int lightIndex)

cmd.SetGlobalVectorArray(PerCameraBuffer._AdditionalLightSpotAttenuation, m_LightSpotAttenuations);
}
private void SetupShadowCasterConstants(CommandBuffer cmd, ref VisibleLight visibleLight, Matrix4x4 proj, float cascadeResolution)
private void SetupShadowCasterConstants(CommandBuffer cmd, ref VisibleLight visibleLight, Matrix4x4 proj, float cascadeResolution, int cascadeIndex = 0)
{
Light light = visibleLight.light;
float bias = 0.0f;

// Since we are applying normal bias on caster side we want an inset normal offset
// thus we use a negative normal bias.
normalBias = -light.shadowNormalBias * texelSize * kernelRadius;
// Currently, cascades are computed in such a way to reduce code between directional/spot etc. We aren't given the index here, so we must cache + clear every frame for now.
// This is needed for offsetting transmission.
m_CascadeBiases[cascadeIndex].x = bias;
m_CascadeBiases[cascadeIndex].y = normalBias;
}
else if (visibleLight.lightType == LightType.Spot)
{

if (!success)
break;
SetupShadowCasterConstants(cmd, ref shadowLight, proj, shadowResolution);
SetupShadowCasterConstants(cmd, ref shadowLight, proj, shadowResolution, cascadeIdx);
SetupShadowSliceTransform(cascadeIdx, shadowResolution, proj, view);
RenderShadowSlice(cmd, ref context, cascadeIdx, proj, view, settings);
}

2
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Subsurface/DiffusionProfile/PreintegratedScatter.shader


//-------------------------------------------------------------------------------------
float4 _StdDev1, _StdDev2;
float _LerpWeight, _MaxRadius; // See 'SubsurfaceScatteringParameters'
float _LerpWeight; // See 'SubsurfaceScatteringParameters'
//-------------------------------------------------------------------------------------
// Implementation

52
ScriptableRenderPipeline/LightweightPipeline/LWRP/Shaders/Subsurface/Lighting.hlsl


//Diffusion Profile Constants
int _DiffusionProfile;
float4 _ThicknessRemaps[DIFFUSION_PROFILE_COUNT];
float4 _TransmissionTints[DIFFUSION_PROFILE_COUNT];
float4 _ThicknessRemap[DIFFUSION_PROFILE_COUNT];
float4 _TransmissionTint[DIFFUSION_PROFILE_COUNT];
float4 _CascadeBiases[4];
TEXTURE2D_ARRAY(_PreintegratedDiffuseScatteringTextures);
SAMPLER(sampler_PreintegratedDiffuseScatteringTextures);

half3 DiffuseScattering(BRDFData brdfData, half3 normalHighWS, half3 normalLowWS, half3 lightDirectionWS)
{
float3 c = 1.0 - float3(0.5, 0.3, 0.22); //TODO: BRDFData?
float3 rN = lerp(normalHighWS, normalLowWS, c.r);
float3 gN = lerp(normalHighWS, normalLowWS, c.g);
float3 bN = lerp(normalHighWS, normalLowWS, c.b);
float3 NdotL = float3(dot(rN, lightDirectionWS),
dot(gN, lightDirectionWS),
dot(bN, lightDirectionWS));
float NdotL = dot(normalHighWS, lightDirectionWS);
float3 scatteredDiffuse;
scatteredDiffuse.r = SAMPLE_TEXTURE2D_ARRAY(_PreintegratedDiffuseScatteringTextures, sampler_PreintegratedDiffuseScatteringTextures, float2(NdotL.r, brdfData.curvature), _DiffusionProfile).r;
scatteredDiffuse.g = SAMPLE_TEXTURE2D_ARRAY(_PreintegratedDiffuseScatteringTextures, sampler_PreintegratedDiffuseScatteringTextures, float2(NdotL.g, brdfData.curvature), _DiffusionProfile).g;
scatteredDiffuse.b = SAMPLE_TEXTURE2D_ARRAY(_PreintegratedDiffuseScatteringTextures, sampler_PreintegratedDiffuseScatteringTextures, float2(NdotL.b, brdfData.curvature), _DiffusionProfile).b;
return scatteredDiffuse;
//NOTE: We only do broad-form scattering, too expensive on mobile to sample 3x for high detail normals.
return SAMPLE_TEXTURE2D_ARRAY(_PreintegratedDiffuseScatteringTextures, sampler_PreintegratedDiffuseScatteringTextures, float2(NdotL, brdfData.curvature), _DiffusionProfile).rgb;
}
half3 ShadowScattering(half shadow, half NdotL)

}
SamplerState Sampler_Linear_Clamp;
float4 _ShadowBiasForTransmission[4];
//TODO: Normal bias here?
//First, shrink the world pos to eliminate aliasing.
//Calculate the distance traveled through the media.
//BIAS: Mimic the normal bias done in the shadow pass for proper distance calculation.
float invNdotL = 1.0 - saturate(dot(light.direction, normalWS));
float scale = invNdotL * _CascadeBiases[cascadeIndex].y;
positionWS = normalWS * scale.xxx + positionWS;
//NOTE: Need to counteract the bias done during shadowmap construction, to properly calculate thickness
shadowCoord.z += _ShadowBiasForTransmission[cascadeIndex].x;
//BIAS: Mimic the bias done in the shadow pass for proper distance calculation.
shadowCoord.z += _CascadeBiases[cascadeIndex].x;
//Calculate the distance traveled through the media.
half d = abs(d1 - d2);
float thickness = _ThicknessRemaps[_DiffusionProfile].x + _ThicknessRemaps[_DiffusionProfile].y * (abs(d1 - d2) * _Thickness);
float3 transmittance = ComputeTransmittanceJimenez( _HalfRcpVariancesAndWeights[_DiffusionProfile][0].rgb,
_HalfRcpVariancesAndWeights[_DiffusionProfile][0].a,
_HalfRcpVariancesAndWeights[_DiffusionProfile][1].rgb,
_HalfRcpVariancesAndWeights[_DiffusionProfile][1].a,
_TransmissionTints[_DiffusionProfile].rgb,
thickness);
float thickness = _ThicknessRemap[_DiffusionProfile].x + _ThicknessRemap[_DiffusionProfile].y * (d * _Thickness);
float3 transmittance = ComputeTransmittanceJimenez(_HalfRcpVariancesAndWeights[_DiffusionProfile][0].rgb,
_HalfRcpVariancesAndWeights[_DiffusionProfile][0].a,
_HalfRcpVariancesAndWeights[_DiffusionProfile][1].rgb,
_HalfRcpVariancesAndWeights[_DiffusionProfile][1].a,
_TransmissionTint[_DiffusionProfile].rgb,
thickness);
float irradiance = max(0.3 + dot(-normalWS, light.direction), 0.0);
return transmittance * irradiance * light.color * albedo;

half3 color = GlobalIllumination (brdfData, inputData.bakedGI, occlusion, inputData.normalWS, inputData.viewDirectionWS);
color += LightingPhysicallyBased (brdfData, mainLight, inputData.normalWS, inputData.viewDirectionWS);
color += Transmittance (inputData.positionWS, inputData.normalWS, albedo, mainLight);
return half4(color ,1);
#ifdef _ADDITIONAL_LIGHTS
int pixelLightCount = GetPixelLightCount();

正在加载...
取消
保存