Merge pull request #2054 from notargs/mtoon_urp

MToonをURPに対応
This commit is contained in:
Masataka SUMI
2023-05-09 15:49:12 +09:00
committed by GitHub
29 changed files with 779 additions and 14 deletions

View File

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

View File

@@ -0,0 +1,49 @@
using System;
using System.Linq;
using UniGLTF;
using UnityEngine;
using VRMShaders;
using VRMShaders.VRM10.MToon10.Runtime;
namespace UniVRM10
{
/// <summary>
/// Convert MToon parameters from glTF specification to Unity implementation.(for URP)
/// </summary>
public static class UrpVrm10MToonMaterialImporter
{
/// <summary>
/// VMRC_materials_mtoon の場合にマテリアル生成情報を作成する
/// </summary>
public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
{
var m = data.GLTF.materials[i];
if (!UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(m.extensions, out var mtoon))
{
// Fallback to glTF, when MToon extension does not exist.
matDesc = default;
return false;
}
// use material.name, because material name may renamed in GltfParser.
matDesc = new MaterialDescriptor(
m.name,
Shader.Find(MToon10Meta.URPUnityShaderName),
null,
Vrm10MToonTextureImporter.EnumerateAllTextures(data, m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.Item2.Item2),
BuiltInVrm10MToonMaterialImporter.TryGetAllFloats(m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.value),
BuiltInVrm10MToonMaterialImporter.TryGetAllColors(m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.value),
BuiltInVrm10MToonMaterialImporter.TryGetAllFloatArrays(m, mtoon).ToDictionary(tuple => tuple.key, tuple => tuple.value),
new Action<Material>[]
{
material =>
{
// Set hidden properties, keywords from float properties.
new MToonValidator(material).Validate();
}
});
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 57d820ccc1ff7e2469f7fb5cc435bd5b
timeCreated: 1620630516

View File

@@ -10,8 +10,10 @@ namespace UniVRM10
{
public MaterialDescriptor Get(GltfData data, int i)
{
// mtoon
if (UrpVrm10MToonMaterialImporter.TryCreateParam(data, i, out var matDesc)) return matDesc;
// unlit
if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out MaterialDescriptor matDesc)) return matDesc;
if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
// pbr
if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;

View File

@@ -32,7 +32,7 @@ MonoBehaviour:
m_MainLightShadowmapResolution: 2048
m_AdditionalLightsRenderingMode: 1
m_AdditionalLightsPerObjectLimit: 4
m_AdditionalLightShadowsSupported: 0
m_AdditionalLightShadowsSupported: 1
m_AdditionalLightsShadowmapResolution: 512
m_ShadowDistance: 50
m_ShadowCascadeCount: 4

View File

@@ -12,8 +12,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: de640fe3d0db1804a85f9fc8f5cadab6, type: 3}
m_Name: UniversalRenderPipelineAsset_Renderer
m_EditorClassIdentifier:
m_RendererFeatures: []
m_RendererFeatureMap:
m_RendererFeatures:
- {fileID: 6807519784255233043}
m_RendererFeatureMap: 131cfa0d2d2b795e
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
xrSystemData: {fileID: 11400000, guid: 60e1133243b97e347b653163a8c01b64, type: 2}
shaders:
@@ -43,3 +44,16 @@ MonoBehaviour:
m_ShadowTransparentReceive: 1
m_RenderingMode: 0
m_AccurateGbufferNormals: 0
--- !u!114 &6807519784255233043
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5b6d00cd3f0249a2b4ee7dc86f9a8d63, type: 3}
m_Name: MToonOutlineRenderFeature
m_EditorClassIdentifier:
m_Active: 1

View File

@@ -1,8 +1,12 @@
#ifndef VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED
#define VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#include <AutoLight.cginc>
#endif
struct Attributes
{
@@ -25,8 +29,21 @@ struct Varyings
half4 tangentWS : TEXCOORD3;
#endif
float3 viewDirWS : TEXCOORD4;
#ifdef MTOON_URP
half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD6;
#endif
DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 7);
#else
UNITY_FOG_COORDS(5)
UNITY_LIGHTING_COORDS(6,7)
#endif
float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS"
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO

View File

@@ -12,7 +12,11 @@
#endif
#ifndef MTOON_FRONT_FACE_TYPE
// from https://docs.unity3d.com/Manual/SL-ShaderSemantics.html
#define MTOON_FRONT_FACE_TYPE fixed
#ifdef MTOON_URP
#define MTOON_FRONT_FACE_TYPE float
#else
#define MTOON_FRONT_FACE_TYPE fixed
#endif
#endif
#ifndef MTOON_IS_FRONT_VFACE
#define MTOON_IS_FRONT_VFACE(VAL, FRONT, BACK) ((VAL > 0.0) ? (FRONT) : (BACK))
@@ -28,7 +32,9 @@ inline bool MToon_IsPbrCorrectOn()
// Compile-time constant
inline bool MToon_IsForwardBasePass()
{
#if defined(UNITY_PASS_FORWARDBASE)
#if defined(MTOON_URP)
return true;
#elif defined(UNITY_PASS_FORWARDBASE)
return true;
#elif defined(UNITY_PASS_FORWARDADD)
return false;

View File

@@ -1,7 +1,12 @@
#ifndef VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED
#define VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -26,7 +31,11 @@ half4 MToonFragment(const FragmentInput fragmentInput) : SV_Target
const float2 uv = GetMToonGeometry_Uv(input.uv);
// Get LitColor with Alpha
#ifdef MTOON_URP
const half4 litColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv) * _Color;
#else
const half4 litColor = UNITY_SAMPLE_TEX2D(_MainTex, uv) * _Color;
#endif
// Alpha Test
const half alpha = GetMToonGeometry_Alpha(litColor);
@@ -46,8 +55,22 @@ half4 MToonFragment(const FragmentInput fragmentInput) : SV_Target
mtoonInput.alpha = alpha;
half4 col = GetMToonLighting(unityLighting, mtoonInput);
#if defined(MTOON_URP) && defined(_ADDITIONAL_LIGHTS) && !defined(MTOON_PASS_OUTLINE)
uint pixelLightCount = GetAdditionalLightsCount();
for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex)
{
UnityLighting additionalUnityLighting = GetAdditionalUnityLighting(input, normalWS, lightIndex);
col.rgb += GetMToonURPAdditionalLighting(additionalUnityLighting, mtoonInput).rgb;
}
#endif
// Apply Fog
#ifdef MTOON_URP
float fogCoord = input.fogFactorAndVertexLight.x;
col.rgb = MixFog(col.rgb, fogCoord);
#else
UNITY_APPLY_FOG(fragmentInput.varyings.fogCoord, col);
#endif
return col;
}

