Skip to content

Commit

Permalink
select projection to focus on by clicking #20
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanatra committed Feb 5, 2025
1 parent 17ae6c2 commit add09c5
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,37 @@ class VantageRenderer extends HTMLElement {
const screens = new Group()
screens.name = 'vantage:screens'
this.scene.add(setupLights(), screens)

this.createFocusMarker()

this.renderer.domElement.addEventListener('click', () => {
this.focusOnCamera()
})
}
focusOnCamera() {
const raycaster = new Raycaster()
raycaster.setFromCamera(this.mouse, this.cameraOperator.mapCamera)

let candidate = null
let minDistance = Infinity

Object.values(this.projections).forEach((p) => {
if (p.attributes && p.attributes.orthographic) return
const targetObj = p.plane || p.helper
const intersects = raycaster.intersectObject(targetObj, true)
if (intersects.length > 0 && intersects[0].distance < minDistance) {
minDistance = intersects[0].distance
candidate = p
}
})

if (candidate) {
Object.values(this.projections).forEach((p) => {
if (p !== candidate) p.focus = false
})
candidate.focus = true
this.cameraOperator.camera = candidate.camera
}
}
createFocusMarker() {
const geom = new SphereGeometry(3, 16, 16)
const mat = new MeshBasicMaterial({
Expand Down Expand Up @@ -235,7 +262,7 @@ class VantageRenderer extends HTMLElement {

const index = Array.prototype.indexOf.call(this.children, element)
Object.values(this.projections).forEach((projection) => {
if (projection.index >= index) projection.index++
if (projection.index >= index) projection.index
})

const projection = new Projection({
Expand Down Expand Up @@ -290,12 +317,7 @@ class VantageRenderer extends HTMLElement {
break
default:
projection[property] = value
// projection.update()
}
// if (projection.focus) {
// // console.log('set focus')
// this.cameraOperator.camera = projection.camera
// }
}

removeProjection({ id }) {
Expand Down Expand Up @@ -390,4 +412,4 @@ class VantageProjection extends HTMLElement {

customElements.define('vantage-renderer', VantageRenderer)
customElements.define('vantage-projection', VantageProjection)
export default VantageRenderer
export default VantageRenderer

0 comments on commit add09c5

Please sign in to comment.