浏览代码

LowEndMobile Pipeline - Added Shadows.

/vr_sandbox
Felipe Lira 8 年前
当前提交
5ccdddfc
共有 21 个文件被更改,包括 1994 次插入15 次删除
  1. 33
      Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs
  2. 10
      ProjectSettings/GraphicsSettings.asset
  3. 12
      ProjectSettings/QualitySettings.asset
  4. 9
      Assets/LowEndRenderLoop.meta
  5. 21
      Assets/LowEndRenderLoop/LowEndPipeline.asset
  6. 8
      Assets/LowEndRenderLoop/LowEndPipeline.asset.meta
  7. 1001
      Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity
  8. 8
      Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity.meta
  9. 273
      Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader
  10. 9
      Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader.meta
  11. 279
      Assets/LowEndRenderLoop/LowEndRenderPipeline.cs
  12. 12
      Assets/LowEndRenderLoop/LowEndRenderPipeline.cs.meta
  13. 74
      Assets/LowEndRenderLoop/MobileColors.mat
  14. 8
      Assets/LowEndRenderLoop/MobileColors.mat.meta
  15. 74
      Assets/LowEndRenderLoop/MobileColorsTile.mat
  16. 8
      Assets/LowEndRenderLoop/MobileColorsTile.mat.meta
  17. 75
      Assets/LowEndRenderLoop/MobileGlass.mat
  18. 8
      Assets/LowEndRenderLoop/MobileGlass.mat.meta
  19. 79
      Assets/LowEndRenderLoop/MobilePlane.mat
  20. 8
      Assets/LowEndRenderLoop/MobilePlane.mat.meta

33
Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs


[System.Serializable]
public class ShadowSettings
{
public enum ShadowType : int
{
SCREENSPACE = 0,
LIGHTSPACE,
SHADOWTYPE_COUNT
}
public bool enabled;
public int shadowAtlasWidth;
public int shadowAtlasHeight;

public Vector3 directionalLightCascades;
public int maxShadowLightsSupported;
public RenderTextureFormat renderTextureFormat;
public ShadowType shadowType;
public static ShadowSettings Default
{

settings.directionalLightCascades = new Vector3(0.05F, 0.2F, 0.3F);
settings.directionalLightCascadeCount = 4;
settings.maxShadowDistance = 1000.0F;
settings.maxShadowLightsSupported = -1;
settings.renderTextureFormat = RenderTextureFormat.Shadowmap;
settings.shadowType = ShadowType.SCREENSPACE;
return settings;
}
}

var setRenderTargetCommandBuffer = new CommandBuffer();
setRenderTargetCommandBuffer.name = "Render packed shadows";
setRenderTargetCommandBuffer.GetTemporaryRT(m_ShadowTexName, m_Settings.shadowAtlasWidth, m_Settings.shadowAtlasHeight, k_DepthBuffer, FilterMode.Bilinear, RenderTextureFormat.Shadowmap, RenderTextureReadWrite.Linear);
setRenderTargetCommandBuffer.GetTemporaryRT(m_ShadowTexName, m_Settings.shadowAtlasWidth, m_Settings.shadowAtlasHeight, k_DepthBuffer, FilterMode.Bilinear, m_Settings.renderTextureFormat, RenderTextureReadWrite.Linear);
setRenderTargetCommandBuffer.SetRenderTarget(new RenderTargetIdentifier(m_ShadowTexName));
setRenderTargetCommandBuffer.ClearRenderTarget(true, true, Color.green);

