VRDisplay: requestAnimationFrame() Methode
Deprecated
Avoid using this feature in new projects. Development of the WebVR API halted in favor of WebXR. This feature may be a candidate for removal from web standards or browsers.>
Consider using the following features instead: WebXR.>
Nicht standardisiert: Diese Funktion ist nicht standardisiert. Wir raten davon ab, nicht-standardisierte Funktionen auf produktiven Webseiten zu verwenden, da sie nur von bestimmten Browsern unterstützt werden und sich in Zukunft ändern oder entfernt werden können. Unter Umständen kann sie jedoch eine geeignete Option sein, wenn es keine standardisierte Alternative gibt.
Die requestAnimationFrame() Methode des VRDisplay Interfaces ist eine spezielle Implementierung von Window.requestAnimationFrame, die eine Rückruffunktion enthält, die jedes Mal aufgerufen wird, wenn ein neues Frame der VRDisplay-Präsentation gerendert wird:
Hinweis: Diese Methode war Teil der alten WebVR API. Sie wurde durch die WebXR Device API ersetzt.
- Wenn das
VRDisplaykeine Szene präsentiert, ist dies funktional äquivalent zuWindow.requestAnimationFrame. - Wenn das
VRDisplaypräsentiert, wird der Rückruf in der nativen Bildwiederholrate aufgerufen.
Syntax
requestAnimationFrame(callback)
Parameter
callback-
Eine Rückruffunktion, die jedes Mal aufgerufen wird, wenn ein neues Frame der
VRDisplay-Präsentation gerendert wird.
Rückgabewert
Ein langer Wert, der den Handle des requestAnimationFrame()-Aufrufs darstellt. Dieser kann dann an einen VRDisplay.cancelAnimationFrame() Aufruf übergeben werden, um den Rückruf abzumelden.
Beispiele
const frameData = new VRFrameData();
let vrDisplay;
navigator.getVRDisplays().then((displays) => {
vrDisplay = displays[0];
console.log("Display found");
// Starting the presentation when the button is clicked: It can only be called in response to a user gesture
btn.addEventListener("click", () => {
vrDisplay.requestPresent([{ source: canvas }]).then(() => {
drawVRScene();
});
});
});
// WebVR: Draw the scene for the WebVR display.
function drawVRScene() {
// WebVR: Request the next frame of the animation
vrSceneFrame = vrDisplay.requestAnimationFrame(drawVRScene);
// Populate frameData with the data of the next frame to display
vrDisplay.getFrameData(frameData);
// You can get the position, orientation, etc. of the display from the current frame's pose
const curFramePose = frameData.pose;
const curPos = curFramePose.position;
const curOrient = curFramePose.orientation;
// Clear the canvas before we start drawing on it.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// WebVR: Create the required projection and view matrix locations needed
// for passing into the uniformMatrix4fv methods below
const projectionMatrixLocation = gl.getUniformLocation(
shaderProgram,
"projMatrix",
);
const viewMatrixLocation = gl.getUniformLocation(shaderProgram, "viewMatrix");
// WebVR: Render the left eye's view to the left half of the canvas
gl.viewport(0, 0, canvas.width * 0.5, canvas.height);
gl.uniformMatrix4fv(
projectionMatrixLocation,
false,
frameData.leftProjectionMatrix,
);
gl.uniformMatrix4fv(viewMatrixLocation, false, frameData.leftViewMatrix);
drawGeometry();
// WebVR: Render the right eye's view to the right half of the canvas
gl.viewport(canvas.width * 0.5, 0, canvas.width * 0.5, canvas.height);
gl.uniformMatrix4fv(
projectionMatrixLocation,
false,
frameData.rightProjectionMatrix,
);
gl.uniformMatrix4fv(viewMatrixLocation, false, frameData.rightViewMatrix);
drawGeometry();
function drawGeometry() {
// draw the view for each eye
}
// …
// WebVR: Indicate that we are ready to present the rendered frame to the VR display
vrDisplay.submitFrame();
}
Hinweis: Sie können diesen vollständigen Code unter raw-webgl-example sehen.
Spezifikationen
Diese Methode war Teil der alten WebVR API, die durch die WebXR Device API ersetzt wurde. Sie ist nicht mehr auf dem Weg, ein Standard zu werden.
Bis alle Browser die neuen WebXR APIs implementiert haben, wird empfohlen, sich auf Frameworks wie A-Frame, Babylon.js oder Three.js oder ein Polyfill zu verlassen, um WebXR-Anwendungen zu entwickeln, die in allen Browsern funktionieren werden. Lesen Sie Metas Porting von WebVR zu WebXR Leitfaden für weitere Informationen.