Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MToon 1.0 functions. #1101

Merged
merged 8 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Assets/VRM10/Tests/MaterialImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void MaterialImporterTest()

var matDesc = new Vrm10MaterialDescriptorGenerator().Get(data, 0);
Assert.AreEqual("Alicia_body", matDesc.Name);
Assert.AreEqual("Hidden/VRM10/vrmc_materials_mtoon", matDesc.ShaderName);
Assert.AreEqual("VRM10/MToon10", matDesc.ShaderName);
Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_MainTex"].UnityObjectName);
Assert.AreEqual("Alicia_body", matDesc.TextureSlots["_ShadeTex"].UnityObjectName);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum MToon10EditorEditMode
{
Basic = 0,
Advanced = 1,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

195 changes: 135 additions & 60 deletions Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro

EditorGUI.BeginChangeCheck();

EditorGUILayout.HelpBox("This shader is for VRM 1.0.", MessageType.Info);

using (new LabelScope("Editor Settings"))
{
PopupEnum<MToon10EditorEditMode>("Edit Mode", props[MToon10Prop.EditorEditMode], materialEditor);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Inspector mode

}
var editMode = (MToon10EditorEditMode) (int) props[MToon10Prop.EditorEditMode].floatValue;
var isAdvancedEditMode = editMode == MToon10EditorEditMode.Advanced;

using (new LabelScope("Rendering"))
{
PopupEnum<MToon10AlphaMode>("Alpha Mode", props[MToon10Prop.AlphaMode], materialEditor);
var alphaMode = (MToon10AlphaMode) (int) props[MToon10Prop.AlphaMode].floatValue;

if (alphaMode == MToon10AlphaMode.Transparent)
if (isAdvancedEditMode && alphaMode == MToon10AlphaMode.Transparent)
{
PopupEnum<MToon10TransparentWithZWriteMode>(
"Transparent With ZWrite Mode",
Expand All @@ -40,7 +49,10 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro

PopupEnum<MToon10DoubleSidedMode>("Double Sided", props[MToon10Prop.DoubleSided], materialEditor);

materialEditor.ShaderProperty(props[MToon10Prop.RenderQueueOffsetNumber], "RenderQueue Offset");
if (isAdvancedEditMode)
{
materialEditor.ShaderProperty(props[MToon10Prop.RenderQueueOffsetNumber], "RenderQueue Offset");
}
}

using (new LabelScope("Lighting"))
Expand All @@ -55,23 +67,73 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
props[MToon10Prop.ShadeColorTexture],
props[MToon10Prop.ShadeColorFactor]
);
materialEditor.TexturePropertySingleLine(
new GUIContent("Normal Map", "Normal Map (RGB)"),
props[MToon10Prop.NormalTexture],
props[MToon10Prop.NormalTextureScale]
);
materialEditor.ShaderProperty(props[MToon10Prop.ShadingShiftFactor], "Shading Shift");
materialEditor.ShaderProperty(props[MToon10Prop.ShadingToonyFactor], "Shading Toony");
materialEditor.TexturePropertySingleLine(
new GUIContent("Additive Shading Shift", "Shading Shift (R)"),
props[MToon10Prop.ShadingShiftTexture],
props[MToon10Prop.ShadingShiftTextureScale]
);
if (isAdvancedEditMode)
{
materialEditor.TexturePropertySingleLine(
new GUIContent("Normal Map", "Normal Map (RGB)"),
props[MToon10Prop.NormalTexture],
props[MToon10Prop.NormalTextureScale]
);
}
EditorGUILayout.Space();

if (isAdvancedEditMode)
{
materialEditor.ShaderProperty(props[MToon10Prop.ShadingToonyFactor], "Shading Toony");
materialEditor.ShaderProperty(props[MToon10Prop.ShadingShiftFactor], "Shading Shift");
materialEditor.TexturePropertySingleLine(
new GUIContent("Additive Shading Shift", "Shading Shift (R)"),
props[MToon10Prop.ShadingShiftTexture],
props[MToon10Prop.ShadingShiftTextureScale]
);
}
else
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Presets");
if (GUILayout.Button("Default"))
{
props[MToon10Prop.ShadingToonyFactor].floatValue = 0.95f;
props[MToon10Prop.ShadingShiftFactor].floatValue = -0.05f;
props[MToon10Prop.ShadingShiftTexture].textureValue = null;
}
if (GUILayout.Button("Lambert"))
{
props[MToon10Prop.ShadingToonyFactor].floatValue = 0.5f;
props[MToon10Prop.ShadingShiftFactor].floatValue = -0.5f;
props[MToon10Prop.ShadingShiftTexture].textureValue = null;
}
if (GUILayout.Button("Cartoon"))
{
props[MToon10Prop.ShadingToonyFactor].floatValue = 1.0f;
props[MToon10Prop.ShadingShiftFactor].floatValue = 0.0f;
props[MToon10Prop.ShadingShiftTexture].textureValue = null;
}
EditorGUILayout.EndHorizontal();

GUILayout.BeginVertical(GUI.skin.box);
materialEditor.ShaderProperty(props[MToon10Prop.ShadingToonyFactor], "Shading Toony");
materialEditor.ShaderProperty(props[MToon10Prop.ShadingShiftFactor], "Shading Shift");
GUILayout.EndVertical();
}

if (props[MToon10Prop.ShadingShiftTexture].textureValue == null)
{
var toony = props[MToon10Prop.ShadingToonyFactor].floatValue;
var shift = props[MToon10Prop.ShadingShiftFactor].floatValue;
if (toony - shift < 1.0f - 0.001f)
{
EditorGUILayout.HelpBox("The lit area includes non-lit area.", MessageType.Warning);
}
}
}

