Skip to content

Based on example Locar and ThreeJS. The model drifts when the device is rotated. #18

@AR-Go-Coder

Description

@AR-Go-Coder

Hello.
Please help, I have implemented a simple scene with one object similar to the example. But the model will drift when the device rotates, without walking. It looks like deviceOrientationControls.update() is slowly updating.

locar version - master
threejs - 0.175.0

import * as THREE from 'three'
import * as LocAR from 'locar'
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/three@0.175.0/examples/jsm/loaders/GLTFLoader.js'

...

const camera = new THREE.PerspectiveCamera(80,window.innerWidth / window.innerHeight,0.1,1000)

const renderer = new THREE.WebGLRenderer({antialias: true,logarithmicDepthBuffer: true})
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)

const scene = new THREE.Scene()

const locar = new LocAR.LocationBased(scene, camera)

window.addEventListener('resize', e => {
  renderer.setSize(window.innerWidth, window.innerHeight)
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()
})

const cam = new LocAR.Webcam({
  idealWidth: isIOS || isAndroid ? window.innerHeight : window.innerWidth, //1024,
  idealHeight: isIOS || isAndroid ? window.innerWidth : window.innerHeight, //768,
  onVideoStarted: texture => {
    scene.background = texture
  }
})

let firstLocation = true
let mainModelWorldPos = new THREE.Vector3()

const deviceOrientationControls = new LocAR.DeviceOrientationControls(camera)
const loader = new GLTFLoader()

locar.on('gpsupdate', (pos, distMoved) => {
  if (firstLocation) {
    loader.load(
      modelUrl,
      gltf => {
        const model = gltf.scene
        model.scale.set(...modelScale)
        model.rotation.set(
          THREE.MathUtils.degToRad(modelRotation[0]),
          THREE.MathUtils.degToRad(modelRotation[1]),
          THREE.MathUtils.degToRad(modelRotation[2])
        )
        locar.add(model, targetLon, targetLat)
        model.position.y = -1.6
        model.getWorldPosition(mainModelWorldPos)
      },
      undefined,
      error => console.error('Loading model error:', error)
    )

    firstLocation = false
  }
})

locar.startGps()

renderer.setAnimationLoop(animate)

function animate () {
  const delta = clock.getDelta()
  deviceOrientationControls.update()

  renderer.render(scene, camera)
}

I also tried moving the sensor processing to the interval, but the behavior remained the same

setInterval(() => {
  deviceOrientationControls.update()
}, 1)

link to video for visual demonstration of the problem

Is there any way to make the model stand more stable?
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions