Skip to content

Commit 6c4ecee

Browse files
committed
IT WORKS! - Stopped render using the XR SDK:
Back to old hacks for bindFramebuffer and transparent background. Added a hack to modify Unity's draw display shader.
1 parent 1b84866 commit 6c4ecee

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

Packages/webxr-interactions/Runtime/Scripts/SpectatorCamera.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using UnityEngine.Rendering;
23

34
namespace WebXR.Interactions
45
{
@@ -24,11 +25,21 @@ private void OnEnable()
2425
WebXRManager.OnXRChange += HandleOnXRChange;
2526
currentXRState = WebXRManager.Instance.XRState;
2627
TryUpdateCameraState();
28+
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
2729
}
2830

2931
private void OnDisable()
3032
{
3133
WebXRManager.OnXRChange -= HandleOnXRChange;
34+
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
35+
}
36+
37+
void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
38+
{
39+
if (camera == _camera)
40+
{
41+
WebXRManager.Instance.PreRenderSpectatorCamera();
42+
}
3243
}
3344

3445
private void OnPreRender()

Packages/webxr/Runtime/Plugins/WebGL/WebXRDisplayProvider.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ UnitySubsystemErrorCode WebXRDisplayProvider::Start()
108108
s_PoseXPositionPerPass[1] = 0;
109109
}
110110
transparentBackground = *(m_ViewsDataArray + 55) > 0;
111-
webXRFrameBuffer = WebXRInitDisplayRender();
111+
/*webXRFrameBuffer = WebXRInitDisplayRender();
112112
113113
webxrVertexShader = glCreateShader(GL_VERTEX_SHADER);
114114
const GLchar* vertexSource = R"(#version 300 es
@@ -155,7 +155,7 @@ UnitySubsystemErrorCode WebXRDisplayProvider::Start()
155155
glBufferData(GL_ARRAY_BUFFER, sizeof(uvData), uvData, GL_STATIC_DRAW);
156156
157157
glEnableVertexAttribArray(webXRTextureCoordsAttributeLocation);
158-
glVertexAttribPointer(webXRTextureCoordsAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, uvData);
158+
glVertexAttribPointer(webXRTextureCoordsAttributeLocation, 2, GL_FLOAT, GL_FALSE, 0, uvData);*/
159159

