I’m excited to share two new behavior classes I’ve created to help make your BabylonJS scenes even more dynamic and interactive:
CameraShake: This behavior class adds a camera shake effect, complete with customizable parameters.
PropertyDistanceAttractor: This behavior class allows you to interpolate numeric values based on the distance between a target and an attractor. It’s perfect for creating effects that change intensity as objects move closer or further apart.
Feel free to try them out, and I would appreciate any feedback or suggestions you might have.
Thank you for question! While I trying to make walking camer I found some bugs, and of course fixed them.
Now I publish new version, and add an instruction for walking effect into readme.
Here is it:
// Walking camera settings, but with zero influence by default
const cameraShakeOptions: CameraShakeOptions = {
shakePattern: 'sin',
amplitude: new Vector3(0.1, 0.1, 0),
frequency: new Vector3(3, 9, 0),
rotation: new Vector3(0.01, 0, 0),
rotationFrequency: new Vector3(9, 1, 0),
influence: 0,
};
const cameraShake = new CameraShake(cameraShakeOptions);
camera.addBehavior(cameraShake);
// Initialize variables to track camera movement
let previousPosition = camera.position.clone();
let previousTime = performance.now();
this.scene.onBeforeRenderObservable.add(function() {
// Calculate time difference and distance moved since last frame
const currentTime = performance.now();
const timeDiff = currentTime - previousTime;
const distanceMoved = Vector3.Distance(camera.position, previousPosition);
// Calculate speed in units per second
const speed = distanceMoved / (timeDiff / 1000);
// Set cameraShakeOptions.influence relative to camera speed
const cameraSpeedMax = 4;
cameraShakeOptions.influence = Math.min(speed / cameraSpeedMax, 1);
// Update the previous position and time
previousPosition.copyFrom(camera.position);
previousTime = currentTime;
});