# graphics3d/camerashake (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/graphics3d/camerashake/

3D camera shake. (an engine shake in the form of Unity Cinemachine Impulse and Unreal Camera Shake)

## CameraShake

3D camera shake. (an engine shake in the form of Unity Cinemachine Impulse and Unreal Camera Shake)
- add(options) adds one shake. Each shake holds its own signal for 3 position axes and 3 rotation axes (pitch, yaw, roll), strength, frequency, duration, attack and decay.
- Signal: each axis layers two sine waves with slightly different frequencies. Phases and frequency ratios are drawn from the shake's seed. (same seed and same age give the same value)
- Envelope: 0 while age is negative (starts that much later), rises from 0 to 1 over attack seconds, decays as (1 - age / duration) to the power decayExponent and reaches 0 at duration.
- Stacking: all live shakes are summed. (unlike the 2D Shaker, which follows the stronger one)
- Age: options.age is the seconds already elapsed. The signal is also computed from age, so an event seen late and added with that age continues with the same values as if it had been added from the start.
- update(seconds) advances ages and removes finished shakes. read() gives the current offset { position, rotation }. (camera local axes)
- Where to apply: pass it to Camera3D.setViewOffset(position, rotation), or apply it with createOffsetViewMatrix to a view matrix the game computed.
- Seed: a shake added without options.seed draws its seed in turn from this object's seed (the seed given at construction).

Example: Applying an explosion shake to Camera3D

```js
import { CameraShake, Camera3D, Vector3 } from "./vanilla.js";
const camera = new Camera3D();
camera.setPerspective(Math.PI / 3, 0.1, 500);
camera.lookAt(Vector3.create(0, 4, 10), Vector3.create(0, 0, 0));
const cameraShake = new CameraShake({ seed: 7 });
cameraShake.add({ amplitude: 0.09, rotationAmplitude: 0.01, duration: 0.25, positionAxes: [1, 0.5, 1] });
cameraShake.add({ amplitude: 0.04, duration: 0.6, age: 0.1 });
cameraShake.update(1 / 60);
const shake = cameraShake.read();
camera.setViewOffset(shake.position, shake.rotation);
const viewProjectionMatrix = camera.getViewProjectionMatrix();
console.log(cameraShake.isShaking(), viewProjectionMatrix.getElements().length);
```

- `constructor(options = {})` (constructor, since 0.4.0): Constructor.
  - `options` object: { seed = 1 } base seed from which shakes without a seed draw theirs.
- `add(options = {})` (method, since 0.4.0): Adds a shake. (summed with the live shakes)
  - `options` object: { amplitude, rotationAmplitude, positionAxes, rotationAxes, frequency, duration, attack, decayExponent, age, seed }
  - amplitude: largest position offset. (scene length, default 0.1)
  - rotationAmplitude: largest rotation angle. (radians, default 0)
  - positionAxes: [x, y, z] share per position axis. (camera local axes right, up, back, default [1, 1, 1])
  - rotationAxes: [pitch, yaw, roll] share per rotation axis. (default [1, 1, 1])
  - frequency: frequency. (times per second, default 10)
  - duration: duration. (seconds, default 0.25)
  - attack: seconds to rise from 0 to full strength. (default 0)
  - decayExponent: decay exponent. (default 2, (1 - age / duration) to this power)
  - age: seconds already elapsed. (default 0, negative starts that much later)
  - seed: this shake's seed. (drawn in turn from the base seed when absent)
- `update(deltaTime)` (method, since 0.4.0): Advances age. (removes finished shakes)
  - `deltaTime` number: Time since the last frame. (Seconds)
- `read()` (method, since 0.4.0): The current offset. (sum of all live shakes, camera local axes)
  - returns object: { position: Vector3, rotation: Vector3 } position (scene length, x right, y up, z back) and rotation (radians, x pitch, y yaw, z roll). (both zero vectors when there is no shake)
- `isShaking()` (method, since 0.4.0): Whether any shake remains. (shakes that start later also count)
  - returns boolean: true if at least one shake is live.
- `clear()` (method, since 0.4.0): Stops all shakes at once.

## Functions and constants

- `createOffsetViewMatrix(viewMatrix, position, rotation)` (method, since 0.4.0): Applies an offset in camera local axes to a view matrix. (the view of the camera moved by position and turned by rotation in place, new matrix)
  - position is in camera local axes. (x right, y up, z back, the camera looks down -Z)
  - rotation is angles (radians) in camera local axes. (x is pitch, y is yaw, z is roll, applied roll, then pitch, then yaw)
  - The offset camera is (camera) x translation x yaw x pitch x roll, and the view matrix is its inverse, so the inverse of (translation x yaw x pitch x roll) is multiplied in front.
  - `viewMatrix` Matrix4: View matrix before the offset. (world → camera, not modified)
  - `position` Vector3: Position offset in camera local axes. (scene length)
  - `rotation` Vector3: Rotation offset in camera local axes. (radians, x pitch, y yaw, z roll)
  - returns Matrix4: The offset view matrix. (new matrix)

  Example: Offsetting a view matrix 0.1 to the right and tilting the head up by 0.02 radians

  ```js
  import { createOffsetViewMatrix, Matrix4, Vector3 } from "./vanilla.js";
  const viewMatrix = Matrix4.createLookAt(Vector3.create(0, 2, 6), Vector3.create(0, 1, 0), Vector3.create(0, 1, 0));
  const shakenViewMatrix = createOffsetViewMatrix(viewMatrix, Vector3.create(0.1, 0, 0), Vector3.create(0.02, 0, 0));
  console.log(shakenViewMatrix.getElements().length);
  ```

