UNPKG

molstar

Version:

A comprehensive macromolecular library.

59 lines (58 loc) 3.28 kB
/** * Copyright (c) 2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __assign, __awaiter, __generator } from "tslib"; import { clamp } from '../../../mol-math/interpolate'; import { Quat } from '../../../mol-math/linear-algebra/3d/quat'; import { Vec3 } from '../../../mol-math/linear-algebra/3d/vec3'; import { degToRad } from '../../../mol-math/misc'; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { PluginStateAnimation } from '../model'; var _dir = Vec3(), _axis = Vec3(), _rot = Quat(); export var AnimateCameraRock = PluginStateAnimation.create({ name: 'built-in.animate-camera-rock', display: { name: 'Camera Rock', description: 'Rock the 3D scene around the x-axis in view space' }, isExportable: true, params: function () { return ({ durationInMs: PD.Numeric(4000, { min: 100, max: 20000, step: 100 }), speed: PD.Numeric(1, { min: 1, max: 10, step: 1 }, { description: 'How many times to rock from side to side.' }), angle: PD.Numeric(10, { min: 0, max: 180, step: 1 }, { description: 'How many degrees to rotate in each direction.' }), }); }, initialState: function (p, ctx) { return ({ snapshot: ctx.canvas3d.camera.getSnapshot() }); }, getDuration: function (p) { return ({ kind: 'fixed', durationMs: p.durationInMs }); }, teardown: function (_, state, ctx) { var _a; (_a = ctx.canvas3d) === null || _a === void 0 ? void 0 : _a.requestCameraReset({ snapshot: state.snapshot, durationMs: 0 }); }, apply: function (animState, t, ctx) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var snapshot, phase, angle, position; return __generator(this, function (_c) { if (t.current === 0) { return [2 /*return*/, { kind: 'next', state: animState }]; } snapshot = animState.snapshot; if (snapshot.radiusMax < 0.0001) { return [2 /*return*/, { kind: 'finished' }]; } phase = t.animation ? ((_a = t.animation) === null || _a === void 0 ? void 0 : _a.currentFrame) / (t.animation.frameCount + 1) : clamp(t.current / ctx.params.durationInMs, 0, 1); angle = Math.sin(phase * ctx.params.speed * Math.PI * 2) * degToRad(ctx.params.angle); Vec3.sub(_dir, snapshot.position, snapshot.target); Vec3.normalize(_axis, snapshot.up); Quat.setAxisAngle(_rot, _axis, angle); Vec3.transformQuat(_dir, _dir, _rot); position = Vec3.add(Vec3(), snapshot.target, _dir); (_b = ctx.plugin.canvas3d) === null || _b === void 0 ? void 0 : _b.requestCameraReset({ snapshot: __assign(__assign({}, snapshot), { position: position }), durationMs: 0 }); if (phase >= 0.99999) { return [2 /*return*/, { kind: 'finished' }]; } return [2 /*return*/, { kind: 'next', state: animState }]; }); }); } });