浏览代码

Reduced amount of shader variants by optimizing away a few keywords.

/fptl_cleanup
Felipe Lira 8 年前
当前提交
c063a880
共有 6 个文件被更改,包括 47 次插入73 次删除
  1. 4
      Assets/LowEndMobilePipeline/Editor/LowendMobilePipelineMaterialEditor.cs
  2. 35
      Assets/LowEndMobilePipeline/Editor/LowendUpgraders.cs
  3. 1
      Assets/LowEndMobilePipeline/Editor/UpgradeCommon.cs
  4. 28
      Assets/LowEndMobilePipeline/LowEndMobilePipeline.cs
  5. 15
      Assets/LowEndMobilePipeline/Shaders/LowEndMobilePipeline.shader
  6. 37
      Assets/LowEndMobilePipeline/Shaders/LowEndMobilePipelineCore.cginc

4
Assets/LowEndMobilePipeline/Editor/LowendMobilePipelineMaterialEditor.cs


EditorGUI.BeginChangeCheck();
source = EditorGUILayout.Popup(Styles.specularSourceLabel, source, Styles.specSourceNames);
if (EditorGUI.EndChangeCheck())
{
if (source == (int)SpecularSource.BaseTexture)
glossinessSourceProp.floatValue = (float)GlossinessSource.BaseAlpha;
}
SpecularSource specSource = (SpecularSource)specularSourceProp.floatValue;
if (specSource != SpecularSource.NoSpecular)

35
Assets/LowEndMobilePipeline/Editor/LowendUpgraders.cs


UpdateMaterialSpecularSource(material);
UpdateMaterialReflectionSource(material);
SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
SetKeyword(material, "_SPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
SetKeyword(material, "_CUBEMAP_REFLECTION", material.GetTexture("_Cube"));
SetKeyword(material, "_EMISSION_MAP", material.GetTexture("_EmissionMap"));
}

SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecSource");
if (specSource == SpecularSource.NoSpecular)
{
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
SetKeyword(material, "_SPECULAR_MAP", false);
SetKeyword(material, "_SPECULAR_COLOR", false);
}
else if (specSource == SpecularSource.BaseTexture)
{
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", true);
SetKeyword(material, "_SPECULAR_MAP", false);
SetKeyword(material, "_SPECGLOSSMAP", false);
SetKeyword(material, "_SPECGLOSSMAP_BASE_ALPHA", false);
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
SetKeyword(material, "_SPECULAR_MAP", true);
GlossinessSource glossSource = (GlossinessSource)material.GetFloat("_GlossinessSource");
if (glossSource == GlossinessSource.BaseAlpha)
{
SetKeyword(material, "_SPECGLOSSMAP", false);
SetKeyword(material, "_SPECGLOSSMAP_BASE_ALPHA", true);
}
else
{
SetKeyword(material, "_SPECGLOSSMAP", true);
SetKeyword(material, "_SPECGLOSSMAP_BASE_ALPHA", false);
}
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
SetKeyword(material, "_SPECULAR_MAP", false);
SetKeyword(material, "_SPECGLOSSMAP", false);
SetKeyword(material, "_SPECGLOSSMAP_BASE_ALPHA", false);
GlossinessSource glossSource = (GlossinessSource)material.GetFloat("_GlossinessSource");
if (glossSource == GlossinessSource.BaseAlpha)
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", true);
else
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
}
private static void UpdateMaterialReflectionSource(Material material)

1
Assets/LowEndMobilePipeline/Editor/UpgradeCommon.cs


public enum SpecularSource
{
SpecularTextureAndColor,
BaseTexture,
NoSpecular
}

28
Assets/LowEndMobilePipeline/LowEndMobilePipeline.cs


else
cmd.DisableShaderKeyword("_VERTEX_LIGHTS");
if (m_ShadowCasterCascadesCount == 1)
cmd.DisableShaderKeyword("_SHADOW_CASCADES");
else
cmd.EnableShaderKeyword("_SHADOW_CASCADES");
string[] shadowKeywords = new string[] { "_HARD_SHADOWS", "_SOFT_SHADOWS", "_HARD_SHADOWS_CASCADES", "_SOFT_SHADOWS_CASCADES" };
for (int i = 0; i < shadowKeywords.Length; ++i)
cmd.DisableShaderKeyword(shadowKeywords[i]);
ShadowType shadowType = (m_ShadowLightIndex != -1) ? m_Asset.CurrShadowType : ShadowType.NO_SHADOW;
switch (shadowType)
if (m_ShadowLightIndex != -1 || m_Asset.CurrShadowType != ShadowType.NO_SHADOW)
case ShadowType.NO_SHADOW:
cmd.DisableShaderKeyword("HARD_SHADOWS");
cmd.DisableShaderKeyword("SOFT_SHADOWS");
break;
case ShadowType.HARD_SHADOWS:
cmd.EnableShaderKeyword("HARD_SHADOWS");
cmd.DisableShaderKeyword("SOFT_SHADOWS");
break;
case ShadowType.SOFT_SHADOWS:
cmd.DisableShaderKeyword("HARD_SHADOWS");
cmd.EnableShaderKeyword("SOFT_SHADOWS");
break;
int keywordIndex = (int)m_Asset.CurrShadowType - 1;
if (m_Asset.CascadeCount > 1)
keywordIndex += 2;
cmd.EnableShaderKeyword(shadowKeywords[keywordIndex]);
}
}

