Change directory structures: Move UniGLTF/UniUnlit -> UniUnlit

This commit is contained in:
yutopp
2018-12-28 21:27:19 +09:00
parent 475ab731b2
commit f9e0e26786
10 changed files with 289 additions and 289 deletions

View File

@@ -1,149 +1,149 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public class UniUnlitEditor : ShaderGUI
{
private MaterialProperty _mainTex;
private MaterialProperty _color;
private MaterialProperty _cutoff;
private MaterialProperty _blendMode;
private MaterialProperty _cullMode;
private MaterialProperty _vColBlendMode;
// private MaterialProperty _srcBlend;
// private MaterialProperty _dstBlend;
// private MaterialProperty _zWrite;
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
_mainTex = FindProperty(Utils.PropNameMainTex, properties);
_color = FindProperty(Utils.PropNameColor, properties);
_cutoff = FindProperty(Utils.PropNameCutoff, properties);
_blendMode = FindProperty(Utils.PropNameBlendMode, properties);
_cullMode = FindProperty(Utils.PropNameCullMode, properties);
_vColBlendMode = FindProperty(Utils.PropeNameVColBlendMode, properties);
// _srcBlend = FindProperty(PropNameSrcBlend, properties);
// _dstBlend = FindProperty(PropNameDstBlend, properties);
// _zWrite = FindProperty(PropNameZWrite, properties);
var materials = materialEditor.targets.Select(x => x as Material).ToArray();
EditorGUI.BeginChangeCheck();
{
DrawRenderingBox(materialEditor, materials);
DrawColorBox(materialEditor, materials);
DrawOptionsBox(materialEditor, materials);
}
EditorGUI.EndChangeCheck();
}
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
var blendMode = UniUnlitRenderMode.Opaque;
if (material.HasProperty(Utils.PropNameStandardShadersRenderMode)) // from Standard shader
{
blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(Utils.PropNameStandardShadersRenderMode));
}
// assigns UniUnlit's properties...
base.AssignNewShaderToMaterial(material, oldShader, newShader);
// take over old value
material.SetFloat(Utils.PropNameBlendMode, (float) blendMode);
Utils.ValidateProperties(material, isRenderModeChangedByUser: true);
}
private void DrawRenderingBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Rendering", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
if (PopupEnum<UniUnlitRenderMode>("Rendering Type", _blendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
if (PopupEnum<UniUnlitCullMode>("Cull Mode", _cullMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
EditorGUILayout.Space();
switch ((UniUnlitRenderMode) _blendMode.floatValue)
{
case UniUnlitRenderMode.Cutout:
materialEditor.ShaderProperty(_cutoff, "Cutoff");
break;
case UniUnlitRenderMode.Opaque:
case UniUnlitRenderMode.Transparent:
break;
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawColorBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Color", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
materialEditor.TexturePropertySingleLine(new GUIContent("Main Tex", "(RGBA)"), _mainTex, _color);
materialEditor.TextureScaleOffsetProperty(_mainTex);
EditorGUILayout.Space();
if (PopupEnum<UniUnlitVertexColorBlendOp>("Vertex Color Blend Mode", _vColBlendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawOptionsBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Options", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
#if UNITY_5_6_OR_NEWER
// materialEditor.EnableInstancingField();
materialEditor.DoubleSidedGIField();
#endif
materialEditor.RenderQueueField();
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private static bool PopupEnum<T>(string name, MaterialProperty property, MaterialEditor editor) where T : struct
{
EditorGUI.showMixedValue = property.hasMixedValue;
EditorGUI.BeginChangeCheck();
var ret = EditorGUILayout.Popup(name, (int) property.floatValue, Enum.GetNames(typeof(T)));
var changed = EditorGUI.EndChangeCheck();
if (changed)
{
editor.RegisterPropertyChangeUndo("EnumPopUp");
property.floatValue = ret;
}
EditorGUI.showMixedValue = false;
return changed;
}
private static void ModeChanged(Material[] materials, bool isRenderModeChangedByUser = false)
{
foreach (var material in materials)
{
Utils.ValidateProperties(material, isRenderModeChangedByUser);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public class UniUnlitEditor : ShaderGUI
{
private MaterialProperty _mainTex;
private MaterialProperty _color;
private MaterialProperty _cutoff;
private MaterialProperty _blendMode;
private MaterialProperty _cullMode;
private MaterialProperty _vColBlendMode;
// private MaterialProperty _srcBlend;
// private MaterialProperty _dstBlend;
// private MaterialProperty _zWrite;
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
_mainTex = FindProperty(Utils.PropNameMainTex, properties);
_color = FindProperty(Utils.PropNameColor, properties);
_cutoff = FindProperty(Utils.PropNameCutoff, properties);
_blendMode = FindProperty(Utils.PropNameBlendMode, properties);
_cullMode = FindProperty(Utils.PropNameCullMode, properties);
_vColBlendMode = FindProperty(Utils.PropeNameVColBlendMode, properties);
// _srcBlend = FindProperty(PropNameSrcBlend, properties);
// _dstBlend = FindProperty(PropNameDstBlend, properties);
// _zWrite = FindProperty(PropNameZWrite, properties);
var materials = materialEditor.targets.Select(x => x as Material).ToArray();
EditorGUI.BeginChangeCheck();
{
DrawRenderingBox(materialEditor, materials);
DrawColorBox(materialEditor, materials);
DrawOptionsBox(materialEditor, materials);
}
EditorGUI.EndChangeCheck();
}
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
var blendMode = UniUnlitRenderMode.Opaque;
if (material.HasProperty(Utils.PropNameStandardShadersRenderMode)) // from Standard shader
{
blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(Utils.PropNameStandardShadersRenderMode));
}
// assigns UniUnlit's properties...
base.AssignNewShaderToMaterial(material, oldShader, newShader);
// take over old value
material.SetFloat(Utils.PropNameBlendMode, (float) blendMode);
Utils.ValidateProperties(material, isRenderModeChangedByUser: true);
}
private void DrawRenderingBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Rendering", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
if (PopupEnum<UniUnlitRenderMode>("Rendering Type", _blendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
if (PopupEnum<UniUnlitCullMode>("Cull Mode", _cullMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
EditorGUILayout.Space();
switch ((UniUnlitRenderMode) _blendMode.floatValue)
{
case UniUnlitRenderMode.Cutout:
materialEditor.ShaderProperty(_cutoff, "Cutoff");
break;
case UniUnlitRenderMode.Opaque:
case UniUnlitRenderMode.Transparent:
break;
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawColorBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Color", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
materialEditor.TexturePropertySingleLine(new GUIContent("Main Tex", "(RGBA)"), _mainTex, _color);
materialEditor.TextureScaleOffsetProperty(_mainTex);
EditorGUILayout.Space();
if (PopupEnum<UniUnlitVertexColorBlendOp>("Vertex Color Blend Mode", _vColBlendMode, materialEditor))
{
ModeChanged(materials, isRenderModeChangedByUser: true);
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private void DrawOptionsBox(MaterialEditor materialEditor, Material[] materials)
{
EditorGUILayout.LabelField("Options", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
#if UNITY_5_6_OR_NEWER
// materialEditor.EnableInstancingField();
materialEditor.DoubleSidedGIField();
#endif
materialEditor.RenderQueueField();
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
private static bool PopupEnum<T>(string name, MaterialProperty property, MaterialEditor editor) where T : struct
{
EditorGUI.showMixedValue = property.hasMixedValue;
EditorGUI.BeginChangeCheck();
var ret = EditorGUILayout.Popup(name, (int) property.floatValue, Enum.GetNames(typeof(T)));
var changed = EditorGUI.EndChangeCheck();
if (changed)
{
editor.RegisterPropertyChangeUndo("EnumPopUp");
property.floatValue = ret;
}
EditorGUI.showMixedValue = false;
return changed;
}
private static void ModeChanged(Material[] materials, bool isRenderModeChangedByUser = false)
{
foreach (var material in materials)
{
Utils.ValidateProperties(material, isRenderModeChangedByUser);
}
}
}
}

View File

@@ -1,139 +1,139 @@
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public enum UniUnlitRenderMode
{
Opaque = 0,
Cutout = 1,
Transparent = 2,
}
public enum UniUnlitCullMode
{
Off = 0,
// Front = 1,
Back = 2,
}
public enum UniUnlitVertexColorBlendOp
{
None = 0,
Multiply = 1,
}
public static class Utils
{
public const string PropNameMainTex = "_MainTex";
public const string PropNameColor = "_Color";
public const string PropNameCutoff = "_Cutoff";
public const string PropNameBlendMode = "_BlendMode";
public const string PropNameCullMode = "_CullMode";
public const string PropeNameVColBlendMode = "_VColBlendMode";
public const string PropNameSrcBlend = "_SrcBlend";
public const string PropNameDstBlend = "_DstBlend";
public const string PropNameZWrite = "_ZWrite";
public const string PropNameStandardShadersRenderMode = "_Mode";
public const string KeywordAlphaTestOn = "_ALPHATEST_ON";
public const string KeywordAlphaBlendOn = "_ALPHABLEND_ON";
public const string KeywordVertexColMul = "_VERTEXCOL_MUL";
public const string TagRenderTypeKey = "RenderType";
public const string TagRenderTypeValueOpaque = "Opaque";
public const string TagRenderTypeValueTransparentCutout = "TransparentCutout";
public const string TagRenderTypeValueTransparent = "Transparent";
public static void SetRenderMode(Material material, UniUnlitRenderMode mode)
{
material.SetInt(PropNameBlendMode, (int)mode);
}
public static void SetCullMode(Material material, UniUnlitCullMode mode)
{
material.SetInt(PropNameCullMode, (int) mode);
}
public static UniUnlitRenderMode GetRenderMode(Material material)
{
return (UniUnlitRenderMode)material.GetInt(PropNameBlendMode);
}
public static UniUnlitCullMode GetCullMode(Material material)
{
return (UniUnlitCullMode)material.GetInt(PropNameCullMode);
}
/// <summary>
/// Validate target material's UniUnlitRenderMode, UniUnlitVertexColorBlendOp.
/// Set appropriate hidden properites & keywords.
/// This will change RenderQueue independent to UniUnlitRenderMode if isRenderModeChagedByUser is true.
/// </summary>
/// <param name="material">Target material</param>
/// <param name="isRenderModeChangedByUser">Is changed by user</param>
public static void ValidateProperties(Material material, bool isRenderModeChangedByUser = false)
{
SetupBlendMode(material, (UniUnlitRenderMode)material.GetFloat(PropNameBlendMode),
isRenderModeChangedByUser);
SetupVertexColorBlendOp(material, (UniUnlitVertexColorBlendOp)material.GetFloat(PropeNameVColBlendMode));
}
private static void SetupBlendMode(Material material, UniUnlitRenderMode renderMode,
bool isRenderModeChangedByUser = false)
{
switch (renderMode)
{
case UniUnlitRenderMode.Opaque:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueOpaque);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = -1;
break;
case UniUnlitRenderMode.Cutout:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparentCutout);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, true);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.AlphaTest;
break;
case UniUnlitRenderMode.Transparent:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparent);
material.SetInt(PropNameSrcBlend, (int)BlendMode.SrcAlpha);
material.SetInt(PropNameDstBlend, (int)BlendMode.OneMinusSrcAlpha);
material.SetInt(PropNameZWrite, 0);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, true);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.Transparent;
break;
}
}
private static void SetupVertexColorBlendOp(Material material, UniUnlitVertexColorBlendOp vColBlendOp)
{
switch (vColBlendOp)
{
case UniUnlitVertexColorBlendOp.None:
SetKeyword(material, KeywordVertexColMul, false);
break;
case UniUnlitVertexColorBlendOp.Multiply:
SetKeyword(material, KeywordVertexColMul, true);
break;
}
}
private static void SetKeyword(Material mat, string keyword, bool required)
{
if (required)
mat.EnableKeyword(keyword);
else
mat.DisableKeyword(keyword);
}
}
using UnityEngine;
using UnityEngine.Rendering;
namespace UniGLTF.UniUnlit
{
public enum UniUnlitRenderMode
{
Opaque = 0,
Cutout = 1,
Transparent = 2,
}
public enum UniUnlitCullMode
{
Off = 0,
// Front = 1,
Back = 2,
}
public enum UniUnlitVertexColorBlendOp
{
None = 0,
Multiply = 1,
}
public static class Utils
{
public const string PropNameMainTex = "_MainTex";
public const string PropNameColor = "_Color";
public const string PropNameCutoff = "_Cutoff";
public const string PropNameBlendMode = "_BlendMode";
public const string PropNameCullMode = "_CullMode";
public const string PropeNameVColBlendMode = "_VColBlendMode";
public const string PropNameSrcBlend = "_SrcBlend";
public const string PropNameDstBlend = "_DstBlend";
public const string PropNameZWrite = "_ZWrite";
public const string PropNameStandardShadersRenderMode = "_Mode";
public const string KeywordAlphaTestOn = "_ALPHATEST_ON";
public const string KeywordAlphaBlendOn = "_ALPHABLEND_ON";
public const string KeywordVertexColMul = "_VERTEXCOL_MUL";
public const string TagRenderTypeKey = "RenderType";
public const string TagRenderTypeValueOpaque = "Opaque";
public const string TagRenderTypeValueTransparentCutout = "TransparentCutout";
public const string TagRenderTypeValueTransparent = "Transparent";
public static void SetRenderMode(Material material, UniUnlitRenderMode mode)
{
material.SetInt(PropNameBlendMode, (int)mode);
}
public static void SetCullMode(Material material, UniUnlitCullMode mode)
{
material.SetInt(PropNameCullMode, (int) mode);
}
public static UniUnlitRenderMode GetRenderMode(Material material)
{
return (UniUnlitRenderMode)material.GetInt(PropNameBlendMode);
}
public static UniUnlitCullMode GetCullMode(Material material)
{
return (UniUnlitCullMode)material.GetInt(PropNameCullMode);
}
/// <summary>
/// Validate target material's UniUnlitRenderMode, UniUnlitVertexColorBlendOp.
/// Set appropriate hidden properites & keywords.
/// This will change RenderQueue independent to UniUnlitRenderMode if isRenderModeChagedByUser is true.
/// </summary>
/// <param name="material">Target material</param>
/// <param name="isRenderModeChangedByUser">Is changed by user</param>
public static void ValidateProperties(Material material, bool isRenderModeChangedByUser = false)
{
SetupBlendMode(material, (UniUnlitRenderMode)material.GetFloat(PropNameBlendMode),
isRenderModeChangedByUser);
SetupVertexColorBlendOp(material, (UniUnlitVertexColorBlendOp)material.GetFloat(PropeNameVColBlendMode));
}
private static void SetupBlendMode(Material material, UniUnlitRenderMode renderMode,
bool isRenderModeChangedByUser = false)
{
switch (renderMode)
{
case UniUnlitRenderMode.Opaque:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueOpaque);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = -1;
break;
case UniUnlitRenderMode.Cutout:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparentCutout);
material.SetInt(PropNameSrcBlend, (int)BlendMode.One);
material.SetInt(PropNameDstBlend, (int)BlendMode.Zero);
material.SetInt(PropNameZWrite, 1);
SetKeyword(material, KeywordAlphaTestOn, true);
SetKeyword(material, KeywordAlphaBlendOn, false);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.AlphaTest;
break;
case UniUnlitRenderMode.Transparent:
material.SetOverrideTag(TagRenderTypeKey, TagRenderTypeValueTransparent);
material.SetInt(PropNameSrcBlend, (int)BlendMode.SrcAlpha);
material.SetInt(PropNameDstBlend, (int)BlendMode.OneMinusSrcAlpha);
material.SetInt(PropNameZWrite, 0);
SetKeyword(material, KeywordAlphaTestOn, false);
SetKeyword(material, KeywordAlphaBlendOn, true);
if (isRenderModeChangedByUser) material.renderQueue = (int)RenderQueue.Transparent;
break;
}
}
private static void SetupVertexColorBlendOp(Material material, UniUnlitVertexColorBlendOp vColBlendOp)
{
switch (vColBlendOp)
{
case UniUnlitVertexColorBlendOp.None:
SetKeyword(material, KeywordVertexColMul, false);
break;
case UniUnlitVertexColorBlendOp.Multiply:
SetKeyword(material, KeywordVertexColMul, true);
break;
}
}
private static void SetKeyword(Material mat, string keyword, bool required)
{
if (required)
mat.EnableKeyword(keyword);
else
mat.DisableKeyword(keyword);
}
}
}

View File

@@ -1,3 +1,3 @@
fileFormatVersion: 2
guid: e96cbbd810384352a6799dd731533178
fileFormatVersion: 2
guid: e96cbbd810384352a6799dd731533178
timeCreated: 1537534399