-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1032 from 3DStreet/focus-component
Use a focus-animation component instead of a fake component
- Loading branch information
Showing
4 changed files
with
104 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
function easeInOutQuad(t) { | ||
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; | ||
} | ||
|
||
AFRAME.registerComponent('focus-animation', { | ||
schema: { | ||
speed: { | ||
type: 'string', | ||
oneOf: ['immediate', 'fast', 'slow'], | ||
default: 'fast' | ||
} | ||
}, | ||
|
||
init() { | ||
this.camera = null; | ||
// Those variables are set by EditorControls | ||
this.transitioning = false; | ||
this.transitionProgress = 0; | ||
this.transitionCamPosStart = new THREE.Vector3(); | ||
this.transitionCamPosEnd = new THREE.Vector3(); | ||
this.transitionCamQuaternionStart = new THREE.Quaternion(); | ||
this.transitionCamQuaternionEnd = new THREE.Quaternion(); | ||
}, | ||
|
||
update() { | ||
if (this.data.speed === 'slow') { | ||
this.transitionSpeed = 0.00025; | ||
} else { | ||
this.transitionSpeed = 0.001; | ||
} | ||
}, | ||
|
||
// Called by EditorControls initially | ||
setCamera(camera, changeEventCallback) { | ||
this.camera = camera; | ||
this.changeEventCallback = changeEventCallback; | ||
}, | ||
|
||
tick(t, delta) { | ||
if (!this.camera) return; | ||
if (this.transitioning) { | ||
if (this.data.speed === 'immediate') { | ||
this.transitioning = false; | ||
this.camera.position.copy(this.transitionCamPosEnd); | ||
this.camera.quaternion.copy(this.transitionCamQuaternionEnd); | ||
this.changeEventCallback(); | ||
return; | ||
} | ||
this.transitionProgress += delta * this.transitionSpeed; | ||
const easeInOutTransitionProgress = easeInOutQuad( | ||
this.transitionProgress | ||
); | ||
|
||
// Set camera position | ||
this.camera.position.lerpVectors( | ||
this.transitionCamPosStart, | ||
this.transitionCamPosEnd, | ||
easeInOutTransitionProgress | ||
); | ||
|
||
this.camera.quaternion.slerpQuaternions( | ||
this.transitionCamQuaternionStart, | ||
this.transitionCamQuaternionEnd, | ||
easeInOutTransitionProgress | ||
); | ||
|
||
if (this.transitionProgress >= 1) { | ||
this.transitioning = false; | ||
this.camera.position.copy(this.transitionCamPosEnd); | ||
this.camera.quaternion.copy(this.transitionCamQuaternionEnd); | ||
} | ||
this.changeEventCallback(); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters