I am trying to change the position of the camera using
camera.position.set(a, 0, 0);
controls.update();
with “a” being a Number changing each frame from 0 to 20, then from 20 to 0 and so on.
The camera’s position is changing when the immersive-vr session is still off:
However when the vr button is triggered, the immersive-vr session opens, but the scene isn’t changing position.
Using the webxr dev tool from the browser, I can change the orientation of the headset so the image isn’t frozen.
Any idea?
The html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js ar - cones</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<!-- <div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> ar - cones<br/>(Chrome Android 81+)
</div> -->
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "/node_modules/three/build/three.module.js"
}
}
</script>
<script type="module" src="immersion4.js"></script>
</body>
</html>
The immersion4.js script :
import * as THREE from '/node_modules/three/build/three.module.js';
import { GLTFLoader } from '/node_modules/three/examples/jsm/loaders/GLTFLoader.js';
import { ARButton } from '/node_modules/three/examples/jsm/webxr/ARButton.js';
import { VRButton } from '/node_modules/three/examples/jsm/webxr/VRButton.js';
import { OrbitControls } from '/node_modules/three/examples/jsm/controls/OrbitControls.js';
let camera, scene, renderer, views, frame, refSpace, pos;
let controls;
let a=0, b=null;
init();
animate();
function init() {
const container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
const light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
light.position.set( 0.5, 1, 0.25 );
scene.add( light );
let loader = new GLTFLoader()
loader.load(
'media/gltf/space/space.gltf',
function ( gltf ) {
gltf.scale = [0.1, 0.1, 0.1];
scene.add(gltf.scene);
},
// called while loading is progressing
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened: ' + error);
});
//
renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.xr.enabled = true;
renderer.xr.setReferenceSpaceType('local'); // thought this could change something
container.appendChild( renderer.domElement );
document.body.appendChild( VRButton.createButton( renderer ) );
//
controls = new OrbitControls( camera, renderer.domElement );
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
// To make the back and fourth motion
if ( b == null ) {
a += 0.1;
} else if ( b == 'back' ) {
a -= 0.1;
}
if ( a > 20 && b == null ) {
b = 'back';
} else if ( a < 0 ) {
b = null;
}
frame = renderer.xr.getFrame();
// frame = null when none XRSession got requested
if ( frame != null ) {
refSpace = renderer.xr.getReferenceSpace();
views = frame.getViewerPose(refSpace).views;
pos = views[0].transform.position;
//console.log([pos.x, pos.y, pos.z]);
//console.log(pos);
//camera.position.set(pos.x + a, pos.y, pos.z);
// SCENE ISN'T CHANGING WHEN XRSession IS OPENED
camera.position.set(a, 0, 0);
controls.update();
console.log(camera.position);
}
// SCENE IS CHANGING IF NO XRSession IS OPENED
camera.position.set(a, 0, 0);
controls.update();
console.log(camera.position);
renderer.render( scene, camera );
}
Thank youuu