Skip to content

Commit

Permalink
feat: improvements to ParameterInfo (#312)
Browse files Browse the repository at this point in the history
* fix: some issues preventing preview overrides from changing object enable states

* feat: add default value to ProvidedParameter

* feat: Add support for ComputeContext invalidation to ParameterInfo

* chore: CHANGELOG update
  • Loading branch information
bdunderscore authored Aug 5, 2024
1 parent ae9d4cd commit 37efd62
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#312] Added a default value field to ProvidedParameter
- [#312] Added support for invalidating ComputeContext to ParameterInfo

### Fixed
- [#312] Fix issues preventing preview overrides from changing object enable states

### Changed

Expand Down
1 change: 1 addition & 0 deletions Editor/ChangeStream/ObjectWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ public void Dispose()

private void DoDispose()
{
if (_orig == null) return;
foreach (var orig in _orig) orig.Dispose();
}
}
Expand Down
6 changes: 4 additions & 2 deletions Editor/ChangeStream/ShadowGameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ internal class ShadowHierarchy

internal IDisposable RegisterRootSetListener(ListenerSet<HierarchyEvent>.Filter filter, ComputeContext ctx)
{
if (ctx.IsInvalidated) return new NullDisposable();

return _rootSetListener.Register(filter, ctx);
}

Expand All @@ -67,7 +69,7 @@ internal IDisposable RegisterGameObjectListener(
ComputeContext ctx
)
{
if (targetObject == null) return new NullDisposable();
if (targetObject == null || ctx.IsInvalidated) return new NullDisposable();

ShadowGameObject shadowObject = ActivateShadowObject(targetObject);

Expand All @@ -79,7 +81,7 @@ internal IDisposable RegisterObjectListener(UnityObject targetComponent,
ComputeContext ctx
)
{
if (targetComponent == null) return new NullDisposable();
if (targetComponent == null || ctx.IsInvalidated) return new NullDisposable();

if (!_otherObjects.TryGetValue(targetComponent.GetInstanceID(), out var shadowComponent))
{
Expand Down
13 changes: 13 additions & 0 deletions Editor/PreviewSystem/ComputeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Threading.Tasks;
using JetBrains.Annotations;

#endregion

Expand All @@ -13,8 +14,14 @@ namespace nadena.dev.ndmf.preview
/// </summary>
public sealed class ComputeContext
{
[PublicAPI] public static ComputeContext NullContext { get; }
private readonly TaskCompletionSource<object> _invalidater = new();

static ComputeContext()
{
NullContext = new ComputeContext(null);
}

/// <summary>
/// An Action which can be used to invalidate this compute context (possibly triggering a recompute).
/// </summary>
Expand All @@ -34,6 +41,12 @@ internal ComputeContext()
OnInvalidate = _invalidater.Task;
}

private ComputeContext(object nullToken)
{
Invalidate = () => { };
OnInvalidate = Task.CompletedTask;
}

/// <summary>
/// Invalidate the `other` compute context when this compute context is invalidated.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Editor/PreviewSystem/ProxyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ private static void OnPreCull(Camera cam)

foreach (var (original, replacement) in sess.GetReplacements())
{
if (original == null || replacement == null || !original.enabled ||
!original.gameObject.activeInHierarchy)
// TODO: Optimize to cull meshes that don't have an active-state override registered
if (original == null || replacement == null || !original.enabled)
{
if (replacement != null) replacement.forceRenderingOff = true;
continue;
Expand Down
4 changes: 3 additions & 1 deletion Editor/PreviewSystem/Rendering/ProxyObjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ internal bool OnPreFrame()
var target = _replacementRenderer;
var original = _originalRenderer;

target.gameObject.SetActive(original.enabled && original.gameObject.activeInHierarchy);

SkinnedMeshRenderer smr = null;
if (_originalRenderer is SkinnedMeshRenderer smr_)
{
Expand Down Expand Up @@ -154,7 +156,7 @@ internal bool OnPreFrame()
_replacementRenderer.sharedMaterials = _originalRenderer.sharedMaterials;

if (target.gameObject.scene != original.gameObject.scene &&
original.gameObject.scene.IsValid())
original.gameObject.scene.IsValid() && target.transform.parent == null)
{
SceneManager.MoveGameObjectToScene(target.gameObject, original.gameObject.scene);
}
Expand Down
5 changes: 4 additions & 1 deletion Editor/PreviewSystem/Rendering/ShadowBoneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ private static Transform CopyState(BoneState entry)
entry.proxy.localPosition = t.localPosition;
entry.proxy.localRotation = t.localRotation;
entry.proxy.localScale = t.localScale;


if (entry.proxy.parent == null && entry.proxy.gameObject.scene != t.gameObject.scene)
SceneManager.MoveGameObjectToScene(entry.proxy.gameObject, t.gameObject.scene);

return parent;
}
}
Expand Down
5 changes: 5 additions & 0 deletions Editor/VRChat/ParameterIntrospection/IParameterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ public bool IsAnimatorOnly
/// </summary>
public bool WantSynced { get; set; }

/// <summary>
/// The default value of this parameter, if known.
/// </summary>
public float? DefaultValue { get; set; }

public IEnumerable<ProvidedParameter> SubParameters()
{
if (Namespace == ParameterNamespace.Animator)
Expand Down
27 changes: 24 additions & 3 deletions Editor/VRChat/ParameterIntrospection/ParameterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using nadena.dev.ndmf.preview;
using nadena.dev.ndmf.runtime;
using UnityEditor;
using UnityEngine;
Expand All @@ -17,14 +18,20 @@ namespace nadena.dev.ndmf
/// </summary>
public sealed class ParameterInfo
{
public static readonly ParameterInfo ForUI = new ParameterInfo(null);
public static readonly ParameterInfo ForUI = new((BuildContext)null);

public static ParameterInfo ForContext(BuildContext context)
{
return context.GetState(ctx => new ParameterInfo(ctx));
}

public static ParameterInfo ForPreview(ComputeContext context)
{
return new ParameterInfo(context);
}

private readonly BuildContext _context;
private readonly ComputeContext _computeContext;

public enum ConflictType
{
Expand All @@ -46,6 +53,12 @@ static void Init()
private ParameterInfo(BuildContext context)
{
_context = context;
_computeContext = ComputeContext.NullContext;
}

private ParameterInfo(ComputeContext ctx)
{
_computeContext = ctx;
}

private long _parameterCount; // used to maintain declaration order
Expand Down Expand Up @@ -95,13 +108,15 @@ public IEnumerable<ProvidedParameter> GetParametersForObject(GameObject obj, Con

if (!RuntimeUtil.IsAvatarRoot(obj.transform))
{
_computeContext.ObservePath(obj.transform);
mappings = GetParameterRemappingsAt(obj.transform.parent.gameObject);
}

foreach (var component in obj.GetComponents(typeof(Component)))
foreach (var component in _computeContext.GetComponents(obj, typeof(Component)))
{
if (EnhancerDatabase<ParameterProviderFor, IParameterProvider>.Query(component, out var provider))
{
_computeContext.Observe(component); // TODO: Can we add an extract condition here?
provider.RemapParameters(ref mappings, _context);
}
}
Expand All @@ -124,10 +139,11 @@ public IEnumerable<ProvidedParameter> GetParametersForObject(GameObject obj, Con

if (!RuntimeUtil.IsAvatarRoot(c.transform))
{
_computeContext.ObservePath(c.transform);
mappings = GetParameterRemappingsAt(c.transform.parent.gameObject);
}

foreach (var component in c.GetComponents(typeof(Component)))
foreach (var component in _computeContext.GetComponents(c.gameObject, typeof(Component)))
{
if (component == c && !includeSelf)
{
Expand All @@ -136,6 +152,7 @@ public IEnumerable<ProvidedParameter> GetParametersForObject(GameObject obj, Con

if (EnhancerDatabase<ParameterProviderFor, IParameterProvider>.Query(component, out var provider))
{
_computeContext.Observe(component); // TODO: Can we add an extract condition here?
provider.RemapParameters(ref mappings, _context);
}

Expand All @@ -158,6 +175,8 @@ private IEnumerable<RegisteredParameter> GetParametersForObject(GameObject root,
continue;
}

_computeContext.Observe(component); // TODO: Can we add an extract condition here?

// Apply mappings first
provider.RemapParameters(ref remaps, _context);

Expand Down Expand Up @@ -255,6 +274,8 @@ private void ResolveConflict(ConflictHandler onConflict, RegisteredParameter old
oldP.Parameter.IsAnimatorOnly &= newP.IsAnimatorOnly;
oldP.Parameter.IsHidden &= newP.IsHidden;
oldP.Parameter.WantSynced = oldP.Parameter.WantSynced || newP.WantSynced;

oldP.Parameter.DefaultValue = newP.DefaultValue ?? oldP.Parameter.DefaultValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public IEnumerable<ProvidedParameter> GetSuppliedParameters(BuildContext context
)
{
WantSynced = p.networkSynced,
DefaultValue = p.defaultValue
};
});
}
Expand Down

0 comments on commit 37efd62

Please sign in to comment.