15
Assets/LowEndMobilePipeline/Shaders/LowEndMobilePipeline.shader


#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON
#pragma shader_feature _ _SHARED_SPECULAR_DIFFUSE _SPECGLOSSMAP _SPECULAR_COLOR
#pragma shader_feature _GLOSSINESS_FROM_BASE_ALPHA
#pragma shader_feature _ _SPECGLOSSMAP _SPECGLOSSMAP_BASE_ALPHA _SPECULAR_COLOR
#pragma multi_compile _ HARD_SHADOWS SOFT_SHADOWS
#pragma multi_compile _ _SHADOW_CASCADES
#pragma multi_compile _ _HARD_SHADOWS _SOFT_SHADOWS _HARD_SHADOWS_CASCADES _SOFT_SHADOWS_CASCADES
#pragma enable_d3d11_debug_symbols
#include "UnityCG.cginc"
#include "UnityStandardBRDF.cginc"

o.shadowCoord = mul(_WorldToShadow[0], float4(o.posWS, 1.0));
#endif
#ifndef LIGHTMAP_ON
#endif
UNITY_TRANSFER_FOG(o, o.hpos);
return o;

NormalMap(i, normal);
half4 specularGloss;
SpecularGloss(diffuse, alpha, specularGloss);
SpecularGloss(i.uv01.xy, diffuse, alpha, specularGloss);
// Indirect Light Contribution
half3 indirect;

half3 viewDir = i.viewDir.xyz;
half3 directColor = EvaluateOneLight(mainLight, diffuse, specularGloss, normal, i.posWS, viewDir);
#if defined(HARD_SHADOWS) || defined(SOFT_SHADOWS)
#ifdef _SHADOWS
#endif
#endif
// Compute direct contribution from additional lights.
for (int lightIndex = 1; lightIndex < globalLightCount.x; ++lightIndex)

37
Assets/LowEndMobilePipeline/Shaders/LowEndMobilePipelineCore.cginc


light.atten = globalLightAtten[lightIndex]; \
light.spotDir = globalLightSpotDir[lightIndex]
#if defined(_HARD_SHADOWS) || defined(_SOFT_SHADOWS) || defined(_HARD_SHADOWS_CASCADES) || defined(_SOFT_SHADOWS_CASCADES)
#define _SHADOWS
#endif
#if defined(_HARD_SHADOWS_CASCADES) || defined(_SOFT_SHADOWS_CASCADES)
#define _SHADOW_CASCADES
#endif
struct LightInput
{
half4 pos;

#endif
}
inline void SpecularGloss(half3 diffuse, half alpha, out half4 specularGloss)
inline void SpecularGloss(half2 uv, half3 diffuse, half alpha, out half4 specularGloss)
#ifdef _SHARED_SPECULAR_DIFFUSE
specularGloss.rgb = diffuse;
specularGloss.a = alpha;
#elif defined(_SHARED_SPECULAR_DIFFUSE)
#if _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.rgb = tex2D(_SpecGlossMap, i.uv01.xy) * _SpecColor;
specularGloss.a = alpha;
#else
specularGloss = tex2D(_SpecGlossMap, i.uv01.xy) * _SpecColor;
#endif
#else
#if _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.rgb = _SpecColor;
#ifdef _SPECGLOSSMAP
specularGloss = tex2D(_SpecGlossMap, uv) * _SpecColor;
#elif defined(_SPECGLOSSMAP_BASE_ALPHA)
specularGloss = tex2D(_SpecGlossMap, uv) * _SpecColor;
#endif
#endif
}

inline void Indirect(v2f i, half3 diffuse, half3 normal, half glossiness, out half3 indirect)
{
#ifdef LIGHTMAP_ON
indirect = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) * diffuse;
indirect = (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * diffuse;
#else
indirect = i.fogCoord.yzw * diffuse;
#endif

inline half ComputeShadowAttenuation(v2f i)
{
#ifndef _SHADOW_CASCADES
#ifndef _SHADOW_CASCADES
#else
#else
half4 shadowCoord;
int cascadeIndex = ComputeCascadeIndex(i.posWS);
if (cascadeIndex < 4)
shadowCoord = mul(_WorldToShadow[cascadeIndex], half4(i.posWS, 1.0));

shadowCoord.xyz /= shadowCoord.w;
shadowCoord.z = saturate(shadowCoord.z);
#ifdef SOFT_SHADOWS
#if defined(_SOFT_SHADOWS) || defined(_SOFT_SHADOWS_CASCADES)
return ShadowPCF(shadowCoord.xyz);
#else
return ShadowAttenuation(shadowCoord.xyz);
正在加载...
取消
保存