View File

@@ -1,8 +1,14 @@
#ifndef VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED
#define VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#else
#include <UnityCG.cginc>
#include <AutoLight.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -23,26 +29,58 @@ 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.positionWS = position.positionWS.xyz;
#ifdef MTOON_URP
output.normalWS = TransformObjectToWorldNormal(-v.normalOS);
#else
output.normalWS = UnityObjectToWorldNormal(-v.normalOS);
#endif
}
else
{
const VertexPositionInfo position = MToon_GetVertex(v.vertex.xyz);
output.pos = position.positionCS;
output.positionWS = position.positionWS;
output.positionWS = position.positionWS.xyz;
#ifdef MTOON_URP
output.normalWS = TransformObjectToWorldNormal(v.normalOS);
#else
output.normalWS = UnityObjectToWorldNormal(v.normalOS);
#endif
}
output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS);
#if defined(_NORMALMAP)
const half tangentSign = v.tangentOS.w * unity_WorldTransformParams.w;
#ifdef MTOON_URP
output.tangentWS = half4(TransformObjectToWorldDir(v.tangentOS), tangentSign);
#else
output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign);
#endif
#endif
#ifdef MTOON_URP
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
OUTPUT_LIGHTMAP_UV(input.texcoord1, unity_LightmapST, output.lightmapUV);
OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
half3 vertexLight = VertexLighting(output.positionWS, output.normalWS);
half fogFactor = ComputeFogFactor(output.pos.z);
output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
output.shadowCoord = GetShadowCoord(vertexInput);
#endif
#else
UNITY_TRANSFER_FOG(output, output.pos);
UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy);
#endif
return output;
}

View File

