-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement auto camera animation
- Loading branch information
1 parent
a036c8d
commit a13ab93
Showing
9 changed files
with
319 additions
and
264 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
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,177 @@ | ||
import { CatmullRomCurve3, Vector3 } from "three"; | ||
import { gsap } from "gsap"; | ||
|
||
// EXPERIENCES | ||
import { HomeExperience } from "."; | ||
|
||
// BLUEPRINTS | ||
import { ExperienceBasedBlueprint } from "~/blueprints/experiences/experience-based.blueprint"; | ||
import { Config } from "~/config"; | ||
|
||
export const defaultCameraPath = new CatmullRomCurve3([ | ||
new Vector3(0, 5.5, 21), | ||
new Vector3(12, 10, 12), | ||
new Vector3(21, 5.5, 0), | ||
new Vector3(12, 3.7, 12), | ||
new Vector3(0, 5.5, 21), | ||
]); | ||
export const defaultCameraTarget = new Vector3(0, 2, 0); | ||
|
||
export class CameraAnimation extends ExperienceBasedBlueprint { | ||
protected readonly _experience = new HomeExperience(); | ||
|
||
private readonly _appSizes = this._experience.app.sizes; | ||
private readonly _appCamera = this._experience.app.camera; | ||
private readonly _camera = this._experience.camera; | ||
private readonly _navigation = this._experience.navigation; | ||
|
||
public enabled = false; | ||
public cameraPath = defaultCameraPath; | ||
public cameraTarget = defaultCameraTarget; | ||
public progress = { | ||
current: 0, | ||
target: 0, | ||
ease: 0.1, | ||
}; | ||
public positionInCurve = new Vector3(); | ||
public reversed = false; | ||
public timeline = gsap.timeline(); | ||
public mouseDowned = false; | ||
public cursorCoordinate = { x: 0, y: 0 }; | ||
public normalizedCursorCoordinate = { x: 0, y: 0 }; | ||
|
||
private _onWheel?: (e: WheelEvent) => unknown; | ||
private _onMousemove?: (e: MouseEvent) => unknown; | ||
private _onMousedown?: (e: MouseEvent) => unknown; | ||
private _onMouseUp?: (e: MouseEvent) => unknown; | ||
|
||
private _wheelEvent(e: WheelEvent) { | ||
if (!this.enabled) return; | ||
|
||
if (e.deltaY < 0) { | ||
this.progress.target += 0.05; | ||
this.reversed = false; | ||
|
||
return; | ||
} | ||
|
||
this.progress.target -= 0.05; | ||
this.reversed = true; | ||
} | ||
|
||
private _mouseMoveEvent(e: MouseEvent) { | ||
if (this.enabled && this.mouseDowned) { | ||
if (e.clientX < this.cursorCoordinate.x) { | ||
this.progress.target += 0.002; | ||
this.reversed = false; | ||
} else if (e.clientX > this.cursorCoordinate.x) { | ||
this.progress.target -= 0.002; | ||
this.reversed = true; | ||
} | ||
} | ||
|
||
if (!this.enabled) { | ||
this.normalizedCursorCoordinate.x = | ||
e.clientX / this._appSizes?.width - 0.5; | ||
this.normalizedCursorCoordinate.y = | ||
e.clientY / this._appSizes?.height - 0.5; | ||
} | ||
|
||
this.cursorCoordinate = { x: e.clientX, y: e.clientY }; | ||
} | ||
|
||
private _mouseDownEvent(e: MouseEvent) { | ||
this.mouseDowned = true; | ||
// if (this.focusedPosition && !this.mouseOverBubble) | ||
// this._camera?.cameraZoomIn(); | ||
} | ||
|
||
private _mouseUpEvent(e: MouseEvent) { | ||
this.mouseDowned = false; | ||
// if (this.focusedPosition && !this.mouseOverBubble) | ||
// this._experience.camera?.cameraZoomOut(); | ||
} | ||
|
||
public construct() { | ||
this._onWheel = (e) => this._wheelEvent(e); | ||
this._onMousemove = (e) => this._mouseMoveEvent(e); | ||
this._onMousedown = (e) => this._mouseDownEvent(e); | ||
this._onMouseUp = (e) => this._mouseUpEvent(e); | ||
|
||
window.addEventListener("wheel", this._onWheel); | ||
window.addEventListener("mousemove", this._onMousemove); | ||
window.addEventListener("mousedown", this._onMousedown); | ||
window.addEventListener("mouseup", this._onMouseUp); | ||
} | ||
|
||
public destruct() { | ||
this._onWheel && window.removeEventListener("wheel", this._onWheel); | ||
this._onMousemove && | ||
window.removeEventListener("mousemove", this._onMousemove); | ||
this._onMousedown && | ||
window.removeEventListener("mousedown", this._onMousedown); | ||
this._onMouseUp && window.removeEventListener("mouseup", this._onMouseUp); | ||
} | ||
|
||
public enable() { | ||
if (this.enabled) return; | ||
if (this._navigation?.view) this._navigation.view.controls = false; | ||
|
||
const toPosition = this.cameraPath.getPointAt( | ||
this.progress.current, | ||
this.positionInCurve | ||
); | ||
|
||
this._navigation | ||
?.updateCameraPosition(toPosition, this.cameraTarget) | ||
.then(() => { | ||
this.enabled = true; | ||
}); | ||
} | ||
|
||
public disable() { | ||
if (!this.enabled) return; | ||
this.enabled = false; | ||
|
||
if (this.timeline.isActive()) this.timeline.progress(1); | ||
|
||
this._navigation | ||
?.updateCameraPosition( | ||
this.positionInCurve.setY(2), | ||
this.cameraTarget, | ||
Config.GSAP_ANIMATION_DURATION * 0.5 | ||
) | ||
.then(() => { | ||
if (this._navigation?.view) this._navigation.view.controls = true; | ||
}); | ||
} | ||
|
||
public update(): void { | ||
if (!this.enabled || this.timeline.isActive()) return; | ||
|
||
this.progress.current = gsap.utils.interpolate( | ||
this.progress.current, | ||
this.progress.target, | ||
this.progress.ease | ||
); | ||
this.progress.target = | ||
this.progress.target + (this.reversed ? -0.0001 : 0.0001); | ||
|
||
if (this.progress.target > 1) { | ||
setTimeout(() => { | ||
this.reversed = true; | ||
}, 1000); | ||
} | ||
|
||
if (this.progress.target < 0) { | ||
setTimeout(() => { | ||
this.reversed = false; | ||
}, 1000); | ||
} | ||
this.progress.target = gsap.utils.clamp(0, 1, this.progress.target); | ||
this.progress.current = gsap.utils.clamp(0, 1, this.progress.current); | ||
this.cameraPath.getPointAt(this.progress.current, this.positionInCurve); | ||
|
||
this._navigation?.setPositionInSphere(this.positionInCurve); | ||
} | ||
} |
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
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
Oops, something went wrong.