Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Custom Materials start.
Browse files Browse the repository at this point in the history
Not working Yet
  • Loading branch information
Xlinka authored Sep 14, 2022
2 parents cefcdef + 981d9f3 commit ea28fae
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 77 deletions.
56 changes: 0 additions & 56 deletions Logix/Avatar/NearestUser.cs

This file was deleted.

65 changes: 65 additions & 0 deletions Materials/Effects/Hologram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using BaseX;
using CloudX.Shared;
using FrooxEngine;
using NEOSPlus.Shaders;

[Category(new string[] { "Assets/Materials/NeosPlus/Effects" })]
public class Hologram : SingleShaderMaterialProvider
{
protected override Uri ShaderURL => ShaderInjection.FragmentHologram;

// Main
public readonly AssetRef<ITexture2D> MainTexture;
public readonly Sync<color> MainColor;
// Repeating ramp
public readonly AssetRef<ITexture2D> RampTexture;
public readonly Sync<float> RampScale;
public readonly Sync<float> ScrollSpeed;
// Fresnel
public readonly Sync<color> FresnelColor;
public readonly Sync<float> FresnelStrength;
public readonly Sync<float> FresnelPower;

[DefaultValue(-1)]
public readonly Sync<int> RenderQueue;
private static PropertyState _propertyInitializationState;

// Main
private static MaterialProperty _MainColor = new MaterialProperty("_MainColor");
private static MaterialProperty _MainTexture = new MaterialProperty("_MainTexture");
// Repeating ramp
private static MaterialProperty _RampTexture = new MaterialProperty("_RampTexture");
private static MaterialProperty _RampScale = new MaterialProperty("_RampScale");
private static MaterialProperty _ScrollSpeed = new MaterialProperty("_ScrollSpeed");
// Fresnel
private static MaterialProperty _FresnelColor = new MaterialProperty("_FresnelColor");
private static MaterialProperty _FresnelStrength = new MaterialProperty("_FresnelStrength");
private static MaterialProperty _FresnelPower = new MaterialProperty("_FresnelPower");

public override PropertyState PropertyInitializationState
{
get => _propertyInitializationState;
protected set => _propertyInitializationState = value;
}
protected override void UpdateMaterial(Material material)
{
// Main
material.UpdateTexture(_MainTexture, MainTexture);
material.UpdateColor(_MainColor, MainColor);
// Repeating ramp
material.UpdateTexture(_RampTexture, RampTexture);
material.UpdateFloat(_RampScale, RampScale);
material.UpdateFloat(_ScrollSpeed, ScrollSpeed);
// Fresnel
material.UpdateColor(_FresnelColor, FresnelColor);
material.UpdateFloat(_FresnelStrength, FresnelStrength);
material.UpdateFloat(_FresnelPower, FresnelPower);

if (!RenderQueue.GetWasChangedAndClear()) return;
var renderQueue = RenderQueue.Value;
if ((int)RenderQueue == -1) renderQueue = 2600;
material.SetRenderQueue(renderQueue);
}
protected override void UpdateKeywords(ShaderKeywords keywords) { }
}
8 changes: 1 addition & 7 deletions Materials/TestMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
[Category(new string[] { "Assets/Materials/NeosPlus" })]
public class TestMaterial : SingleShaderMaterialProvider
{
protected override Uri ShaderURL => NeosAssets.Shaders.NeosPBSColorSplatSpecular; //Temp testing using the NeosPBS shader
public readonly Sync<float> Scale;
public readonly Sync<float3> Offset;
protected override Uri ShaderURL => NeosAssets.Shaders.NeosPBSColorSplatSpecular;
[DefaultValue(-1)]
public readonly Sync<int> RenderQueue;
private static PropertyState _propertyInitializationState;
private static MaterialProperty _Scale = new MaterialProperty("_Scale");
private static MaterialProperty _Offset = new MaterialProperty("_Offset");
public override PropertyState PropertyInitializationState
{
get => _propertyInitializationState;
Expand All @@ -25,8 +21,6 @@ protected override void UpdateKeywords(ShaderKeywords keywords)
}
protected override void UpdateMaterial(Material material)
{
material.UpdateFloat(_Scale, Scale);
material.UpdateFloat3(_Offset, Offset);
if (!RenderQueue.GetWasChangedAndClear()) return;
var renderQueue = RenderQueue.Value;
if ((int)RenderQueue == -1) renderQueue = 2600;
Expand Down
3 changes: 2 additions & 1 deletion NEOSPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
<Compile Include="Components\Wizards\MeshColliderManagementTools.cs" />
<Compile Include="Components\Audio\FFT\FFTSource.cs" />
<Compile Include="Logix\Avatar\AvatarRootSlot.cs" />
<Compile Include="Logix\Avatar\NearestUser.cs" />
<Compile Include="Logix\Avatar\NearestUserList.cs" />
<Compile Include="Logix\DynamicMesh\Bone\AddBone.cs" />
<Compile Include="Logix\DynamicMesh\Bone\GetBone.cs" />
Expand Down Expand Up @@ -205,9 +204,11 @@
<Compile Include="Logix\Playback\IsPaused.cs" />
<Compile Include="Logix\Playback\IsStopped.cs" />
<Compile Include="Logix\Users\SetUserScaling.cs" />
<Compile Include="Materials\Effects\Hologram.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Materials\TestMaterial.cs" />
<Compile Include="NeosPlusUtil.cs" />
<Compile Include="Shaders\ExecutionHook.cs" />
<Compile Include="Shaders\ShaderInjection.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
46 changes: 46 additions & 0 deletions Shaders/ExecutionHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

using BaseX;
using FrooxEngine;
using System;

namespace NEOSPlus.Shaders
{
[ImplementableClass(true)]
internal class ExecutionHook
{
#pragma warning disable CS0169
private static Type? __connectorType;
private static Type? __connectorTypes;
#pragma warning restore CS0169

static ExecutionHook()
{
try
{
Engine.Current.OnReady += () =>
{
ShaderInjection.AppendShaders();
};
}
catch (Exception e)
{
UniLog.Log($"Thrown Exception \n{e}");
}
}

private static DummyConnector InstantiateConnector()
{
return new DummyConnector();
}

private class DummyConnector : IConnector
{
public IImplementable? Owner { get; private set; }
public void ApplyChanges() { }
public void AssignOwner(IImplementable owner) => Owner = owner;
public void Destroy(bool destroyingWorld) { }
public void Initialize() { }
public void RemoveOwner() => Owner = null;
}
}
}
22 changes: 9 additions & 13 deletions Shaders/ShaderInjection.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseX;
using CloudX;
using CloudX.Shared;
using FrooxEngine;

namespace NEOSPlus.Shaders
{
internal class ShaderInjection
{
async Task RegisterShader(Uri uri)
public static readonly Uri FragmentHologram = new Uri("neosdb:///af2c3da741e0b1913dd145978731462ea9ff2da3eb8ff3283872adcc82e27b66.neoshader");

private static async Task RegisterShader(Uri uri)
{
// This is the important bit of the code
string signature = AssetUtil.ExtractSignature(uri);
// Check if the shader is already in the database
var shaderExists = await Engine.Current.LocalDB.ReadVariableAsync(signature, false);
if (!shaderExists)
{
// If the shader isn't in the database yet add it
await Engine.Current.LocalDB.WriteVariableAsync(signature, true);
}
if (!shaderExists) await Engine.Current.LocalDB.WriteVariableAsync(signature, true);
}

private void AppendShader()
public static void AppendShaders()
{
// Put all your shader urls in this array
List<Uri> shaders = new List<Uri>();
shaders.Add(FragmentHologram);
UniLog.Log($"Added {FragmentHologram} to the LocalDB");

List<Task> tasks = new List<Task>();

foreach (var shader in shaders)
Expand Down

0 comments on commit ea28fae

Please sign in to comment.