Skip to content

Commit

Permalink
separately export all private keys to eliminate circular dependency w…
Browse files Browse the repository at this point in the history
…arning

Summary: Refactor only: export all private keys from private.ts to eliminate the circular dependency warning when building.

Reviewed By: zjm-meta

Differential Revision:
D67112604

Privacy Context Container: L1233623

fbshipit-source-id: cd62a733e9b894a1f664918b4c23bb26ba2707c0
  • Loading branch information
felixtrz authored and facebook-github-bot committed Dec 11, 2024
1 parent 7a33284 commit 2b25338
Show file tree
Hide file tree
Showing 33 changed files with 795 additions and 880 deletions.
106 changes: 56 additions & 50 deletions src/action/ActionPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@
import {
Axis,
Button,
PRIVATE as GAMEPAD_PRIVATE,
Gamepad,
GamepadButton,
GamepadMappingType,
} from '../gamepad/Gamepad.js';
import {
P_ACTION_PLAYER,
P_GAMEPAD,
P_JOINT_SPACE,
P_SPACE,
} from '../private.js';
import { XRHand, XRHandJoint } from '../input/XRHand.js';
import {
XRHandedness,
XRInputSource,
XRTargetRayMode,
} from '../input/XRInputSource.js';
import {
PRIVATE as XRJOINTSPACE_PRIVATE,
XRJointSpace,
} from '../spaces/XRJointSpace.js';
import {
XRReferenceSpace,
XRReferenceSpaceType,
} from '../spaces/XRReferenceSpace.js';
import { PRIVATE as XRSPACE_PRIVATE, XRSpace } from '../spaces/XRSpace.js';
import { mat4, quat, vec3 } from 'gl-matrix';

import { InputSchema } from './ActionRecorder.js';
import { XREye } from '../views/XRView.js';
import { XRJointSpace } from '../spaces/XRJointSpace.js';
import { XRSpace } from '../spaces/XRSpace.js';

