Skip to content

Commit

Permalink
feat: add support for disabling preview on a pass or plugin basis
Browse files Browse the repository at this point in the history
Closes: #296
  • Loading branch information
bdunderscore committed Jul 29, 2024
1 parent 903b598 commit 2807243
Show file tree
Hide file tree
Showing 15 changed files with 380 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#297] Added UI for turning preview on/off at a plugin or pass level

### Fixed

Expand Down
37 changes: 28 additions & 9 deletions Editor/API/Solver/PluginResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using nadena.dev.ndmf.model;
using nadena.dev.ndmf.preview;
using nadena.dev.ndmf.preview.UI;

#endregion

Expand All @@ -30,6 +31,7 @@ internal class ConcretePass
internal IPass InstantiatedPass { get; }
internal ImmutableList<Type> DeactivatePlugins { get; }
internal ImmutableList<Type> ActivatePlugins { get; }
internal bool HasPreviews { get; set; }

public void Execute(BuildContext context)
{
Expand All @@ -49,9 +51,10 @@ internal ConcretePass(IPluginInternal plugin, IPass pass, ImmutableList<Type> de

internal class PluginResolver
{
internal PreviewSession PreviewSession { get; private set; }
internal ImmutableList<(BuildPhase, IList<ConcretePass>)> Passes { get; }

private readonly List<SolverPass> _allPasses = new();

public PluginResolver() : this(
AppDomain.CurrentDomain.GetAssemblies().SelectMany(
assembly => assembly.GetCustomAttributes(typeof(ExportsPlugin), false))
Expand All @@ -71,8 +74,6 @@ public PluginResolver(IEnumerable<Type> plugins) : this(

public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates)
{
PreviewSession = new PreviewSession();

var solverContext = new SolverContext();

foreach (var plugin in pluginTemplates)
Expand Down Expand Up @@ -156,6 +157,7 @@ public PluginResolver(IEnumerable<IPluginInternal> pluginTemplates)
#endif

var sorted = TopoSort.DoSort(passes, constraints);
_allPasses.AddRange(sorted);

var concrete = ToConcretePasses(phase, sorted);

Expand Down Expand Up @@ -196,13 +198,11 @@ ImmutableList<ConcretePass> ToConcretePasses(BuildPhase phase, IEnumerable<Solve
}
}

concrete.Add(new ConcretePass(pass.Plugin, pass.Pass, toDeactivate.ToImmutableList(),
toActivate.ToImmutableList()));
var concretePass = new ConcretePass(pass.Plugin, pass.Pass, toDeactivate.ToImmutableList(),
toActivate.ToImmutableList());
concretePass.HasPreviews = pass.RenderFilters.Count > 0;

foreach (var filter in pass.RenderFilters)
{
PreviewSession.AddMutator(new SequencePoint(), filter);
}
concrete.Add(concretePass);
}

if (activeExtensions.Count > 0)
Expand All @@ -219,5 +219,24 @@ ImmutableList<ConcretePass> ToConcretePasses(BuildPhase phase, IEnumerable<Solve

return concrete.ToImmutableList();
}

internal PreviewSession PreviewSession
{
get
{
var session = new PreviewSession();

foreach (var pass in _allPasses)
{
if (!PreviewPrefs.instance.IsPreviewPluginEnabled(pass.Plugin.QualifiedName)) continue;

if (PreviewPrefs.instance.IsPreviewPassEnabled(pass.Pass.QualifiedName))
foreach (var filter in pass.RenderFilters)
session.AddMutator(new SequencePoint(), filter);
}

return session;
}
}
}
}
10 changes: 10 additions & 0 deletions Editor/GlobalInit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#region

using nadena.dev.ndmf.preview;
using nadena.dev.ndmf.preview.UI;
using nadena.dev.ndmf.runtime;
using UnityEditor;

Expand All @@ -19,6 +20,15 @@ static GlobalInit()
{
var resolver = new PluginResolver();
PreviewSession.Current = resolver.PreviewSession;

PreviewPrefs.instance.OnPreviewConfigChanged += () =>
{
var oldSession = PreviewSession.Current;
PreviewSession.Current = resolver.PreviewSession;
oldSession.Dispose();

SceneView.RepaintAll();
};
};
}
}
Expand Down
24 changes: 12 additions & 12 deletions Editor/PreviewSystem/PreviewSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace nadena.dev.ndmf.preview
///
/// (For now, this isn't very useful; use `DeclaringPass.PreviewingWith` instead)
/// </summary>
public class PreviewSession // : IDisposable
internal class PreviewSession // : IDisposable
{
#region Static State

/// <summary>
/// The PreviewSession used for any cameras not overriden using `OverrideCamera`.
/// </summary>
public static PreviewSession Current { get; set; } = null;
public static PreviewSession Current { get; set; }

#if FUTURE_API
/// <summary>
Expand All @@ -48,27 +48,27 @@ public static void ClearCameraOverride(Camera target)

internal IEnumerable<(Renderer, Renderer)> GetReplacements()
{
return _session?.OnPreCull() ?? Enumerable.Empty<(Renderer, Renderer)>();
return _proxySession?.OnPreCull() ?? Enumerable.Empty<(Renderer, Renderer)>();
}

internal ImmutableDictionary<Renderer, Renderer> OriginalToProxyRenderer =>
_session?.OriginalToProxyRenderer ?? ImmutableDictionary<Renderer, Renderer>.Empty;
_proxySession?.OriginalToProxyRenderer ?? ImmutableDictionary<Renderer, Renderer>.Empty;

internal ImmutableDictionary<GameObject, GameObject> OriginalToProxyObject =>
_session?.OriginalToProxyObject ?? ImmutableDictionary<GameObject, GameObject>.Empty;
_proxySession?.OriginalToProxyObject ?? ImmutableDictionary<GameObject, GameObject>.Empty;

internal ImmutableDictionary<GameObject, GameObject> ProxyToOriginalObject =>
_session?.ProxyToOriginalObject ?? ImmutableDictionary<GameObject, GameObject>.Empty;
_proxySession?.ProxyToOriginalObject ?? ImmutableDictionary<GameObject, GameObject>.Empty;

private readonly Sequencer _sequence = new Sequencer();

private Dictionary<SequencePoint, IRenderFilter> _filters = new();

private ProxySession _session;
private readonly ProxySession _proxySession;

public PreviewSession()
{
_session = new ProxySession(ImmutableList<IRenderFilter>.Empty);
_proxySession = new ProxySession(ImmutableList<IRenderFilter>.Empty);
}

/// <summary>
Expand Down Expand Up @@ -117,7 +117,7 @@ void RebuildSequence()
var sequence = _sequence.Sequence;
var filters = sequence.Select(p => _filters.GetValueOrDefault(p)).Where(f => f != null).ToImmutableList();

_session.Filters = filters;
_proxySession.Filters = filters;
}

