Skip to content

Commit

Permalink
MToon and Wireframe
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed Jul 14, 2024
1 parent 11213ad commit 800ed69
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 3 deletions.
7 changes: 5 additions & 2 deletions ProjectObsidian/Injection/ShaderInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uri> Shaders = new()
{
ObsidianTestShader,
ParallaxOcclusion
ParallaxOcclusion,
Wireframe,
Mtoon
};
public static string ExtractSignature(Uri uri)
{
Expand Down
46 changes: 46 additions & 0 deletions ProjectObsidian/Materials/Wireframe.cs
Original file line number Diff line number Diff line change
@@ -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<colorX> WireframeColor;
public readonly Sync<colorX> BackgroundColor;

private static MaterialProperty _WireframeColor = new MaterialProperty("_WireframeColor");
private static MaterialProperty _BackgroundColor = new MaterialProperty("_BackgroundColor");

[DefaultValue(-1)]
public readonly Sync<int> 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
}
}
156 changes: 156 additions & 0 deletions ProjectObsidian/Materials/mtoon.cs
Original file line number Diff line number Diff line change
@@ -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<float> AlphaCutoff;
public readonly Sync<colorX> LitColorAlpha;
public readonly Sync<colorX> ShadeColor;
public readonly AssetRef<ITexture2D> LitTextureAlpha;
public readonly AssetRef<ITexture2D> ShadeTexture;
public readonly Sync<float> NormalScale;
public readonly AssetRef<ITexture2D> NormalTexture;
public readonly Sync<float> ReceiveShadow;
public readonly AssetRef<ITexture2D> ReceiveShadowTexture;
public readonly Sync<float> ShadingGrade;
public readonly AssetRef<ITexture2D> ShadingGradeTexture;
public readonly Sync<float> ShadeShift;
public readonly Sync<float> ShadeToony;
public readonly Sync<float> LightColorAttenuation;
public readonly Sync<float> IndirectLightIntensity;
public readonly Sync<colorX> RimColor;
public readonly AssetRef<ITexture2D> RimTexture;
public readonly Sync<float> RimLightingMix;
public readonly Sync<float> RimFresnelPower;
public readonly Sync<float> RimLift;
public readonly AssetRef<ITexture2D> SphereTextureAdd;
public readonly Sync<colorX> EmissionColor;
public readonly AssetRef<ITexture2D> Emission;
public readonly AssetRef<ITexture2D> OutlineWidthTex;
public readonly Sync<float> OutlineWidth;
public readonly Sync<float> OutlineScaledMaxDistance;
public readonly Sync<colorX> OutlineColor;
public readonly Sync<float> OutlineLightingMix;
public readonly AssetRef<ITexture2D> UVAnimationMask;
public readonly Sync<float> UVAnimationScrollX;
public readonly Sync<float> UVAnimationScrollY;
public readonly Sync<float> 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<int> 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;
}
}
3 changes: 2 additions & 1 deletion ProjectObsidian/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Reflection;

using Elements.Core;
Expand All @@ -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)]
[assembly: DataModelAssembly(DataModelAssemblyType.Core)]

0 comments on commit 800ed69

Please sign in to comment.