浏览代码

Added options to use specular from base map and to choose wheter to use glossines from diffuse alpha (specular mask) or specular texture.

/vr_sandbox
Felipe Lira 8 年前
当前提交
00dbd214
共有 12 个文件被更改,包括 216 次插入83 次删除
  1. 185
      Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs
  2. 3
      Assets/LowEndRenderPipeline/Editor/StandardSpecularToLDMaterialUpgrader.cs
  3. 2
      Assets/LowEndRenderPipeline/LDRenderPipelineBasicScene.unity
  4. 2
      Assets/LowEndRenderPipeline/LowEndPipeline.asset
  5. 4
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/LDSpecularSphere0.mat
  6. 7
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileColors.mat
  7. 4
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileColorsTile.mat
  8. 6
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileGlass.mat
  9. 5
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobilePlane.mat
  10. 5
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobilePlane.mat
  11. 67
      Assets/LowEndRenderPipeline/Shaders/LDRenderPipe-Specular.shader
  12. 9
      Assets/ScriptableRenderLoop.meta

185
Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs


private MaterialProperty albedoMap = null;
private MaterialProperty albedoColor = null;
private MaterialProperty alphaCutoff = null;
private MaterialProperty specularSource = null;
private MaterialProperty glossinessSourceProp = null;
private MaterialProperty shininess = null;
private MaterialProperty specularStrength = null;
private MaterialProperty bumpMap = null;
private MaterialProperty emissionColor = null;

Alpha
}
public enum SpecularSource
{
SpecularTextureAndColor,
BaseTexture,
NoSpecular
}
public enum GlossinessSource
{
AlphaFromBaseTextureAndColor,
AlphaFromSpecularTextureAndColor
}
Styles.warningStyle.normal.textColor = Color.yellow;
public static GUIContent albedoGlosinessLabel = new GUIContent("Base (RGB)", "Base Color");
public static GUIContent[] albedoGlosinessLabels =
{
new GUIContent("Base (RGB) Glossiness (A)", "Base Color (RGB) and Glossiness (A)"),
new GUIContent("Base (RGB)", "Base Color (RGB)")
};
public static GUIContent alphaCutoffText = new GUIContent("Alpha Cutoff", "Threshold for alpha cutoff");
public static GUIContent specularGlossMapLabel = new GUIContent("Specular Color (RGB) Glossiness (A)", "Specular Color (RGB) Glossiness (a)");
public static GUIContent[] specularGlossMapLabels =
{
new GUIContent("Specular Color (RGB)", "Specular Color (RGB)"),
new GUIContent("Specular Color (RGB) Glossiness (A)", "Specular Color (RGB) Glossiness (A)")
};
public static GUIContent emissionText = new GUIContent("Color", "Emission (RGB)");
public static GUIContent alphaCutoutWarning = new GUIContent("This material has alpha cutout enabled. Alpha cutout has severe performance impact on mobile!");
public static GUIStyle warningStyle = new GUIStyle();
public static readonly string[] specSourceNames = Enum.GetNames(typeof(SpecularSource));
public static readonly string[] glossinessSourceNames = Enum.GetNames(typeof(GlossinessSource));
public static string renderingModeLabel = "RenderingMode";
public static string renderingModeLabel = "Rendering Mode";
public static string specularSourceLabel = "Specular Color Source";
public static string glossinessSourceLable = "Glossiness Source";
public static string glossinessSource = "Glossiness Source";
public static string alphaCutoffLabel = "Cutoff";
public static string specularColorLabel = "Specular Color (RGB) Glossiness (A)";
public static string shininessLabel = "Shininess";
public static string alphaCutoffLabel = "Alpha Cutoff";
public static string specularStrength = "Specular Strength";
public static string normalMapLabel = "Normal map";
public static string emissionColorLabel = "Emission Color";
}

albedoColor = GetMaterialProperty(mats, "_Color");
alphaCutoff = GetMaterialProperty(mats, "_Cutoff");
specularSource = GetMaterialProperty(mats, "_SpecSource");
glossinessSourceProp = GetMaterialProperty(mats, "_GlossinessSource");
shininess = GetMaterialProperty(mats, "_Glossiness");
specularStrength = GetMaterialProperty(mats, "_SpecularStrength");
bumpMap = GetMaterialProperty(mats, "_BumpMap");
emissionColor = GetMaterialProperty(mats, "_EmissionColor");
}