@@ -1,7 +1,12 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -16,8 +21,14 @@ inline float3 GetMToonGeometry_Normal(const Varyings input, const MTOON_FRONT_FA
const half3 normalWS = MTOON_IS_FRONT_VFACE(facing, input.normalWS, -input.normalWS);
#if defined(_NORMALMAP)
// Get Normal in WorldSpace from Normalmap if available
#ifdef MTOON_URP
const half3 normalTS = normalize(UnpackNormalScale(SAMPLE_TEXTURE2D(_BumpMap, sampler_MainTex, mtoonUv), _BumpScale));
#else
const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, mtoonUv), _BumpScale));
#endif
return normalize(mul(normalTS, MToon_GetTangentToWorld(normalWS, input.tangentWS)));
#else
return GetMToonGeometry_NormalWithoutNormalMap(normalWS);

View File

@@ -1,7 +1,12 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -10,7 +15,11 @@ inline float GetMToonGeometry_Uv_Time(const float2 uvRaw)
{
if (MToon_IsParameterMapOn())
{
#ifdef MTOON_URP
return SAMPLE_TEXTURE2D(_UvAnimMaskTex, sampler_UvAnimMaskTex, uvRaw);
#else
return UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y;
#endif
}
else
{

View File

@@ -1,7 +1,13 @@
#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED
#define VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -16,7 +22,7 @@ inline half MToon_GetOutlineVertex_OutlineWidth(const float2 uv)
{
if (MToon_IsParameterMapOn())
{
return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0);
return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0).x;
}
else
{
@@ -49,12 +55,25 @@ inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const
if (MToon_IsOutlineModeWorldCoordinates())
{
const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz;
#ifdef MTOON_URP
const half3 normalWS = TransformObjectToWorldNormal(normalOS);
#else
const half3 normalWS = UnityObjectToWorldNormal(normalOS);
#endif
const half outlineWidth = MToon_GetOutlineVertex_OutlineWidth(uv);
VertexPositionInfo output;
output.positionWS = float4(positionWS + normalWS * outlineWidth, 1);
#ifdef MTOON_URP
output.positionCS = TransformWorldToHClip(output.positionWS.xyz);
#else
output.positionCS = UnityWorldToClipPos(output.positionWS);
#endif
return output;
}
else if (MToon_IsOutlineModeScreenCoordinates())
@@ -65,9 +84,20 @@ inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const
const float4 nearUpperRight = mul(unity_CameraInvProjection, float4(1, 1, UNITY_NEAR_CLIP_VALUE, _ProjectionParams.y));
const half aspect = abs(nearUpperRight.y / nearUpperRight.x);
#ifdef MTOON_URP
const float4 positionCS = TransformObjectToHClip(positionOS);
#else
const float4 positionCS = UnityObjectToClipPos(positionOS);
#endif
const half3 normalVS = MToon_GetObjectToViewNormal(normalOS);
#ifdef MTOON_URP
const half3 normalWS = TransformObjectToWorldNormal(normalOS.xyz);
const half3 normalCS = TransformWorldToHClipDir(normalWS.xyz);
#else
const half3 normalCS = TransformViewToProjection(normalVS.xyz);
#endif
half2 normalProjectedCS = normalize(normalCS.xy);
const float clipSpaceHeight = 2.0f;
@@ -85,7 +115,13 @@ inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const
{
VertexPositionInfo output;
output.positionWS = mul(unity_ObjectToWorld, float4(positionOS * 0.001, 1));
#ifdef MTOON_URP
output.positionCS = TransformWorldToHClip(output.positionWS.xyz);
#else
output.positionCS = UnityWorldToClipPos(output.positionWS);
#endif
return output;
}
}
@@ -94,7 +130,14 @@ inline VertexPositionInfo MToon_GetVertex(const float3 positionOS)
{
VertexPositionInfo output;
output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1));
#ifdef MTOON_URP
output.positionCS = TransformWorldToHClip(output.positionWS.xyz);
#else
output.positionCS = UnityWorldToClipPos(output.positionWS);
#endif
return output;
}

View File

