Skip to documentation
CranberryClock.Ideas / Systems / Design

INDEPENDENT TOOL / v0.1.0

FluxTutorial

Work through the Flux example and adapt the pattern to your own project.

Tutorial: a projectile trail

javascript
import * as THREE from 'three';
import { Trail } from '@cranberry-forge/flux';

const trail = new Trail({
  capacity: 256,
  lifetime: 2,
  width: 0.4,
  color: '#b2ffcd',
  tailColor: '#1d7392',
  intensity: 2.2,
  taper: 1.4
});
scene.add(trail);

const projectilePosition = new THREE.Vector3();
const cameraPosition = new THREE.Vector3();
const start = performance.now();

renderer.setAnimationLoop(() => {
  const time = (performance.now() - start) / 1000;
  // Move projectile in your own simulation before sampling it.
  projectile.getWorldPosition(projectilePosition);
  camera.getWorldPosition(cameraPosition);
  trail.push(projectilePosition, time);
  trail.update(time, cameraPosition);
  renderer.render(scene, camera);
});

// Stop emitting: omit push(), keep calling update() until the tail expires.
// Level exit:
// trail.dispose();

Add the trail directly to an untransformed scene root. Positions are world-space, so applying another object/parent transform double-transforms them. The trail must be updated for the camera used to render it. For multiple viewports/cameras, update immediately before rendering each view.

View versioned source on GitHub ↗