160160
return kUnitySubsystemErrorCodeSuccess;
161161
}
@@ -164,13 +164,13 @@ UnitySubsystemErrorCode WebXRDisplayProvider::GfxThread_Start(UnityXRRenderingCa
164164
{
165165
renderingCaps.noSinglePassRenderingSupport = true;
166166
renderingCaps.invalidateRenderStateAfterEachCallback = false;
167-
renderingCaps.skipPresentToMainScreen = true;
167+
renderingCaps.skipPresentToMainScreen = false;
168168
return kUnitySubsystemErrorCodeSuccess;
169169
}
170170

171171
UnitySubsystemErrorCode WebXRDisplayProvider::GfxThread_SubmitCurrentFrame()
172172
{
173-
glBindFramebuffer(GL_FRAMEBUFFER, webXRFrameBuffer);
173+
/*glBindFramebuffer(GL_FRAMEBUFFER, webXRFrameBuffer);
174174
glViewport(0, 0, static_cast<int>(frameBufferWidth), static_cast<int>(frameBufferHeight));
175175
glClearColor(0, 0, 0, 0);
176176
glDepthMask(false); // solves bug in some android phones
@@ -200,8 +200,7 @@ UnitySubsystemErrorCode WebXRDisplayProvider::GfxThread_SubmitCurrentFrame()
200200
glBindTexture(GL_TEXTURE_2D, uDesc.color.referenceTextureId);
201201
glUniform1i(webXRTextureAttributeLocation, 0);
202202
203-
glDrawArrays(GL_TRIANGLES, 0, 6);
204-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
203+
glDrawArrays(GL_TRIANGLES, 0, 6);*/
205204

206205
return kUnitySubsystemErrorCodeSuccess;
207206
}
@@ -362,12 +361,12 @@ UnitySubsystemErrorCode WebXRDisplayProvider::GfxThread_FinalBlitToGameViewBackB
362361

363362
void WebXRDisplayProvider::Stop()
364363
{
365-
WebXRDestructDisplayRender(webXRFrameBuffer);
364+
/*WebXRDestructDisplayRender(webXRFrameBuffer);
366365
glDeleteProgram(webXRDisplayProgram);
367366
glDeleteShader(webxrVertexShader);
368367
glDeleteShader(webxrFragmentShader);
369368
glDeleteBuffers(1, &webXRPositionBuffer);
370-
glDeleteBuffers(1, &webXRUVBuffer);
369+
glDeleteBuffers(1, &webXRUVBuffer);*/
371370
}
372371

373372
void WebXRDisplayProvider::Shutdown()

Packages/webxr/Runtime/Plugins/WebGL/transparent_bg.jslib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// More details at https://support.unity.com/hc/en-us/articles/208892946-How-can-I-make-the-canvas-transparent-on-WebGL-
33
var LibraryGLClear = {
44
$colorMaskValue: [true, true, true, true],
5-
glClear1: function (mask) { // disable hack while WIP
5+
glClear: function (mask) {
66
if (mask == 0x00004000 && GLctx.dontClearAlphaOnly) {
77
if (!colorMaskValue[0] && !colorMaskValue[1] && !colorMaskValue[2] && colorMaskValue[3])
88
// We are trying to clear alpha only -- skip.
99
return;
1010
}
1111
GLctx.clear(mask);
1212
},
13-
glColorMask1: function (red, green, blue, alpha) { // disable hack while WIP
13+
glColorMask: function (red, green, blue, alpha) {
1414
colorMaskValue[0] = !!red;
1515
colorMaskValue[1] = !!green;
1616
colorMaskValue[2] = !!blue;

Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,58 @@ setTimeout(function () {
1818

1919
return GL.createContextOld(canvas, contextAttributes);
2020
}
21+
22+
const shaderBug = `#version 300 es
23+
24+
#define HLSLCC_ENABLE_UNIFORM_BUFFERS 1
25+
#if HLSLCC_ENABLE_UNIFORM_BUFFERS
26+
#define UNITY_UNIFORM
27+
#else
28+
#define UNITY_UNIFORM uniform
29+
#endif
30+
#define UNITY_SUPPORTS_UNIFORM_LOCATION 0
31+
#if UNITY_SUPPORTS_UNIFORM_LOCATION
32+
#define UNITY_LOCATION(x) layout(location = x)
33+
#define UNITY_BINDING(x) layout(binding = x, std140)
34+
#else
35+
#define UNITY_LOCATION(x)
36+
#define UNITY_BINDING(x) layout(std140)
37+
#endif
38+
uniform vec4 _ScaleBias;
39+
uniform vec4 _ScaleBiasRt;
40+
out highp vec2 vs_TEXCOORD0;
41+
vec4 u_xlat0;
42+
int u_xlati0;
43+
uvec2 u_xlatu0;
44+
vec4 u_xlat1;
45+
int u_xlati4;
46+
void main()
47+
{
48+
u_xlati0 = int(uint(uint(gl_VertexID) & 1u));
49+
u_xlatu0.y = uint(uint(gl_VertexID) >> 1u);
50+
u_xlati4 = (-u_xlati0) + (-int(u_xlatu0.y));
51+
u_xlati0 = u_xlati0 + int(u_xlatu0.y);
52+
u_xlatu0.x = uint(uint(u_xlati0) & 1u);
53+
u_xlat1.xw = vec2(u_xlatu0.yx);
54+
vs_TEXCOORD0.xy = u_xlat1.xw * _ScaleBias.xy + _ScaleBias.zw;
55+
u_xlati0 = u_xlati4 + 1;
56+
u_xlatu0.x = uint(uint(u_xlati0) & 1u);
57+
u_xlat1.y = float(u_xlatu0.x);
58+
u_xlat0.xy = u_xlat1.xy * _ScaleBiasRt.xy + _ScaleBiasRt.zw;
59+
u_xlat0.z = float(-1.0);
60+
u_xlat0.w = float(1.0);
61+
gl_Position = u_xlat0 * vec4(2.0, -2.0, 1.0, 1.0) + vec4(-1.0, 1.0, 0.0, 0.0);
62+
return;
63+
}`
64+
GL.getSourceOld = GL.getSource;
65+
GL.getSource = function (shader, count, string, length) {
66+
var source = GL.getSourceOld(shader, count, string, length);
67+
if (shaderBug == source) {
68+
source = source.replace("vs_TEXCOORD0.xy = u_xlat1.xw * _ScaleBias.xy + _ScaleBias.zw;",
69+
"vs_TEXCOORD0.xy = u_xlat1.xw * vec2(1.0, 1.0);");
70+
}
71+
return source
72+
}
2173
}
2274

2375

@@ -626,7 +678,7 @@ setTimeout(function () {
626678
}
627679

628680
// bindFramebuffer frameBufferObject null in XRSession should use XRWebGLLayer FBO instead
629-
/*thisXRMananger.ctx.oldBindFramebuffer = thisXRMananger.ctx.bindFramebuffer;
681+
thisXRMananger.ctx.oldBindFramebuffer = thisXRMananger.ctx.bindFramebuffer;
630682
thisXRMananger.ctx.bindFramebuffer = function (target, fbo) {
631683
if (!fbo && !Module.WebXR.isSpectatorCameraRendering) {
632684
if (thisXRMananger.xrSession && thisXRMananger.xrSession.isInSession) {
@@ -636,7 +688,7 @@ setTimeout(function () {
636688
}
637689
}
638690
return thisXRMananger.ctx.oldBindFramebuffer(target, fbo)
639-
};*/
691+
};
640692
}
641693
}
642694

@@ -965,15 +1017,15 @@ setTimeout(function () {
9651017
}
9661018

9671019
Module.WebXR.isSpectatorCameraRendering = false;
968-
/*this.ctx.bindFramebuffer(this.ctx.FRAMEBUFFER, glLayer.framebuffer);
1020+
this.ctx.bindFramebuffer(this.ctx.FRAMEBUFFER, glLayer.framebuffer);
9691021
if (session.isAR) {
9701022
// Workaround for Chromium depth bug https://bugs.chromium.org/p/chromium/issues/detail?id=1167450#c21
9711023
this.ctx.depthMask(false);
9721024
this.ctx.clear(this.ctx.DEPTH_BUFFER_BIT);
9731025
this.ctx.depthMask(true);
9741026
} else {
9751027
this.ctx.clear(this.ctx.COLOR_BUFFER_BIT | this.ctx.DEPTH_BUFFER_BIT);
976-
}*/
1028+
}
9771029

9781030
var pose = frame.getViewerPose(session.refSpace);
9791031
if (!pose) {

0 commit comments

Comments
 (0)