Merge pull request #1003 from Santarh/mtoonDefine

Implements vrmc_materials_mtoon shader code
This commit is contained in:
ousttrue 2021-06-07 20:57:54 +09:00 committed by GitHub
commit 79af87b3bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
54 changed files with 1281 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3f051e7d892a4bccbcc8fd1f4c2ca162
timeCreated: 1622456532

View File

@ -0,0 +1,9 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum AlphaMode
{
Opaque = 0,
Cutout = 1,
Transparent = 2,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4915ab0041b841b6aa0cdfecae764008
timeCreated: 1622453782

View File

@ -0,0 +1,8 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum TransparentWithZWriteMode
{
Off = 0,
On = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d5df515a36d3460891b348872459476f
timeCreated: 1622456521

View File

@ -0,0 +1,64 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace VRMShaders.VRM10.MToon10.Editor
{
public class MToonInspector : ShaderGUI
{
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
var props = PropExtensions.PropertyNames
.ToDictionary(x => x.Key, x => FindProperty(x.Value, properties));
var materials = materialEditor.targets.Select(x => x as Material).ToArray();
EditorGUILayout.LabelField("Rendering", EditorStyles.boldLabel);
EditorGUILayout.BeginVertical(GUI.skin.box);
{
EditorGUILayout.LabelField("Mode", EditorStyles.boldLabel);
if (PopupEnum<AlphaMode>("Alpha Mode", props[Prop.AlphaMode], materialEditor))
{
Validate(materials);
}
if (PopupEnum<TransparentWithZWriteMode>("Transparent With ZWrite Mode", props[Prop.TransparentWithZWrite], materialEditor))
{
Validate(materials);
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
base.OnGUI(materialEditor, properties);
}
private static void Validate(Material[] materials)
{
foreach (var material in materials)
{
new MToonValidator(material).Validate();
}
}
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b8b6f1e6a8a133a4987361f178d9f548
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,80 @@
using System;
using UnityEngine;
using UnityEngine.Rendering;
namespace VRMShaders.VRM10.MToon10.Editor
{
public sealed class MToonValidator
{
private readonly Material _material;
public MToonValidator(Material material)
{
_material = material;
}
public void Validate()
{
var alphaMode = (AlphaMode) _material.GetInt(Prop.AlphaMode);
var zWriteMode = (TransparentWithZWriteMode) _material.GetInt(Prop.TransparentWithZWrite);
SetUnityRenderSettings(_material, alphaMode, zWriteMode);
}
private static void SetUnityRenderSettings(Material material, AlphaMode alphaMode, TransparentWithZWriteMode zWriteMode)
{
material.SetInt(Prop.AlphaMode, (int) alphaMode);
material.SetInt(Prop.TransparentWithZWrite, (int) zWriteMode);
switch (alphaMode)
{
case AlphaMode.Opaque:
material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.OpaqueValue);
material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.One);
material.SetInt(Prop.UnityDstBlend, (int) BlendMode.Zero);
material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On);
material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off);
material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false);
material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, false);
material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false);
material.renderQueue = (int) RenderQueue.Geometry;
break;
case AlphaMode.Cutout:
material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentCutoutValue);
material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.One);
material.SetInt(Prop.UnityDstBlend, (int) BlendMode.Zero);
material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On);
material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.On);
material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, true);
material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, false);
material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false);
material.renderQueue = (int) RenderQueue.AlphaTest;
break;
case AlphaMode.Transparent when zWriteMode == TransparentWithZWriteMode.Off:
material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentValue);
material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.SrcAlpha);
material.SetInt(Prop.UnityDstBlend, (int) BlendMode.OneMinusSrcAlpha);
material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.Off);
material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off);
material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false);
material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, true);
material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false);
material.renderQueue = (int) RenderQueue.Transparent;
break;
case AlphaMode.Transparent when zWriteMode == TransparentWithZWriteMode.On:
material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentValue);
material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.SrcAlpha);
material.SetInt(Prop.UnityDstBlend, (int) BlendMode.OneMinusSrcAlpha);
material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On);
material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off);
material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false);
material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, true);
material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false);
material.renderQueue = (int) RenderQueue.GeometryLast + 1; // Transparent First
break;
default:
SetUnityRenderSettings(material, AlphaMode.Opaque, TransparentWithZWriteMode.Off);
break;
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 18b52251a22d40ccbfc16c4a89d09c51
timeCreated: 1622453963