@@ -1,9 +1,25 @@
#ifndef VRMC_MATERIALS_MTOON_INPUT_INCLUDED
#define VRMC_MATERIALS_MTOON_INPUT_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityShaderVariables.cginc>
#endif
// Textures
#ifdef MTOON_URP
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
TEXTURE2D(_ShadeTex); SAMPLER(sampler_ShadeTex);
TEXTURE2D(_BumpMap); SAMPLER(sampler_BumpMap);
TEXTURE2D(_ShadingShiftTex); SAMPLER(sampler_ShadingShiftTex);
TEXTURE2D(_EmissionMap); SAMPLER(sampler_EmissionMap);
TEXTURE2D(_MatcapTex); SAMPLER(sampler_MatcapTex);
TEXTURE2D(_RimTex); SAMPLER(sampler_RimTex);
TEXTURE2D(_OutlineWidthTex); SAMPLER(sampler_OutlineWidthTex);
// NOTE: "tex2d() * _Time.y" returns mediump value if sampler is half precision in Android VR platform
TEXTURE2D_FLOAT(_UvAnimMaskTex); SAMPLER(sampler_UvAnimMaskTex);
#else
UNITY_DECLARE_TEX2D(_MainTex);
UNITY_DECLARE_TEX2D(_ShadeTex);
UNITY_DECLARE_TEX2D(_BumpMap);
@@ -14,6 +30,7 @@ UNITY_DECLARE_TEX2D(_RimTex);
UNITY_DECLARE_TEX2D(_OutlineWidthTex);
// NOTE: "tex2d() * _Time.y" returns mediump value if sampler is half precision in Android VR platform
UNITY_DECLARE_TEX2D_FLOAT(_UvAnimMaskTex);
#endif
CBUFFER_START(UnityPerMaterial)
// Vector

View File