using (new LabelScope("Global Illumination"))
if (isAdvancedEditMode)
{
materialEditor.ShaderProperty(props[MToon10Prop.GiEqualizationFactor], "GI Equalization");
using (new LabelScope("Global Illumination"))
{
materialEditor.ShaderProperty(props[MToon10Prop.GiEqualizationFactor], "GI Equalization");
}
}

using (new LabelScope("Emission"))
Expand All @@ -89,16 +151,19 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
new GUIContent("Rim Color", "Rim Color (RGB)"),
props[MToon10Prop.RimMultiplyTexture]
);
materialEditor.ShaderProperty(
props[MToon10Prop.RimLightingMixFactor],
new GUIContent("Rim LightingMix")
);
EditorGUILayout.Space();
if (isAdvancedEditMode)
{
materialEditor.ShaderProperty(
props[MToon10Prop.RimLightingMixFactor],
new GUIContent("Rim LightingMix")
);
EditorGUILayout.Space();

materialEditor.TexturePropertySingleLine(
new GUIContent("Matcap Rim", "Matcap Rim (RGB)"),
props[MToon10Prop.MatcapTexture]
);
materialEditor.TexturePropertySingleLine(
new GUIContent("Matcap Rim", "Matcap Rim (RGB)"),
props[MToon10Prop.MatcapTexture]
);
}
EditorGUILayout.Space();

materialEditor.ShaderProperty(
Expand All @@ -118,52 +183,62 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
using (new LabelScope("Outline"))
{
PopupEnum<MToon10OutlineMode>("Outline Mode", props[MToon10Prop.OutlineWidthMode], materialEditor);
materialEditor.TexturePropertySingleLine(
new GUIContent("Outline Width", "Outline Width (G) [meter]"),
props[MToon10Prop.OutlineWidthMultiplyTexture],
props[MToon10Prop.OutlineWidthFactor]
);
materialEditor.ShaderProperty(
props[MToon10Prop.OutlineColorFactor],
new GUIContent("Outline Color")
);
materialEditor.ShaderProperty(
props[MToon10Prop.OutlineLightingMixFactor],
new GUIContent("Outline LightingMix")
);
var hasOutline = (MToon10OutlineMode) (int) props[MToon10Prop.OutlineWidthMode].floatValue != MToon10OutlineMode.None;

if (hasOutline)
{
materialEditor.TexturePropertySingleLine(
new GUIContent("Outline Width", "Outline Width (G) [meter]"),
props[MToon10Prop.OutlineWidthMultiplyTexture],
props[MToon10Prop.OutlineWidthFactor]
);
materialEditor.ShaderProperty(
props[MToon10Prop.OutlineColorFactor],
new GUIContent("Outline Color")
);
if (isAdvancedEditMode)
{
materialEditor.ShaderProperty(
props[MToon10Prop.OutlineLightingMixFactor],
new GUIContent("Outline LightingMix")
);
}
}
}

using (new LabelScope("UV Animation"))
if (isAdvancedEditMode)
{
materialEditor.TexturePropertySingleLine(
new GUIContent("Mask", "Mask (B)"),
props[MToon10Prop.UvAnimationMaskTexture]
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationScrollXSpeedFactor],
new GUIContent("Translate X")
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationScrollYSpeedFactor],
new GUIContent("Translate Y")
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationRotationSpeedFactor],
new GUIContent("Rotation")
);
using (new LabelScope("UV Animation"))
{
materialEditor.TexturePropertySingleLine(
new GUIContent("Mask", "Mask (B)"),
props[MToon10Prop.UvAnimationMaskTexture]
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationScrollXSpeedFactor],
new GUIContent("Translate X")
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationScrollYSpeedFactor],
new GUIContent("Translate Y")
);
materialEditor.ShaderProperty(
props[MToon10Prop.UvAnimationRotationSpeedFactor],
new GUIContent("Rotation")
);
}
}

if (EditorGUI.EndChangeCheck())
{
Validate(materials);
}

