-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RenderGraph support for Distortion #111
base: main
Are you sure you want to change the base?
Changes from 15 commits
314516d
4abb886
4c5efc1
6e4d2e1
fdfcfa8
a901028
2441b38
b64b60e
62b4622
1905425
afb1900
7d301c1
cef3c48
d11c1b4
2fcf1e1
c87b647
a8deaa0
9d4f75e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// -------------------------------------------------------------- | ||
// Copyright 2025 CyberAgent, Inc. | ||
// -------------------------------------------------------------- | ||
|
||
#if UNITY_2023_3_OR_NEWER | ||
using UnityEngine; | ||
using UnityEngine.Rendering; | ||
using UnityEngine.Rendering.RenderGraphModule; | ||
using UnityEngine.Rendering.Universal; | ||
|
||
namespace Nova.Runtime.Core.Scripts | ||
{ | ||
public partial class ApplyDistortionPass : ScriptableRenderPass | ||
{ | ||
private const string DistortedCameraColorTextureName = "DistortedCameraColorTexture"; | ||
|
||
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) | ||
{ | ||
if (_material == null) | ||
return; | ||
if (!_applyToSceneView && frameData.Get<UniversalCameraData>().cameraType == CameraType.SceneView) | ||
return; | ||
|
||
if (!frameData.Contains<DistortionContextItem>()) | ||
{ | ||
Debug.LogError("[NOVA] Cannot execute ApplyDistortionPass. DistortedUvBufferPass must be executed."); | ||
return; | ||
} | ||
|
||
var resourceData = frameData.Get<UniversalResourceData>(); | ||
TextureHandle destTexture; | ||
{ | ||
var desc = renderGraph.GetTextureDesc(resourceData.activeColorTexture); | ||
desc.name = DistortedCameraColorTextureName; | ||
destTexture = renderGraph.CreateTexture(desc); | ||
} | ||
|
||
using (var builder = | ||
renderGraph.AddRasterRenderPass<PassData>("NOVA.ApplyDistortionPass", out var passData)) | ||
{ | ||
// DestTexture | ||
builder.SetRenderAttachment(destTexture, 0); | ||
|
||
// MainTexture | ||
{ | ||
passData.MainTex = resourceData.activeColorTexture; | ||
builder.UseTexture(passData.MainTex); | ||
passData.MainTexPropertyId = _mainTexPropertyId; | ||
} | ||
|
||
// DistortedUvBuffer | ||
{ | ||
var contextItem = frameData.Get<DistortionContextItem>(); | ||
passData.DistortedUvBuffer = contextItem.DistortedUvTexture; | ||
builder.UseTexture(passData.DistortedUvBuffer); | ||
passData.DistortionBufferPropertyId = _distortionBufferPropertyId; | ||
} | ||
|
||
passData.Material = _material; | ||
|
||
builder.SetRenderFunc(static (PassData data, RasterGraphContext context) => | ||
{ | ||
var cmd = context.cmd; | ||
data.Material.SetTexture(data.MainTexPropertyId, data.MainTex); | ||
data.Material.SetTexture(data.DistortionBufferPropertyId, data.DistortedUvBuffer); | ||
Blitter.BlitTexture(context.cmd, data.MainTex, Vector2.one, data.Material, 0); | ||
}); | ||
} | ||
|
||
resourceData.cameraColor = destTexture; | ||
} | ||
|
||
private class PassData | ||
{ | ||
internal TextureHandle DistortedUvBuffer; | ||
internal int DistortionBufferPropertyId; | ||
internal TextureHandle MainTex; | ||
internal int MainTexPropertyId; | ||
internal Material Material; | ||
} | ||
} | ||
} | ||
#endif |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// -------------------------------------------------------------- | ||
// Copyright 2025 CyberAgent, Inc. | ||
// -------------------------------------------------------------- | ||
|
||
#if UNITY_2023_3_OR_NEWER | ||
using UnityEngine; | ||
using UnityEngine.Experimental.Rendering; | ||
using UnityEngine.Rendering; | ||
using UnityEngine.Rendering.RenderGraphModule; | ||
using UnityEngine.Rendering.Universal; | ||
|
||
namespace Nova.Runtime.Core.Scripts | ||
{ | ||
public partial class DistortedUvBufferPass : ScriptableRenderPass | ||
{ | ||
private const string DistortedUvBufferTexName = "DistortedUvBuffer"; | ||
|
||
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) | ||
{ | ||
using (var builder = renderGraph.AddRasterRenderPass<PassData>("NOVA.DistortedUvBufferPass", | ||
out var passData)) | ||
{ | ||
var distortedUvTexture = CreateRenderTarget(renderGraph, frameData); | ||
// Insert data to be passed to ApplyDistortionPass. | ||
{ | ||
var contextItem = frameData.Create<DistortionContextItem>(); | ||
contextItem.DistortedUvTexture = distortedUvTexture; | ||
} | ||
builder.SetRenderAttachment(distortedUvTexture, 0); | ||
|
||
RendererListHandle renderList; | ||
{ | ||
var renderingData = frameData.Get<UniversalRenderingData>(); | ||
var cameraData = frameData.Get<UniversalCameraData>(); | ||
var lightData = frameData.Get<UniversalLightData>(); | ||
|
||
var param = InitRendererListParams(_shaderTagId, renderingData, cameraData, lightData, | ||
SortingCriteria.CommonTransparent, _filteringSettings); | ||
renderList = renderGraph.CreateRendererList(param); | ||
} | ||
builder.UseRendererList(renderList); | ||
passData.RendererList = renderList; | ||
|
||
builder.SetRenderFunc(static (PassData data, RasterGraphContext context) => | ||
{ | ||
context.cmd.DrawRendererList(data.RendererList); | ||
}); | ||
} | ||
} | ||
|
||
private static TextureHandle CreateRenderTarget(RenderGraph renderGraph, ContextContainer frameData) | ||
{ | ||
var resourceData = frameData.Get<UniversalResourceData>(); | ||
var desc = renderGraph.GetTextureDesc(resourceData.activeColorTexture); | ||
desc.name = DistortedUvBufferTexName; | ||
desc.depthBufferBits = 0; | ||
desc.clearColor = Color.gray; | ||
if (SystemInfo.IsFormatSupported(GraphicsFormat.R16G16_SFloat, GraphicsFormatUsage.Render)) | ||
desc.colorFormat = GraphicsFormat.R16G16_SFloat; | ||
return renderGraph.CreateTexture(desc); | ||
} | ||
|
||
private static RendererListParams InitRendererListParams( | ||
ShaderTagId shaderTagId, | ||
UniversalRenderingData renderingData, | ||
UniversalCameraData cameraData, | ||
UniversalLightData lightData, | ||
SortingCriteria sortingCriteria, | ||
FilteringSettings filteringSettings) | ||
{ | ||
var drawSettings = RenderingUtils.CreateDrawingSettings(shaderTagId, renderingData, cameraData, lightData, | ||
sortingCriteria); | ||
return new RendererListParams(renderingData.cullResults, drawSettings, filteringSettings); | ||
} | ||
|
||
private class PassData | ||
{ | ||
internal RendererListHandle RendererList; | ||
} | ||
} | ||
} | ||
#endif |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// -------------------------------------------------------------- | ||
// Copyright 2025 CyberAgent, Inc. | ||
// -------------------------------------------------------------- | ||
|
||
#if UNITY_2023_3_OR_NEWER | ||
using UnityEngine.Rendering; | ||
using UnityEngine.Rendering.RenderGraphModule; | ||
|
||
namespace Nova.Runtime.Core.Scripts | ||
{ | ||
public class DistortionContextItem : ContextItem | ||
{ | ||
public TextureHandle DistortedUvTexture; | ||
|
||
public override void Reset() | ||
{ | ||
DistortedUvTexture = TextureHandle.nullHandle; | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
partialがないのでコンパイルエラーが起きています
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unity 6000.0.40f1にて添付している画像のコンパイルエラーが起きています。

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
プロジェクトを開くときに古いAPIを使っています。置き換えますか?というダイアログでNoを選ぶと確認できると思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
追加でRender Graphを有効にしてApplyDistortionPass.cs、ApplyDistortionPass.cs、ScreenSpaceDistortion.cs再インポートしてコンパイルを走らせると添付の画像の警告が出てきます。
ObsoleteのAPIを利用しているために出ている警告です。
Siriusに同様の警告を抑止している処理があるので、そちらを参考に実装してみてください。