View File

@ -0,0 +1,29 @@
using UnityEngine;
namespace VRMShaders.VRM10.MToon10.Editor
{
public static class MaterialExtensions
{
public static void SetKeyword(this Material mat, string keyword, bool isEnabled)
{
if (isEnabled)
{
mat.EnableKeyword(keyword);
}
else
{
mat.DisableKeyword(keyword);
}
}
public static int GetInt(this Material mat, Prop prop)
{
return mat.GetInt(prop.ToName());
}
public static void SetInt(this Material mat, Prop prop, int val)
{
mat.SetInt(prop.ToName(), val);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 173b50f65d2a44109cc67a84f18bde53
timeCreated: 1622456072

View File

@ -0,0 +1,22 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum Prop
{
// Rendering
AlphaMode,
TransparentWithZWrite,
AlphaCutoff,
RenderQueueOffsetNumber,
DoubleSided,
// Lighting
BaseColorFactor,
// Unity Required
UnityCullMode,
UnitySrcBlend,
UnityDstBlend,
UnityZWrite,
UnityAlphaToMask,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aea6e3bb9944499e94c1026d60104432
timeCreated: 1622453772

View File

@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace VRMShaders.VRM10.MToon10.Editor
{
public static class PropExtensions
{
private static readonly Dictionary<Prop, string> _propertyNames = new Dictionary<Prop, string>
{
[Prop.AlphaMode] = "_AlphaMode",
[Prop.TransparentWithZWrite] = "_TransparentWithZWrite",
[Prop.AlphaCutoff] = "_Cutoff",
[Prop.RenderQueueOffsetNumber] = "_RenderQueueOffset",
[Prop.DoubleSided] = "_DoubleSided",
[Prop.BaseColorFactor] = "_Color",
[Prop.UnityCullMode] = "_M_CullMode",
[Prop.UnitySrcBlend] = "_M_SrcBlend",
[Prop.UnityDstBlend] = "_M_DstBlend",
[Prop.UnityZWrite] = "_M_ZWrite",
[Prop.UnityAlphaToMask] = "_M_AlphaToMask",
};
public static IReadOnlyDictionary<Prop, string> PropertyNames => _propertyNames;
public static string ToName(this Prop prop)
{
return PropertyNames[prop];
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3c8828cd778c400f81b8a1c93b54ee50
timeCreated: 1622454119

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5b210153ed9c4949ad794ca34f18e1b1
timeCreated: 1622456318

View File

@ -0,0 +1,9 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public static class UnityAlphaModeKeyword
{
public const string AlphaTest = "_ALPHATEST_ON";
public const string AlphaBlend = "_ALPHABLEND_ON";
public const string AlphaPremultiply = "_ALPHAPREMULTIPLY_ON";
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1e9a4286845a4c998a402e50da51ecc9
timeCreated: 1622456299

View File

@ -0,0 +1,8 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum UnityAlphaToMaskMode
{
Off = 0,
On = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d254dee2e3b3407abcdc3f6f84b3cd59
timeCreated: 1622456292

View File

@ -0,0 +1,10 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public static class UnityRenderTag
{
public const string Key = "RenderType";
public const string OpaqueValue = "Opaque";
public const string TransparentCutoutValue = "TransparentCutout";
public const string TransparentValue = "Transparent";
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df547717467444c29ff3dcec1e555e17
timeCreated: 1622456295

View File

@ -0,0 +1,8 @@
namespace VRMShaders.VRM10.MToon10.Editor
{
public enum UnityZWriteMode
{
Off = 0,
On = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4c80d709af9c46f9b57290659de4c1c6
timeCreated: 1622456288

View File

@ -0,0 +1,15 @@
{
"name": "VRMShaders.VRM10.MToon10.Editor",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,205 @@
Shader "Hidden/VRM10/vrmc_materials_mtoon"
{
Properties
{
// Rendering
_AlphaMode ("alphaMode", Int) = 0
_TransparentWithZWrite ("mtoon.transparentWithZWrite", Int) = 0
_Cutoff ("alphaCutoff", Range(0, 1)) = 0.5 // Unity specified name
_RenderQueueOffset ("mtoon.renderQueueOffsetNumber", Int) = 0
_DoubleSided ("doubleSided", Int) = 0
// Lighting
_Color ("pbrMetallicRoughness.baseColorFactor", Color) = (1, 1, 1, 1) // Unity specified name
_MainTex ("pbrMetallicRoughness.baseColorTexture", 2D) = "white" {} // Unity specified name
_ShadeColor ("mtoon.shadeColorFactor", Color) = (1, 1, 1, 1)
_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
_ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R
_ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1
_ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9
// _ShadingToonyTex ("mtoon.shadingToonyTexture", 2D) = "black" {} // parameter texture // need?
// _ShadingToonyTexScale ("mtoon.shadingToonyTexture.scale", Float) = 1 // need?
// GI
_GiEqualization ("mtoon.giEqualizationFactor", Range(0, 1)) = 0.9
// Emission
_EmissionColor ("emissiveFactor", Color) = (0, 0, 0, 1) // Unity specified name
_EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name
// Rim Lighting
_MatcapTex ("mtoon.matcapTexture", 2D) = "black" {}
_RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0, 1)
_RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Float) = 5.0
_RimLift ("mtoon.parametricRimLiftFactor", Float) = 0
_RimTex ("mtoon.rimMultiplyTexture", 2D) = "white" {}
_RimLightingMix ("mtoon.rimLightingMixFactor", Float) = 1
// Outline
_OutlineWidthMode ("mtoon.outlineWidthMode", Int) = 0
_OutlineWidth ("mtoon.outlineWidthFactor", Float) = 0
_OutlineWidthTex ("mtoon.outlineWidthMultiplyTexture", 2D) = "white" {} // channel G
_OutlineColor ("mtoon.outlineColorFactor", Color) = (0, 0, 0, 1)
_OutlineLightingMix ("mtoon.outlineLightingMixFactor", Float) = 1
// UV Animation
_UvAnimMaskTex ("mtoon.uvAnimationMaskTexture", 2D) = "white" {} // channel B
_UvAnimScrollXSpeed ("mtoon.uvAnimationScrollXSpeedFactor", Float) = 0
_UvAnimScrollYSpeed ("mtoon.uvAnimationScrollYSpeedFactor", Float) = 0
_UvAnimRotationSpeed ("mtoon.uvAnimationRotationSpeedFactor", Float) = 0
// Unity ShaderPass Mode
_M_CullMode ("_CullMode", Float) = 2.0
_M_SrcBlend ("_SrcBlend", Float) = 1.0
_M_DstBlend ("_DstBlend", Float) = 0.0
_M_ZWrite ("_ZWrite", Float) = 1.0
_M_AlphaToMask ("_AlphaToMask", Float) = 0.0
// etc
_M_DebugMode ("_DebugMode", Float) = 0.0
}
// Shader Model 3.0
SubShader
{
Tags { "RenderType" = "Opaque" "Queue" = "Geometry" }
// Built-in Forward Base Pass
Pass
{
Name "FORWARD_BASE"
Tags { "LightMode" = "ForwardBase" }
Cull [_M_CullMode]
Blend [_M_SrcBlend] [_M_DstBlend]
ZWrite [_M_ZWrite]
ZTest LEqual
BlendOp Add, Max
AlphaToMask [_M_AlphaToMask]
HLSLPROGRAM
#pragma target 3.0
// Unity defined keywords
#pragma multi_compile_fwdbase nolightmap nodynlightmap nodirlightmap novertexlight
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _MTOON_EMISSIVEMAP
#pragma multi_compile_local __ _MTOON_PARAMETERMAP
#pragma vertex MToonVertex
#pragma fragment MToonFragment
#include "./vrmc_materials_mtoon_forward_vertex.hlsl"
#include "./vrmc_materials_mtoon_forward_fragment.hlsl"
ENDHLSL
}
// Built-in Forward Base Pass: OUTLINE
Pass
{
Name "FORWARD_BASE_OUTLINE"
Tags { "LightMode" = "ForwardBase" }
Cull Front
Blend [_M_SrcBlend] [_M_DstBlend]
ZWrite [_M_ZWrite]
ZTest LEqual
BlendOp Add, Max
AlphaToMask [_M_AlphaToMask]
HLSLPROGRAM
#pragma target 3.0
// Unity defined keywords
#pragma multi_compile_fwdbase nolightmap nodynlightmap nodirlightmap novertexlight
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _MTOON_EMISSIVEMAP
#pragma multi_compile_local __ _MTOON_PARAMETERMAP
#define MTOON_PASS_OUTLINE
#pragma vertex MToonVertex
#pragma fragment MToonFragment
#include "./vrmc_materials_mtoon_forward_vertex.hlsl"
#include "./vrmc_materials_mtoon_forward_fragment.hlsl"
ENDHLSL
}
// Built-in Forward Add Pass
Pass
{
Name "FORWARD_ADD"
Tags { "LightMode" = "ForwardAdd" }
Cull [_M_CullMode]
Blend [_M_SrcBlend] One
ZWrite Off
ZTest LEqual
BlendOp Add, Max
AlphaToMask [_M_AlphaToMask]
HLSLPROGRAM
#pragma target 3.0
// Unity defined keywords
#pragma multi_compile_fwdadd_fullshadows nolightmap nodynlightmap nodirlightmap novertexlight
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_local __ _NORMALMAP
#pragma multi_compile_local __ _MTOON_EMISSIVEMAP
#pragma multi_compile_local __ _MTOON_PARAMETERMAP
#pragma vertex MToonVertex
#pragma fragment MToonFragment
#include "./vrmc_materials_mtoon_forward_vertex.hlsl"
#include "./vrmc_materials_mtoon_forward_fragment.hlsl"
ENDHLSL
}
// Shadow rendering pass
Pass
{
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
Cull [_M_CullMode]
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma target 3.0
// Unity defined keywords
#pragma multi_compile_shadowcaster nolightmap nodynlightmap nodirlightmap novertexlight
#pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON
// Use unity standard shadow implementation.
// internal usage:
// keywords: _ALPHATEST_ON _ALPHABLEND_ON
// variables: _MainTex.a _Color.a _Cutoff
#pragma vertex vertShadowCaster
#pragma fragment fragShadowCaster
#include "UnityStandardShadow.cginc"
ENDCG
}
}
Fallback "Unlit/Texture"
CustomEditor "VRMShaders.VRM10.MToon10.Editor.MToonInspector"
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e0edbf68d81d1f340ae8b110086b7063
timeCreated: 1514111466
licenseType: Free
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,36 @@
#ifndef VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED
#define VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED
#include <UnityCG.cginc>
#include <AutoLight.cginc>
struct Attributes
{
float4 vertex : POSITION; // UnityCG macro specified name. Accurately "positionOS"
float3 normalOS : NORMAL;
#if defined(_NORMALMAP)
float4 tangentOS : TANGENT;
#endif
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
float3 positionWS : TEXCOORD1;
half3 normalWS : TEXCOORD2;
#if defined(_NORMALMAP)
half4 tangentWS : TEXCOORD3;
#endif
float3 viewDirWS : TEXCOORD4;
half outlineFactor : TEXCOORD5;
UNITY_FOG_COORDS(6)
UNITY_LIGHTING_COORDS(7,8)
float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS"
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 82d864eead0e48f79295fe490a6d614f
timeCreated: 1622450640

View File

@ -0,0 +1,81 @@
#ifndef VRMC_MATERIALS_MTOON_DEFINE_INCLUDED
#define VRMC_MATERIALS_MTOON_DEFINE_INCLUDED
#ifndef UNITY_SAMPLE_TEX2D_LOD
#define UNITY_SAMPLE_TEX2D_LOD(tex,coord,lod) tex.SampleLevel (sampler##tex,coord,lod)
#endif
// Compile-time constant
inline bool MToon_IsForwardBasePass()
{
#if defined(UNITY_PASS_FORWARDBASE)
return true;
#elif defined(UNITY_PASS_FORWARDADD)
return false;
#else
// unexpected
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsOutlinePass()
{
#if defined(MTOON_PASS_OUTLINE)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsNormalMapOn()
{
#if defined(_NORMALMAP)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsEmissiveMapOn()
{
#if defined(_MTOON_EMISSIVEMAP)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsParameterMapOn()
{
#if defined(_MTOON_PARAMETERMAP)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsOutlineModeWorldCoordinates()
{
#if defined(MTOON_OUTLINE_WIDTH_WORLD)
return true;
#else
return false;
#endif
}
// Compile-time constant
inline bool MToon_IsOutlineModeScreenCoordinates()
{
#if defined(MTOON_OUTLINE_WIDTH_SCREEN)
return true;
#else
return false;
#endif
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cfd10dec96ea4e3f9fad70e2e5dc148e
timeCreated: 1622451960

View File

@ -0,0 +1,56 @@
#ifndef VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED
#define VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED
#include <UnityCG.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_attribute.hlsl"
#include "./vrmc_materials_mtoon_geometry_uv.hlsl"
#include "./vrmc_materials_mtoon_geometry_normal.hlsl"
#include "./vrmc_materials_mtoon_lighting_unity.hlsl"
#include "./vrmc_materials_mtoon_lighting_mtoon.hlsl"
half4 MToonFragment(const Varyings input) : SV_Target
{
// Get MToon UV (with UVAnimation)
const float2 uv = GetMToonGeometry_Uv(input.uv);
// Get LitColor with Alpha
const half4 litColor = UNITY_SAMPLE_TEX2D(_MainTex, uv) * _Color;
// Alpha Test
#if defined(_ALPHATEST_ON)
const half rawAlpha = litColor.a;
const half tmpAlpha = (rawAlpha - _Cutoff) / max(fwidth(rawAlpha), 0.00001) + 0.5; // Alpha to Coverage
clip(tmpAlpha - _Cutoff);
const half alpha = 1.0;
#elif defined(_ALPHABLEND_ON)
const half alpha = litColor.a;
clip(alpha - EPS_COL);
#else
const half alpha = 1.0;
#endif
// Get Normal
const float3 normalWS = GetMToonGeometry_Normal(input, uv);
// Get Unity Lighting
const UnityLighting unityLighting = GetUnityLighting(input, normalWS);
// Get MToon Lighting
MToonInput mtoonInput;
mtoonInput.uv = uv;
mtoonInput.normalWS = normalWS;
mtoonInput.viewDirWS = input.viewDirWS;
mtoonInput.litColor = litColor.rgb;
mtoonInput.alpha = alpha;
mtoonInput.outlineFactor = input.outlineFactor;
const half4 col = GetMToonLighting(unityLighting, mtoonInput);
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
#endif

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 3717321cda361ad48abfc6b4fcb7a320
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,51 @@
#ifndef VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED
#define VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED
#include <UnityCG.cginc>
#include <AutoLight.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_attribute.hlsl"
#include "./vrmc_materials_mtoon_geometry_vertex.hlsl"
Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name.
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.uv = TRANSFORM_TEX(v.texcoord0, _MainTex);
output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS);
if (MToon_IsOutlinePass())
{
output.normalWS = UnityObjectToWorldNormal(-v.normalOS);
const VertexPositionInfo position = MToon_GetOutlineVertex(v.vertex.xyz, normalize(v.normalOS), output.uv);
output.pos = position.positionCS;
output.positionWS = position.positionWS;
output.outlineFactor = 1;
}
else
{
output.normalWS = UnityObjectToWorldNormal(v.normalOS);
const VertexPositionInfo position = MToon_GetVertex(v.vertex.xyz);
output.pos = position.positionCS;
output.positionWS = position.positionWS;
output.outlineFactor = 0;
}
#if defined(_NORMALMAP)
const half tangentSign = v.tangentOS.w * unity_WorldTransformParams.w;
output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign);
#endif
UNITY_TRANSFER_FOG(output, output.positionWS);
UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy);
return output;
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 26b921255f4e4d62a8c0ec730521831f
timeCreated: 1622458970

View File

@ -0,0 +1,25 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED
#include <UnityCG.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
inline float3 GetMToonGeometry_NormalWithoutNormalMap(const half3 normalWS)
{
return normalize(normalWS);
}
inline float3 GetMToonGeometry_Normal(const Varyings input, const float2 mtoonUv)
{
#if defined(_NORMALMAP)
// Get Normal in WorldSpace from Normalmap if available
const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, mtoonUv), _BumpScale));
return normalize(mul(normalTS, MToon_GetTangentToWorld(input.normalWS, input.tangentWS)));
#else
return GetMToonGeometry_NormalWithoutNormalMap(input.normalWS);
#endif
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5ddfcfc47ee84eed9365b902bf76ce75
timeCreated: 1622633324

View File

@ -0,0 +1,42 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED
#include <UnityCG.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
inline float GetMToonGeometry_Uv_Time(const float2 uvRaw)
{
if (MToon_IsParameterMapOn())
{
return UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y;
}
else
{
return _Time.y;
}
}
inline float2 GetMToonGeometry_Uv(const float2 geometryUv)
{
// get raw uv with _MainTex_ST
const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex);
if (MToon_IsParameterMapOn())
{
const float uvAnimationTime = GetMToonGeometry_Uv_Time(uvRaw);
const float2 translate = uvAnimationTime * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed);
const float rotateRad = uvAnimationTime * _UvAnimRotationSpeed * PI_2;
const float cosRotate = cos(rotateRad);
const float sinRotate = sin(rotateRad);
const float2 rotatePivot = float2(0.5, 0.5);
return mul(float2x2(cosRotate, -sinRotate, sinRotate, cosRotate), uvRaw + translate - rotatePivot) + rotatePivot;
}
else
{
return uvRaw;
}
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1381f11f525a4d90a57348ada020ea01
timeCreated: 1622451732

View File

@ -0,0 +1,78 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED
#include <UnityCG.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
struct VertexPositionInfo
{
float4 positionWS;
float4 positionCS;
};
inline half MToon_GetOutlineVertex_OutlineWidth(const float2 uv)
{
if (MToon_IsParameterMapOn())
{
return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0);
}
else
{
return _OutlineWidth;
}
}
inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const half3 normalOS, const float2 uv)
{
if (MToon_IsOutlineModeWorldCoordinates())
{
const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz;
const half outlineWidth = MToon_GetOutlineVertex_OutlineWidth(uv);
const half3 normalWS = UnityObjectToWorldNormal(normalOS);
VertexPositionInfo output;
output.positionWS = float4(positionWS + normalWS * outlineWidth, 1);
output.positionCS = UnityWorldToClipPos(output.positionWS);
return output;
}
else if (MToon_IsOutlineModeScreenCoordinates())
{
const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz;
const half outlineWidth = _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0);
const float4 nearUpperRight = mul(unity_CameraInvProjection, float4(1, 1, UNITY_NEAR_CLIP_VALUE, _ProjectionParams.y));
const half aspect = abs(nearUpperRight.y / nearUpperRight.x);
float4 positionCS = UnityObjectToClipPos(positionOS);
const half3 normalVS = MToon_GetObjectToViewNormal(normalOS);
const half3 normalCS = TransformViewToProjection(normalVS.xyz);
half2 normalProjectedCS = normalize(normalCS.xy);
normalProjectedCS *= positionCS.w;
normalProjectedCS.x *= aspect;
positionCS.xy += outlineWidth * normalProjectedCS.xy * saturate(1 - abs(normalVS.z)); // ignore offset when normal toward camera
VertexPositionInfo output;
output.positionWS = float4(positionWS, 1);
output.positionCS = positionCS;
return output;
}
else
{
VertexPositionInfo output;
output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1));
output.positionCS = UnityWorldToClipPos(output.positionWS);
return output;
}
}
inline VertexPositionInfo MToon_GetVertex(const float3 positionOS)
{
VertexPositionInfo output;
output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1));
output.positionCS = UnityWorldToClipPos(output.positionWS);
return output;
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 92e89e208e604f28acdce8a142d1e241
timeCreated: 1622805067

View File

@ -0,0 +1,50 @@
#ifndef VRMC_MATERIALS_MTOON_INPUT_INCLUDED
#define VRMC_MATERIALS_MTOON_INPUT_INCLUDED
#include <UnityShaderVariables.cginc>
// Textures
UNITY_DECLARE_TEX2D(_MainTex);
UNITY_DECLARE_TEX2D(_ShadeTex);
UNITY_DECLARE_TEX2D(_BumpMap);
UNITY_DECLARE_TEX2D(_ShadingShiftTex);
UNITY_DECLARE_TEX2D(_EmissionMap);
UNITY_DECLARE_TEX2D(_MatcapTex);
UNITY_DECLARE_TEX2D(_RimTex);
UNITY_DECLARE_TEX2D(_OutlineWidthTex);
UNITY_DECLARE_TEX2D(_UvAnimMaskTex);
CBUFFER_START(UnityPerMaterial)
// Vector
float4 _MainTex_ST;
// Colors
half4 _Color;
half4 _ShadeColor;
half4 _EmissionColor;
half4 _RimColor;
half4 _OutlineColor;
// Floats
half _Cutoff;
half _BumpScale;
half _ShadingShiftFactor;
half _ShadingShiftTexScale;
half _ShadingToonyFactor;
half _GiEqualization;
half _RimFresnelPower;
half _RimLift;
half _RimLightingMix;
half _OutlineWidth;
half _OutlineLightingMix;
half _UvAnimScrollXSpeed;
half _UvAnimScrollYSpeed;
half _UvAnimRotationSpeed;
CBUFFER_END
// No Using on shader
// half _AlphaMode;
// half _TransparentWithZWrite;
// half _RenderQueueOffset;
// half _DoubleSided;
// half _OutlineWidthMode;
#endif

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: c7f37d6d268db6f4da25955d0ca7cb01
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,130 @@
#ifndef VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED
#define VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED
#include <UnityShaderVariables.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_lighting_unity.hlsl"
struct MToonInput
{
float2 uv;
half3 normalWS;
half3 viewDirWS;
half3 litColor;
half alpha;
half outlineFactor;
};
inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input)
{
if (MToon_IsParameterMapOn())
{
return UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor;
}
else
{
return _ShadingShiftFactor;
}
}
inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MToonInput input)
{
const half dotNL = dot(input.normalWS, lighting.directLightDirection);
const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation);
const half shadingShift = GetMToonLighting_Reflectance_ShadingShift(input);
const half shadingToony = _ShadingToonyFactor;
return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift);
}
inline half3 GetMToonLighting_DirectLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance)
{
if (MToon_IsForwardBasePass())
{
const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb;
return lerp(shadeColor, input.litColor, reflectance) * unityLight.directLightColor;
}
else
{
return input.litColor * reflectance * unityLight.directLightColor * 0.5;
}
}
inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, const MToonInput input)
{
if (MToon_IsForwardBasePass())
{
return input.litColor * lerp(unityLight.indirectLight, unityLight.indirectLightEqualized, _GiEqualization);
}
else
{
return 0;
}
}
inline half3 GetMToonLighting_Emissive(const MToonInput input)
{
if (MToon_IsForwardBasePass() && MToon_IsEmissiveMapOn())
{
return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb;
}
else
{
return _EmissionColor.rgb;
}
}
inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input)
{
if (MToon_IsParameterMapOn())
{
const half3 worldUpWS = half3(0, 1, 0);
// TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0
const half3 matcapRightAxisWS = normalize(cross(input.viewDirWS, worldUpWS));
const half3 matcapUpAxisWS = normalize(cross(matcapRightAxisWS, input.viewDirWS));
const half2 matcapUv = float2(dot(matcapRightAxisWS, input.normalWS), dot(matcapUpAxisWS, input.normalWS)) * 0.5 + 0.5;
return UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb;
}
else
{
return 0;
}
}
inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting)
{
if (MToon_IsForwardBasePass())
{
const half3 parametricRimFactor = pow(saturate(1.0 - dot(input.normalWS, input.viewDirWS) + _RimLift), _RimFresnelPower) * _RimColor.rgb;
const half3 rimLightingFactor = lerp(half3(1, 1, 1), lighting, _RimLightingMix);
const half3 matcapFactor = GetMToonLighting_Rim_Matcap(input);
return (matcapFactor + parametricRimFactor) * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb * rimLightingFactor;
}
else
{
return 0;
}
}
half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input)
{
const half reflectance = GetMToonLighting_Reflectance(unityLight, input);
const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, reflectance);
const half3 indirect = GetMToonLighting_GlobalIllumination(unityLight, input);
const half3 lighting = direct + indirect;
const half3 emissive = GetMToonLighting_Emissive(input);
const half3 rim = GetMToonLighting_Rim(input, lighting);
const half3 baseCol = lighting + emissive + rim;
const half3 outlineCol = _OutlineColor.rgb * lerp(half3(1, 1, 1), baseCol, _OutlineLightingMix);
const half3 col = lerp(baseCol, outlineCol, input.outlineFactor);
return half4(col, input.alpha);
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 54d13d2ffd7249259772084874b76659
timeCreated: 1622449184