VisibleLight[] visibleLights = cullResults.visibleLights;
var shadowSlices = packedShadows.shadowSlices;
int shadowLightsCount = (m_Settings.maxShadowLightsSupported > 0)
? Mathf.Min(packedShadows.shadowLights.Length)
: packedShadows.shadowLights.Length;
for (int lightIndex = 0; lightIndex < packedShadows.shadowLights.Length; lightIndex++)
for (int lightIndex = 0; lightIndex < shadowLightsCount; lightIndex++)
{
int shadowSliceCount = packedShadows.shadowLights[lightIndex].shadowSliceCount;
if (shadowSliceCount == 0)

var matScaleBias = Matrix4x4.identity;
matScaleBias.m00 = 0.5f;
matScaleBias.m11 = 0.5f;
matScaleBias.m22 = 0.5f;
matScaleBias.m22 = 0.5f;
matScaleBias.m23 = 0.5f;
matScaleBias.m23 = 0.5f;
// TODO: Projection Matrix is changed after SetViewProjectoinMatrix depending on zbuffer params and api.
// TODO: Provide API to check zBuffer direction
if (m_Settings.shadowType == ShadowSettings.ShadowType.LIGHTSPACE)
matScaleBias.m22 = -0.5f;
var matTile = Matrix4x4.identity;
matTile.m00 = (float)lightData.shadowResolution / (float)m_Settings.shadowAtlasWidth;

lightData.shadowTransform = matTile * matScaleBias * proj * view;
}

commandBuffer.SetGlobalVector("g_vLightDirWs", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z));
commandBuffer.SetViewProjectionMatrices(view, proj);
// commandBuffer.SetGlobalDepthBias (1.0F, 1.0F);
loop.ExecuteCommandBuffer(commandBuffer);
commandBuffer.Dispose();

10
ProjectSettings/GraphicsSettings.asset


--- !u!30 &1
GraphicsSettings:
m_ObjectHideFlags: 0
serializedVersion: 10
serializedVersion: 11
m_Deferred:
m_Mode: 0
m_Shader: {fileID: 0}

m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
m_CustomRenderPipeline: {fileID: 11400000, guid: 2400b74f5ce370c4481e5dc417d03703,
m_CustomRenderPipeline: {fileID: 11400000, guid: 24bd6bbef6a97cb478bf5f9fc34b5a06,
type: 2}
m_TransparencySortMode: 0
m_TransparencySortAxis: {x: 0, y: 0, z: 1}

m_FogStripping: 0
m_LightmapKeepPlain: 1
m_LightmapKeepDirCombined: 1
m_LightmapKeepDirSeparate: 1
m_LightmapKeepDynamicDirSeparate: 1
m_LightmapKeepShadowMask: 1
m_LightmapKeepSubtractive: 1
m_LightsUseLinearIntensity: 0
m_LightsUseCCT: 0

12
ProjectSettings/QualitySettings.asset


excludedTargetPlatforms: []
- serializedVersion: 2
name: Simple
pixelLightCount: 1
pixelLightCount: 4
shadowCascades: 1
shadowDistance: 20
shadowCascades: 4
shadowDistance: 50
shadowNearPlaneOffset: 2
shadowCascade2Split: 0.33333334
shadowCascade4Split: {x: 0.06666667, y: 0.2, z: 0.46666667}

shadows: 2
shadowResolution: 2
shadowProjection: 1
shadowCascades: 2
shadowDistance: 200
shadowCascades: 4
shadowDistance: 100
shadowCascade4Split: {x: 0.021409363, y: 0.051549375, z: 0.07213539}
shadowCascade4Split: {x: 0.10035671, y: 0.20015624, z: 0.29968998}
blendWeights: 4
textureQuality: 0
anisotropicTextures: 1

9
Assets/LowEndRenderLoop.meta


fileFormatVersion: 2
guid: 7a2706b41ea31e54d9e4ca93be22278a
folderAsset: yes
timeCreated: 1481548458
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

21
Assets/LowEndRenderLoop/LowEndPipeline.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ae13646e45aa3634da32df7c7da1c380, type: 3}
m_Name: LowEndPipeline
m_EditorClassIdentifier:
m_SupportsVertexLight: 1
m_ShadowSettings:
enabled: 1
shadowAtlasWidth: 4096
shadowAtlasHeight: 4096
maxShadowDistance: 1000
directionalLightCascadeCount: 4
directionalLightCascades: {x: 0.05, y: 0.2, z: 0.3}

8
Assets/LowEndRenderLoop/LowEndPipeline.asset.meta


fileFormatVersion: 2
guid: 24bd6bbef6a97cb478bf5f9fc34b5a06
timeCreated: 1481796869
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

1001
Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity
文件差异内容过多而无法显示
查看文件

8
Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity.meta


fileFormatVersion: 2
guid: 57e3c6f39b3e40b408a2baf5b0da667c
timeCreated: 1478094608
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

273
Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader


// Shader targeted for LowEnd mobile devices. Single Pass Forward Rendering. Shader Model 2
//
// The parameters and inspector of the shader are the same as Standard shader,
// for easier experimentation.
Shader "RenderLoop/LowEnd"
{
// Properties is just a copy of Standard.shader. Our example shader does not use all of them,
// but the inspector UI expects all these to exist.
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo", 2D) = "white" {}
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
_GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
[Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
[Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
_MetallicGlossMap("Metallic", 2D) = "white" {}
[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
_BumpScale("Scale", Float) = 1.0
_BumpMap("Normal Map", 2D) = "bump" {}
_Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
_ParallaxMap("Height Map", 2D) = "black" {}
_OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
_OcclusionMap("Occlusion", 2D) = "white" {}
_EmissionColor("Color", Color) = (0,0,0)
_EmissionMap("Emission", 2D) = "white" {}
_DetailMask("Detail Mask", 2D) = "white" {}
_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
_DetailNormalMapScale("Scale", Float) = 1.0
_DetailNormalMap("Normal Map", 2D) = "bump" {}
[Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
[HideInInspector] _Mode("__mode", Float) = 0.0
[HideInInspector] _SrcBlend("__src", Float) = 1.0
[HideInInspector] _DstBlend("__dst", Float) = 0.0
[HideInInspector] _ZWrite("__zw", Float) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "PerformanceChecks" = "False" }
LOD 300
// Include forward (base + additive) pass from regular Standard shader.
// They are not used by the scriptable render loop; only here so that
// if we turn off our example loop, then regular forward rendering kicks in
// and objects look just like with a Standard shader.
UsePass "Standard/FORWARD"
UsePass "Standard/FORWARD_DELTA"
Pass
{
Tags { "LightMode" = "LowEndForwardBase" }
// Use same blending / depth states as Standard shader
Blend[_SrcBlend][_DstBlend]
ZWrite[_ZWrite]
CGPROGRAM
#pragma enable_d3d11_debug_symbols
#pragma target 2.0
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _METALLICGLOSSMAP
#include "UnityCG.cginc"
#include "UnityStandardBRDF.cginc"
#include "UnityStandardUtils.cginc"
#define DEBUG_CASCADES 0
#define MAX_SHADOW_CASCADES 4
#define MAX_LIGHTS 8
// The variables are very similar to built-in unity_LightColor, unity_LightPosition,
// unity_LightAtten, unity_SpotDirection as used by the VertexLit shaders, except here
// we use world space positions instead of view space.
half4 globalLightColor[MAX_LIGHTS];
float4 globalLightPos[MAX_LIGHTS];
float4 globalLightSpotDir[MAX_LIGHTS];
float4 globalLightAtten[MAX_LIGHTS];
int4 globalLightCount; // x: pixelLightCount, y = totalLightCount (pixel + vert)
// Global ambient/SH probe, similar to unity_SH* built-in variables.
float4 globalSH[7];
sampler2D _MainTex; float4 _MainTex_ST;
sampler2D _MetallicGlossMap;
sampler2D g_tShadowBuffer;
float _Metallic;
float _Glossiness;
float4x4 _WorldToShadow[MAX_SHADOW_CASCADES];
float _PSSMDistances[MAX_SHADOW_CASCADES - 1];
struct LightInput
{
half4 pos;
half4 color;
half4 atten;
half4 spotDir;
};
inline half3 EvaluateOneLight(LightInput lightInput, half3 diffuseColor, half3 specularColor, float3 normal, float3 posWorld, half3 viewDir)
{
float3 posToLight = lightInput.pos.xyz;
posToLight -= posWorld * lightInput.pos.w;
float distanceSqr = max(dot(posToLight, posToLight), 0.001);
float lightAtten = 1.0 / (1.0 + distanceSqr * lightInput.atten.z);
float3 lightDir = normalize(posToLight);
float SdotL = saturate(dot(lightInput.spotDir.xyz, lightDir));
lightAtten *= saturate((SdotL - lightInput.atten.x) / lightInput.atten.y);
float cutoff = step(distanceSqr, lightInput.atten.w);
lightAtten *= cutoff;
float NdotL = saturate(dot(normal, lightDir));
half3 halfVec = normalize(lightDir + viewDir);
half NdotH = saturate(dot(normal, halfVec));
half3 lightColor = lightInput.color.rgb * lightAtten;
half3 diffuse = diffuseColor * lightColor * NdotL;
half3 specular = specularColor * lightColor * pow(NdotH, 128.0f) * _Glossiness;
return diffuse + specular;
}
inline half3 EvaluateOneLightAndShadow(LightInput lightInput, half3 diffuseColor, half3 specularColor, float3 normal, float3 posWorld, half3 viewDir, half3 shadowCoord)
{
// TODO: Apply proper bias considering NdotL
half bias = 0.001;
half shadowDepth = tex2D(g_tShadowBuffer, shadowCoord.xy).r;
half shadowAttenuation = 1.0;
#if defined(UNITY_REVERSED_Z)
shadowAttenuation = step(shadowDepth - bias, shadowCoord.z);
#else
shadowAttenuation = step(shadowCoord.z - bias, shadowDepth);
#endif
half3 color = EvaluateOneLight(lightInput, diffuseColor, specularColor, normal, posWorld, viewDir);
return color * shadowAttenuation;
}
inline int ComputeCascadeIndex(float eyeZ)
{
for (int index = 0; index < MAX_SHADOW_CASCADES - 1; ++index)
{
if (eyeZ < _PSSMDistances[index])
return index;
}
return MAX_SHADOW_CASCADES - 2;
}
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float3 texcoord : TEXCOORD0;
};
// Vertex shader
struct v2f
{
float2 uv : TEXCOORD0;
float3 positionWS : TEXCOORD1;
float3 normalWS : TEXCOORD2;
float3 color : TEXCOORD3;
float3 shadowCoord : TEXCOORD4;
float4 hpos : SV_POSITION;
};
v2f vert(appdata_base v)
{
v2f o;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.hpos = UnityObjectToClipPos(v.vertex);
o.positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
o.normalWS = UnityObjectToWorldNormal(v.normal);
half eyePosZ = mul(UNITY_MATRIX_V, float4(o.positionWS, 1.0)).z;
half3 diffuseAndSpecularColor = half3(1.0, 1.0, 1.0);
half3 viewDir = normalize(_WorldSpaceCameraPos - o.positionWS);
int cascadeIndex = ComputeCascadeIndex(eyePosZ);
o.shadowCoord = mul(_WorldToShadow[3], float4(o.positionWS, 1.0));
for (int lightIndex = globalLightCount.x; lightIndex < globalLightCount.y; ++lightIndex)
{
LightInput lightInput;
lightInput.pos = globalLightPos[lightIndex];
lightInput.color = globalLightColor[lightIndex];
lightInput.atten = globalLightAtten[lightIndex];
lightInput.spotDir = globalLightSpotDir[lightIndex];
o.color += EvaluateOneLight(lightInput, diffuseAndSpecularColor, diffuseAndSpecularColor, o.normalWS, o.positionWS, viewDir);
}
#if DEBUG_CASCADES
half4 cascadeColors[MAX_SHADOW_CASCADES] = { half4(0.5, 0, 0, 1) , half4(0, 0.5, 0, 1) , half4(0, 0.0, 0.5, 1) , half4(0.5, 0, 0.5, 1) };
o.color = cascadeColors[cascadeIndex];
#endif
return o;
}
half4 frag(v2f i) : SV_Target
{
i.normalWS = normalize(i.normalWS);
half3 viewDir = normalize(_WorldSpaceCameraPos - i.positionWS);
half4 diffuseAlbedo = tex2D(_MainTex, i.uv);
half2 metalSmooth;
#ifdef _METALLICGLOSSMAP
metalSmooth = tex2D(_MetallicGlossMap, i.uv).ra;
#else
metalSmooth.r = _Metallic;
metalSmooth.g = _Glossiness;
#endif
half3 specColor;
half oneMinuReflectivity;
half3 diffuse = DiffuseAndSpecularFromMetallic(diffuseAlbedo.rgb, metalSmooth.x, specColor, oneMinuReflectivity);
half3 color = i.color * diffuseAlbedo.rgb;
#if !DEBUG_CASCADES
for (int lightIndex = 0; lightIndex < globalLightCount.x; ++lightIndex)
{
LightInput lightInput;
lightInput.pos = globalLightPos[lightIndex];
lightInput.color = globalLightColor[lightIndex];
lightInput.atten = globalLightAtten[lightIndex];
lightInput.spotDir = globalLightSpotDir[lightIndex];
if (lightIndex == 0)
color += EvaluateOneLightAndShadow(lightInput, diffuse, specColor, i.normalWS, i.positionWS, viewDir, i.shadowCoord);
else
color += EvaluateOneLight(lightInput, diffuse, specColor, i.normalWS, i.positionWS, viewDir);
}
#endif
return half4(color, diffuseAlbedo.a);
}
ENDCG
}
Pass
{
Tags { "Lightmode" = "ShadowCaster" }
ZWrite On ZTest LEqual
CGPROGRAM
#pragma enable_d3d11_debug_symbols
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 vert(float4 position : POSITION) : SV_POSITION
{
return UnityObjectToClipPos(position);
}
half4 frag() : SV_TARGET
{
return 0;
}
ENDCG
}
}
CustomEditor "StandardShaderGUI"
}

9
Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader.meta


fileFormatVersion: 2
guid: 319d30749b10bd544a9e4fa5a9c36c1e
timeCreated: 1478093850
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

279
Assets/LowEndRenderLoop/LowEndRenderPipeline.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.ScriptableRenderLoop;
using UnityEngine.Rendering;
using UnityEngine.ScriptableRenderPipeline;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LowEndRenderPipeline : RenderPipeline
{
#if UNITY_EDITOR
[UnityEditor.MenuItem("Renderloop/Create Low End Pipeline")]
static void CreateLowEndPipeline()
{
var instance = ScriptableObject.CreateInstance<LowEndRenderPipeline>();
AssetDatabase.CreateAsset(instance, "Assets/LowEndRenderLoop/LowEndPipeline.asset");
}
#endif
private class LowEndRenderPipelineDataStore : RenderingDataStore
{
public LowEndRenderPipelineDataStore(BaseRenderPipeline pipe) : base(pipe)
{
}
protected override void InternalBuild()
{
base.InternalBuild();
LowEndRenderPipeline theOwner = owner as LowEndRenderPipeline;
if (theOwner != null)
theOwner.Build();
}
protected override void InternalCleanup()
{
base.InternalCleanup();
LowEndRenderPipeline theOwner = owner as LowEndRenderPipeline;
if (theOwner != null)
theOwner.Cleanup();
}
}
public bool m_SupportsVertexLight = true;
[SerializeField]
ShadowSettings m_ShadowSettings = ShadowSettings.Default;
ShadowRenderPass m_ShadowPass;
public void Build()
{
BuildShadowSettings();
m_ShadowPass = new ShadowRenderPass(m_ShadowSettings);
}
public void Cleanup()
{
}
[NonSerialized]
List<Camera> m_CamerasToRender = new List<Camera>();
public override IScriptableRenderDataStore ConstructDataStore()
{
return new LowEndRenderPipelineDataStore(this);
}
public override void Render(ScriptableRenderContext context, IScriptableRenderDataStore dataStore)
{
base.Render(context, dataStore);
cameraProvider.GetCamerasToRender(m_CamerasToRender);
foreach (Camera camera in m_CamerasToRender)
{
CullingParameters cullingParameters;
camera.farClipPlane = 1000.0f;
if (!CullResults.GetCullingParameters(camera, out cullingParameters))
continue;
cullingParameters.shadowDistance = QualitySettings.shadowDistance;
CullResults cull = CullResults.Cull(ref cullingParameters, context);
var cmd = new CommandBuffer() { name = "Clear" };
cmd.ClearRenderTarget(true, false, Color.black);
context.ExecuteCommandBuffer(cmd);
cmd.Dispose();
ShadowOutput shadowOutput;
m_ShadowPass.Render(context, cull, out shadowOutput);
SetupShadowShaderVariables(shadowOutput, context, camera.nearClipPlane, cullingParameters.shadowDistance);
context.SetupCameraProperties(camera);
SetupLightShaderVariables(cull.visibleLights, context);
var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("LowEndForwardBase"));
settings.sorting.flags = SortFlags.CommonOpaque;
settings.inputFilter.SetQueuesOpaque();
context.DrawRenderers(ref settings);
context.DrawSkybox(camera);
settings.sorting.flags = SortFlags.CommonTransparent;
settings.inputFilter.SetQueuesTransparent();
context.DrawRenderers(ref settings);
}
context.Submit();
CleanCameras(m_CamerasToRender);
m_CamerasToRender.Clear();
}
private void BuildShadowSettings()
{
m_ShadowSettings = ShadowSettings.Default;
m_ShadowSettings.directionalLightCascadeCount = QualitySettings.shadowCascades; ;
m_ShadowSettings.shadowAtlasWidth = 1024;
m_ShadowSettings.shadowAtlasHeight = 1024;
m_ShadowSettings.maxShadowDistance = QualitySettings.shadowDistance;
m_ShadowSettings.maxShadowLightsSupported = 1;
m_ShadowSettings.shadowType = ShadowSettings.ShadowType.LIGHTSPACE;
m_ShadowSettings.renderTextureFormat = RenderTextureFormat.Depth;
switch (m_ShadowSettings.directionalLightCascadeCount)
{
case 1:
m_ShadowSettings.directionalLightCascades = new Vector3(1.0f, 0.0f, 0.0f);
break;
case 2:
m_ShadowSettings.directionalLightCascades = new Vector3(QualitySettings.shadowCascade2Split, 1.0f, 0.0f);
break;
case 4:
m_ShadowSettings.directionalLightCascades = QualitySettings.shadowCascade4Split;
break;
default:
Debug.LogError("Invalid Shadow Cascade Settings");
m_ShadowSettings = ShadowSettings.Default;
break;
}
}
private void SetupLightShaderVariables(VisibleLight[] lights, ScriptableRenderContext context)
{
if (lights.Length <= 0)
return;
const int kMaxLights = 8;
Vector4[] lightPositions = new Vector4[kMaxLights];
Vector4[] lightColors = new Vector4[kMaxLights];
Vector4[] lightAttenuations = new Vector4[kMaxLights];
Vector4[] lightSpotDirections = new Vector4[kMaxLights];
Vector4[] lightIntensity = new Vector4[kMaxLights];
// TODO: Sort Lighting Importance
int pixelLightCount = Mathf.Min(lights.Length, QualitySettings.pixelLightCount);
int vertexLightCount = Mathf.Min(lights.Length - pixelLightCount, kMaxLights);
int totalLightCount = pixelLightCount + vertexLightCount;
for (int i = 0; i < totalLightCount; ++i)
{
VisibleLight currLight = lights[i];
if (currLight.lightType == LightType.Directional)
{
Vector4 dir = currLight.localToWorld.GetColumn(2);
lightPositions[i] = new Vector4(-dir.x, -dir.y, -dir.z, 0.0f);
}
else
{
Vector4 pos = currLight.localToWorld.GetColumn(3);
lightPositions[i] = new Vector4(pos.x, pos.y, pos.z, 1.0f);
}
lightColors[i] = currLight.finalColor;
lightIntensity[i] = new Vector4(currLight.light.intensity, 0.0f, 0.0f, 0.0f);
float rangeSq = currLight.range * currLight.range;
float minCutoff = 0.01f;
float quadAtten = (currLight.lightType == LightType.Directional) ? 0.0f : 25.0f / rangeSq;
if (currLight.lightType == LightType.Spot)
{
Vector4 dir = currLight.localToWorld.GetColumn(2);
lightSpotDirections[i] = new Vector4(-dir.x, -dir.y, -dir.z, 0.0f);
float spotAngle = Mathf.Deg2Rad * currLight.spotAngle;
float cosOuterAngle = Mathf.Cos(spotAngle * 0.5f);
float cosInneAngle = Mathf.Cos(spotAngle * 0.25f);
float angleRange = cosInneAngle - cosOuterAngle;
lightAttenuations[i] = new Vector4(cosOuterAngle,
Mathf.Approximately(angleRange, 0.0f) ? 1.0f : angleRange, quadAtten, rangeSq);
}
else
{
lightSpotDirections[i] = new Vector4(0.0f, 0.0f, 1.0f, 0.0f);
lightAttenuations[i] = new Vector4(-1.0f, 1.0f, quadAtten, rangeSq);
}
}
// ambient lighting spherical harmonics values
const int kSHCoefficients = 7;
Vector4[] shConstants = new Vector4[kSHCoefficients];
SphericalHarmonicsL2 ambientSH = RenderSettings.ambientProbe * RenderSettings.ambientIntensity;
GetShaderConstantsFromNormalizedSH(ref ambientSH, shConstants);
CommandBuffer cmd = new CommandBuffer() {name = "SetupShadowShaderConstants"};
cmd.SetGlobalVectorArray("globalLightPos", lightPositions);
cmd.SetGlobalVectorArray("globalLightColor", lightColors);
cmd.SetGlobalVectorArray("globalLightAtten", lightAttenuations);
cmd.SetGlobalVectorArray("globalLightSpotDir", lightSpotDirections);
cmd.SetGlobalVector("globalLightCount", new Vector4(pixelLightCount, totalLightCount, 0.0f, 0.0f));
cmd.SetGlobalVectorArray("globalSH", shConstants);
context.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}
private void RenderShadowPass(CullResults results, ScriptableRenderContext context, out ShadowOutput shadow)
{
m_ShadowPass.Render(context, results, out shadow);
}
void SetupShadowShaderVariables(ShadowOutput shadowOutput, ScriptableRenderContext context, float shadowNear, float shadowFar)
{
// PSSM distance settings
float shadowFrustumDepth = shadowNear - shadowFar;
Vector3 shadowSplitRatio = m_ShadowSettings.directionalLightCascades;
// TODO: check z buffer direction to invert eye space depths
float[] PSSMDistances =
{
shadowNear + shadowSplitRatio.x * shadowFrustumDepth,
shadowNear + shadowSplitRatio.y * shadowFrustumDepth,
shadowNear + shadowSplitRatio.z * shadowFrustumDepth,
};
Matrix4x4[] shadowMatrices =
{
shadowOutput.shadowSlices[0].shadowTransform,
shadowOutput.shadowSlices[1].shadowTransform,
shadowOutput.shadowSlices[2].shadowTransform,
shadowOutput.shadowSlices[3].shadowTransform
};
var setupShadow = new CommandBuffer() { name = "SetupShadowShaderConstants" };
setupShadow.SetGlobalMatrixArray("_WorldToShadow", shadowMatrices);
setupShadow.SetGlobalFloatArray("_PSSMDistances", PSSMDistances);
context.ExecuteCommandBuffer(setupShadow);
setupShadow.Dispose();
}
private void GetShaderConstantsFromNormalizedSH(ref SphericalHarmonicsL2 ambientProbe, Vector4[] outCoefficients)
{
for (int channelIdx = 0; channelIdx < 3; ++channelIdx)
{
// Constant + Linear
// In the shader we multiply the normal is not swizzled, so it's normal.xyz.
// Swizzle the coefficients to be in { x, y, z, DC } order.
outCoefficients[channelIdx].x = ambientProbe[channelIdx, 3];
outCoefficients[channelIdx].y = ambientProbe[channelIdx, 1];
outCoefficients[channelIdx].z = ambientProbe[channelIdx, 2];
outCoefficients[channelIdx].w = ambientProbe[channelIdx, 0] - ambientProbe[channelIdx, 6];
// Quadratic polynomials
outCoefficients[channelIdx + 3].x = ambientProbe[channelIdx, 4];
outCoefficients[channelIdx + 3].y = ambientProbe[channelIdx, 5];
outCoefficients[channelIdx + 3].z = ambientProbe[channelIdx, 6] * 3.0f;
outCoefficients[channelIdx + 3].w = ambientProbe[channelIdx, 7];
}
// Final quadratic polynomial
outCoefficients[6].x = ambientProbe[0, 8];
outCoefficients[6].y = ambientProbe[1, 8];
outCoefficients[6].z = ambientProbe[2, 8];
outCoefficients[6].w = 1.0f;
}
}

