generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation of preview system API v0.3 (#254)
- Loading branch information
1 parent
b692d2f
commit 83761b0
Showing
14 changed files
with
405 additions
and
546 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#region | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using nadena.dev.ndmf.rq; | ||
using UnityEngine; | ||
|
||
#endregion | ||
|
||
namespace nadena.dev.ndmf.preview | ||
{ | ||
internal class NodeController : IDisposable | ||
{ | ||
private class RefCount | ||
{ | ||
public int Count = 1; | ||
} | ||
|
||
private readonly IRenderFilter _filter; | ||
private readonly IRenderFilterNode _node; | ||
private readonly List<(Renderer, ProxyObjectController)> _proxies; | ||
private readonly RefCount _refCount; | ||
internal ulong WhatChanged = IRenderFilterNode.Everything; | ||
|
||
internal ProxyObjectController GetProxyFor(Renderer r) | ||
{ | ||
return _proxies.Find(p => p.Item1 == r).Item2; | ||
} | ||
|
||
private NodeController( | ||
IRenderFilterNode node, | ||
List<(Renderer, ProxyObjectController)> proxies, | ||
RefCount refCount | ||
) | ||
{ | ||
_node = node; | ||
_proxies = proxies; | ||
_refCount = refCount; | ||
|
||
OnFrame(); | ||
} | ||
|
||
internal void OnFrame() | ||
{ | ||
foreach (var (original, proxy) in _proxies) | ||
{ | ||
if (original != null && proxy.Renderer != null) | ||
{ | ||
_node.OnFrame(original, proxy.Renderer); | ||
} | ||
} | ||
} | ||
|
||
public static async Task<NodeController> Create( | ||
IRenderFilter filter, | ||
List<(Renderer, ProxyObjectController)> proxies) | ||
{ | ||
var invalidater = new TaskCompletionSource<object>(); | ||
|
||
ComputeContext context = new ComputeContext(() => filter.ToString()); | ||
context.Invalidate = () => invalidater.SetResult(null); | ||
context.OnInvalidate = invalidater.Task; | ||
|
||
var node = await filter.Instantiate( | ||
proxies.Select(p => (p.Item1, p.Item2.Renderer)), | ||
context | ||
); | ||
|
||
return new NodeController(node, proxies, new RefCount()); | ||
} | ||
|
||
public async Task<NodeController> Refresh( | ||
List<(Renderer, ProxyObjectController)> proxies, | ||
ulong changes | ||
) | ||
{ | ||
var invalidater = new TaskCompletionSource<object>(); | ||
|
||
ComputeContext context = new ComputeContext(() => _node.ToString()); | ||
context.Invalidate = () => invalidater.SetResult(null); | ||
context.OnInvalidate = invalidater.Task; | ||
|
||
var node = await _node.Refresh( | ||
proxies.Select(p => (p.Item1, p.Item2.Renderer)), | ||
context, | ||
changes | ||
); | ||
|
||
RefCount refCount; | ||
if (node == _node) | ||
{ | ||
refCount = _refCount; | ||
refCount.Count++; | ||
} | ||
else if (node == null) | ||
{ | ||
return await Create(_filter, proxies); | ||
} | ||
else | ||
{ | ||
refCount = new RefCount(); | ||
} | ||
|
||
var controller = new NodeController(node, proxies, refCount); | ||
controller.WhatChanged = changes | node.WhatChanged; | ||
return controller; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (--_refCount.Count == 0) | ||
{ | ||
_node.Dispose(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.