Skip to content

Commit e8ed6c1

Browse files
durswdSantarh
andcommitted
17x fix stereo (#114)
* wip * Unity6 + URP + (RenderPass or not) + SinglePass Instanced XR 環境で Distortion, Depth オプションを有効化して正常に描画できる * comment * Unity 2022.3 / 6.0 でコンパイルできる --------- Co-authored-by: Masataka SUMI <santarh@gmail.com>
1 parent f118cf2 commit e8ed6c1

File tree

8 files changed

+90
-45
lines changed

8 files changed

+90
-45
lines changed

Dev/Plugin/Assets/Effekseer/External/URP/EffekseerURPRenderPassFeature.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,36 @@
1212

1313
public class UrpBlitter : IEffekseerBlitter
1414
{
15-
public static readonly int sourceTex = Shader.PropertyToID("_SourceTex");
16-
private Material blitMaterial;
17-
18-
public UrpBlitter()
19-
{
20-
this.blitMaterial = CoreUtils.CreateEngineMaterial("Hidden/Universal Render Pipeline/Blit");
21-
}
22-
2315
public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, bool xrRendering)
2416
{
2517
if (xrRendering)
2618
{
27-
CoreUtils.SetRenderTarget(
28-
cmd,
29-
dest,
30-
RenderBufferLoadAction.Load,
31-
RenderBufferStoreAction.Store,
32-
ClearFlag.None,
33-
Color.black);
34-
cmd.SetGlobalTexture(sourceTex, source);
35-
cmd.DrawProcedural(Matrix4x4.identity, blitMaterial, 0, MeshTopology.Quads, 4);
19+
CoreUtils.SetRenderTarget(cmd, dest);
20+
// FIXME: Scaling is ignored.
21+
// The interface should take RTHandle instead of RenderTargetIdentifier and use Blitter.BlitCameraTexture.
22+
// However, this will cause issues in terms of compatibility with Built-in RP support.
23+
Blitter.BlitTexture(cmd, source, Vector2.one, Blitter.GetBlitMaterial(TextureXR.dimension), 0);
3624
}
3725
else
3826
{
3927
cmd.Blit(source, dest);
4028
}
4129
}
30+
31+
public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, Material material, bool xrRendering)
32+
{
33+
cmd.Blit(source, dest, material);
34+
}
35+
36+
public void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, bool xrRendering)
37+
{
38+
CoreUtils.SetRenderTarget(cmd, color);
39+
}
40+
41+
public void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, RenderTargetIdentifier depth, bool xrRendering)
42+
{
43+
CoreUtils.SetRenderTarget(cmd, color, depth);
44+
}
4245
}
4346

4447
public class EffekseerURPRenderPassFeature : ScriptableRendererFeature

Dev/Plugin/Assets/Effekseer/Materials/EffekseerShaderAdVS.cginc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ VS_Output vert(VS_Input i)
367367
Output.PosP = Output.PosVS;
368368
#endif
369369

370+
Output.PosVS = UnityObjectToClipPos(worldPos);
370371
return Output;
371372
}
372373

@@ -535,6 +536,7 @@ VS_Output vert(VS_Input i)
535536
Output.PosP = Output.PosVS;
536537
#endif
537538

539+
Output.PosVS = UnityObjectToClipPos(worldPos);
538540
return Output;
539541
}
540542

Dev/Plugin/Assets/Effekseer/Materials/EffekseerShaderLitUnlitPS.cginc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <UnityInstancing.cginc>
12

23
// cbuffer PS_ConstanBuffer : register(b0)
34
// {
@@ -62,6 +63,8 @@ struct PS_Input
6263
half3 WorldT : TEXCOORD3;
6364
#endif
6465
float4 PosP : TEXCOORD4;
66+
67+
UNITY_VERTEX_OUTPUT_STEREO
6568
};
6669

6770
#include "EffekseerShaderSoftParticlePS.cginc"
@@ -70,6 +73,8 @@ struct PS_Input
7073
float4 frag(const PS_Input Input)
7174
: SV_Target
7275
{
76+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(Input);
77+
7378
bool convColorSpace = convertColorSpace != 0.0f;
7479

7580
float4 Output = ConvertFromSRGBTexture(_colorTex.Sample(sampler_colorTex, Input.UV), convColorSpace) * Input.Color;

Dev/Plugin/Assets/Effekseer/Materials/EffekseerShaderVS.cginc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ VS_Output vert(VS_Input i)
153153
#endif
154154

155155
Output.PosP = Output.PosVS;
156+
Output.PosVS = UnityObjectToClipPos(worldPos);
156157

157158
return Output;
158159
}
@@ -249,6 +250,7 @@ VS_Output vert(VS_Input i)
249250
#endif
250251

