Repository: moeru-ai/airi
File: packages/stage-ui/src/components/Scenes/VRM.vue (Vue)

Code before:

watch(cameraFOV, (newFov) => {
  if (camera.value) {
    camera.value.fov = newFov
    camera.value.updateProjectionMatrix()
  }
})

// not sure if we need this
// watch([width, height], () => {
//   if (camera.value) {
//     camera.value.aspect = width.value / height.value
//     camera.value.updateProjectionMatrix()
//   }
// })
function handleLoadModelProgress(val: number) {
  if (val === 100 && camera.value && controlsRef.value && controlsRef.value.controls) {
    // 设置摄像机位置
    camera.value.position.set(
      initialCameraPosition.value.x,
      initialCameraPosition.value.y,
      initialCameraPosition.value.z,
    )
    camera.value.updateProjectionMatrix()

    // 设置摄像机目标点
    controlsRef.value.controls.target.set(
      modelOrigin.value.x,
      modelOrigin.value.y,
      modelOrigin.value.z,
    )
    controlsRef.value.controls.update()
  }
}

// Bidirectional watch between slider and OrbitControls
watch(() => controlsRef.value?.getDistance(), (newDistance) => {
  if (newDistance !== undefined) {
    // To avoid floating point inaccuracies causing a feedback loop with the other watcher,
    // we can check if the distance has changed significantly.
    if (Math.abs(cameraDistance.value - newDistance) > 1e-6) {
      cameraDistance.value = newDistance
      initialCameraPosition.value = {
        x: camera.value.position.x,
        y: camera.value.position.y,
        z: camera.value.position.z,
      }
    }
  }
})
watch(cameraDistance, (newDistance) => {

Review comment (suggestion):
```suggestion
    // Set camera pos
    camera.value.position.set(
      initialCameraPosition.value.x,
      initialCameraPosition.value.y,
      initialCameraPosition.value.z,
    )
    camera.value.updateProjectionMatrix()

    // Set camera target
```

Code after:

watch(cameraFOV, (newFov) => {
  if (camera.value) {
    camera.value.fov = newFov
    camera.value.updateProjectionMatrix()
  }
})

// not sure if we need this
// watch([width, height], () => {
//   if (camera.value) {
//     camera.value.aspect = width.value / height.value
//     camera.value.updateProjectionMatrix()
//   }
// })
function handleLoadModelProgress(val: number) {
  if (val === 100 && camera.value && controlsRef.value && controlsRef.value.controls) {
    // Set camera pos
    camera.value.position.set(
      initialCameraPosition.value.x,
      initialCameraPosition.value.y,
      initialCameraPosition.value.z,
    )
    camera.value.updateProjectionMatrix()

    // Set camera target
    controlsRef.value.controls.target.set(
      modelOrigin.value.x,
      modelOrigin.value.y,
      modelOrigin.value.z,
    )
    controlsRef.value.controls.update()
  }
}

// Bidirectional watch between slider and OrbitControls
watch(() => controlsRef.value?.getDistance(), (newDistance) => {
  if (newDistance !== undefined) {
    // To avoid floating point inaccuracies causing a feedback loop with the other watcher,
    // we can check if the distance has changed significantly.
    if (Math.abs(cameraDistance.value - newDistance) > 1e-6) {
      cameraDistance.value = newDistance
      initialCameraPosition.value = {
        x: camera.value.position.x,
        y: camera.value.position.y,
        z: camera.value.position.z,
      }
    }
  }
})
watch(cameraDistance, (newDistance) => {