Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | import { Quat, Script, Vec3 } from 'playcanvas';
/**
* Auto-rotator script for PlayCanvas CameraControls.
* Automatically orbits the camera around its focus point after a period of inactivity.
*/
export class AutoRotator extends Script {
static scriptName = 'autoRotator';
/**
* Rotation speed in degrees per second when auto-rotating.
*
* @attribute
* @title Speed
*/
speed = 4;
/**
* Delay in seconds before auto-rotation starts after initialization.
*
* @attribute
* @title Start Delay
*/
startDelay = 5;
/**
* Delay in seconds before auto-rotation restarts after the camera stops moving.
*
* @attribute
* @title Restart Delay
*/
restartDelay = 2;
/**
* Duration in seconds to fade in the rotation speed.
*
* @attribute
* @title Start Fade In Time
*/
startFadeInTime = 5;
/**
* Internal timer tracking how long the camera has been idle.
* @private
*/
private timer = 0;
/**
* Flag to track if rotation has started at least once.
* @private
*/
private hasStartedRotating = false;
/**
* Flag to track if we're currently rotating.
* @private
*/
private isCurrentlyRotating = false;
/**
* Last known pitch angle for movement detection.
* @private
*/
private pitch: number | null = null;
/**
* Last known yaw angle for movement detection.
* @private
*/
private yaw: number | null = null;
/**
* Reference to the camera entity.
* @private
*/
private cameraEntity: any = null;
/**
* Reference to the CameraControls script.
* @private
*/
private cameraControls: any = null;
/**
* Reference to the WalkthroughCamera script (used when CameraControls is absent).
* @private
*/
private walkthroughCamera: any = null;
/**
* Bound event handlers for cleanup.
* @private
*/
private onPointerDown?: () => void;
private onKeyDown?: () => void;
/**
* Handle user input events.
* @private
*/
private handleUserInput() {
this.timer = 0;
this.isCurrentlyRotating = false;
this.hasStartedRotating = true;
}
/**
* Initialize the script.
*/
initialize() {
this.onPointerDown = this.handleUserInput.bind(this);
this.onKeyDown = this.handleUserInput.bind(this);
if (this.app.graphicsDevice.canvas) {
this.app.graphicsDevice.canvas.addEventListener('pointerdown', this.onPointerDown);
}
window.addEventListener('keydown', this.onKeyDown);
this.cameraEntity = this.entity.findByName('camera');
if (this.cameraEntity) {
this.cameraControls = this.cameraEntity.script?.cameraControls ?? null;
this.walkthroughCamera = this.cameraEntity.script?.walkthroughCamera ?? null;
}
}
/**
* Update loop that handles auto-rotation logic.
* Runs after CameraControls updates.
*/
postUpdate(dt: number) {
const hasControls = this.cameraControls && this.cameraControls._pose;
const hasWalkthrough = !!this.walkthroughCamera;
if (!hasControls && !hasWalkthrough) {
return;
}
const currentPitch = hasControls ? this.cameraControls._pose.angles.x : this.walkthroughCamera.currentPitch;
const currentYaw = hasControls ? this.cameraControls._pose.angles.y : this.walkthroughCamera.currentYaw;
// Initialize angles on first frame
if (this.pitch === null || this.yaw === null) {
this.pitch = currentPitch;
this.yaw = currentYaw;
}
// Check for camera movement only after initial rotation has started and while not actively rotating
if (this.hasStartedRotating && !this.isCurrentlyRotating && this.pitch !== null && this.yaw !== null) {
const pitchDiff = Math.abs(this.pitch - currentPitch);
const yawDiff = Math.abs(this.yaw - currentYaw);
if (pitchDiff > 0.1 || yawDiff > 0.1) {
this.pitch = currentPitch;
this.yaw = currentYaw;
this.timer = 0;
this.isCurrentlyRotating = false;
this.hasStartedRotating = true;
return;
}
}
this.timer += dt;
// Determine which delay to use
const currentDelay = this.hasStartedRotating ? this.restartDelay : this.startDelay;
// Start auto-rotation after delay
if (this.timer >= currentDelay) {
this.isCurrentlyRotating = true;
const time = this.timer - currentDelay;
const fadeIn = this.smoothStep(time / this.startFadeInTime);
const yawDelta = dt * fadeIn * this.speed;
if (hasControls) {
const pose = this.cameraControls._pose;
const focusPoint = pose.getFocus(new Vec3());
const offset = new Vec3().sub2(pose.position.clone(), focusPoint);
const rotationQuat = new Quat().setFromAxisAngle(Vec3.UP, yawDelta);
const rotatedOffset = new Vec3();
rotationQuat.transformVector(offset, rotatedOffset);
const newPos = new Vec3().add2(focusPoint, rotatedOffset);
pose.look(newPos, focusPoint);
if (this.cameraControls._controller?.attach) {
this.cameraControls._controller.attach(pose, false);
}
this.cameraEntity.setPosition(pose.position);
this.cameraEntity.setEulerAngles(pose.angles);
this.pitch = pose.angles.x;
this.yaw = pose.angles.y;
} else {
this.walkthroughCamera.orbitStep(yawDelta);
this.pitch = this.walkthroughCamera.currentPitch;
this.yaw = this.walkthroughCamera.currentYaw;
}
}
}
/**
* Smooth step interpolation function.
*
* @param {number} x - Input value.
* @returns {number} - Smoothly interpolated value.
* @private
*/
private smoothStep(x: number): number {
if (x <= 0) {
return 0;
}
if (x >= 1) {
return 1;
}
return Math.sin((x - 0.5) * Math.PI) * 0.5 + 0.5;
}
/**
* Clean up event listeners when the script is destroyed.
*/
destroy() {
if (this.app.graphicsDevice.canvas && this.onPointerDown) {
this.app.graphicsDevice.canvas.removeEventListener('pointerdown', this.onPointerDown);
}
if (this.onKeyDown) {
window.removeEventListener('keydown', this.onKeyDown);
}
}
}
|