Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Created September 25, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robksawyer/1e99a5becf15de3f38e38953884c0921 to your computer and use it in GitHub Desktop.
Save robksawyer/1e99a5becf15de3f38e38953884c0921 to your computer and use it in GitHub Desktop.
Class((function GazeCamera(_input, _group) {
Inherit(this, BaseCamera);
const _this = this;
var _strength = {
v: 1
},
_move = new Vector3,
_position = new Vector3,
_wobble = new Vector3,
_rotation = 0,
_wobbleAngle = Math.radians(Math.rand(0, 360)),
_innerGroup = new Group;
function loop() {
if (_this.useAccelerometer && Device.mobile) _move.x = _this.position.x + Math.range(Mobile.Accelerometer.x, -2, 2, -1, 1, !0) * _strength.v * _this.moveXY.x * _this.strength, _move.y = 0;
else {
_move.x = _this.position.x + Math.range(Mouse.x, 0, Stage.width, -1, 1, !0) * _strength.v * _this.moveXY.x * _this.strength, _move.y = _this.position.y + Math.range(Mouse.y, 0, Stage.height, -1, 1, !0) * _strength.v * _this.moveXY.y * _this.strength;
let rotateStrength = Math.range(Math.abs(Mouse.delta.x) / Stage.width, 0, .02, 0, 1, !0) * _strength.v;
_rotation = Math.lerp(Math.radians(_this.deltaRotate) * rotateStrength * Math.sign(Mouse.delta.x), _rotation, .02 * _this.deltaLerp * _strength.v), _this.group.rotation.z = Math.lerp(_rotation, _this.group.rotation.z, .07 * _this.deltaLerp)
}
if (_move.z = _this.position.z, _position.lerp(_move, _this.lerpSpeed2), _this.camera.position.lerp(_position, _this.lerpSpeed), _this.camera.lookAt(_this.lookAt), _this.wobbleStrength > 0) {
let t = Render.TIME;
_wobble.x = Math.cos(_wobbleAngle + t * (75e-5 * _this.wobbleSpeed)) * (_wobbleAngle + 200 * Math.sin(t * (95e-5 * _this.wobbleSpeed))), _wobble.y = Math.sin(Math.asin(Math.cos(_wobbleAngle + t * (85e-5 * _this.wobbleSpeed)))) * (150 * Math.sin(_wobbleAngle + t * (75e-5 * _this.wobbleSpeed))), _wobble.x *= 2 * Math.sin(_wobbleAngle + t * (75e-5 * _this.wobbleSpeed)), _wobble.y *= 1.75 * Math.cos(_wobbleAngle + t * (65e-5 * _this.wobbleSpeed)), _wobble.x *= 1.1 * Math.cos(_wobbleAngle + t * (75e-5 * _this.wobbleSpeed)), _wobble.y *= 1.15 * Math.sin(_wobbleAngle + t * (25e-5 * _this.wobbleSpeed)), _wobble.z = Math.sin(_wobbleAngle + .0025 * _wobble.x) * (100 * _this.wobbleZ), _wobble.multiplyScalar(.001 * _this.wobbleStrength * _strength.v), _innerGroup.position.lerp(_wobble, .07)
}
}
this.strength = 1, this.moveXY = new Vector2(4, 4), this.position = new function Position() {
Inherit(this, Component);
var _x = 0,
_y = 0,
_z = 0;
this.get("x", _ => _x), this.get("y", _ => _y), this.get("z", _ => _z), this.set("x", x => {
_x = x
}), this.set("y", y => {
_y = y
}), this.set("z", z => {
_z = z, _move.z = _z, _this.camera.position.copy(_move), _position.copy(_move)
}), this.set = function (x, y, z, noCopy) {
_x = x, _y = y, _z = z, _move.z = z, noCopy || _this.camera.position.copy(_move), _position.copy(_move)
}, this.toArray = function () {
return [_x, _y, _z]
}, this.fromArray = function (array) {
_x = array[0], _y = array[1], _z = array[2], _move.set(_x, _y, _z), _this.camera.position.copy(_move), _position.copy(_move)
}
}, this.lerpSpeed = .04, this.lerpSpeed2 = 1, this.lookAt = new Vector3(0, 0, 0), this.deltaRotate = 10, this.deltaLerp = 1, this.wobbleSpeed = 1, this.wobbleStrength = 0, this.wobbleZ = 1, _input && (_this.prefix = _input.prefix, CameraUIL.add(_this, _group).setLabel("Camera")), Mobile.Accelerometer.capture(), _this.startRender(loop), _innerGroup.add(_this.camera), _this.group.add(_innerGroup), this.orbit = function (time = 1e3, ease = "easeInOutSine") {
return tween(_strength, {
v: 1
}, time, ease)
}, this.still = function (time = 300, ease = "easeInOutSine") {
return tween(_strength, {
v: 0
}, time, ease)
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment