Merge pull request #1101 from Santarh/renameMtoon10

Fix MToon 1.0 functions.
This commit is contained in:
ousttrue
2021-07-15 21:13:27 +09:00
committed by GitHub
17 changed files with 267 additions and 70 deletions

View File

@@ -26,7 +26,7 @@ namespace UniVRM10
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);

View File

@@ -0,0 +1,8 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum MToon10EditorEditMode
{
Basic = 0,
Advanced = 1,
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 22a9170004064d029bc205fc5b5a1acc
timeCreated: 1626335293

View File

@@ -19,12 +19,21 @@ namespace VRMShaders.VRM10.MToon10.Editor
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);
}
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",
@@ -40,7 +49,10 @@ namespace VRMShaders.VRM10.MToon10.Editor
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"))
@@ -55,23 +67,73 @@ namespace VRMShaders.VRM10.MToon10.Editor
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"))
@@ -89,16 +151,19 @@ namespace VRMShaders.VRM10.MToon10.Editor
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(
@@ -118,39 +183,50 @@ namespace VRMShaders.VRM10.MToon10.Editor
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())
@@ -158,12 +234,11 @@ namespace VRMShaders.VRM10.MToon10.Editor
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());
@@ -193,7 +268,7 @@ namespace VRMShaders.VRM10.MToon10.Editor
var changed = EditorGUI.EndChangeCheck();
if (changed)
{
editor.RegisterPropertyChangeUndo("EnumPopUp");
editor.RegisterPropertyChangeUndo($"Change {name}");
property.floatValue = ret;
}

View File

@@ -1,4 +1,4 @@
Shader "Hidden/VRM10/vrmc_materials_mtoon"
Shader "VRM10/MToon10"
{
Properties
{
@@ -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
@@ -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

View File

@@ -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()
{

View File

@@ -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);
}
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)

View File

@@ -38,10 +38,16 @@ inline half GetMToonLighting_Shade(const UnityLighting lighting, const MToonInpu
const half shadeShift = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadeToony = _ShadingToonyFactor;
if (MToon_IsPbrCorrectOn())
{
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) * lighting.directLightAttenuation);
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift);
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
{
@@ -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;

View File

@@ -25,8 +25,8 @@ namespace VRMShaders.VRM10.MToon10.Runtime
{
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);
}
/// <summary>

View File

@@ -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";
}
}

View File

@@ -55,5 +55,8 @@
UnityDstBlend,
UnityZWrite,
UnityAlphaToMask,
// for Editor
EditorEditMode,
}
}

View File

@@ -51,6 +51,8 @@ namespace VRMShaders.VRM10.MToon10.Runtime
[MToon10Prop.UnityDstBlend] = "_M_DstBlend",
[MToon10Prop.UnityZWrite] = "_M_ZWrite",
[MToon10Prop.UnityAlphaToMask] = "_M_AlphaToMask",
[MToon10Prop.EditorEditMode] = "_M_EditMode",
};
public static IReadOnlyDictionary<MToon10Prop, string> UnityShaderLabNames => _unityShaderLabNames;

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f5d3d10dc09b0524abb5c55599bb14e8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using NUnit.Framework;
using VRMShaders.VRM10.MToon10.Runtime;
namespace VRMShaders.VRM10.MToon10.Tests
{
public sealed class MigrationTests
{
[Test]
public void MigrateToonyAndShift()
{
var delta = 0.001f;
// 0.x default
Assert.AreEqual(0.95f,MToon10Migrator.MigrateToShadingToony(0.9f, 0f), delta);
Assert.AreEqual(-0.05f,MToon10Migrator.MigrateToShadingShift(0.9f, 0f), delta);
// lambert
Assert.AreEqual(0.5f, MToon10Migrator.MigrateToShadingToony(0, 0), delta);
Assert.AreEqual(-0.5f, MToon10Migrator.MigrateToShadingShift(0, 0), delta);
// half lambert
Assert.AreEqual(0.0f, MToon10Migrator.MigrateToShadingToony(0, -1), delta);
Assert.AreEqual(0.0f, MToon10Migrator.MigrateToShadingShift(0, -1), delta);
// random
Assert.AreEqual(0.79f, MToon10Migrator.MigrateToShadingToony(0.7f, -0.4f), delta);
Assert.AreEqual(0.19f, MToon10Migrator.MigrateToShadingShift(0.7f, -0.4f), delta);
}
[Test]
public void MigrateGiIntensity()
{
var delta = 0.001f;
// normal
Assert.AreEqual(0f, MToon10Migrator.MigrateToGiEqualization(1f));
// equalized
Assert.AreEqual(1f, MToon10Migrator.MigrateToGiEqualization(0f));
// intermediate
Assert.AreEqual(0.25f, MToon10Migrator.MigrateToGiEqualization(0.75f));
Assert.AreEqual(0.333f, MToon10Migrator.MigrateToGiEqualization(0.667f));
Assert.AreEqual(0.125f, MToon10Migrator.MigrateToGiEqualization(0.875f));
Assert.AreEqual(0.75f, MToon10Migrator.MigrateToGiEqualization(0.25f));
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6530bdc57fd7d354db37f84b93222964
timeCreated: 1626335293

View File

@@ -0,0 +1,18 @@
{
"name": "VRMShaders.VRM10.MToon10.Tests",
"references": [
"VRMShaders.VRM10.MToon10.Runtime"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": []
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a8e4a462662564e48931da8e2a390e60
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: