Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment Bubbles WASD Fix #3920

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions packages/frontend-2/lib/viewer/composables/anchorPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,44 @@ export function useViewerAnchoredPointCalculator(params: {
*/
const calculate = (target: Vector3) => {
let targetLoc: Nullable<{ x: number; y: number }> = null
let inFrustum: boolean | undefined = false
if (parentEl.value) {
const targetProjectionResult = viewer.query<PointQuery>({
point: target,
operation: 'Project'
})
targetLoc = viewer.Utils.NDCToScreen(
targetProjectionResult.x,
targetProjectionResult.y,
parentEl.value.clientWidth,
parentEl.value.clientHeight
)
inFrustum ||= targetProjectionResult.inFrustum
/** If not in camera's frustum, don't bother projecting */
if (inFrustum)
targetLoc = viewer.Utils.NDCToScreen(
targetProjectionResult.x,
targetProjectionResult.y,
parentEl.value.clientWidth,
parentEl.value.clientHeight
)

// round it out
if (targetLoc) {
targetLoc.x = round(targetLoc.x)
targetLoc.y = round(targetLoc.y)
}

// logger.debug(targetLoc, targetProjectionResult, target, new Date().toISOString())
}

const targetOcclusionRes = viewer.query<IntersectionQuery>({
point: target,
tolerance: 0.001,
operation: 'Occlusion'
})
let isOccluded: boolean | undefined = true
/** If not in camera's frustum don't bother intersecting */
if (inFrustum) {
const targetOcclusionRes = viewer.query<IntersectionQuery>({
point: target,
tolerance: 0.001,
operation: 'Occlusion'
})
isOccluded = !!targetOcclusionRes.objects?.length
}

return {
screenLocation: targetLoc?.x && targetLoc?.y ? targetLoc : null,
isOccluded: !!targetOcclusionRes.objects?.length,
isOccluded,
style: <Partial<CSSProperties>>{
...(targetLoc?.x && targetLoc?.y
? {
Expand Down
42 changes: 34 additions & 8 deletions packages/viewer/src/modules/queries/PointQuerySolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Vector3 } from 'three'
import { Frustum, Vector3 } from 'three'
import SpeckleRenderer from '../SpeckleRenderer.js'
import type { PointQuery, PointQueryResult } from './Query.js'
import Logger from '../utils/Logger.js'

const frustumBuf: Frustum = new Frustum()
const vec3Buff: Vector3 = new Vector3()

export class PointQuerySolver {
private renderer!: SpeckleRenderer
private renderer: SpeckleRenderer

public setContext(renderer: SpeckleRenderer) {
this.renderer = renderer
Expand All @@ -25,28 +28,51 @@ export class PointQuerySolver {
private solveProjection(query: PointQuery): PointQueryResult {
// WORLD
const projected = new Vector3(query.point.x, query.point.y, query.point.z)
if (this.renderer.renderingCamera) projected.project(this.renderer.renderingCamera)
else Logger.error('Could not run query. Camera is null')
let inFrustum = false

if (this.renderer.renderingCamera) {
/** We need to check frustum inclusion in view space. */
vec3Buff.copy(projected)
vec3Buff.applyMatrix4(this.renderer.renderingCamera.matrixWorldInverse)

/** We check in-frustum *before* projection */
inFrustum = frustumBuf
.setFromProjectionMatrix(this.renderer.renderingCamera.projectionMatrix)
.containsPoint(vec3Buff)
projected.project(this.renderer.renderingCamera)
} else Logger.error('Could not run query. Camera is null')

return {
// NDC
x: projected.x,
y: projected.y,
z: projected.z
z: projected.z,
inFrustum
}
}

private solveUnprojection(query: PointQuery): PointQueryResult {
// NDC
let inFrustum = false
const unprojected = new Vector3(query.point.x, query.point.y, query.point.z)
if (this.renderer.renderingCamera)
if (this.renderer.renderingCamera) {
unprojected.unproject(this.renderer.renderingCamera)
else Logger.error('Could not run query. Camera is null')

/** We need to check frustum inclusion in view space. */
vec3Buff.copy(unprojected)
vec3Buff.applyMatrix4(this.renderer.renderingCamera.matrixWorldInverse)

/** We check in-frustum *after* projection */
inFrustum = frustumBuf
.setFromProjectionMatrix(this.renderer.renderingCamera.projectionMatrix)
.containsPoint(vec3Buff)
} else Logger.error('Could not run query. Camera is null')
return {
// WORLD
x: unprojected.x,
y: unprojected.y,
z: unprojected.z
z: unprojected.z,
inFrustum
}
}
}
1 change: 1 addition & 0 deletions packages/viewer/src/modules/queries/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface PointQueryResult {
y: number
z?: number
w?: number
inFrustum?: boolean
}

export interface IntersectionQueryResult {
Expand Down
Loading