251252
Output.PosP = Output.PosVS;
253+
Output.PosVS = UnityObjectToClipPos(worldPos);
252254

253255
return Output;
254256
}
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using UnityEngine.Rendering;
1+
using UnityEngine;
2+
using UnityEngine.Rendering;
23

34
namespace Effekseer.Internal
45
{
56
public interface IEffekseerBlitter
67
{
78
void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, bool xrRendering);
9+
void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, Material material, bool xrRendering);
10+
void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, bool xrRendering);
11+
void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, RenderTargetIdentifier depth, bool xrRendering);
812
}
913

1014
public class StandardBlitter : IEffekseerBlitter
@@ -13,5 +17,34 @@ public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetI
1317
{
1418
cmd.Blit(source, dest);
1519
}
20+
21+
public void Blit(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier dest, Material material, bool xrRendering)
22+
{
23+
cmd.Blit(source, dest, material);
24+
}
25+
26+
public void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, bool xrRendering)
27+
{
28+
if (xrRendering)
29+
{
30+
cmd.SetRenderTarget(color, 0, CubemapFace.Unknown, -1);
31+
}
32+
else
33+
{
34+
cmd.SetRenderTarget(color);
35+
}
36+
}
37+
38+
public void SetRenderTarget(CommandBuffer cmd, RenderTargetIdentifier color, RenderTargetIdentifier depth, bool xrRendering)
39+
{
40+
if (xrRendering)
41+
{
42+
cmd.SetRenderTarget(color, depth, 0, CubemapFace.Unknown, -1);
43+
}
44+
else
45+
{
46+
cmd.SetRenderTarget(color, depth);
47+
}
48+
}
1649
}
1750
}

Dev/Plugin/Assets/Effekseer/Scripts/EffekseerRenderer.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public RenderTargetProperty()
100100
{
101101
}
102102

103-
internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRenderTexture)
103+
internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRenderTexture, IEffekseerBlitter blitter)
104104
{
105105
if (depthRenderTexture != null)
106106
{
@@ -125,11 +125,11 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRen
125125
{
126126
if (canGrabDepth)
127127
{
128-
cb.Blit(null, depthRenderTexture.renderTexture, grabDepthMat);
128+
blitter.Blit(cb, new RenderTargetIdentifier(), depthRenderTexture.renderTexture, grabDepthMat);
129129
}
130130
else
131131
{
132-
cb.SetRenderTarget(depthRenderTexture.renderTexture);
132+
blitter.SetRenderTarget(cb, depthRenderTexture.renderTexture, xrRendering);
133133
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
134134
}
135135
}
@@ -154,11 +154,11 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRen
154154
// restore
155155
if (depthTargetIdentifier.HasValue)
156156
{
157-
cb.SetRenderTarget(colorTargetIdentifier, depthTargetIdentifier.Value);
157+
blitter.SetRenderTarget(cb, colorTargetIdentifier, depthTargetIdentifier.Value, xrRendering);
158158
}
159159
else
160160
{
161-
cb.SetRenderTarget(colorTargetIdentifier);
161+
blitter.SetRenderTarget(cb, colorTargetIdentifier, xrRendering);
162162
}
163163
}
164164
}
@@ -176,9 +176,9 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, BackgroundRenderTexture bac
176176
Viewport.height / colorTargetRenderTexture.height,
177177
Viewport.x / colorTargetRenderTexture.width,
178178
Viewport.y / colorTargetRenderTexture.height));
179-
cb.SetRenderTarget(backgroundRenderTexture.renderTexture);
179+
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
180180
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
181-
cb.Blit(colorTargetIdentifier, backgroundRenderTexture.renderTexture, m);
181+
blitter.Blit(cb, colorTargetIdentifier, backgroundRenderTexture.renderTexture, m, xrRendering);
182182
}
183183
else
184184
{
@@ -189,9 +189,9 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, BackgroundRenderTexture bac
189189
Viewport.height / colorTargetRenderTexture.height,
190190
Viewport.x / colorTargetRenderTexture.width,
191191
Viewport.y / colorTargetRenderTexture.height));
192-
cb.SetRenderTarget(backgroundRenderTexture.renderTexture);
192+
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
193193
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
194-
cb.Blit(colorTargetIdentifier, backgroundRenderTexture.renderTexture, m);
194+
blitter.Blit(cb, colorTargetIdentifier, backgroundRenderTexture.renderTexture, m, xrRendering);
195195
}
196196
}
197197
else if (isRequiredToCopyBackground)
@@ -206,11 +206,11 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, BackgroundRenderTexture bac
206206
// restore
207207
if (depthTargetIdentifier.HasValue)
208208
{
209-
cb.SetRenderTarget(colorTargetIdentifier, depthTargetIdentifier.Value);
209+
blitter.SetRenderTarget(cb, colorTargetIdentifier, depthTargetIdentifier.Value, xrRendering);
210210
}
211211
else
212212
{
213-
cb.SetRenderTarget(colorTargetIdentifier);
213+
blitter.SetRenderTarget(cb, colorTargetIdentifier, xrRendering);
214214
}
215215
}
216216

