浏览代码

Added custom shader material to set correctly shader keywords for LRRenderPipeline.

/vr_sandbox
Felipe Lira 8 年前
当前提交
9f7c7922
共有 13 个文件被更改,包括 227 次插入87 次删除
  1. 13
      Assets/LowEndRenderPipeline/Editor/MobileToLDMaterialUpgrader.cs
  2. 4
      Assets/LowEndRenderPipeline/LDRenderPipelineBasicScene.unity
  3. 4
      Assets/LowEndRenderPipeline/Materials/LDRenderPipeMaterials/MobileColors.mat
  4. 4
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileAlpha.mat
  5. 2
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileColors.mat
  6. 2
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileGlass.mat
  7. 2
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobilePlane.mat
  8. 2
      Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileSphere.mat
  9. 53
      Assets/LowEndRenderPipeline/Shaders/LDRenderPipe-Specular.shader
  10. 175
      Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs
  11. 12
      Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs.meta
  12. 29
      Assets/LowEndRenderPipeline/Editor/LegacyBumpSpecularToLDMaterialUpgrader.cs
  13. 12
      Assets/LowEndRenderPipeline/Editor/LegacyBumpSpecularToLDMaterialUpgrader.cs.meta

13
Assets/LowEndRenderPipeline/Editor/MobileToLDMaterialUpgrader.cs


public static void UpgradeMaterialsToLD()
{
List<MaterialUpgrader> materialUpgraders = new List<MaterialUpgrader>();
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Diffuse")); // _MainTex
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Specular")); // _Shininess, _MainTex, _BumpMap
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Specular(1 Directional Light)")); // ""
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Diffuse")); // _MainTex, _BumpMap
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Unlit (Supports Lightmap)")); // _MainTex
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/VertexLit")); // MainTex
materialUpgraders.Add(new MobileToLDMaterialUpgrader("TerrainSurface"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Diffuse"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Specular"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Specular(1 Directional Light)"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Bumped Diffuse"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/Unlit (Supports Lightmap)"));
materialUpgraders.Add(new MobileToLDMaterialUpgrader("Mobile/VertexLit"));
MaterialUpgrader.UpgradeProjectFolder(materialUpgraders, "Upgrade to LD Materials");
}

4
Assets/LowEndRenderPipeline/LDRenderPipelineBasicScene.unity


m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: 2cbb16400afc3434f82480d3d0491ae6, type: 2}
- {fileID: 2100000, guid: 7d3b639840adb124384d0527c0515c18, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0

m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_Materials:
- {fileID: 2100000, guid: 2cbb16400afc3434f82480d3d0491ae6, type: 2}
- {fileID: 2100000, guid: 7d3b639840adb124384d0527c0515c18, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0

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


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

m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.21323532, g: 0.21323532, b: 0.21323532, a: 1}
- _SpecColor: {r: 1, g: 1, b: 1, a: 1}

4
Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileAlpha.mat


m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: LegacyMobileAlpha
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_Shader: {fileID: 33, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0

- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _Shininess: 0.078125
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1

- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.5, g: 0.5, b: 0.5, a: 0}

2
Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileColors.mat


m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileColors
m_Name: LegacyMobileColors
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION _NORMALMAP
m_LightmapFlags: 1

2
Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileGlass.mat


m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileGlass
m_Name: LegacyMobileGlass
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _ALPHABLEND_ON _EMISSION _METALLICGLOSSMAP
m_LightmapFlags: 1

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


m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobilePlane
m_Name: LegacyMobilePlane
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords: _EMISSION _NORMALMAP
m_LightmapFlags: 1

2
Assets/LowEndRenderPipeline/Materials/LegacyMobileMaterials/LegacyMobileSphere.mat


m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: MobileSphere
m_Name: LegacyMobileSphere
m_Shader: {fileID: 4800000, guid: 8d2bb70cbf9db8d4da26e15b26e74248, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4

53
Assets/LowEndRenderPipeline/Shaders/LDRenderPipe-Specular.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.
// Properties is just a copy of Standard (Specular Setup).shader. Our example shader does not use all of them,
// but the inspector UI expects all these to exist.
// Keep properties of StandardSpecular shader for upgrade reasons.
Properties
{
_Color("Color", Color) = (1,1,1,1)

_Glossiness("Shininess", Range(0.0, 1.0)) = 0.5
[HideInInspector] _GlossMapScale("Smoothness Factor", Range(0.0, 1.0)) = 1.0
[HideInInspector] [Enum(Specular Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
_Glossiness("Shininess", Range(0.01, 1.0)) = 0.5
_GlossMapScale("Smoothness Factor", Range(0.0, 1.0)) = 1.0
[Enum(Specular Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
[HideInInspector] _SpecGlossMap("Specular", 2D) = "white" {}
[HideInInspector] [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[HideInInspector] [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
_SpecGlossMap("Specular", 2D) = "white" {}
[ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
[ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
[HideInInspector] _Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
[HideInInspector] _ParallaxMap("Height Map", 2D) = "black" {}
_Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
_ParallaxMap("Height Map", 2D) = "black" {}
[HideInInspector] _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
[HideInInspector] _OcclusionMap("Occlusion", 2D) = "white" {}
_OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
_OcclusionMap("Occlusion", 2D) = "white" {}
[HideInInspector] _EmissionMap("Emission", 2D) = "white" {}
_EmissionMap("Emission", 2D) = "white" {}
[HideInInspector] _DetailMask("Detail Mask", 2D) = "white" {}
_DetailMask("Detail Mask", 2D) = "white" {}
[HideInInspector] _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
[HideInInspector] _DetailNormalMapScale("Scale", Float) = 1.0
[HideInInspector] _DetailNormalMap("Normal Map", 2D) = "bump" {}
_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
_DetailNormalMapScale("Scale", Float) = 1.0
_DetailNormalMap("Normal Map", 2D) = "bump" {}
[HideInInspector] [Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
[Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
// Blending state
[HideInInspector] _Mode("__mode", Float) = 0.0

float4 shadowCoord = mul(_WorldToShadow[cascadeIndex], float4(posWorld.xyz, 1.0));
shadowCoord.z = saturate(shadowCoord.z);
#ifdef SHADOWS_FILTERING_VSM
half shadowAttenuation = ShadowVSM(shadowCoord);
#elif defined(SHADOWS_FILTERING_PCF)
#ifdef SHADOWS_FILTERING_PCF
half shadowAttenuation = ShadowPCF(shadowCoord);
#else
half shadowAttenuation = ShadowAttenuation(shadowCoord.xy, shadowCoord.z);

half4 frag(v2f i) : SV_Target
{
half4 diffuseAlpha = tex2D(_MainTex, i.uv01.xy);
half3 diffuse = diffuseAlpha.rgb;
half3 diffuse = diffuseAlpha.rgb * _Color.rgb;
half alpha = diffuseAlpha.a * _Color.a;
#ifdef _ALPHATEST_ON

half3 normal = normalize(i.normal);
#endif
#if _SPECGLOSSMAP
half4 specularGloss = tex2D(_SpecGlossMap, i.uv01.xy);
#ifdef _SPECGLOSSMAP
half4 specularGloss = tex2D(_SpecGlossMap, i.uv01.xy) * _SpecColor;
#else
half4 specularGloss = _SpecColor;
#endif

NAME "Dummy2"
}
// This pass it not used during regular rendering.
// This pass it not used during regular rendering, only for lightmap baking.
Pass
{
Name "META"

}
}
Fallback "Standard (Specular setup)"
//CustomEditor "StandardShaderGUI"
CustomEditor "LDRenderPipelineMaterialEditor"
}

175
Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs


using System;
using UnityEditor;
using UnityEngine;
// TODO: Implement AssignNewShaderToMaterial to handle cases from update to different mobile shaders having different props
public class LDRenderPipelineMaterialEditor : MaterialEditor
{
private MaterialProperty blendMode = null;
private MaterialProperty albedoMap = null;
private MaterialProperty albedoColor = null;
private MaterialProperty alphaCutoff = null;
private MaterialProperty specularGlossMap = null;
private MaterialProperty specularColor = null;
private MaterialProperty shininess = null;
private MaterialProperty bumpMap = null;
private MaterialProperty emissionColor = null;
public enum BlendMode
{
Opaque,
Cutout,
Alpha
}
private void Awake()
{
Material material = target as Material;
FindMaterialProperties(material);
UpdateMaterialKeywords(material);
}
private static class Styles
{
public static GUIContent albedoGlosinessLabel = new GUIContent("Base (RGB)", "Base Color");
public static GUIContent albedoAlphaLabel = new GUIContent("Base (RGB) Alpha (A)", "Base Color (RGB) and Transparency (A)");
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 normalMapText = new GUIContent("Normal Map", "Normal Map");
public static GUIContent emissionText = new GUIContent("Color", "Emission (RGB)");
public static readonly string[] blendNames = Enum.GetNames(typeof(BlendMode));
public static string renderingModeLabel = "RenderingMode";
public static string albedoColorLabel = "Base Color";
public static string albedoMapAlphaLabel = "Base(RGB) Alpha(A)";
public static string albedoMapGlossinessLabel = "Base(RGB) Glossiness (A)";
public static string alphaCutoffLabel = "Cutoff";
public static string specularColorLabel = "Specular Color (RGB) Glossiness (A)";
public static string shininessLabel = "Shininess";
public static string normalMapLabel = "Normal map";
public static string emissionColorLabel = "Emission Color";
}
private void FindMaterialProperties(Material material)
{
Material[] mats = { material };
blendMode = GetMaterialProperty(mats, "_Mode");
albedoMap = GetMaterialProperty(mats, "_MainTex");
albedoColor = GetMaterialProperty(mats, "_Color");
alphaCutoff = GetMaterialProperty(mats, "_Cutoff");
specularGlossMap = GetMaterialProperty(mats, "_SpecGlossMap");
specularColor = GetMaterialProperty(mats, "_SpecColor");
shininess = GetMaterialProperty(mats, "_Glossiness");
bumpMap = GetMaterialProperty(mats, "_BumpMap");
emissionColor = GetMaterialProperty(mats, "_EmissionColor");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
var shaderObj = serializedObject.FindProperty("m_Shader");
if (isVisible && shaderObj.objectReferenceValue != null)
{
Material material = target as Material;
FindMaterialProperties(material);
EditorGUI.BeginChangeCheck();
DoBlendMode();
BlendMode mode = (BlendMode)blendMode.floatValue;
EditorGUILayout.Space();
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);
EditorGUILayout.Space();
TexturePropertySingleLine(Styles.normalMapText, bumpMap);
EditorGUILayout.Space();
ColorProperty(emissionColor, Styles.emissionColorLabel);
if (EditorGUI.EndChangeCheck())
UpdateMaterialKeywords(material);
}
}
private void DoBlendMode()
{
int mode = (int)blendMode.floatValue;
EditorGUI.BeginChangeCheck();
mode = EditorGUILayout.Popup(Styles.renderingModeLabel, mode, Styles.blendNames);
if (EditorGUI.EndChangeCheck())
blendMode.floatValue = mode;
}
private void UpdateMaterialKeywords(Material material)
{
UpdateMaterialBlendMode(material);
SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
SetKeyword(material, "_SPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
}
private void UpdateMaterialBlendMode(Material material)
{
BlendMode mode = (BlendMode)blendMode.floatValue;
switch (mode)
{
case BlendMode.Opaque:
material.SetOverrideTag("RenderType", "");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
SetKeyword(material, "_ALPHATEST_ON", false);
SetKeyword(material, "_ALPHABLEND_ON", false);
break;
case BlendMode.Cutout:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
SetKeyword(material, "_ALPHATEST_ON", true);
SetKeyword(material, "_ALPHABLEND_ON", false);
break;
case BlendMode.Alpha:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
SetKeyword(material, "_ALPHATEST_ON", false);
SetKeyword(material, "_ALPHABLEND_ON", true);
break;
}
}
private void SetKeyword(Material material, string keyword, bool enable)
{
if (enable)
material.EnableKeyword(keyword);
else
material.DisableKeyword(keyword);
}
}

12
Assets/LowEndRenderPipeline/Editor/LDRenderPipelineMaterialEditor.cs.meta


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

29
Assets/LowEndRenderPipeline/Editor/LegacyBumpSpecularToLDMaterialUpgrader.cs


using System.Collections.Generic;
using UnityEditor.Experimental.Rendering;
using UnityEditor;
public class LegacyBumpSpecularToLDMaterialUpgrader : MaterialUpgrader
{
[MenuItem("LDRenderPipeline/Upgrade Legacy Shaders to LDRenderPipeline Materials")]
public static void UpgradeMaterialsToLD()
{
List<MaterialUpgrader> materialUpgraders = new List<MaterialUpgrader>();
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Mobile/Bumped Specular"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Mobile/Bumped Specular(1 Directional Light)"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Lightmapped/Bumped Diffuse"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Lightmapped/Bumped Specular"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Lightmapped/Diffuse"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Bumped Diffuse"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Bumped Specular"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Diffuse"));
materialUpgraders.Add(new LegacyBumpSpecularToLDMaterialUpgrader("Legacy Shaders/Lightmapped/Bumped Specular"));
MaterialUpgrader.UpgradeProjectFolder(materialUpgraders, "Upgrade to LD Materials");
}
LegacyBumpSpecularToLDMaterialUpgrader(string oldShaderName)
{
RenameShader(oldShaderName, "LDRenderPipeline/Specular");
RenameFloat("_Shininess", "_Glossiness");
}
}

12
Assets/LowEndRenderPipeline/Editor/LegacyBumpSpecularToLDMaterialUpgrader.cs.meta


fileFormatVersion: 2
guid: 9a1c411526f5afc44a4cfc7cf16c5cde
timeCreated: 1487765059
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存