-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from noir-neo/share-texture
Share texture
- Loading branch information
Showing
40 changed files
with
1,140 additions
and
148 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,25 +1,58 @@ | ||
using System; | ||
using Hatbor.Config; | ||
using Hatbor.Rig; | ||
using UniRx; | ||
using UnityEngine.Rendering.Universal; | ||
using VContainer; | ||
using VContainer.Unity; | ||
|
||
namespace Hatbor.Camera | ||
{ | ||
public sealed class Camera : ITickable | ||
public sealed class Camera : IStartable, ITickable, IDisposable | ||
{ | ||
readonly UnityEngine.Camera camera; | ||
readonly UnityEngine.Camera mainCamera; | ||
readonly UniversalAdditionalCameraData cameraData; | ||
readonly ICameraRig cameraRig; | ||
readonly RenderTextureProvider renderTextureProvider; | ||
readonly RenderConfig renderConfig; | ||
|
||
readonly CompositeDisposable disposables = new(); | ||
|
||
[Inject] | ||
public Camera(UnityEngine.Camera camera, ICameraRig cameraRig) | ||
public Camera(UnityEngine.Camera mainCamera, | ||
ICameraRig cameraRig, | ||
RenderTextureProvider renderTextureProvider, | ||
RenderConfig renderConfig) | ||
{ | ||
this.camera = camera; | ||
this.mainCamera = mainCamera; | ||
this.cameraData = mainCamera.GetComponent<UniversalAdditionalCameraData>(); | ||
this.cameraRig = cameraRig; | ||
this.renderTextureProvider = renderTextureProvider; | ||
this.renderConfig = renderConfig; | ||
} | ||
|
||
void IStartable.Start() | ||
{ | ||
mainCamera.targetTexture = renderTextureProvider.RenderTexture; | ||
|
||
renderTextureProvider.OnSizeChanged | ||
.Subscribe(_ => mainCamera.ResetAspect()) | ||
.AddTo(disposables); | ||
|
||
// TODO: support transparent with post-processing | ||
renderConfig.TransparentBackground | ||
.Subscribe(b => cameraData.renderPostProcessing = !b) | ||
.AddTo(disposables); | ||
} | ||
|
||
void ITickable.Tick() | ||
{ | ||
cameraRig.Update(camera); | ||
cameraRig.Update(mainCamera); | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
disposables.Dispose(); | ||
} | ||
} | ||
} |
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,55 @@ | ||
using System; | ||
using System.Numerics; | ||
using UniRx; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using VContainer; | ||
using VContainer.Unity; | ||
using Vector2 = UnityEngine.Vector2; | ||
|
||
namespace Hatbor.Camera | ||
{ | ||
public sealed class CameraCanvas : IStartable, IDisposable | ||
{ | ||
readonly RenderTextureProvider renderTextureProvider; | ||
readonly RawImage rawImage; | ||
|
||
readonly CompositeDisposable disposables = new(); | ||
|
||
[Inject] | ||
public CameraCanvas(RenderTextureProvider renderTextureProvider, | ||
RawImage rawImage) | ||
{ | ||
this.renderTextureProvider = renderTextureProvider; | ||
this.rawImage = rawImage; | ||
} | ||
|
||
void IStartable.Start() | ||
{ | ||
rawImage.texture = renderTextureProvider.RenderTexture; | ||
rawImage.SetNativeSize(); | ||
|
||
var parent = rawImage.transform.parent as RectTransform; | ||
parent.ObserveEveryValueChanged(p => p.sizeDelta) | ||
.CombineLatest(renderTextureProvider.OnSizeChanged, | ||
(parentSize, textureSize) => (parentSize, textureSize)) | ||
.Select(t => CalcCoveredSize(t.parentSize, t.textureSize)) | ||
.Subscribe(size => rawImage.rectTransform.sizeDelta = size) | ||
.AddTo(disposables); | ||
} | ||
|
||
static Vector2 CalcCoveredSize(Vector2 parentSize, Vector2 textureSize) | ||
{ | ||
var parentAspect = parentSize.x / parentSize.y; | ||
var aspect = textureSize.x / textureSize.y; | ||
return aspect < parentAspect | ||
? new Vector2(parentSize.x, parentSize.x / aspect) | ||
: new Vector2(parentSize.y * aspect, parentSize.y); | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
disposables.Dispose(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,21 @@ | ||
{ | ||
"name": "Hatbor.Camera", | ||
"rootNamespace": "Hatbor.Camera", | ||
"references": [ | ||
"VContainer", | ||
"UniTask", | ||
"UniRx", | ||
"Hatbor.Rig" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
"name": "Hatbor.Camera", | ||
"rootNamespace": "Hatbor.Camera", | ||
"references": [ | ||
"VContainer", | ||
"UniTask", | ||
"UniRx", | ||
"Hatbor.Rig", | ||
"Hatbor.Config", | ||
"Unity.RenderPipelines.Universal.Runtime" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
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,71 @@ | ||
using System; | ||
using Hatbor.Config; | ||
using UniRx; | ||
using UnityEngine; | ||
using VContainer; | ||
using VContainer.Unity; | ||
|
||
namespace Hatbor.Camera | ||
{ | ||
public sealed class RenderTextureProvider : IStartable, IDisposable | ||
{ | ||
const int Depth = 24; | ||
const RenderTextureFormat Format = RenderTextureFormat.DefaultHDR; | ||
|
||
readonly RenderConfig renderConfig; | ||
|
||
readonly ISubject<Vector2Int> sizeChangedSubject; | ||
|
||
public RenderTexture RenderTexture { get; } | ||
public IObservable<Vector2Int> OnSizeChanged => sizeChangedSubject; | ||
|
||
readonly CompositeDisposable disposables = new(); | ||
|
||
[Inject] | ||
public RenderTextureProvider(RenderConfig renderConfig) | ||
{ | ||
this.renderConfig = renderConfig; | ||
var size = ClampSize(renderConfig.Size.Value); | ||
RenderTexture = new RenderTexture(size.x, size.y, Depth, Format, 0) | ||
{ | ||
filterMode = FilterMode.Point, | ||
wrapMode = TextureWrapMode.Clamp, | ||
}; | ||
RenderTexture.Create(); | ||
sizeChangedSubject = new BehaviorSubject<Vector2Int>(size); | ||
} | ||
|
||
void IStartable.Start() | ||
{ | ||
renderConfig.Size | ||
.Subscribe(Resize) | ||
.AddTo(disposables); | ||
} | ||
|
||
void Resize(Vector2Int size) | ||
{ | ||
var clampedSize = ClampSize(size); | ||
if (RenderTexture.width == clampedSize.x && RenderTexture.height == clampedSize.y) | ||
{ | ||
return; | ||
} | ||
RenderTexture.Release(); | ||
RenderTexture.width = clampedSize.x; | ||
RenderTexture.height = clampedSize.y; | ||
RenderTexture.Create(); | ||
sizeChangedSubject.OnNext(clampedSize); | ||
} | ||
|
||
static Vector2Int ClampSize(Vector2Int v) | ||
{ | ||
v.x = Math.Max(v.x, 4); | ||
v.y = Math.Max(v.y, 4); | ||
return v; | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
disposables.Dispose(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
Assets/Hatbor/Scripts/Config/InspectableReactiveProperty.cs
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,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UniRx; | ||
using UnityEngine; | ||
|
||
namespace Hatbor.Config | ||
{ | ||
/// <summary>Inspectable ReactiveProperty.</summary> | ||
[Serializable] | ||
public class Vector2IntReactiveProperty : ReactiveProperty<Vector2Int> | ||
{ | ||
static readonly IEqualityComparer<Vector2Int> EqualityComparerCache = new Vector2IntEqualityComparer(); | ||
|
||
public Vector2IntReactiveProperty() | ||
{ | ||
} | ||
|
||
public Vector2IntReactiveProperty(Vector2Int initialValue) | ||
: base(initialValue) | ||
{ | ||
} | ||
|
||
protected override IEqualityComparer<Vector2Int> EqualityComparer => EqualityComparerCache; | ||
|
||
sealed class Vector2IntEqualityComparer : IEqualityComparer<Vector2Int> | ||
{ | ||
public bool Equals(Vector2Int self, Vector2Int vector) | ||
{ | ||
return self.x.Equals(vector.x) && self.y.Equals(vector.y); | ||
} | ||
|
||
public int GetHashCode(Vector2Int obj) | ||
{ | ||
return obj.x.GetHashCode() ^ obj.y.GetHashCode() << 2; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
3 changes: 3 additions & 0 deletions
3
Assets/Hatbor/Scripts/Config/InspectableReactiveProperty.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
using System; | ||
using UniRx; | ||
using UnityEngine; | ||
|
||
namespace Hatbor.Config | ||
{ | ||
[Serializable, ConfigGroup("Rendering")] | ||
public sealed class RenderConfig : IConfigurable | ||
{ | ||
public string PersistentKey => "RenderConfig"; | ||
|
||
[SerializeField] Vector2IntReactiveProperty size = new(new Vector2Int(1920, 1080)); | ||
[SerializeField] BoolReactiveProperty enabledSharingTexture = new(true); | ||
[SerializeField] BoolReactiveProperty transparentBackground = new(true); | ||
|
||
[ConfigProperty("Size")] | ||
public ReactiveProperty<Vector2Int> Size => size; | ||
#if UNITY_STANDALONE_OSX | ||
[ConfigProperty("Syphon")] | ||
#elif UNITY_STANDALONE_WIN | ||
[ConfigProperty("Spout2")] | ||
#endif | ||
public ReactiveProperty<bool> EnabledSharingTexture => enabledSharingTexture; | ||
[ConfigProperty("Transparent Background")] | ||
public ReactiveProperty<bool> TransparentBackground => transparentBackground; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
Assets/Hatbor/Scripts/LifetimeScope/CameraLifetimeScope.cs
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,22 @@ | ||
using Hatbor.Camera; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using VContainer; | ||
using VContainer.Unity; | ||
|
||
namespace Hatbor.LifetimeScope | ||
{ | ||
public sealed class CameraLifetimeScope : VContainer.Unity.LifetimeScope | ||
{ | ||
[SerializeField] UnityEngine.Camera mainCamera; | ||
[SerializeField] RawImage rawImage; | ||
|
||
protected override void Configure(IContainerBuilder builder) | ||
{ | ||
builder.RegisterInstance(mainCamera); | ||
builder.RegisterInstance(rawImage); | ||
builder.RegisterEntryPoint<Camera.Camera>(Lifetime.Singleton); | ||
builder.RegisterEntryPoint<CameraCanvas>(Lifetime.Singleton); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/Hatbor/Scripts/LifetimeScope/CameraLifetimeScope.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.