@@ -1,7 +1,12 @@
#ifndef VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED
#define VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityShaderVariables.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_utility.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
@@ -20,7 +25,11 @@ inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input)
{
if (MToon_IsParameterMapOn())
{
#ifdef MTOON_URP
return SAMPLE_TEXTURE2D(_ShadingShiftTex, sampler_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor;
#else
return UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor;
#endif
}
else
{
@@ -47,7 +56,7 @@ inline half GetMToonLighting_Shade(const UnityLighting lighting, const MToonInpu
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;
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift) * lighting.directLightAttenuation.x;
}
else
{
@@ -60,7 +69,7 @@ inline half GetMToonLighting_Shadow(const UnityLighting lighting, const half dot
{
if (MToon_IsPbrCorrectOn())
{
return lighting.directLightAttenuation * step(0, dotNL);
return lighting.directLightAttenuation.x * step(0, dotNL);
}
if (MToon_IsForwardBasePass())
@@ -72,13 +81,17 @@ inline half GetMToonLighting_Shadow(const UnityLighting lighting, const half dot
// heuristic term for weak lights.
// 0.5: heuristic.
// min(0, dotNL) + 1: darken if (dotNL < 0) by using half lambert.
return lighting.directLightAttenuation * 0.5 * (min(0, dotNL) + 1);
return lighting.directLightAttenuation.x * 0.5 * (min(0, dotNL) + 1);
}
}
inline half3 GetMToonLighting_DirectLighting(const UnityLighting unityLight, const MToonInput input, const half shade, const half shadow)
{
#ifdef MTOON_URP
const half3 shadeColor = SAMPLE_TEXTURE2D(_ShadeTex, sampler_ShadeTex, input.uv).rgb * _ShadeColor.rgb;
#else
const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb;
#endif
const half3 albedo = lerp(shadeColor, input.litColor, shade);
return albedo * unityLight.directLightColor * shadow;
@@ -102,7 +115,11 @@ inline half3 GetMToonLighting_Emissive(const MToonInput input)
{
if (MToon_IsEmissiveMapOn())
{
#ifdef MTOON_URP
return SAMPLE_TEXTURE2D(_EmissionMap, sampler_EmissionMap, input.uv).rgb * _EmissionColor.rgb;
#else
return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb;
#endif
}
else
{
@@ -124,7 +141,12 @@ inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input)
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;
#ifdef MTOON_URP
return _MatcapColor.rgb * SAMPLE_TEXTURE2D(_MatcapTex, sampler_MatcapTex, matcapUv).rgb;
#else
return _MatcapColor.rgb * UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb;
#endif
}
else
{
@@ -152,7 +174,11 @@ inline half3 GetMToonLighting_Rim(const UnityLighting unityLight, const MToonInp
if (MToon_IsRimMapOn())
{
#ifdef MTOON_URP
return (matcapFactor + parametricRimFactor) * rimLightingFactor * SAMPLE_TEXTURE2D(_RimTex, sampler_RimTex, input.uv).rgb;
#else
return (matcapFactor + parametricRimFactor) * rimLightingFactor * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb;
#endif
}
else
{
@@ -185,4 +211,48 @@ half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input)
}
}
#ifdef MTOON_URP
inline half GetMToonURPAdditionalLighting_Shadow(const UnityLighting lighting, const half dotNL)
{
if (MToon_IsPbrCorrectOn())
{
return lighting.directLightAttenuation.x * step(0, dotNL);
}
return lighting.directLightAttenuation.x * 0.5 * (min(0, dotNL) + 1);
}
inline half GetMToonURPAdditionalLighting_Shade(const UnityLighting lighting, const MToonInput input, const half dotNL)
{
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);
}
const half shadeInput = dotNL;
return mtoon_linearstep(-1.0 + shadeToony, +1.0 - shadeToony, shadeInput + shadeShift);
}
half4 GetMToonURPAdditionalLighting(const UnityLighting unityLight, const MToonInput input)
{
const half dotNL = GetMToonLighting_DotNL(unityLight, input);
const half shade = GetMToonURPAdditionalLighting_Shade(unityLight, input, dotNL);
const half shadow = GetMToonURPAdditionalLighting_Shadow(unityLight, dotNL);
const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, shade, shadow);
const half3 lighting = direct;
const half3 baseCol = lighting;
return half4(baseCol, input.alpha);
}
#endif
#endif

View File

@@ -1,9 +1,15 @@
#ifndef VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED
#define VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#else
#include <UnityCG.cginc>
#include <AutoLight.cginc>
#include <Lighting.cginc>
#endif
#include "./vrmc_materials_mtoon_define.hlsl"
#include "./vrmc_materials_mtoon_input.hlsl"
#include "./vrmc_materials_mtoon_attribute.hlsl"
@@ -19,16 +25,41 @@ struct UnityLighting
UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS)
{
#ifdef MTOON_URP
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
float4 shadowCoord = TransformWorldToShadowCoord(input.positionWS);
#else
float4 shadowCoord = float4(0, 0, 0, 0);
#endif
half4 shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
Light mainLight = GetMainLight(shadowCoord, input.positionWS, shadowMask);
const half3 lightDir = mainLight.direction;
const half3 lightColor = mainLight.color.rgb;
float atten = mainLight.shadowAttenuation;
#else
UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS);
const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS));
const half3 lightColor = _LightColor0.rgb;
#endif
if (MToon_IsForwardBasePass())
{
UnityLighting output;
#ifdef MTOON_URP
output.indirectLight = SampleSH(normalWS);
output.indirectLightEqualized = (SampleSH(half3(0, 1, 0)) + SampleSH(half3(0, -1, 0))) * 0.5;
#else
output.indirectLight = ShadeSH9(half4(normalWS, 1));
output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5;
#endif
output.directLightColor = lightColor;
output.directLightDirection = lightDir;
output.directLightAttenuation = atten;
@@ -46,4 +77,29 @@ UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS)
}
}
#ifdef MTOON_URP
UnityLighting GetAdditionalUnityLighting(const Varyings input, const half3 normalWS, int lightIndex)
{
// TODO: Duplicate in GetUnityLighting
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord = input.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
float4 shadowCoord = TransformWorldToShadowCoord(input.positionWS);
#else
float4 shadowCoord = float4(0, 0, 0, 0);
#endif
half4 shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
Light light = GetAdditionalLight(lightIndex, input.positionWS, shadowMask);
UnityLighting output;
output.indirectLight = 0;
output.indirectLightEqualized = 0;
output.directLightColor = light.color;
output.directLightDirection = light.direction;
output.directLightAttenuation = light.shadowAttenuation * light.distanceAttenuation;
return output;
}
#endif
#endif

View File

@@ -0,0 +1,34 @@
#ifndef VRMC_MATERIALS_MTOON_SHADOWCASTER_FRAGMENT_INCLUDED
#define VRMC_MATERIALS_MTOON_SHADOWCASTER_FRAGMENT_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#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_alpha.hlsl"
#include "./vrmc_materials_mtoon_geometry_normal.hlsl"
#include "./vrmc_materials_mtoon_lighting_unity.hlsl"
#include "./vrmc_materials_mtoon_lighting_mtoon.hlsl"
half4 MToonShadowCasterFragment(const FragmentInput fragmentInput) : SV_Target
{
const Varyings input = fragmentInput.varyings;
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Get MToon UV (with UVAnimation)
const float2 uv = GetMToonGeometry_Uv(input.uv);
// Get LitColor with Alpha
const half4 litColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv) * _Color;
GetMToonGeometry_Alpha(litColor);
return 0;
}
#endif
#endif