12
Assets/LowEndRenderLoop/LowEndRenderPipeline.cs.meta


fileFormatVersion: 2
guid: ae13646e45aa3634da32df7c7da1c380
timeCreated: 1481548757
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

74
Assets/LowEndRenderLoop/MobileColors.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileColors
m_Shader: {fileID: 4800000, guid: 319d30749b10bd544a9e4fa5a9c36c1e, type: 3}
m_ShaderKeywords: _EMISSION
m_LightmapFlags: 1
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 10, y: 10}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 4e96aadeab75a4745b17d60a1fea54c5, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

8
Assets/LowEndRenderLoop/MobileColors.mat.meta


fileFormatVersion: 2
guid: 1b54f485d9718e64dbff824f0defbb92
timeCreated: 1481626510
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

74
Assets/LowEndRenderLoop/MobileColorsTile.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileColorsTile
m_Shader: {fileID: 4800000, guid: 319d30749b10bd544a9e4fa5a9c36c1e, type: 3}
m_ShaderKeywords: _EMISSION
m_LightmapFlags: 1
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 10, y: 10}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 6}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 4e96aadeab75a4745b17d60a1fea54c5, type: 3}
m_Scale: {x: 1, y: 6}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

8
Assets/LowEndRenderLoop/MobileColorsTile.mat.meta