EditorGUI.BeginChangeCheck();
DoBlendMode();
BlendMode mode = (BlendMode)blendMode.floatValue;
EditorGUILayout.Space();
switch (mode)
{
case BlendMode.Opaque:
TexturePropertySingleLine(Styles.albedoGlosinessLabel, albedoMap, albedoColor);
TextureScaleOffsetProperty(albedoMap);
break;
case BlendMode.Cutout:
TexturePropertySingleLine(Styles.albedoGlosinessLabel, albedoMap, albedoColor);
TextureScaleOffsetProperty(albedoMap);
RangeProperty(alphaCutoff, "Cutoff");
break;
case BlendMode.Alpha:
TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMap, albedoColor);
TextureScaleOffsetProperty(albedoMap);
break;
}
EditorGUILayout.Space();
TexturePropertySingleLine(Styles.specularGlossMapLabel, specularGlossMap, specularColor);
RangeProperty(shininess, Styles.shininessLabel);
DoSpecular();
EditorGUILayout.Space();
TexturePropertySingleLine(Styles.normalMapText, bumpMap);

if (EditorGUI.EndChangeCheck())
UpdateMaterialKeywords(material);
EditorGUILayout.Space();
EditorGUILayout.Space();
if ((BlendMode) blendMode.floatValue == BlendMode.Cutout)
{
Styles.warningStyle.normal.textColor = Color.yellow;
EditorGUILayout.LabelField(Styles.alphaCutoutWarning, Styles.warningStyle);
}
int mode = (int)blendMode.floatValue;
int modeValue = (int)blendMode.floatValue;
EditorGUI.BeginChangeCheck();
modeValue = EditorGUILayout.Popup(Styles.renderingModeLabel, modeValue, Styles.blendNames);
if (EditorGUI.EndChangeCheck())
blendMode.floatValue = modeValue;
BlendMode mode = (BlendMode)blendMode.floatValue;
EditorGUILayout.Space();
if (mode == BlendMode.Opaque)
{
int glossSource = (int) glossinessSourceProp.floatValue;
TexturePropertySingleLine(Styles.albedoGlosinessLabels[glossSource], albedoMap, albedoColor);
TextureScaleOffsetProperty(albedoMap);
}
else
{
TexturePropertySingleLine(Styles.albedoAlphaLabel, albedoMap, albedoColor);
TextureScaleOffsetProperty(albedoMap);
if (mode == BlendMode.Cutout)
RangeProperty(alphaCutoff, "Cutoff");
}
}
private void DoSpecular()
{
EditorGUILayout.Space();
int source = (int)specularSource.floatValue;
mode = EditorGUILayout.Popup(Styles.renderingModeLabel, mode, Styles.blendNames);
source = EditorGUILayout.Popup(Styles.specularSourceLabel, source, Styles.specSourceNames);
blendMode.floatValue = mode;
{
specularSource.floatValue = source;
if (source == (int)SpecularSource.BaseTexture)
glossinessSourceProp.floatValue = (float)GlossinessSource.AlphaFromBaseTextureAndColor;
}
SpecularSource specSource = (SpecularSource)specularSource.floatValue;
if (specSource != SpecularSource.NoSpecular)
{
int glossinessSource = (int) glossinessSourceProp.floatValue;
EditorGUI.BeginChangeCheck();
glossinessSource = EditorGUILayout.Popup(Styles.glossinessSourceLable, glossinessSource, Styles.glossinessSourceNames);
if (EditorGUI.EndChangeCheck())
glossinessSourceProp.floatValue = (float)glossinessSource;
}
int glossSource = (int)glossinessSourceProp.floatValue;
if (specSource == SpecularSource.SpecularTextureAndColor)
{
TexturePropertySingleLine(Styles.specularGlossMapLabels[glossSource], specularGlossMap, specularColor);
}
if (specSource != SpecularSource.NoSpecular)
{
RangeProperty(specularStrength, Styles.specularStrength);
}
UpdateMaterialSpecularSource(material);
SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
SetKeyword(material, "_SPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
}

SetKeyword(material, "_ALPHABLEND_ON", true);
break;
}
}
private void UpdateMaterialSpecularSource(Material material)
{
SpecularSource specSource = (SpecularSource) specularSource.floatValue;
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, "_SPECULAR_COLOR", false);
}
else if (specSource == SpecularSource.SpecularTextureAndColor && material.GetTexture("_SpecGlossMap"))
{
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
SetKeyword(material, "_SPECULAR_MAP", true);
SetKeyword(material, "_SPECULAR_COLOR", false);
}
else
{
SetKeyword(material, "_SHARED_SPECULAR_DIFFUSE", false);
SetKeyword(material, "_SPECULAR_MAP", false);
SetKeyword(material, "_SPECULAR_COLOR", true);
}
GlossinessSource glossSource = (GlossinessSource) glossinessSourceProp.floatValue;
if (glossSource == GlossinessSource.AlphaFromBaseTextureAndColor)
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", true);
else
SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
}
private void SetKeyword(Material material, string keyword, bool enable)