View File

@ -0,0 +1,49 @@
#ifndef VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED
#define VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED
#include <UnityCG.cginc>
#include <AutoLight.cginc>
#include <Lighting.cginc>
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_attribute.hlsl"
struct UnityLighting
{
half3 indirectLight;
half3 indirectLightEqualized;
half3 directLightColor;
half3 directLightDirection;
half3 directLightAttenuation;
};
UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS)
{
UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS);
const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS));
const half3 lightColor = _LightColor0.rgb;
if (MToon_IsForwardBasePass())
{
UnityLighting output;
output.indirectLight = ShadeSH9(half4(normalWS, 1));
output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5;
output.directLightColor = lightColor;
output.directLightDirection = lightDir;
output.directLightAttenuation = atten;
return output;
}
else
{
UnityLighting output;
output.indirectLight = 0;
output.indirectLightEqualized = 0;
output.directLightColor = lightColor;
output.directLightDirection = lightDir;
output.directLightAttenuation = atten;
return output;
}
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 08491980bb224017bbb8e432761db8c4
timeCreated: 1622450586

View File

@ -0,0 +1,47 @@
#ifndef VRMC_MATERIALS_MTOON_UTILITY_INCLUDED
#define VRMC_MATERIALS_MTOON_UTILITY_INCLUDED
#include <UnityCG.cginc>
// define
static const float PI_2 = 6.28318530718;
static const float EPS_COL = 0.00001;
inline half3 mtoon_linearstep(const half3 start, const half3 end, const half t)
{
return min(max((t - start) / (end - start), 0.0), 1.0);
}
inline bool MToon_IsPerspective()
{
return unity_OrthoParams.w != 1.0;
}
inline float3 MToon_GetWorldSpaceNormalizedViewDir(const float3 positionWS)
{
if (MToon_IsPerspective())
{
return normalize(_WorldSpaceCameraPos.xyz - positionWS);
}
else
{
return normalize(UNITY_MATRIX_V[2].xyz);
}
}
inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangentWS)
{
const half3 normalizedNormalWS = normalize(normalWS);
const half3 normalizedTangentWS = normalize(tangentWS.xyz);
const half3 normalizedBitangentWS = normalize(tangentWS.w * cross(normalizedNormalWS, normalizedTangentWS));
return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS);
}
inline half3 MToon_GetObjectToViewNormal(const half3 normalOS)
{
return normalize(mul((half3x3)UNITY_MATRIX_IT_MV, normalOS));
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 79a73f8bac4e45498341e9b6c1ea841e
timeCreated: 1622632833