View File

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

View File

@@ -0,0 +1,54 @@
#ifndef VRMC_MATERIALS_MTOON_SHADOWCASTER_VERTEX_INCLUDED
#define VRMC_MATERIALS_MTOON_SHADOWCASTER_VERTEX_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#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"
float3 _LightDirection;
VertexPositionInfo MToon_GetShadowCasterVertex(const float3 positionOS, const float3 normalWS)
{
VertexPositionInfo output;
output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1));
float4 positionCS = TransformWorldToHClip(ApplyShadowBias(output.positionWS, normalWS, _LightDirection));
#if UNITY_REVERSED_Z
positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
#else
positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
#endif
output.positionCS = positionCS;
return output;
}
Varyings MToonShadowCasterVertex(const Attributes v)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float3 normalWS = TransformObjectToWorldNormal(v.normalOS);
const VertexPositionInfo position = MToon_GetShadowCasterVertex(v.vertex.xyz, normalWS);
output.pos = position.positionCS;
output.positionWS = position.positionWS.xyz;
output.normalWS = TransformObjectToWorldNormal(normalWS);
output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS);
return output;
}
#endif
#endif

View File

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

View File

@@ -0,0 +1,198 @@
Shader "VRM10/Universal Render Pipeline/MToon10"
{
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.05
_ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R
_ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1
_ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.95
// GI
_GiEqualization ("mtoon.giEqualizationFactor", Range(0, 1)) = 0.9
// Emission
[HDR] _EmissionColor ("emissiveFactor", Color) = (0, 0, 0, 1) // Unity specified name
_EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name
// Rim Lighting
_MatcapColor ("mtoon.matcapFactor", Color) = (0, 0, 0, 1)
_MatcapTex ("mtoon.matcapTexture", 2D) = "black" {}
_RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0, 1)
_RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Range(0, 100)) = 5.0
_RimLift ("mtoon.parametricRimLiftFactor", Range(0, 1)) = 0
_RimTex ("mtoon.rimMultiplyTexture", 2D) = "white" {}
_RimLightingMix ("mtoon.rimLightingMixFactor", Range(0, 1)) = 1
// Outline
_OutlineWidthMode ("mtoon.outlineWidthMode", Int) = 0
[PowerSlider(2.2)] _OutlineWidth ("mtoon.outlineWidthFactor", Range(0, 0.05)) = 0
_OutlineWidthTex ("mtoon.outlineWidthMultiplyTexture", 2D) = "white" {} // channel G
_OutlineColor ("mtoon.outlineColorFactor", Color) = (0, 0, 0, 1)
_OutlineLightingMix ("mtoon.outlineLightingMixFactor", Range(0, 1)) = 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
// for Editor
_M_EditMode ("_EditMode", Float) = 0.0
}
// Shader Model 3.0
SubShader
{
Tags
{
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalPipeline"
"UniversalMaterialType" = "Lit"
"IgnoreProjector" = "True"
}
// Built-in Forward Base Pass
Pass
{
Name "UniversalForward"
Tags { "LightMode" = "UniversalForward" }
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 __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile __ _NORMALMAP
#pragma multi_compile __ _MTOON_EMISSIVEMAP
#pragma multi_compile __ _MTOON_RIMMAP
#pragma multi_compile __ _MTOON_PARAMETERMAP
// -------------------------------------
// Universal Pipeline keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT
#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
#pragma multi_compile _ SHADOWS_SHADOWMASK
#pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
#pragma vertex MToonVertex
#pragma fragment MToonFragment
#define MTOON_URP
#include "./vrmc_materials_mtoon_forward_vertex.hlsl"
#include "./vrmc_materials_mtoon_forward_fragment.hlsl"
ENDHLSL
}
// Built-in Forward Base Pass
Pass
{
Name "MToonOutline"
Tags { "LightMode" = "MToonOutline" }
Cull Front
Blend [_M_SrcBlend] [_M_DstBlend]
ZWrite [_M_ZWrite]
ZTest LEqual
Offset 1, 1
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 __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile __ _NORMALMAP
#pragma multi_compile __ _MTOON_EMISSIVEMAP
#pragma multi_compile __ _MTOON_RIMMAP
#pragma multi_compile __ _MTOON_PARAMETERMAP
#pragma multi_compile __ _MTOON_OUTLINE_WORLD _MTOON_OUTLINE_SCREEN
#pragma vertex MToonVertex
#pragma fragment MToonFragment
#define MTOON_URP
#define MTOON_PASS_OUTLINE
#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
HLSLPROGRAM
#pragma target 3.0
// Unity defined keywords
#pragma multi_compile_shadowcaster nolightmap nodynlightmap nodirlightmap novertexlight
#pragma multi_compile_instancing
#pragma multi_compile __ _ALPHATEST_ON _ALPHABLEND_ON
#pragma vertex MToonShadowCasterVertex
#pragma fragment MToonShadowCasterFragment
#define MTOON_URP
#include "./vrmc_materials_mtoon_shadowcaster_vertex.hlsl"
#include "./vrmc_materials_mtoon_shadowcaster_fragment.hlsl"
ENDHLSL
}
}
FallBack "Hidden/Universal Render Pipeline/FallbackError"
CustomEditor "VRMShaders.VRM10.MToon10.Editor.MToonInspector"
}

View File

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

View File

@@ -1,7 +1,11 @@
#ifndef VRMC_MATERIALS_MTOON_UTILITY_INCLUDED
#define VRMC_MATERIALS_MTOON_UTILITY_INCLUDED
#ifdef MTOON_URP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#else
#include <UnityCG.cginc>
#endif
// define
static const float PI_2 = 6.28318530718;

View File

@@ -0,0 +1,24 @@
#if MTOON_URP
using UnityEngine.Rendering.Universal;
namespace VRMShaders.VRM10.MToon10.Runtime
{
public sealed class MToonOutlineRenderFeature : ScriptableRendererFeature
{
private MToonOutlineRenderPass _pass;
public override void Create()
{
_pass = new MToonOutlineRenderPass
{
renderPassEvent = RenderPassEvent.AfterRenderingOpaques
};
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(_pass);
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5b6d00cd3f0249a2b4ee7dc86f9a8d63
timeCreated: 1682661280

View File

@@ -0,0 +1,41 @@
#if MTOON_URP
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace VRMShaders.VRM10.MToon10.Runtime
{
public sealed class MToonOutlineRenderPass : ScriptableRenderPass
{
private const string ProfilerTag = nameof(MToonOutlineRenderPass);
private readonly ProfilingSampler _profilingSampler = new ProfilingSampler(ProfilerTag);
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, _profilingSampler))
{
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
var camera = renderingData.cameraData.camera;
var shaderTagId = new ShaderTagId("MToonOutline");
var sortingSettings = new SortingSettings(camera);
var drawingSettings = new DrawingSettings(shaderTagId, sortingSettings)
{
perObjectData = PerObjectData.ReflectionProbes | PerObjectData.Lightmaps |
PerObjectData.LightProbe | PerObjectData.LightData | PerObjectData.OcclusionProbe |
PerObjectData.ShadowMask
};
var filteringSettings = FilteringSettings.defaultValue;
var renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings,
ref renderStateBlock);
}
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
CommandBufferPool.Release(cmd);
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df162971c1034acdbe28ff1ec94d8601
timeCreated: 1682665616

View File

@@ -3,5 +3,6 @@
public static class MToon10Meta
{
public static readonly string UnityShaderName = "VRM10/MToon10";
public static readonly string URPUnityShaderName = "VRM10/Universal Render Pipeline/MToon10";
}
}

View File

@@ -1,6 +1,10 @@
{
"name": "VRMShaders.VRM10.MToon10.Runtime",
"references": [],
"rootNamespace": "",
"references": [
"GUID:15fc0a57446b3144c949da3e2b9737a9",
"GUID:df380645f10b7bc4b97d4f5eb6303d95"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
@@ -8,6 +12,12 @@
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "com.unity.render-pipelines.core",
"expression": "",
"define": "MTOON_URP"
}
],
"noEngineReferences": false
}