export interface CompressedRecording {
schema: {
Expand All @@ -41,8 +43,6 @@ export interface CompressedRecording {
frames: any[];
}

export const PRIVATE = Symbol('@immersive-web-emulation-runtime/action-player');

type ProcessedInputData = {
targetRayTransform: number[];
gripTransform?: number[];
Expand All @@ -52,7 +52,7 @@ type ProcessedInputData = {
};

export class ActionPlayer {
[PRIVATE]: {
[P_ACTION_PLAYER]: {
refSpace: XRReferenceSpace;
inputSources: Map<number, { active: boolean; source: XRInputSource }>;
inputSchemas: Map<number, InputSchema>;
Expand Down Expand Up @@ -93,7 +93,7 @@ export class ActionPlayer {
[XREye.Right]: new XRSpace(viewerSpace),
[XREye.None]: new XRSpace(viewerSpace),
};
this[PRIVATE] = {
this[P_ACTION_PLAYER] = {
refSpace,
inputSources: new Map(),
inputSchemas: new Map(),
Expand All @@ -110,11 +110,11 @@ export class ActionPlayer {
};

mat4.fromTranslation(
this[PRIVATE].viewSpaces[XREye.Left][XRSPACE_PRIVATE].offsetMatrix,
this[P_ACTION_PLAYER].viewSpaces[XREye.Left][P_SPACE].offsetMatrix,
vec3.fromValues(-ipd / 2, 0, 0),
);
mat4.fromTranslation(
this[PRIVATE].viewSpaces[XREye.Right][XRSPACE_PRIVATE].offsetMatrix,
this[P_ACTION_PLAYER].viewSpaces[XREye.Right][P_SPACE].offsetMatrix,
vec3.fromValues(ipd / 2, 0, 0),
);

Expand Down Expand Up @@ -158,69 +158,75 @@ export class ActionPlayer {
schema.hasHand ? hand : undefined,
);

this[PRIVATE].inputSources.set(index, {
this[P_ACTION_PLAYER].inputSources.set(index, {
active: false,
source: inputSource,
});
this[PRIVATE].inputSchemas.set(index, schema);
this[P_ACTION_PLAYER].inputSchemas.set(index, schema);
});
}

play() {
this[PRIVATE].recordedFramePointer = 0;
this[PRIVATE].playbackTime = this[PRIVATE].startingTimeStamp;
this[PRIVATE].playing = true;
this[PRIVATE].actualTimeStamp = performance.now();
this[P_ACTION_PLAYER].recordedFramePointer = 0;
this[P_ACTION_PLAYER].playbackTime =
this[P_ACTION_PLAYER].startingTimeStamp;
this[P_ACTION_PLAYER].playing = true;
this[P_ACTION_PLAYER].actualTimeStamp = performance.now();
}

stop() {
this[PRIVATE].playing = false;
this[P_ACTION_PLAYER].playing = false;
}

get playing() {
return this[PRIVATE].playing;
return this[P_ACTION_PLAYER].playing;
}

get viewerSpace() {
return this[PRIVATE].viewerSpace;
return this[P_ACTION_PLAYER].viewerSpace;
}

get viewSpaces() {
return this[PRIVATE].viewSpaces;
return this[P_ACTION_PLAYER].viewSpaces;
}

get inputSources() {
return Array.from(this[PRIVATE].inputSources.values())
return Array.from(this[P_ACTION_PLAYER].inputSources.values())
.filter((wrapper) => wrapper.active)
.map((wrapper) => wrapper.source);
}

playFrame() {
const now = performance.now();
const delta = now - this[PRIVATE].actualTimeStamp!;
this[PRIVATE].actualTimeStamp = now;
this[PRIVATE].playbackTime! += delta;
if (this[PRIVATE].playbackTime! > this[PRIVATE].endingTimeStamp) {
const delta = now - this[P_ACTION_PLAYER].actualTimeStamp!;
this[P_ACTION_PLAYER].actualTimeStamp = now;
this[P_ACTION_PLAYER].playbackTime! += delta;
if (
this[P_ACTION_PLAYER].playbackTime! >
this[P_ACTION_PLAYER].endingTimeStamp
) {
this.stop();
return;
}
while (
(this[PRIVATE].frames[
this[PRIVATE].recordedFramePointer + 1
][0] as number) < this[PRIVATE].playbackTime
(this[P_ACTION_PLAYER].frames[
this[P_ACTION_PLAYER].recordedFramePointer + 1
][0] as number) < this[P_ACTION_PLAYER].playbackTime
) {
this[PRIVATE].recordedFramePointer++;
this[P_ACTION_PLAYER].recordedFramePointer++;
}
const lastFrameData =
this[PRIVATE].frames[this[PRIVATE].recordedFramePointer];
this[P_ACTION_PLAYER].frames[this[P_ACTION_PLAYER].recordedFramePointer];
const nextFrameData =
this[PRIVATE].frames[this[PRIVATE].recordedFramePointer + 1];
this[P_ACTION_PLAYER].frames[
this[P_ACTION_PLAYER].recordedFramePointer + 1
];
const alpha =
((this[PRIVATE].playbackTime - lastFrameData[0]) as number) /
((this[P_ACTION_PLAYER].playbackTime - lastFrameData[0]) as number) /
(((nextFrameData[0] as number) - lastFrameData[0]) as number);

this.updateXRSpaceFromMergedFrames(
this[PRIVATE].viewerSpace,
this[P_ACTION_PLAYER].viewerSpace,
lastFrameData.slice(1, 8),
nextFrameData.slice(1, 8),
alpha,
Expand All @@ -242,14 +248,14 @@ export class ActionPlayer {
nextFrameInputs.set(index, inputData);
}

this[PRIVATE].inputSources.forEach((sourceWrapper) => {
this[P_ACTION_PLAYER].inputSources.forEach((sourceWrapper) => {
sourceWrapper.active = false;
});

nextFrameInputs.forEach((inputData, index) => {
this[PRIVATE].inputSources.get(index)!.active = true;
const inputSource = this[PRIVATE].inputSources.get(index)!.source;
const schema = this[PRIVATE].inputSchemas.get(index)!;
this[P_ACTION_PLAYER].inputSources.get(index)!.active = true;
const inputSource = this[P_ACTION_PLAYER].inputSources.get(index)!.source;
const schema = this[P_ACTION_PLAYER].inputSchemas.get(index)!;
this.updateInputSource(
inputSource,
schema,
Expand Down Expand Up @@ -304,7 +310,7 @@ export class ActionPlayer {
nextTransformArray,
alpha,
);
jointSpace[XRJOINTSPACE_PRIVATE].radius =
jointSpace[P_JOINT_SPACE].radius =
(nextRadius - lastRadius) * alpha + lastRadius;
}
}
Expand All @@ -313,16 +319,16 @@ export class ActionPlayer {
const gamepad = inputSource.gamepad!;
nextInputData.buttons!.forEach((states, index) => {
const gamepadButton = gamepad.buttons[index]! as GamepadButton;
gamepadButton[GAMEPAD_PRIVATE].pressed = states[0] === 1 ? true : false;
gamepadButton[GAMEPAD_PRIVATE].touched = states[1] === 1 ? true : false;
gamepadButton[P_GAMEPAD].pressed = states[0] === 1 ? true : false;
gamepadButton[P_GAMEPAD].touched = states[1] === 1 ? true : false;
const lastValue = lastInputData.buttons![index][2];
const nextValue = states[2];
gamepadButton[GAMEPAD_PRIVATE].value =
gamepadButton[P_GAMEPAD].value =
(nextValue - lastValue) * alpha + lastValue;
});
nextInputData.axes!.forEach((nextValue, index) => {
const lastValue = lastInputData.axes![index];
gamepad[GAMEPAD_PRIVATE].axesMap[index.toString()].x =
gamepad[P_GAMEPAD].axesMap[index.toString()].x =
(nextValue - lastValue) * alpha + lastValue;
});
}
Expand Down Expand Up @@ -356,18 +362,18 @@ export class ActionPlayer {
nextTransform[5],
nextTransform[6],
);
vec3.lerp(this[PRIVATE].vec3, f1p, f2p, alpha);
quat.slerp(this[PRIVATE].quat, f1q, f2q, alpha);
vec3.lerp(this[P_ACTION_PLAYER].vec3, f1p, f2p, alpha);
quat.slerp(this[P_ACTION_PLAYER].quat, f1q, f2q, alpha);
mat4.fromRotationTranslation(
space[XRSPACE_PRIVATE].offsetMatrix,
this[PRIVATE].quat,
this[PRIVATE].vec3,
space[P_SPACE].offsetMatrix,
this[P_ACTION_PLAYER].quat,
this[P_ACTION_PLAYER].vec3,
);
}

processRawInputData(inputDataRaw: any[]) {
const index = inputDataRaw[0];
const schema = this[PRIVATE].inputSchemas.get(index)!;
const schema = this[P_ACTION_PLAYER].inputSchemas.get(index)!;
const targetRayTransform: number[] = inputDataRaw.slice(1, 8);
const inputData: ProcessedInputData = { targetRayTransform };
let dataCounter = 8;
Expand Down
48 changes: 24 additions & 24 deletions src/action/ActionRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import { mat4, quat, vec3 } from 'gl-matrix';

export const PRIVATE = Symbol(
'@immersive-web-emulation-runtime/action-recorder',
);
import { P_ACTION_RECORDER } from '../private.js';

export interface InputFrame {
index: number;
Expand Down Expand Up @@ -63,7 +61,7 @@ const compress = (arr: vec3 | quat) => {
};

export class ActionRecorder {
[PRIVATE]: {
[P_ACTION_RECORDER]: {
session: XRSession;
refSpace: XRReferenceSpace;
inputMap: Map<XRInputSource, number>;
Expand All @@ -74,7 +72,7 @@ export class ActionRecorder {
};

constructor(session: XRSession, refSpace: XRReferenceSpace) {
this[PRIVATE] = {
this[P_ACTION_RECORDER] = {
session,
refSpace,
inputMap: new Map(),
Expand All @@ -87,8 +85,8 @@ export class ActionRecorder {

recordFrame(frame: XRFrame) {
const timeStamp = performance.now();
const viewerMatrix = frame.getViewerPose(this[PRIVATE].refSpace)?.transform
.matrix;
const viewerMatrix = frame.getViewerPose(this[P_ACTION_RECORDER].refSpace)
?.transform.matrix;
if (!viewerMatrix) return;
const position = mat4.getTranslation(vec3.create(), viewerMatrix);
const quaternion = mat4.getRotation(quat.create(), viewerMatrix);
Expand All @@ -98,8 +96,8 @@ export class ActionRecorder {
quaternion,
inputFrames: [],
};
this[PRIVATE].session.inputSources.forEach((inputSource) => {
if (!this[PRIVATE].inputMap.has(inputSource)) {
this[P_ACTION_RECORDER].session.inputSources.forEach((inputSource) => {
if (!this[P_ACTION_RECORDER].inputMap.has(inputSource)) {
const schema: InputSchema = {
handedness: inputSource.handedness,
targetRayMode: inputSource.targetRayMode,
Expand All @@ -118,15 +116,15 @@ export class ActionRecorder {
schema.numButtons = inputSource.gamepad!.buttons.length;
schema.numAxes = inputSource.gamepad!.axes.length;
}
const index = this[PRIVATE].inputMap.size;
this[PRIVATE].inputMap.set(inputSource, index);
this[PRIVATE].schemaMap.set(index, schema);
const index = this[P_ACTION_RECORDER].inputMap.size;
this[P_ACTION_RECORDER].inputMap.set(inputSource, index);
this[P_ACTION_RECORDER].schemaMap.set(index, schema);
}
const index = this[PRIVATE].inputMap.get(inputSource)!;
const schema = this[PRIVATE].schemaMap.get(index)!;
const index = this[P_ACTION_RECORDER].inputMap.get(inputSource)!;
const schema = this[P_ACTION_RECORDER].schemaMap.get(index)!;
const targetRayMatrix = frame.getPose(
inputSource.targetRaySpace,
this[PRIVATE].refSpace,
this[P_ACTION_RECORDER].refSpace,
)?.transform.matrix;
if (targetRayMatrix) {
const targetRayPosition = mat4.getTranslation(
Expand All @@ -149,7 +147,7 @@ export class ActionRecorder {
if (schema.hasGrip) {
const gripMatrix = frame.getPose(
inputSource.gripSpace!,
this[PRIVATE].refSpace,
this[P_ACTION_RECORDER].refSpace,
)?.transform.matrix;
if (gripMatrix) {
const position = mat4.getTranslation(vec3.create(), gripMatrix);
Expand All @@ -169,13 +167,13 @@ export class ActionRecorder {
allValid &&= frame.fillPoses(
jointSpaces,
inputSource.targetRaySpace,
this[PRIVATE].jointTransforms,
this[P_ACTION_RECORDER].jointTransforms,
);

// @ts-ignore
allValid &&= frame.fillJointRadii(
jointSpaces,
this[PRIVATE].jointRadii,
this[P_ACTION_RECORDER].jointRadii,
);

if (allValid) {
Expand All @@ -187,11 +185,11 @@ export class ActionRecorder {
};
} = {} as any;
for (let offset = 0; offset < 25; offset++) {
const jointMatrix = this[PRIVATE].jointTransforms.slice(
const jointMatrix = this[P_ACTION_RECORDER].jointTransforms.slice(
offset * 16,
(offset + 1) * 16,
);
const radius = this[PRIVATE].jointRadii[offset];
const radius = this[P_ACTION_RECORDER].jointRadii[offset];
const position = mat4.getTranslation(vec3.create(), jointMatrix);
const quaternion = mat4.getRotation(quat.create(), jointMatrix);
const jointName = jointSpaces[offset].jointName as XRHandJoint;
Expand All @@ -216,7 +214,9 @@ export class ActionRecorder {
actionFrame.inputFrames.push(inputFrame);
}
});
this[PRIVATE].compressedFrames.push(this.compressActionFrame(actionFrame));
this[P_ACTION_RECORDER].compressedFrames.push(
this.compressActionFrame(actionFrame),
);
}

compressActionFrame(af: ActionFrame): any[] {
Expand All @@ -227,7 +227,7 @@ export class ActionRecorder {
];
af.inputFrames.forEach((inputFrame) => {
const index = inputFrame.index;
const schema = this[PRIVATE].schemaMap.get(index)!;
const schema = this[P_ACTION_RECORDER].schemaMap.get(index)!;
const inputOut: any[] = [
index,
...compress(inputFrame.targetRayTransform.position),
Expand Down Expand Up @@ -266,8 +266,8 @@ export class ActionRecorder {

log() {
const out = {
schema: Array.from(this[PRIVATE].schemaMap.entries()),
frames: this[PRIVATE].compressedFrames,
schema: Array.from(this[P_ACTION_RECORDER].schemaMap.entries()),
frames: this[P_ACTION_RECORDER].compressedFrames,
};
console.log(JSON.stringify(out));
}
Expand Down
Loading

0 comments on commit 2b25338

Please sign in to comment.