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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 2x 6x 6x 6x 6x 6x 1x 5x 2x 2x 2x 2x 2x 1x 1x 1x | import {
ADDRESS_CLAMP_TO_EDGE,
FILTER_LINEAR,
LAYERID_IMMEDIATE,
Mesh,
MeshInstance,
Quat,
Script,
ShaderMaterial,
Texture,
Vec3,
XRTYPE_VR,
XrInputSource,
} from 'playcanvas';
import { ButtonArmingLatch } from './xr-button-arming';
import { HoldState, INITIAL_HOLD_STATE, advanceHold } from './xr-button-hold';
import { drawExitButtonMask } from './xr-exit-button-mask';
import { FaceButtonPressTracker, secondaryFaceButtonPressed } from './xr-face-buttons';
import { pieShaderProgress } from './xr-progress-pie';
import { createProgressPieMaterial, progressPieQuadMesh } from './xr-progress-pie-material';
/**
* Boolean ray-sphere hit test. Treats the ray as a half-line (t >= 0) and returns true when it
* intersects the sphere or starts inside it. `direction` is normalized internally.
*/
export const raySphereIntersect = (origin: Vec3, direction: Vec3, center: Vec3, radius: number): boolean => {
const dir = direction.clone().normalize();
const m = new Vec3().sub2(origin, center);
const b = m.dot(dir);
const c = m.dot(m) - radius * radius;
if (c > 0 && b > 0) {
return false;
}
return b * b - c >= 0;
};
const TEXTURE_SIZE = 256;
/** Opacity of the button while it is not being aimed at. */
const IDLE_OPACITY = 0.9;
/** Seconds of continuous B/Y hold required to leave the session. Matches the gaze-dwell threshold. */
const EXIT_HOLD_SECONDS = 1.25;
export class XrExitButton extends Script {
static scriptName = 'xrExitButton';
/** Offset from the head, in head space: right, up, and forward (-z) into the upper-right of the view. */
offset = new Vec3(0.45, 0.35, -1.2);
/** Half-extent of the square button quad, in world units at the offset distance. */
halfSize = 0.12;
/** World-space radius of the hover/hit sphere. Slightly larger than halfSize for easier targeting. */
hitRadius = 0.15;
/**
* Called in place of ending the XR session when the user activates the button, leaving the
* session running so the handler decides what happens next.
*/
onClose?: () => void;
private _material?: ShaderMaterial;
private _texture?: Texture;
private _mesh?: Mesh;
private _hovered = false;
private _headPosition = new Vec3();
private _headRotation = new Quat();
private _worldOffset = new Vec3();
private _faceButtons = new FaceButtonPressTracker();
private _hold: HoldState = INITIAL_HOLD_STATE;
private _arming = new ButtonArmingLatch();
private _isVrActive = () => this.app.xr?.active === true && this.app.xr?.type === XRTYPE_VR;
private _onXrStart = () => {
this.entity.enabled = this._isVrActive();
this._resetProgress();
};
private _onXrEnd = () => {
this.entity.enabled = false;
this._hovered = false;
this._resetProgress();
this.entity.setLocalScale(1, 1, 1);
this._material?.setParameter('uOpacity', IDLE_OPACITY);
};
private _onSelect = (inputSource: XrInputSource) => {
if (!this.entity.enabled) {
return;
}
if (this.rayHitsButton(inputSource.getOrigin(), inputSource.getDirection())) {
this.close();
}
};
/** True when the ray points at the button's hit sphere. False while the button entity is disabled. */
rayHitsButton(origin: Vec3, direction: Vec3): boolean {
if (!this.entity.enabled) {
return false;
}
return raySphereIntersect(origin, direction, this.entity.getPosition(), this.hitRadius);
}
/** The button's exit action: the `onClose` handler when one is set, otherwise ending the session. */
close() {
if (this.onClose) {
this.onClose();
return;
}
this.app.xr?.end();
}
/** The button's static artwork, painted once. Only the sweep changes per frame, and that lives in
* the shader. */
private _createMaskTexture(): Texture {
const canvas = document.createElement('canvas');
canvas.width = TEXTURE_SIZE;
canvas.height = TEXTURE_SIZE;
const ctx = canvas.getContext('2d');
if (ctx) {
drawExitButtonMask(ctx, TEXTURE_SIZE);
}
const texture = new Texture(this.app.graphicsDevice, {
name: 'xr-exit-button-mask',
width: TEXTURE_SIZE,
height: TEXTURE_SIZE,
addressU: ADDRESS_CLAMP_TO_EDGE,
addressV: ADDRESS_CLAMP_TO_EDGE,
minFilter: FILTER_LINEAR,
magFilter: FILTER_LINEAR,
mipmaps: true,
});
texture.setSource(canvas);
return texture;
}
private _resetProgress() {
this._hold = INITIAL_HOLD_STATE;
this._material?.setParameter('uProgress', 0);
}
initialize() {
this._texture = this._createMaskTexture();
this._material = createProgressPieMaterial({
uniqueName: 'xr-exit-button',
maskMap: this._texture,
discColor: [20 / 255, 20 / 255, 20 / 255],
discAlpha: 0.75,
});
this._material.setParameter('uOpacity', IDLE_OPACITY);
this._resetProgress();
this._mesh = progressPieQuadMesh(this.app.graphicsDevice, this.halfSize);
const meshInstance = new MeshInstance(this._mesh, this._material);
// Immediate layer draws after the World layer (where the splats render) so the button is never
// composited behind them; depthTest:false keeps it on top within the layer.
this.entity.addComponent('render', { meshInstances: [meshInstance], layers: [LAYERID_IMMEDIATE] });
this.entity.enabled = this._isVrActive();
this.app.xr?.on('start', this._onXrStart);
this.app.xr?.on('end', this._onXrEnd);
this.app.xr?.input?.on('select', this._onSelect);
}
update(dt: number) {
if (!this.entity.enabled) {
return;
}
this._trackHead();
const sources = this.app.xr?.input?.inputSources ?? [];
let hovered = false;
let holdActive = false;
for (const source of sources) {
const sourceHovers = this.rayHitsButton(source.getOrigin(), source.getDirection());
hovered = hovered || sourceHovers;
// A face-button press exits only while that controller is aimed at the button, matching the
// trigger. Edge-detected so holding a button and then aiming onto it doesn't fire.
const facePressed = this._faceButtons.justPressed(source);
if (sourceHovers && facePressed) {
this.close();
return;
}
// Two statements, not `holdActive = holdActive || this._arming.trackPress(...)`: trackPress
// arms the source as a side effect and must run for every source every frame, but `||`
// short-circuits once holdActive is true and would skip arming later sources.
const armedPress = this._arming.trackPress(source, secondaryFaceButtonPressed(source));
holdActive = holdActive || armedPress;
}
const hold = advanceHold(this._hold, holdActive, dt, EXIT_HOLD_SECONDS);
this._hold = hold.state;
this._material?.setParameter('uProgress', pieShaderProgress(hold.progress));
if (hold.justFired) {
this.close();
return;
}
if (hovered !== this._hovered) {
this._hovered = hovered;
const scale = hovered ? 1.15 : 1;
this.entity.setLocalScale(scale, scale, scale);
this._material?.setParameter('uOpacity', hovered ? 1 : IDLE_OPACITY);
}
}
/**
* Pin the button to the upper-right of the headset view. The camera entity's transform is
* overwritten every frame by WalkthroughCamera and is not the rendered head pose, so the head
* pose is read from the XR views (each view's inverse-view matrix is that eye's world transform)
* and the button places itself in world space, offset in head space.
*/
private _trackHead() {
const views = this.app.xr?.views?.list;
if (!views || views.length === 0) {
return;
}
this._headPosition.set(0, 0, 0);
for (const view of views) {
view.viewInvOffMat.getTranslation(this._worldOffset);
this._headPosition.add(this._worldOffset);
}
this._headPosition.mulScalar(1 / views.length);
this._headRotation.setFromMat4(views[0].viewInvOffMat);
this._headRotation.transformVector(this.offset, this._worldOffset);
this.entity.setPosition(
this._headPosition.x + this._worldOffset.x,
this._headPosition.y + this._worldOffset.y,
this._headPosition.z + this._worldOffset.z
);
this.entity.setRotation(this._headRotation);
}
destroy() {
this.app.xr?.off('start', this._onXrStart);
this.app.xr?.off('end', this._onXrEnd);
this.app.xr?.input?.off('select', this._onSelect);
if (this.entity.render) {
this.entity.render.meshInstances = [];
}
this._mesh?.destroy();
this._material?.destroy();
this._texture?.destroy();
this._mesh = undefined;
this._material = undefined;
this._texture = undefined;
}
}
|