Skip to content
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

Add texture-array option to projection layer sample #205

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions layers-samples/proj-layer.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
controllers or menu systems/other UI elements.
<a class="back" href="./index.html">Back</a>
</p>
<input type="checkbox" id="texture-array">Use Texture Array</input>
</details>
</header>
<main style='text-align: center;'>
Expand All @@ -65,6 +66,7 @@
}

// XR globals.
let textureArrayCheck = document.getElementById('texture-array');
let xrButton = null;
let xrSession = null;
let xrRefSpace = null;
Expand All @@ -80,6 +82,7 @@

// Layer globals
let proj_layer = null;
let usingTextureArray = false;

function initXR() {
xrButton = new WebXRButton({
Expand Down Expand Up @@ -144,7 +147,10 @@

session.requestReferenceSpace('local').then((refSpace) => {
xrRefSpace = refSpace;
proj_layer = xrGLFactory.createProjectionLayer({ space: refSpace, stencil: false });
usingTextureArray = textureArrayCheck.checked;
proj_layer = xrGLFactory.createProjectionLayer({
textureType: usingTextureArray ? 'texture-array' : 'texture',
space: refSpace, stencil: false });
session.updateRenderState({ layers: [proj_layer] });

session.requestAnimationFrame(onXRFrame);
Expand Down Expand Up @@ -183,24 +189,39 @@
glLayer.framebuffer = xrFramebuffer;
viewport = glLayer.viewport;
gl.bindFramebuffer(gl.FRAMEBUFFER, xrFramebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glLayer.colorTexture, 0);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, glLayer.depthStencilTexture, 0);

gl.enable(gl.SCISSOR_TEST);
gl.scissor(viewport.x, viewport.y, viewport.width, viewport.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.disable(gl.SCISSOR_TEST);

// Gather all the values needed for one view and push it into the
// array of views to be drawn. WebXRView is a utility class that
// holds all the necessary values for drawing a single view.

// In future samples we'll hide this part away as well by using the
// scene.drawXRViews() function, which handles gathering these
// values internally.
views.push(new WebXRView(view, glLayer, viewport));

if (usingTextureArray) {
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, glLayer.colorTexture, 0, glLayer.imageIndex);
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, glLayer.depthStencilTexture, 0, glLayer.imageIndex);

// No need to set the scissor when clearing a layered texture, since only one layer is exposed at a time.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

scene.drawViewArray([new WebXRView(view, glLayer, viewport)]);
} else {
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, glLayer.colorTexture, 0);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, glLayer.depthStencilTexture, 0);

// If you need to clear a non-layered texture you must set the scissor to the sub image viewport. Otherwise
// you will clear sections of the texture that have already been drawn to by previous views.
gl.enable(gl.SCISSOR_TEST);
gl.scissor(viewport.x, viewport.y, viewport.width, viewport.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.disable(gl.SCISSOR_TEST);

// Gather all the values needed for one view and push it into the
// array of views to be drawn. WebXRView is a utility class that
// holds all the necessary values for drawing a single view.

// In future samples we'll hide this part away as well by using the
// scene.drawXRViews() function, which handles gathering these
// values internally.
views.push(new WebXRView(view, glLayer, viewport));
}
}
if (!usingTextureArray) {
scene.drawViewArray(views);
}
scene.drawViewArray(views);
}
scene.endFrame();
}
Expand Down