Skip to content

Commit ab8b4a4

Browse files
committed
Add texture-array option to projection layer sample
1 parent b4fc6ac commit ab8b4a4

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

layers-samples/proj-layer.html

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
controllers or menu systems/other UI elements.
4545
<a class="back" href="./index.html">Back</a>
4646
</p>
47+
<input type="checkbox" id="texture-array">Use Texture Array</input>
4748
</details>
4849
</header>
4950
<main style='text-align: center;'>
@@ -65,6 +66,7 @@
6566
}
6667

6768
// XR globals.
69+
let textureArrayCheck = document.getElementById('texture-array');
6870
let xrButton = null;
6971
let xrSession = null;
7072
let xrRefSpace = null;
@@ -80,6 +82,7 @@
8082

8183
// Layer globals
8284
let proj_layer = null;
85+
let usingTextureArray = false;
8386

8487
function initXR() {
8588
xrButton = new WebXRButton({
@@ -144,7 +147,10 @@
144147

145148
session.requestReferenceSpace('local').then((refSpace) => {
146149
xrRefSpace = refSpace;
147-
proj_layer = xrGLFactory.createProjectionLayer({ space: refSpace, stencil: false });
150+
usingTextureArray = textureArrayCheck.checked;
151+
proj_layer = xrGLFactory.createProjectionLayer({
152+
textureType: usingTextureArray ? 'texture-array' : 'texture',
153+
space: refSpace, stencil: false });
148154
session.updateRenderState({ layers: [proj_layer] });
149155

150156
session.requestAnimationFrame(onXRFrame);
@@ -183,24 +189,39 @@
183189
glLayer.framebuffer = xrFramebuffer;
184190
viewport = glLayer.viewport;
185191
gl.bindFramebuffer(gl.FRAMEBUFFER, xrFramebuffer);
186-
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glLayer.colorTexture, 0);
187-
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, glLayer.depthStencilTexture, 0);
188-
189-
gl.enable(gl.SCISSOR_TEST);
190-
gl.scissor(viewport.x, viewport.y, viewport.width, viewport.height);
191-
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
192-
gl.disable(gl.SCISSOR_TEST);
193-
194-
// Gather all the values needed for one view and push it into the
195-
// array of views to be drawn. WebXRView is a utility class that
196-
// holds all the necessary values for drawing a single view.
197-
198-
// In future samples we'll hide this part away as well by using the
199-
// scene.drawXRViews() function, which handles gathering these
200-
// values internally.
201-
views.push(new WebXRView(view, glLayer, viewport));
192+
193+
if (usingTextureArray) {
194+
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, glLayer.colorTexture, 0, glLayer.imageIndex);
195+
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, glLayer.depthStencilTexture, 0, glLayer.imageIndex);
196+
197+
// No need to set the scissor when clearing a layered texture, since only one layer is exposed at a time.
198+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
199+
200+
scene.drawViewArray([new WebXRView(view, glLayer, viewport)]);
201+
} else {
202+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glLayer.colorTexture, 0);
203+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, glLayer.depthStencilTexture, 0);
204+
205+
// If you need to clear a non-layered texture you must set the scissor to the sub image viewport. Otherwise
206+
// you will clear sections of the texture that have already been drawn to by previous views.
207+
gl.enable(gl.SCISSOR_TEST);
208+
gl.scissor(viewport.x, viewport.y, viewport.width, viewport.height);
209+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
210+
gl.disable(gl.SCISSOR_TEST);
211+
212+
// Gather all the values needed for one view and push it into the
213+
// array of views to be drawn. WebXRView is a utility class that
214+
// holds all the necessary values for drawing a single view.
215+
216+
// In future samples we'll hide this part away as well by using the
217+
// scene.drawXRViews() function, which handles gathering these
218+
// values internally.
219+
views.push(new WebXRView(view, glLayer, viewport));
220+
}
221+
}
222+
if (!usingTextureArray) {
223+
scene.drawViewArray(views);
202224
}
203-
scene.drawViewArray(views);
204225
}
205226
scene.endFrame();
206227
}

0 commit comments

Comments
 (0)