Refactor runtime target look input to use screen-space projection for candidate selection

This commit is contained in:
2026-04-25 16:26:47 +02:00
parent 4be1e05251
commit ff11b16f42

View File

@@ -5618,138 +5618,168 @@ export class RuntimeHost {
this.setActiveRuntimeTargetReference(null); this.setActiveRuntimeTargetReference(null);
} }
private handleRuntimeTargetLookInput(horizontalIntent: -1 | 0 | 1): boolean { private createRuntimeTargetLookInputResult(
result: Partial<RuntimeTargetLookInputResult> = {}
): RuntimeTargetLookInputResult {
return {
activeTargetLocked: result.activeTargetLocked ?? false,
switchedTarget: result.switchedTarget ?? false,
switchInputHeld: result.switchInputHeld ?? false
};
}
private handleRuntimeTargetLookInput(
input: RuntimeTargetLookInput
): RuntimeTargetLookInputResult {
const activeTarget = this.resolveActiveRuntimeTarget(); const activeTarget = this.resolveActiveRuntimeTarget();
if (activeTarget === null) { if (activeTarget === null) {
if (this.activeRuntimeTargetReference !== null) { if (this.activeRuntimeTargetReference !== null) {
this.setActiveRuntimeTargetReference(null); this.setActiveRuntimeTargetReference(null);
} }
this.runtimeTargetLookInputHeldDirection = null; this.runtimeTargetSwitchInputHeld = false;
return false; return this.createRuntimeTargetLookInputResult();
} }
if (horizontalIntent === 0) { const inputMagnitude = Math.hypot(input.horizontal, input.vertical);
this.runtimeTargetLookInputHeldDirection = null;
return true; if (inputMagnitude <= Number.EPSILON) {
this.runtimeTargetSwitchInputHeld = false;
return this.createRuntimeTargetLookInputResult({
activeTargetLocked: true
});
} }
if (this.runtimeTargetLookInputHeldDirection === horizontalIntent) { if (
return true; this.runtimeTargetSwitchInputHeld ||
inputMagnitude < TARGETING_DIRECTION_SWITCH_INPUT_THRESHOLD
) {
if (inputMagnitude < TARGETING_DIRECTION_SWITCH_INPUT_THRESHOLD) {
this.runtimeTargetSwitchInputHeld = false;
}
return this.createRuntimeTargetLookInputResult({
activeTargetLocked: true,
switchInputHeld: this.runtimeTargetSwitchInputHeld
});
} }
this.runtimeTargetLookInputHeldDirection = horizontalIntent; const directionalTarget = this.resolveRuntimeTargetCandidateInLookDirection(
const origin = this.currentPlayerControllerTelemetry?.eyePosition ?? null;
if (origin === null) {
return true;
}
this.camera.getWorldDirection(this.cameraForward);
const cameraLength = Math.hypot(this.cameraForward.x, this.cameraForward.z);
const fallbackDirection = {
x: activeTarget.center.x - origin.x,
z: activeTarget.center.z - origin.z
};
const fallbackLength = Math.hypot(fallbackDirection.x, fallbackDirection.z);
const cameraDirection =
cameraLength <= Number.EPSILON && fallbackLength > Number.EPSILON
? {
x: fallbackDirection.x / fallbackLength,
z: fallbackDirection.z / fallbackLength
}
: {
x: this.cameraForward.x / Math.max(cameraLength, Number.EPSILON),
z: this.cameraForward.z / Math.max(cameraLength, Number.EPSILON)
};
const sideTarget = this.resolveRuntimeTargetCandidateOnLookSide(
activeTarget, activeTarget,
-horizontalIntent as -1 | 1, input
cameraDirection
); );
if (sideTarget !== null) { if (directionalTarget !== null) {
this.setActiveRuntimeTargetReference({ this.setActiveRuntimeTargetReference({
kind: sideTarget.kind, kind: directionalTarget.kind,
entityId: sideTarget.entityId entityId: directionalTarget.entityId
});
this.runtimeTargetSwitchInputHeld = true;
this.proposedRuntimeTarget = directionalTarget;
return this.createRuntimeTargetLookInputResult({
activeTargetLocked: true,
switchedTarget: true,
switchInputHeld: true
}); });
this.runtimeTargetLookInputHeldDirection = horizontalIntent;
this.proposedRuntimeTarget = sideTarget;
} }
return true; return this.createRuntimeTargetLookInputResult({
activeTargetLocked: true
});
} }
private resolveRuntimeTargetCandidateOnLookSide( private resolveRuntimeTargetScreenPoint(point: {
activeTarget: RuntimeResolvedTarget, x: number;
lookSide: -1 | 1, y: number;
cameraDirection: { x: number; z: number } z: number;
): RuntimeTargetCandidate | null { }) {
const origin = this.currentPlayerControllerTelemetry?.eyePosition ?? null; const projected = new Vector3(point.x, point.y, point.z).project(this.camera);
if (origin === null) { if (
!Number.isFinite(projected.x) ||
!Number.isFinite(projected.y) ||
!Number.isFinite(projected.z) ||
projected.z < -1 ||
projected.z > 1
) {
return null; return null;
} }
const activeDirection = { return {
x: activeTarget.center.x - origin.x, x: projected.x,
z: activeTarget.center.z - origin.z y: projected.y
}; };
const activeLength = Math.hypot(activeDirection.x, activeDirection.z); }
if (activeLength <= Number.EPSILON) { private resolveRuntimeTargetCandidateInLookDirection(
activeTarget: RuntimeResolvedTarget,
input: RuntimeTargetLookInput
): RuntimeTargetCandidate | null {
const inputLength = Math.hypot(input.horizontal, input.vertical);
if (inputLength <= Number.EPSILON) {
return null;
}
const activeScreenPoint = this.resolveRuntimeTargetScreenPoint(
activeTarget.center
);
if (activeScreenPoint === null) {
return null; return null;
} }
let bestCandidate: RuntimeTargetCandidate | null = null; let bestCandidate: RuntimeTargetCandidate | null = null;
let bestCameraAngle = Number.POSITIVE_INFINITY; let bestAlignment = TARGETING_SCREEN_SWITCH_MIN_ALIGNMENT;
const activeX = activeDirection.x / activeLength; let bestScreenDistance = 0;
const activeZ = activeDirection.z / activeLength; const inputX = input.horizontal / inputLength;
const inputY = input.vertical / inputLength;
for (const candidate of this.runtimeTargetCandidates) { for (const candidate of this.runtimeTargetCandidates) {
if (candidate.entityId === activeTarget.entityId) { if (candidate.entityId === activeTarget.entityId) {
continue; continue;
} }
const candidateDirection = { const candidateScreenPoint = this.resolveRuntimeTargetScreenPoint(
x: candidate.center.x - origin.x, candidate.center
z: candidate.center.z - origin.z
};
const candidateLength = Math.hypot(
candidateDirection.x,
candidateDirection.z
);
if (candidateLength <= Number.EPSILON) {
continue;
}
const candidateX = candidateDirection.x / candidateLength;
const candidateZ = candidateDirection.z / candidateLength;
const signedAngle = Math.atan2(
activeZ * candidateX - activeX * candidateZ,
activeX * candidateX + activeZ * candidateZ
);
if (signedAngle * lookSide <= TARGETING_SIDE_SWITCH_EPSILON_RADIANS) {
continue;
}
const cameraAngle = Math.acos(
clampScalar(
candidateX * cameraDirection.x + candidateZ * cameraDirection.z,
-1,
1
)
); );
if (
candidateScreenPoint === null ||
Math.abs(candidateScreenPoint.x) > TARGETING_SCREEN_SWITCH_MAX_ABS_X ||
Math.abs(candidateScreenPoint.y) > TARGETING_SCREEN_SWITCH_MAX_ABS_Y
) {
continue;
}
const screenDeltaX = candidateScreenPoint.x - activeScreenPoint.x;
const screenDeltaY = candidateScreenPoint.y - activeScreenPoint.y;
const screenDistance = Math.hypot(screenDeltaX, screenDeltaY);
if (screenDistance < TARGETING_SCREEN_SWITCH_MIN_DISTANCE) {
continue;
}
const alignment =
(screenDeltaX / screenDistance) * inputX +
(screenDeltaY / screenDistance) * inputY;
if (alignment < TARGETING_SCREEN_SWITCH_MIN_ALIGNMENT) {
continue;
}
if ( if (
bestCandidate === null || bestCandidate === null ||
cameraAngle < bestCameraAngle || alignment > bestAlignment ||
(cameraAngle === bestCameraAngle && candidate.score > bestCandidate.score) (alignment === bestAlignment && screenDistance > bestScreenDistance) ||
(alignment === bestAlignment &&
screenDistance === bestScreenDistance &&
candidate.score > bestCandidate.score)
) { ) {
bestCandidate = candidate; bestCandidate = candidate;
bestCameraAngle = cameraAngle; bestAlignment = alignment;
bestScreenDistance = screenDistance;
} }
} }