3
Assets/LowEndRenderPipeline/Editor/StandardSpecularToLDMaterialUpgrader.cs


StandardSpecularToLDMaterialUpgrader(string oldShaderName)
{
RenameShader(oldShaderName, "LDRenderPipeline/Specular");
//RemoveTexture("_OcclusionMap");
//RemoveTexture("_DetailAlbedoMap");
//RemoveTexture("_DetailNormalMap");
}
}

2
Assets/LowEndRenderPipeline/LDRenderPipelineBasicScene.unity


m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1683150207}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 1.23, z: 23.31}
m_LocalPosition: {x: 0, y: 1.342, z: 23.31}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 541576036}

2
Assets/LowEndRenderPipeline/LowEndPipeline.asset


m_ShadowBias: 0.07
m_ShadowCascades: 4
m_Cascade2Split: 0.25
m_Cascade4Split: {x: 0.2, y: 0.5, z: 0.7}
m_Cascade4Split: {x: 0.25, y: 0.5, z: 0.7}

4
Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/LDSpecularSphere0.mat


m_PrefabInternal: {fileID: 0}
m_Name: LDSpecularSphere0
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords:
m_ShaderKeywords: _SPECULAR_COLOR
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_CustomRenderQueue: -1

- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecSource: 0
- _SpecularStrength: 200
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1

7
Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileColors.mat


m_PrefabInternal: {fileID: 0}
m_Name: MobileColors
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION _NORMALMAP
m_ShaderKeywords: _EMISSION _GLOSSINESS_FROM_BASE_ALPHA _NORMALMAP _SHARED_SPECULAR_DIFFUSE
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_CustomRenderQueue: -1

- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.305
- _Glossiness: 0.107
- _GlossinessSource: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0

- _SpecSource: 1
- _SpecularStrength: 200
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1

4
Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileColorsTile.mat


m_PrefabInternal: {fileID: 0}
m_Name: MobileColorsTile
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION
m_ShaderKeywords: _EMISSION _SPECULAR_COLOR
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_CustomRenderQueue: -1

- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecSource: 0
- _SpecularStrength: 200
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1

6
Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileGlass.mat


m_PrefabInternal: {fileID: 0}
m_Name: MobileGlass
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _ALPHABLEND_ON _EMISSION _METALLICGLOSSMAP
m_ShaderKeywords: _ALPHABLEND_ON _EMISSION _GLOSSINESS_FROM_BASE_ALPHA _METALLICGLOSSMAP
_SPECULAR_COLOR
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_CustomRenderQueue: 3000

- _DstBlend: 10
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossinessSource: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 2

- _SpecSource: 0
- _SpecularStrength: 200
- _SrcBlend: 5
- _UVSec: 0
- _ZWrite: 0

5
Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobilePlane.mat


m_PrefabInternal: {fileID: 0}
m_Name: MobilePlane
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION _NORMALMAP
m_ShaderKeywords: _EMISSION _GLOSSINESS_FROM_BASE_ALPHA _NORMALMAP _SPECULAR_COLOR
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_CustomRenderQueue: -1

- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.474
- _GlossinessSource: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0

- _SpecSource: 0
- _SpecularStrength: 200
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1

5
Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobilePlane.mat


m_PrefabInternal: {fileID: 0}
m_Name: LegacyMobilePlane
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION _NORMALMAP
m_ShaderKeywords: _EMISSION _GLOSSINESS_FROM_BASE_ALPHA _NORMALMAP _SPECULAR_COLOR
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_CustomRenderQueue: -1

- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.078125
- _GlossinessSource: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0

- _SmoothnessTextureChannel: 0
- _SpecSource: 0
- _SpecularStrength: 200
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1

67
Assets/LowEndRenderPipeline/Shaders/LDRenderPipe-Specular.shader