using (new LabelScope("Debug"))
if (isAdvancedEditMode && materials.Length == 1)
{
if (materials.Length == 1)
var mat = materials[0];
using (new LabelScope("Debug"))
{
var mat = materials[0];

EditorGUILayout.LabelField("RenderQueue", mat.renderQueue.ToString());
EditorGUILayout.LabelField("Cull", ((CullMode) props[MToon10Prop.UnityCullMode].floatValue).ToString());
EditorGUILayout.LabelField("SrcBlend", ((BlendMode) props[MToon10Prop.UnitySrcBlend].floatValue).ToString());
Expand Down Expand Up @@ -193,7 +268,7 @@ private static bool PopupEnum<T>(string name, MaterialProperty property, Materia
var changed = EditorGUI.EndChangeCheck();
if (changed)
{
editor.RegisterPropertyChangeUndo("EnumPopUp");
editor.RegisterPropertyChangeUndo($"Change {name}");
property.floatValue = ret;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Shader "Hidden/VRM10/vrmc_materials_mtoon"
Shader "VRM10/MToon10"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename

{
Properties
{
Expand All @@ -16,10 +16,10 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon"
_ShadeTex ("mtoon.shadeMultiplyTexture", 2D) = "white" {}
[Normal] _BumpMap ("normalTexture", 2D) = "bump" {} // Unity specified name
_BumpScale ("normalTexture.scale", Float) = 1.0 // Unity specified name
_ShadingShiftFactor ("mtoon.shadingShiftFactor", Range(-1, 1)) = 0
_ShadingShiftFactor ("mtoon.shadingShiftFactor", Range(-1, 1)) = -0.05
_ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R
_ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1
_ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9
_ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.95

// GI
_GiEqualization ("mtoon.giEqualizationFactor", Range(0, 1)) = 0.9
Expand Down Expand Up @@ -58,6 +58,9 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon"

// etc
_M_DebugMode ("_DebugMode", Float) = 0.0

// for Editor
_M_EditMode ("_EditMode", Float) = 0.0
}

// Shader Model 3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#define MTOON_IS_FRONT_VFACE(VAL, FRONT, BACK) ((VAL > 0.0) ? (FRONT) : (BACK))
#endif

// Compile-time constant
// EXPERIMENTAL
inline bool MToon_IsPbrCorrectOn()
{
return false;
}

// Compile-time constant
inline bool MToon_IsForwardBasePass()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name.
const VertexPositionInfo position = MToon_GetOutlineVertex(v.vertex.xyz, normalize(v.normalOS), output.uv);
output.pos = position.positionCS;
output.positionWS = position.positionWS;
output.normalWS = UnityObjectToWorldNormal(-v.normalOS);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix outline normal

}
else
{
const VertexPositionInfo position = MToon_GetVertex(v.vertex.xyz);
output.pos = position.positionCS;
output.positionWS = position.positionWS;
output.normalWS = UnityObjectToWorldNormal(v.normalOS);
}

output.normalWS = UnityObjectToWorldNormal(v.normalOS);
output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS);

#if defined(_NORMALMAP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ inline half GetMToonLighting_Shade(const UnityLighting lighting, const MToonInpu
const half shadeShift = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadeToony = _ShadingToonyFactor;

if (MToon_IsForwardBasePass())
if (MToon_IsPbrCorrectOn())
{
const half shadeInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation);
const half shadeInput = dotNL;
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift);
}

if (MToon_IsForwardBasePass())
{
const half shadeInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL));
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift) * lighting.directLightAttenuation;
}
else
{
const half shadeInput = dotNL;
Expand All @@ -52,6 +58,11 @@ inline half GetMToonLighting_Shade(const UnityLighting lighting, const MToonInpu

inline half GetMToonLighting_Shadow(const UnityLighting lighting, const half dotNL)
{
if (MToon_IsPbrCorrectOn())
{
return lighting.directLightAttenuation * step(0, dotNL);
}

if (MToon_IsForwardBasePass())
{
return 1;
Expand Down
4 changes: 2 additions & 2 deletions Assets/VRMShaders/VRM10/MToon10/Runtime/MToon10Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public static float MigrateToShadingShift(float shadingToony0X, float shadingShi
{
var (rangeMin, rangeMax) = GetShadingRange0X(shadingToony0X, shadingShift0X);

// new shadingShift is the center of range.
return Mathf.Clamp((rangeMax - rangeMin) * 0.5f, -1, +1);
// new shadingShift is the center of range inverted.
return Mathf.Clamp((rangeMax + rangeMin) * 0.5f * -1f, -1, +1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix migration

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class MToon10Meta
{
public static readonly string UnityShaderName = "Hidden/VRM10/vrmc_materials_mtoon";
public static readonly string UnityShaderName = "VRM10/MToon10";
}
}
Loading