Dev/Plugin/Assets/Effekseer/Scripts/EffekseerRendererNative.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private void SetupEffekseerRenderCommandBuffer(
128128
// to reset shader settings. SetRenderTarget is not applied until drawing
129129
if (fakeMaterial != null)
130130
{
131-
cmbBuf.DrawProcedural(new Matrix4x4(), fakeMaterial, 0, MeshTopology.Triangles, 3);
131+
cmbBuf.DrawProcedural(Matrix4x4.identity, fakeMaterial, 0, MeshTopology.Triangles, 3);
132132
}
133133
}
134134
}
@@ -140,7 +140,7 @@ private void SetupEffekseerRenderCommandBuffer(
140140
{
141141
if (renderTargetProperty != null)
142142
{
143-
renderTargetProperty.ApplyToCommandBuffer(cmbBuf, this.depthTexture);
143+
renderTargetProperty.ApplyToCommandBuffer(cmbBuf, this.depthTexture, blitter);
144144

145145
if (renderTargetProperty.Viewport.width > 0)
146146
{
@@ -158,7 +158,7 @@ private void SetupEffekseerRenderCommandBuffer(
158158
// to reset shader settings. SetRenderTarget is not applied until drawing
159159
if (fakeMaterial != null)
160160
{
161-
cmbBuf.DrawProcedural(new Matrix4x4(), fakeMaterial, 0, MeshTopology.Triangles, 3);
161+
cmbBuf.DrawProcedural(Matrix4x4.identity, fakeMaterial, 0, MeshTopology.Triangles, 3);
162162
}
163163
}
164164
}

Dev/Plugin/Assets/Effekseer/Scripts/EffekseerRendererUnity.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,15 +1023,15 @@ public void Render(Camera camera, int additionalMask, RenderTargetProperty rende
10231023
bool xrRendering = false;
10241024

10251025
blitter.Blit(path.commandBuffer, BuiltinRenderTextureType.CameraTarget, path.renderTexture.renderTexture, xrRendering);
1026-
path.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
1026+
blitter.SetRenderTarget(path.commandBuffer, BuiltinRenderTextureType.CameraTarget, xrRendering);
10271027
}
10281028
}
10291029

10301030
if (path.depthTexture != null)
10311031
{
10321032
if (renderTargetProperty != null)
10331033
{
1034-
renderTargetProperty.ApplyToCommandBuffer(path.commandBuffer, path.depthTexture);
1034+
renderTargetProperty.ApplyToCommandBuffer(path.commandBuffer, path.depthTexture, blitter);
10351035

10361036
if (renderTargetProperty.Viewport.width > 0)
10371037
{
@@ -1044,7 +1044,7 @@ public void Render(Camera camera, int additionalMask, RenderTargetProperty rende
10441044
bool xrRendering = false;
10451045

10461046
blitter.Blit(path.commandBuffer, BuiltinRenderTextureType.Depth, path.depthTexture.renderTexture, xrRendering);
1047-
path.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
1047+
blitter.SetRenderTarget(path.commandBuffer, BuiltinRenderTextureType.CameraTarget, xrRendering);
10481048
}
10491049
}
10501050

@@ -1075,7 +1075,7 @@ public void Render(Camera camera, int additionalMask, RenderTargetProperty rende
10751075
if (renderTargetProperty != null && renderTargetProperty.colorBufferID.HasValue)
10761076
{
10771077
blitter.Blit(path.commandBuffer, renderTargetProperty.colorBufferID.Value, path.renderTexture.renderTexture, renderTargetProperty.xrRendering);
1078-
path.commandBuffer.SetRenderTarget(renderTargetProperty.colorBufferID.Value, 0, CubemapFace.Unknown, -1);
1078+
blitter.SetRenderTarget(path.commandBuffer, renderTargetProperty.colorBufferID.Value, renderTargetProperty.xrRendering);
10791079

10801080
if (renderTargetProperty.Viewport.width > 0)
10811081
{
@@ -1097,7 +1097,7 @@ public void Render(Camera camera, int additionalMask, RenderTargetProperty rende
10971097
bool xrRendering = false;
10981098

10991099
blitter.Blit(path.commandBuffer, BuiltinRenderTextureType.CameraTarget, path.renderTexture.renderTexture, xrRendering);
1100-
path.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
1100+
blitter.SetRenderTarget(path.commandBuffer, BuiltinRenderTextureType.CameraTarget, xrRendering);
11011101
}
11021102
}
11031103

@@ -1279,7 +1279,7 @@ unsafe void RenderSprite(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
12791279
prop.SetTexture("_BackTex", GetCachedTexture(parameter.GetTexturePtr(efkMaterial.asset.textures.Length), background, depth, DummyTextureType.White));
12801280
}
12811281

1282-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
1282+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
12831283
}
12841284
else
12851285
{
@@ -1293,7 +1293,7 @@ unsafe void RenderSprite(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
12931293
prop.SetColor("fLightColor", EffekseerSystem.LightColor);
12941294
prop.SetColor("fLightAmbient", EffekseerSystem.LightAmbientColor);
12951295

1296-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
1296+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
12971297
}
12981298
else if (parameter.MaterialType == Plugin.RendererMaterialType.BackDistortion ||
12991299
parameter.MaterialType == Plugin.RendererMaterialType.AdvancedBackDistortion)
@@ -1302,12 +1302,12 @@ unsafe void RenderSprite(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
13021302

13031303
if (background != null)
13041304
{
1305-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
1305+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
13061306
}
13071307
}
13081308
else
13091309
{
1310-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
1310+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, parameter.ElementCount * 2 * 3, 1, prop);
13111311
}
13121312
}
13131313

@@ -1491,7 +1491,7 @@ unsafe void RenderModdel(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
14911491
prop.SetTexture("_BackTex", GetCachedTexture(parameter.GetTexturePtr(efkMaterial.asset.textures.Length), background, depth, DummyTextureType.White));
14921492
}
14931493

1494-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
1494+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
14951495
}
14961496
else
14971497
{
@@ -1504,20 +1504,20 @@ unsafe void RenderModdel(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
15041504
prop.SetVector("fLightDirection", EffekseerSystem.LightDirection.normalized);
15051505
prop.SetColor("fLightColor", EffekseerSystem.LightColor);
15061506
prop.SetColor("fLightAmbient", EffekseerSystem.LightAmbientColor);
1507-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
1507+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
15081508
}
15091509
else if (parameter.MaterialType == Plugin.RendererMaterialType.BackDistortion ||
15101510
parameter.MaterialType == Plugin.RendererMaterialType.AdvancedBackDistortion)
15111511
{
15121512
prop.SetVector("g_scale", new Vector4(parameter.DistortionIntensity, 0.0f, 0.0f, 0.0f));
15131513
if (background != null)
15141514
{
1515-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
1515+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
15161516
}
15171517
}
15181518
else
15191519
{
1520-
commandBuffer.DrawProcedural(new Matrix4x4(), material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
1520+
commandBuffer.DrawProcedural(Matrix4x4.identity, material, 0, MeshTopology.Triangles, model.IndexCounts[0], allocated, prop);
15211521
}
15221522
}
15231523

0 commit comments

Comments
 (0)