fileFormatVersion: 2
guid: 3647f71343b206c4ca176689c2261a85
timeCreated: 1481626510
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

75
Assets/LowEndRenderLoop/MobileGlass.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileGlass
m_Shader: {fileID: 4800000, guid: 319d30749b10bd544a9e4fa5a9c36c1e, type: 3}
m_ShaderKeywords: _ALPHABLEND_ON _EMISSION _METALLICGLOSSMAP
m_LightmapFlags: 1
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 2, y: 2}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 894ecea3849c1417180f79a5ca555a6a, type: 3}
m_Scale: {x: 2, y: 2}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 2800000, guid: 894ecea3849c1417180f79a5ca555a6a, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 2
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 5
- _UVSec: 0
- _ZWrite: 0
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

8
Assets/LowEndRenderLoop/MobileGlass.mat.meta


fileFormatVersion: 2
guid: 2cbb16400afc3434f82480d3d0491ae6
timeCreated: 1478093906
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

79
Assets/LowEndRenderLoop/MobilePlane.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobilePlane
m_Shader: {fileID: 4800000, guid: 319d30749b10bd544a9e4fa5a9c36c1e, type: 3}
m_ShaderKeywords: _EMISSION
m_LightmapFlags: 1
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 57e020d5e89e349a7b1db77f3f80db54, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _SpecGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999997, g: 0.19999997, b: 0.19999997, a: 1}

8
Assets/LowEndRenderLoop/MobilePlane.mat.meta


fileFormatVersion: 2
guid: a200b6b244ab748488dcdcfb03f2937e
timeCreated: 1481626510
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存