#if FUTURE_API
Expand All @@ -131,11 +131,11 @@ public PreviewSession Fork()
{
throw new NotImplementedException();
}

#endif

public void Dispose()
{
throw new NotImplementedException();
_proxySession.Dispose();
}
#endif
}
}
3 changes: 3 additions & 0 deletions Editor/PreviewSystem/UI.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions Editor/PreviewSystem/UI/PreviewPrefs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using UnityEditor;
using UnityEngine;

namespace nadena.dev.ndmf.preview.UI
{
internal class PreviewPrefs : ScriptableSingleton<PreviewPrefs>
{
[SerializeField] private List<string> _disabledPreviewPasses = new();
private ImmutableHashSet<string> _disabledPreviewPassesSet;

[SerializeField] private List<string> _disabledPreviewPlugins = new();
private ImmutableHashSet<string> _disabledPreviewPluginsSet;

public event Action OnPreviewConfigChanged;

private void OnValidate()
{
_disabledPreviewPassesSet = _disabledPreviewPasses.ToImmutableHashSet();
_disabledPreviewPluginsSet = _disabledPreviewPlugins.ToImmutableHashSet();
}

public bool IsPreviewPassEnabled(string qualifiedName)
{
if (_disabledPreviewPassesSet == null) OnValidate();

return !_disabledPreviewPassesSet.Contains(qualifiedName);
}

public bool IsPreviewPluginEnabled(string qualifiedName)
{
if (_disabledPreviewPluginsSet == null) OnValidate();

return !_disabledPreviewPluginsSet.Contains(qualifiedName);
}

public void SetPreviewPassEnabled(string qualifiedName, bool enabled)
{
if (enabled)
_disabledPreviewPasses.Remove(qualifiedName);
else
_disabledPreviewPasses.Add(qualifiedName);

OnValidate();

OnPreviewConfigChanged?.Invoke();
}

public void SetPreviewPluginEnabled(string qualifiedName, bool enabled)
{
if (enabled)
_disabledPreviewPlugins.Remove(qualifiedName);
else
_disabledPreviewPlugins.Add(qualifiedName);

OnValidate();

OnPreviewConfigChanged?.Invoke();
}
}
}
3 changes: 3 additions & 0 deletions Editor/PreviewSystem/UI/PreviewPrefs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2807243

Please sign in to comment.