_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
_Glossiness("Shininess", Range(0.01, 1.0)) = 0.5
_Glossiness("Shininess", Range(0.0, 1.0)) = 0.5
_SpecularStrength("Specular Strength", Range(0.0, 255.0)) = 200
[HideInInspector] _SpecSource("Specular Color Source", Float) = 0.0
[HideInInspector] _GlossinessSource("Glossiness Source", Float) = 0.0
[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0

Pass
{
Name "SINGLE_PASS_FORWARD"
Name "LD_SINGLE_PASS_FORWARD"
Tags { "LightMode" = "LDForwardLight" }
// Use same blending / depth states as Standard shader

#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON
#pragma shader_feature _SPECGLOSSMAP
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
#pragma shader_feature _ _SHARED_SPECULAR_DIFFUSE _SPECGLOSSMAP _SPECULAR_COLOR
#pragma shader_feature _GLOSSINESS_FROM_BASE_ALPHA
#pragma shader_feature _NORMALMAP
#pragma multi_compile _ LIGHTMAP_ON

#pragma enable_d3d11_debug_symbols
#define DIFFUSE_AND_SPECULAR_INPUT SpecularInput
#include "UnityCG.cginc"
#include "UnityStandardBRDF.cginc"
#include "UnityStandardInput.cginc"

light.atten = globalLightAtten[lightIndex]; \
light.spotDir = globalLightSpotDir[lightIndex]
#define FRESNEL_TERM(normal, viewDir) Pow4(1.0 - saturate(dot(normal, viewDir)))
// 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.

half4x4 _WorldToShadow[MAX_SHADOW_CASCADES];
half4 _PSSMDistancesAndShadowResolution; // xyz: PSSM Distance for 4 cascades, w: 1 / shadowmap resolution. Used for filtering
half _SpecularStrength;
struct LowendVertexInput
{

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

half3 lightColor = lightInput.color.rgb * lightAtten;
half3 diffuse = diffuseColor * lightColor * NdotL;
half3 specular = specularGloss.rgb * lightColor * pow(NdotH, _Glossiness * 128.0) * specularGloss.a;
#if defined(_SHARED_SPECULAR_DIFFUSE) || defined(_SPECGLOSSMAP) || defined(_SPECULAR_COLOR)
half3 specular = specularGloss.rgb * lightColor * pow(NdotH, 256.0 - _SpecularStrength) * specularGloss.a;
#else
return diffuse;
#endif
}
inline half3 EvaluateMainLight(LightInput lightInput, half3 diffuseColor, half4 specularGloss, half3 normal, float4 posWorld, half3 viewDir)

#if DEBUG_CASCADES
half3 cascadeColors[MAX_SHADOW_CASCADES] = { half3(1.0, 0.0, 0.0), half3(0.0, 1.0, 0.0), half3(0.0, 0.0, 1.0), half3(1.0, 0.0, 1.0) };
return cascadeColors[cascadeIndex] * diffuseColor * max(shadowAttenuation, 0.5);
#endif
#ifdef SOFT_SHADOWS
#ifdef SOFT_SHADOWS
#else
#else
#endif
#endif
#if DEBUG_CASCADES
half3 cascadeColors[MAX_SHADOW_CASCADES] = { half3(1.0, 0.0, 0.0), half3(0.0, 1.0, 0.0), half3(0.0, 0.0, 1.0), half3(1.0, 0.0, 1.0) };
return cascadeColors[cascadeIndex] * diffuseColor * max(shadowAttenuation, 0.5);
#endif
return color * shadowAttenuation;
#else
return color;

o.viewDir.xyz = normalize(_WorldSpaceCameraPos - o.posWS.xyz);
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
half fresnelTerm = FRESNEL_TERM(normal, o.viewDir.xyz);
#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;

half3 normal = normalize(i.normal);
#endif
#ifdef _SPECGLOSSMAP
half4 specularGloss = tex2D(_SpecGlossMap, i.uv01.xy) * _SpecColor;
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;
half4 specularGloss = _SpecColor;
specularGloss = tex2D(_SpecGlossMap, i.uv01.xy) * _SpecColor;
#endif
#else
#if _GLOSSINESS_FROM_BASE_ALPHA
specularGloss.rgb = _SpecColor;
specularGloss.a = alpha;
#else
specularGloss = _SpecColor;
#endif
#endif
float3 posWorld = i.posWS.xyz;

Pass
{
Name "SHADOW_CASTER"
Name "LD_SHADOW_CASTER"
Tags { "Lightmode" = "ShadowCaster" }
ZWrite On ZTest LEqual

// This pass it not used during regular rendering, only for lightmap baking.
Pass
{
Name "META"
Name "LD_META"
Tags{ "LightMode" = "Meta" }
Cull Off

9
Assets/ScriptableRenderLoop.meta


fileFormatVersion: 2
guid: cea91ab26f9c6774c8bb9af2381b5c71
folderAsset: yes
timeCreated: 1487080089
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存