2023-05-12 22:06:21 +02:00
|
|
|
xrf.frag.pos = function(v, opts){
|
2023-05-09 17:42:29 +02:00
|
|
|
let { frag, mesh, model, camera, scene, renderer, THREE} = opts
|
2023-11-24 17:32:53 +01:00
|
|
|
|
2024-01-24 18:11:37 +00:00
|
|
|
let pos = v
|
2023-11-24 17:32:53 +01:00
|
|
|
|
|
|
|
|
// spec: indirect coordinate using objectname: https://xrfragment.org/#navigating%203D
|
2024-01-24 18:11:37 +00:00
|
|
|
if( pos.x == undefined ){
|
2023-11-24 17:32:53 +01:00
|
|
|
let obj = scene.getObjectByName(v.string)
|
|
|
|
|
if( !obj ) return
|
2024-01-24 18:11:37 +00:00
|
|
|
pos = obj.position.clone()
|
2023-11-24 17:32:53 +01:00
|
|
|
obj.getWorldPosition(pos)
|
|
|
|
|
camera.position.copy(pos)
|
|
|
|
|
}else{
|
|
|
|
|
// spec: direct coordinate: https://xrfragment.org/#navigating%203D
|
2024-01-24 18:11:37 +00:00
|
|
|
camera.position.x = pos.x
|
|
|
|
|
camera.position.y = pos.y
|
|
|
|
|
camera.position.z = pos.z
|
2023-11-24 17:32:53 +01:00
|
|
|
}
|
2024-01-24 18:11:37 +00:00
|
|
|
|
2024-02-01 09:04:01 +00:00
|
|
|
if( xrf.debug ) console.log(`#pos.js: setting camera to position ${pos.x},${pos.y},${pos.z}`)
|
|
|
|
|
|
2024-04-25 15:56:29 +00:00
|
|
|
xrf.frag.pos.last = v.string // remember
|
2024-01-24 18:11:37 +00:00
|
|
|
|
2023-12-12 18:09:30 +01:00
|
|
|
camera.updateMatrixWorld()
|
2023-05-04 21:28:12 +02:00
|
|
|
}
|
2024-02-01 09:04:01 +00:00
|
|
|
|
2024-04-15 14:06:54 +00:00
|
|
|
xrf.frag.pos.get = function(precision,randomize){
|
|
|
|
|
if( !precision ) precision = 2;
|
|
|
|
|
if( typeof THREE == 'undefined' ) THREE = xrf.THREE
|
|
|
|
|
let radToDeg = THREE.MathUtils.radToDeg
|
|
|
|
|
let toDeg = (x) => x / (Math.PI / 180)
|
|
|
|
|
let camera = xrf.camera
|
|
|
|
|
if( randomize ){
|
|
|
|
|
camera.position.x += Math.random()/10
|
|
|
|
|
camera.position.z += Math.random()/10
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// *TODO* add camera direction
|
|
|
|
|
let direction = new xrf.THREE.Vector3()
|
|
|
|
|
camera.getWorldDirection(direction)
|
|
|
|
|
const pitch = Math.asin(direction.y);
|
|
|
|
|
const yaw = Math.atan2(direction.x, direction.z);
|
|
|
|
|
const pitchInDegrees = pitch * 180 / Math.PI;
|
|
|
|
|
const yawInDegrees = yaw * 180 / Math.PI;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
x: String(camera.position.x.toFixed(2)),
|
|
|
|
|
y: String(camera.position.y.toFixed(2)),
|
|
|
|
|
z: String(camera.position.z.toFixed(2)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 09:04:01 +00:00
|
|
|
xrf.addEventListener('reset', (opts) => {
|
|
|
|
|
// set the player to position 0,0,0
|
|
|
|
|
xrf.camera.position.set(0,0,0)
|
|
|
|
|
})
|