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 | import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { Entity } from '@playcanvas/react';
import { Script } from '@playcanvas/react/components';
import { useApp } from '@playcanvas/react/hooks';
import { Gizmo, Entity as PcEntity, TranslateGizmo } from 'playcanvas';
import { Annotation as PcAnnotation } from 'playcanvas/scripts/esm/annotations.mjs';
import { CameraControls } from 'playcanvas/scripts/esm/camera-controls.mjs';
import { useCameraPosition } from '../../hooks/useCameraPosition';
import './annotation-styles.css';
export type AnnotationIconType = 'text' | 'image' | 'video';
/**
* Target normalized-device x coordinate for a clicked annotation. +0.5 places
* the hotspot three quarters across the screen (25% from the right), leaving room
* for the annotation panel on the left without covering the hotspot.
*/
const VIEW_HORIZONTAL_NDC_BIAS = 0.5;
export interface AnnotationProps {
position: [number, number, number];
title: string;
bodyText?: string;
label?: string;
imageUrls?: string[];
images?: File[];
icon?: AnnotationIconType;
cameraPosition?: [number, number, number];
visible?: boolean;
isEdit?: boolean;
isSelected?: boolean;
isViewed?: boolean;
onSelect?: () => void;
onPositionChange?: (position: [number, number, number]) => void;
onView?: (annotation: AnnotationProps, screenX: number, screenY: number) => void;
onScreenPositionUpdate?: (index: number, screenX: number, screenY: number, size?: number) => void;
}
/**
* A 3D annotation component that displays a hotspot in the scene.
* When clicked, displays annotation text and moves the camera.
* In edit mode, shows a translate gizmo when selected.
*
* @param props.position - The world position of the annotation
* @param props.index - The index of the annotation, used as a unique identifier
* @param props.title - The title displayed in the annotation
* @param props.bodyText - Optional description displayed in the annotation
* @param props.label - Optional label displayed on the hotspot
* @param props.cameraPosition - Optional camera position to move to when clicked. If undefined, camera stays at current position
* @param props.visible - Optional flag to show/hide the annotation. Defaults to true
* @param props.isEdit - Optional flag for edit mode. Defaults to false
* @param props.isSelected - Optional flag indicating if this annotation is selected. Defaults to false
* @param props.onSelect - Optional callback when annotation is clicked in edit mode
* @param props.onPositionChange - Optional callback when annotation position changes
*/
const Annotation = (props: AnnotationProps & { index: number }) => {
const {
position,
cameraPosition,
bodyText,
imageUrls,
icon,
title,
label,
visible = true,
isEdit = false,
isSelected = false,
isViewed = false,
onSelect,
onPositionChange,
onView,
onScreenPositionUpdate,
index,
} = props;
const app = useApp();
const { setCamera } = useCameraPosition();
const gizmoRef = useRef<TranslateGizmo | null>(null);
const layerRef = useRef<any>(null);
// Use refs to always call the latest callback values
const isEditRef = useRef(isEdit);
const onSelectRef = useRef(onSelect);
const onViewRef = useRef(onView);
const onScreenPositionUpdateRef = useRef(onScreenPositionUpdate);
const positionRef = useRef(position);
const cameraPositionRef = useRef(cameraPosition);
const annotationForViewRef = useRef<AnnotationProps>({
position,
title,
label,
bodyText,
imageUrls,
cameraPosition,
});
useEffect(() => {
isEditRef.current = isEdit;
onSelectRef.current = onSelect;
onViewRef.current = onView;
onScreenPositionUpdateRef.current = onScreenPositionUpdate;
positionRef.current = position;
cameraPositionRef.current = cameraPosition;
annotationForViewRef.current = { position, title, label, bodyText, imageUrls, cameraPosition };
}, [isEdit, onSelect, onView, onScreenPositionUpdate, position, cameraPosition, title, label, bodyText, imageUrls]);
// Create a stable callback that reads from refs, because this is read from TfAnnotationManager when the annotation is added to the scene
const handleClick = useCallback(
(screenX?: number, screenY?: number) => {
if (isEditRef.current) {
onSelectRef.current?.();
} else {
setCamera(positionRef.current, cameraPositionRef.current, VIEW_HORIZONTAL_NDC_BIAS);
if (onViewRef.current && screenX !== undefined && screenY !== undefined) {
onViewRef.current(annotationForViewRef.current, screenX, screenY);
}
}
},
[setCamera]
);
// Stable callback so its identity doesn't churn the underlying script prop each frame.
const handleScreenPositionUpdate = useCallback(
(screenX: number, screenY: number, size?: number) => {
onScreenPositionUpdateRef.current?.(index, screenX, screenY, size);
},
[index]
);
const entityName = useMemo(() => `annotation-${index}`, [index]);
useEffect(() => {
const shouldShowGizmo = isEdit && isSelected && visible;
const cleanupGizmo = () => {
if (gizmoRef.current) {
try {
gizmoRef.current.detach();
gizmoRef.current.destroy();
} catch (error) {
// Ignore cleanup errors
}
gizmoRef.current = null;
}
if (layerRef.current && app.scene?.layers) {
try {
const layer = app.scene.layers.getLayerByName('Gizmo');
if (layer) {
app.scene.layers.remove(layerRef.current);
}
} catch (error) {
// Ignore cleanup errors
}
layerRef.current = null;
}
};
if (!shouldShowGizmo) {
cleanupGizmo();
return;
}
const setupGizmo = () => {
const annotationEntity = app.root.findByName(entityName) as PcEntity | null;
const cameraEntity = app.root.findByName('camera') as PcEntity | null;
const cameraComponent = cameraEntity?.camera;
const cameraControls = cameraEntity?.script?.get(CameraControls.scriptName) as any;
if (!annotationEntity || !cameraComponent) {
return false;
}
if (!gizmoRef.current) {
if (!layerRef.current) {
layerRef.current = Gizmo.createLayer(app);
}
const gizmo = new TranslateGizmo(cameraComponent, layerRef.current);
gizmo.size = 0.75;
gizmo.snapIncrement = 0.01;
gizmo.coordSpace = 'world';
gizmo.on('transform:start', () => {
if (cameraControls) {
cameraControls.enabled = false;
}
});
gizmo.on('transform:end', () => {
if (cameraControls) {
cameraControls.enabled = true;
}
if (onPositionChange) {
const pos = annotationEntity.getPosition();
onPositionChange([pos.x, pos.y, pos.z]);
}
});
gizmo.attach([annotationEntity]);
gizmoRef.current = gizmo;
}
return true;
};
const intervalId = setInterval(() => {
if (setupGizmo()) {
clearInterval(intervalId);
}
}, 100);
return () => {
clearInterval(intervalId);
cleanupGizmo();
};
}, [app, isEdit, isSelected, visible, onPositionChange, entityName]);
return (
<Entity name={entityName} position={position}>
<Script
script={PcAnnotation}
title={title}
label={label}
icon={icon}
text={bodyText}
enabled={visible}
onClickCallback={handleClick}
onScreenPositionUpdateCallback={isViewed ? handleScreenPositionUpdate : undefined}
/>
</Entity>
);
};
export default Annotation;
|