浏览代码

Added support to query shadowbias from Light. Fixed glossiness in specular.

/vr_sandbox
Felipe Lira 8 年前
当前提交
e606701e
共有 3 个文件被更改,包括 41 次插入27 次删除
  1. 4
      Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity
  2. 41
      Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader
  3. 23
      Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs

4
Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity


m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1111054116}
m_LocalRotation: {x: 0.7882043, y: 0.51460123, z: 0.11175055, w: 0.31848294}
m_LocalPosition: {x: -8.34, y: 11.7, z: 0.9}
m_LocalPosition: {x: -7.69, y: 11.2, z: -0.1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 722899211}

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1485985477}
m_LocalRotation: {x: -0.45457986, y: -0.24911958, z: 0.7948072, w: -0.31555998}
m_LocalRotation: {x: -0.4545799, y: -0.2491196, z: 0.79480726, w: -0.31556}
m_LocalPosition: {x: 0.11000013, y: 1.55, z: 1.63}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []

41
Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader


#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _SPECGLOSSMAP
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _NORMALMAP
#pragma multi_compile _ LIGHTMAP_ON

#if defined(UNITY_REVERSED_Z)
return step(depth, shadowCoordDepth);
#else
return step(shadowCoordDepth, depth);
return step(shadowCoordDepth, depth);
#endif
}

return attenuation * 0.25;
}
inline half3 EvaluateOneLight(LightInput lightInput, half3 diffuseColor, half3 specularColor, half3 normal, float3 posWorld, half3 viewDir)
inline half3 EvaluateOneLight(LightInput lightInput, half3 diffuseColor, half4 specularGloss, half3 normal, float3 posWorld, half3 viewDir)
{
float3 posToLight = lightInput.pos.xyz;
posToLight -= posWorld * lightInput.pos.w;

half3 lightColor = lightInput.color.rgb * lightAtten;
half3 diffuse = diffuseColor * lightColor * NdotL;
half3 specular = specularColor * lightColor * pow(NdotH, 128.0f) * _Glossiness;
half3 specular = specularGloss.rgb * lightColor * pow(NdotH, 64.0f) * specularGloss.a;
inline half3 EvaluateMainLight(LightInput lightInput, half3 diffuseColor, half3 specularColor, half3 normal, float4 posWorld, half3 viewDir)
inline half3 EvaluateMainLight(LightInput lightInput, half3 diffuseColor, half4 specularGloss, half3 normal, float4 posWorld, half3 viewDir)
{
int cascadeIndex = ComputeCascadeIndex(posWorld.w);
float4 shadowCoord = mul(_WorldToShadow[cascadeIndex], float4(posWorld.xyz, 1.0));

return cascadeColors[cascadeIndex] * diffuseColor * max(shadowAttenuation, 0.5);
#endif
half3 color = EvaluateOneLight(lightInput, diffuseColor, specularColor, normal, posWorld, viewDir);
half3 color = EvaluateOneLight(lightInput, diffuseColor, specularGloss, normal, posWorld, viewDir);
#ifdef SHADOWS_DEPTH
return color * shadowAttenuation;

o.normal = normal;
#endif
half3 diffuseAndSpecularColor = half3(1.0, 1.0, 1.0);
half4 diffuseAndSpecular = half4(1.0, 1.0, 1.0, 1.0);
o.fogCoord.yzw += EvaluateOneLight(lightInput, diffuseAndSpecularColor, diffuseAndSpecularColor, normal, o.posWS.xyz, o.viewDir.xyz);
o.fogCoord.yzw += EvaluateOneLight(lightInput, diffuseAndSpecular.rgb, diffuseAndSpecular, normal, o.posWS.xyz, o.viewDir.xyz);
}
#ifndef LIGHTMAP_ON

#else
half3 normal = normalize(i.normal);
#endif
float3 posWorld = i.posWS.xyz;
half3 viewDir = i.viewDir.xyz;

half4 specGloss = SpecularGloss(i.uv01.xy);
half3 specular = specGloss.rgb;
half smoothness = specGloss.a;
half oneMinusReflectivity;
half4 specularGloss = SpecularGloss(i.uv01.xy);
half3 specular = specularGloss.rgb;
half oneMinusReflectivity;
// Note: UnityStandardCoreForwardSimple is not energy conserving. The lightmodel from LDPipeline will appear
// slither darker when comparing to Standard Simple due to this.
diffuse = EnergyConservationBetweenDiffuseAndSpecular(diffuse, specular, /*out*/ oneMinusReflectivity);
// Indirect Light Contribution

#if DEBUG_CASCADES
return half4(EvaluateMainLight(mainLight, diffuse, specular, normal, i.posWS, viewDir), 1.0);
#endif
half3 directColor = EvaluateMainLight(mainLight, diffuse, specular, normal, i.posWS, viewDir);
half3 directColor = EvaluateMainLight(mainLight, diffuse, specularGloss, normal, i.posWS, viewDir);
// Compute direct contribution from additional lights.
for (int lightIndex = 1; lightIndex < globalLightCount.x; ++lightIndex)

directColor += EvaluateOneLight(additionalLight, diffuse, specular, normal, posWorld, viewDir);
directColor += EvaluateOneLight(additionalLight, diffuse, specularGloss, normal, posWorld, viewDir);
}
half3 color = directColor + indirectDiffuse + _EmissionColor;

#pragma vertex vert
#pragma fragment frag
float4 _ShadowBias;
return UnityApplyLinearShadowBias(clipPos);
#if defined(UNITY_REVERSED_Z)
clipPos.z -= _ShadowBias.x;
#else
clipPos.z += _ShadowBias.x;
#endif
return clipPos;
}
half4 frag() : SV_TARGET

ENDCG
}
}
Fallback "RenderLoop/Error"
CustomEditor "StandardShaderGUI"
}

23
Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs


var shadowCasters = new List<InputShadowLightData>();
var lights = cullResults.visibleLights;
int directionalLightCount = 0;
int shadowLightsCount = 0;
int lightsCount = (settings.maxShadowLightsSupported > 0) ? Mathf.Min(lights.Length, settings.maxShadowLightsSupported) : lights.Length;
for (int i = 0; i < lightsCount; i++)
for (int i = 0; i < lights.Length && shadowLightsCount < settings.maxShadowLightsSupported; i++)
{
//@TODO: ignore baked. move this logic to c++...
if (lights[i].light.shadows == LightShadows.None)

light.shadowResolution = shadowResolution;
shadowCasters.Add(light);
shadowLightsCount++;
}
return shadowCasters;
}

var lightType = visibleLights[lightIndex].lightType;
var lightDirection = visibleLights[lightIndex].light.transform.forward;
var shadowNearPlaneOffset = QualitySettings.shadowNearPlaneOffset;
var shadowBias = visibleLights[lightIndex].light.shadowBias;
int shadowSliceIndex = packedShadows.GetShadowSliceIndex(lightIndex, 0);

bool needRendering = cullResults.ComputeSpotShadowMatricesAndCullingPrimitives(lightIndex, out view, out proj, out settings.splitData);
SetupShadowSplitMatrices(ref packedShadows.shadowSlices[shadowSliceIndex], proj, view);
if (needRendering)
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], lightDirection, proj, view, ref loop, settings);
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], shadowBias, lightDirection, proj, view, ref loop, settings);
}
else if (lightType == LightType.Directional)
{

SetupShadowSplitMatrices(ref shadowSlices[shadowSliceIndex], proj, view);
if (needRendering)
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], lightDirection, proj, view, ref loop, settings);
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], shadowBias, lightDirection, proj, view, ref loop, settings);
}
}
else if (lightType == LightType.Point)

SetupShadowSplitMatrices(ref shadowSlices[shadowSliceIndex], proj, view);
if (needRendering)
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], lightDirection, proj, view, ref loop, settings);
RenderShadowSplit(ref shadowSlices[shadowSliceIndex], shadowBias, lightDirection, proj, view, ref loop, settings);
}
}
Profiler.EndSample();

matScaleBias.m23 = 0.5f;
matScaleBias.m13 = 0.5f;
// Later down the pipeline the proj matrix will be scaled to reverse-z in case of DX.
// We need account for that scale in the shadowTransform.
if (m_Settings.shadowType == ShadowSettings.ShadowType.LIGHTSPACE && SystemInfo.usesReversedZBuffer)
matScaleBias.m22 = -0.5f;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------
private void RenderShadowSplit(ref ShadowSliceData slice, Vector3 lightDirection, Matrix4x4 proj, Matrix4x4 view, ref ScriptableRenderContext loop, DrawShadowsSettings settings)
private void RenderShadowSplit(ref ShadowSliceData slice, float shadowBias, Vector3 lightDirection, Matrix4x4 proj, Matrix4x4 view, ref ScriptableRenderContext loop, DrawShadowsSettings settings)
{
var commandBuffer = new CommandBuffer { name = "ShadowSetup" };

commandBuffer.SetGlobalVector("g_vLightDirWs", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z));
if (m_Settings.shadowType == ShadowSettings.ShadowType.LIGHTSPACE)
commandBuffer.SetGlobalVector("_ShadowBias", new Vector4(shadowBias, 0.0f, 0.0f, 0.0f));
else
commandBuffer.SetGlobalVector("g_vLightDirWs", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z));
// commandBuffer.SetGlobalDepthBias (1.0F, 1.0F);
loop.ExecuteCommandBuffer(commandBuffer);

正在加载...
取消
保存