Skip to content

Commit

Permalink
Merge pull request #8 from noir-neo/share-texture
Browse files Browse the repository at this point in the history
Share texture
  • Loading branch information
noir-neo authored Nov 6, 2023
2 parents a196b10 + 1594533 commit 3b62d0e
Show file tree
Hide file tree
Showing 40 changed files with 1,140 additions and 148 deletions.
523 changes: 412 additions & 111 deletions Assets/Hatbor/Scenes/Main.unity

Large diffs are not rendered by default.

43 changes: 38 additions & 5 deletions Assets/Hatbor/Scripts/Camera/Camera.cs
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();
}
}
}
55 changes: 55 additions & 0 deletions Assets/Hatbor/Scripts/Camera/CameraCanvas.cs
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();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Hatbor/Scripts/Camera/CameraCanvas.cs.meta

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

36 changes: 19 additions & 17 deletions Assets/Hatbor/Scripts/Camera/Hatbor.Camera.asmdef
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
}
71 changes: 71 additions & 0 deletions Assets/Hatbor/Scripts/Camera/RenderTextureProvider.cs
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();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Hatbor/Scripts/Camera/RenderTextureProvider.cs.meta

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

40 changes: 40 additions & 0 deletions Assets/Hatbor/Scripts/Config/InspectableReactiveProperty.cs
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;
}
}

}
}

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

27 changes: 27 additions & 0 deletions Assets/Hatbor/Scripts/Config/RenderConfig.cs
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;
}
}
3 changes: 3 additions & 0 deletions Assets/Hatbor/Scripts/Config/RenderConfig.cs.meta

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

22 changes: 22 additions & 0 deletions Assets/Hatbor/Scripts/LifetimeScope/CameraLifetimeScope.cs
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);
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"Hatbor.Camera",
"Hatbor.Config",
"Hatbor.UI",
"Hatbor.FileBrowser"
"Hatbor.FileBrowser",
"Hatbor.TextureStreaming",
"Hatbor.TextureStreaming.Syphon",
"Hatbor.TextureStreaming.Spout"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Loading

0 comments on commit 3b62d0e

Please sign in to comment.