From 800ed694367ef08aaef625357aa62ecc27ebf4c8 Mon Sep 17 00:00:00 2001 From: xLinka Date: Sun, 14 Jul 2024 21:55:08 +0100 Subject: [PATCH] MToon and Wireframe --- ProjectObsidian/Injection/ShaderInjection.cs | 7 +- ProjectObsidian/Materials/Wireframe.cs | 46 ++++++ ProjectObsidian/Materials/mtoon.cs | 156 +++++++++++++++++++ ProjectObsidian/Properties/AssemblyInfo.cs | 3 +- 4 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 ProjectObsidian/Materials/Wireframe.cs create mode 100644 ProjectObsidian/Materials/mtoon.cs diff --git a/ProjectObsidian/Injection/ShaderInjection.cs b/ProjectObsidian/Injection/ShaderInjection.cs index 1e61c57..4841cc1 100644 --- a/ProjectObsidian/Injection/ShaderInjection.cs +++ b/ProjectObsidian/Injection/ShaderInjection.cs @@ -10,14 +10,17 @@ namespace Obsidian.Shaders { internal class ShaderInjection { - + public static readonly Uri Mtoon = new("resdb:///f9db0509b5413ae1449ca912aedb660aac028d29415c74a7767daf4fafa4c764.unityshader"); + public static readonly Uri Wireframe = new("resdb:///83aaa44a3da87c14713c65108b742676ca78516c30f39e33d00945db6801559d.unityshader"); public static readonly Uri ObsidianTestShader = new("resdb:///65178a8353f8c164cd2c6dc5d07ad3f978157b023f52331b4a0ca2e34781162e.unityshader"); public static readonly Uri ParallaxOcclusion = new("resdb:///93ae78de262e31f8299661725899aac61d96071ac2ebea8b76a5c56febfc3feb.unityshader"); private static readonly List Shaders = new() { ObsidianTestShader, - ParallaxOcclusion + ParallaxOcclusion, + Wireframe, + Mtoon }; public static string ExtractSignature(Uri uri) { diff --git a/ProjectObsidian/Materials/Wireframe.cs b/ProjectObsidian/Materials/Wireframe.cs new file mode 100644 index 0000000..0fcc3a4 --- /dev/null +++ b/ProjectObsidian/Materials/Wireframe.cs @@ -0,0 +1,46 @@ +using System; +using Elements.Core; +using FrooxEngine; +using Obsidian.Shaders; + +[Category(new string[] { "Assets/Materials/Obsidian/Effects" })] +public class WireFrame : SingleShaderMaterialProvider +{ + protected override Uri ShaderURL => ShaderInjection.Wireframe; + + public readonly Sync WireframeColor; + public readonly Sync BackgroundColor; + + private static MaterialProperty _WireframeColor = new MaterialProperty("_WireframeColor"); + private static MaterialProperty _BackgroundColor = new MaterialProperty("_BackgroundColor"); + + [DefaultValue(-1)] + public readonly Sync RenderQueue; + private static PropertyState _propertyInitializationState; + + public override PropertyState PropertyInitializationState + { + get => _propertyInitializationState; + protected set => _propertyInitializationState = value; + } + + protected override void UpdateMaterial(Material material) + { + material.UpdateColor(_WireframeColor, WireframeColor); + material.UpdateColor(_BackgroundColor, BackgroundColor); + + if (!RenderQueue.GetWasChangedAndClear()) return; + var renderQueue = RenderQueue.Value; + if ((int)RenderQueue == -1) renderQueue = 3500; // Transparent+1000 + material.SetRenderQueue(renderQueue); + } + + protected override void UpdateKeywords(ShaderKeywords keywords) { } + + protected override void OnAttach() + { + base.OnAttach(); + WireframeColor.Value = new colorX(1, 1, 1, 1); // Default to white + BackgroundColor.Value = new colorX(0, 0, 0, 1); // Default to black + } +} diff --git a/ProjectObsidian/Materials/mtoon.cs b/ProjectObsidian/Materials/mtoon.cs new file mode 100644 index 0000000..9496490 --- /dev/null +++ b/ProjectObsidian/Materials/mtoon.cs @@ -0,0 +1,156 @@ +using System; +using Elements.Core; +using FrooxEngine; +using Obsidian.Shaders; + +[Category(new string[] { "Assets/Materials/Obsidian/Toon" })] +public class MToonMaterial : SingleShaderMaterialProvider +{ + protected override Uri ShaderURL => ShaderInjection.Mtoon; + + public readonly Sync AlphaCutoff; + public readonly Sync LitColorAlpha; + public readonly Sync ShadeColor; + public readonly AssetRef LitTextureAlpha; + public readonly AssetRef ShadeTexture; + public readonly Sync NormalScale; + public readonly AssetRef NormalTexture; + public readonly Sync ReceiveShadow; + public readonly AssetRef ReceiveShadowTexture; + public readonly Sync ShadingGrade; + public readonly AssetRef ShadingGradeTexture; + public readonly Sync ShadeShift; + public readonly Sync ShadeToony; + public readonly Sync LightColorAttenuation; + public readonly Sync IndirectLightIntensity; + public readonly Sync RimColor; + public readonly AssetRef RimTexture; + public readonly Sync RimLightingMix; + public readonly Sync RimFresnelPower; + public readonly Sync RimLift; + public readonly AssetRef SphereTextureAdd; + public readonly Sync EmissionColor; + public readonly AssetRef Emission; + public readonly AssetRef OutlineWidthTex; + public readonly Sync OutlineWidth; + public readonly Sync OutlineScaledMaxDistance; + public readonly Sync OutlineColor; + public readonly Sync OutlineLightingMix; + public readonly AssetRef UVAnimationMask; + public readonly Sync UVAnimationScrollX; + public readonly Sync UVAnimationScrollY; + public readonly Sync UVAnimationRotation; + + private static MaterialProperty _AlphaCutoff = new MaterialProperty("_Cutoff"); + private static MaterialProperty _LitColorAlpha = new MaterialProperty("_Color"); + private static MaterialProperty _ShadeColor = new MaterialProperty("_ShadeColor"); + private static MaterialProperty _LitTextureAlpha = new MaterialProperty("_MainTex"); + private static MaterialProperty _ShadeTexture = new MaterialProperty("_ShadeTexture"); + private static MaterialProperty _NormalScale = new MaterialProperty("_BumpScale"); + private static MaterialProperty _NormalTexture = new MaterialProperty("_BumpMap"); + private static MaterialProperty _ReceiveShadow = new MaterialProperty("_ReceiveShadowRate"); + private static MaterialProperty _ReceiveShadowTexture = new MaterialProperty("_ReceiveShadowTexture"); + private static MaterialProperty _ShadingGrade = new MaterialProperty("_ShadingGradeRate"); + private static MaterialProperty _ShadingGradeTexture = new MaterialProperty("_ShadingGradeTexture"); + private static MaterialProperty _ShadeShift = new MaterialProperty("_ShadeShift"); + private static MaterialProperty _ShadeToony = new MaterialProperty("_ShadeToony"); + private static MaterialProperty _LightColorAttenuation = new MaterialProperty("_LightColorAttenuation"); + private static MaterialProperty _IndirectLightIntensity = new MaterialProperty("_IndirectLightIntensity"); + private static MaterialProperty _RimColor = new MaterialProperty("_RimColor"); + private static MaterialProperty _RimTexture = new MaterialProperty("_RimTexture"); + private static MaterialProperty _RimLightingMix = new MaterialProperty("_RimLightingMix"); + private static MaterialProperty _RimFresnelPower = new MaterialProperty("_RimFresnelPower"); + private static MaterialProperty _RimLift = new MaterialProperty("_RimLift"); + private static MaterialProperty _SphereTextureAdd = new MaterialProperty("_SphereAdd"); + private static MaterialProperty _EmissionColor = new MaterialProperty("_EmissionColor"); + private static MaterialProperty _Emission = new MaterialProperty("_EmissionMap"); + private static MaterialProperty _OutlineWidthTex = new MaterialProperty("_OutlineWidthTexture"); + private static MaterialProperty _OutlineWidth = new MaterialProperty("_OutlineWidth"); + private static MaterialProperty _OutlineScaledMaxDistance = new MaterialProperty("_OutlineScaledMaxDistance"); + private static MaterialProperty _OutlineColor = new MaterialProperty("_OutlineColor"); + private static MaterialProperty _OutlineLightingMix = new MaterialProperty("_OutlineLightingMix"); + private static MaterialProperty _UVAnimationMask = new MaterialProperty("_UvAnimMaskTexture"); + private static MaterialProperty _UVAnimationScrollX = new MaterialProperty("_UvAnimScrollX"); + private static MaterialProperty _UVAnimationScrollY = new MaterialProperty("_UvAnimScrollY"); + private static MaterialProperty _UVAnimationRotation = new MaterialProperty("_UvAnimRotation"); + + [DefaultValue(-1)] + public readonly Sync RenderQueue; + private static PropertyState _propertyInitializationState; + + public override PropertyState PropertyInitializationState + { + get => _propertyInitializationState; + protected set => _propertyInitializationState = value; + } + + protected override void UpdateMaterial(Material material) + { + material.UpdateFloat(_AlphaCutoff, AlphaCutoff); + material.UpdateColor(_LitColorAlpha, LitColorAlpha); + material.UpdateColor(_ShadeColor, ShadeColor); + material.UpdateTexture(_LitTextureAlpha, LitTextureAlpha); + material.UpdateTexture(_ShadeTexture, ShadeTexture); + material.UpdateFloat(_NormalScale, NormalScale); + material.UpdateTexture(_NormalTexture, NormalTexture); + material.UpdateFloat(_ReceiveShadow, ReceiveShadow); + material.UpdateTexture(_ReceiveShadowTexture, ReceiveShadowTexture); + material.UpdateFloat(_ShadingGrade, ShadingGrade); + material.UpdateTexture(_ShadingGradeTexture, ShadingGradeTexture); + material.UpdateFloat(_ShadeShift, ShadeShift); + material.UpdateFloat(_ShadeToony, ShadeToony); + material.UpdateFloat(_LightColorAttenuation, LightColorAttenuation); + material.UpdateFloat(_IndirectLightIntensity, IndirectLightIntensity); + material.UpdateColor(_RimColor, RimColor); + material.UpdateTexture(_RimTexture, RimTexture); + material.UpdateFloat(_RimLightingMix, RimLightingMix); + material.UpdateFloat(_RimFresnelPower, RimFresnelPower); + material.UpdateFloat(_RimLift, RimLift); + material.UpdateTexture(_SphereTextureAdd, SphereTextureAdd); + material.UpdateColor(_EmissionColor, EmissionColor); + material.UpdateTexture(_Emission, Emission); + material.UpdateTexture(_OutlineWidthTex, OutlineWidthTex); + material.UpdateFloat(_OutlineWidth, OutlineWidth); + material.UpdateFloat(_OutlineScaledMaxDistance, OutlineScaledMaxDistance); + material.UpdateColor(_OutlineColor, OutlineColor); + material.UpdateFloat(_OutlineLightingMix, OutlineLightingMix); + material.UpdateTexture(_UVAnimationMask, UVAnimationMask); + material.UpdateFloat(_UVAnimationScrollX, UVAnimationScrollX); + material.UpdateFloat(_UVAnimationScrollY, UVAnimationScrollY); + material.UpdateFloat(_UVAnimationRotation, UVAnimationRotation); + + if (!RenderQueue.GetWasChangedAndClear()) return; + var renderQueue = RenderQueue.Value; + if ((int)RenderQueue == -1) renderQueue = 2000; + material.SetRenderQueue(renderQueue); + } + + protected override void UpdateKeywords(ShaderKeywords keywords) { } + + protected override void OnAttach() + { + base.OnAttach(); + AlphaCutoff.Value = 0.5f; + LitColorAlpha.Value = new colorX(1, 1, 1, 1); + ShadeColor.Value = new colorX(0.97f, 0.81f, 0.86f, 1); + NormalScale.Value = 1.0f; + ReceiveShadow.Value = 1; + ShadingGrade.Value = 1; + ShadeShift.Value = 0; + ShadeToony.Value = 0.9f; + LightColorAttenuation.Value = 0; + IndirectLightIntensity.Value = 0.1f; + RimColor.Value = new colorX(0, 0, 0); + RimLightingMix.Value = 0; + RimFresnelPower.Value = 1; + RimLift.Value = 0; + EmissionColor.Value = new colorX(0, 0, 0); + OutlineWidth.Value = 0.5f; + OutlineScaledMaxDistance.Value = 1; + OutlineColor.Value = new colorX(0, 0, 0, 1); + OutlineLightingMix.Value = 1; + UVAnimationScrollX.Value = 0; + UVAnimationScrollY.Value = 0; + UVAnimationRotation.Value = 0; + } +} diff --git a/ProjectObsidian/Properties/AssemblyInfo.cs b/ProjectObsidian/Properties/AssemblyInfo.cs index 251da04..43a28ed 100644 --- a/ProjectObsidian/Properties/AssemblyInfo.cs +++ b/ProjectObsidian/Properties/AssemblyInfo.cs @@ -9,6 +9,7 @@ //------------------------------------------------------------------------------ using System; +using System.ComponentModel; using System.Reflection; using Elements.Core; @@ -25,4 +26,4 @@ // Generated by the MSBuild WriteCodeFragment class. //Mark as DataModelAssembly for the Plugin loading system to load this assembly -[assembly: DataModelAssembly(DataModelAssemblyType.Core)] \ No newline at end of file +[assembly: DataModelAssembly(DataModelAssemblyType.Core)]