added new human ui
This commit is contained in:
@@ -4,11 +4,11 @@ import { useFrame, useThree } from '@react-three/fiber';
|
||||
import { useIsDragging, useIsRotating, useSelectedAction, useSelectedEventSphere } from '../../../../../store/simulation/useSimulationStore';
|
||||
import { useProductContext } from '../../../products/productContext';
|
||||
import { useSceneContext } from '../../../../scene/sceneContext';
|
||||
import { Group, Plane, Vector3 } from 'three';
|
||||
import { Group, Plane, Vector2, Vector3 } from 'three';
|
||||
import { useVersionContext } from '../../../../builder/version/versionContext';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import startPoint from "../../../../../assets/gltf-glb/ui/arrow_green.glb";
|
||||
import startEnd from "../../../../../assets/gltf-glb/ui/arrow_red.glb";
|
||||
import startPoint from "../../../../../assets/gltf-glb/ui/human-ui-green.glb";
|
||||
import startEnd from "../../../../../assets/gltf-glb/ui/human-ui-orange.glb";
|
||||
import { upsertProductOrEventApi } from '../../../../../services/simulation/products/UpsertProductOrEventApi';
|
||||
|
||||
function HumanUi() {
|
||||
@@ -18,7 +18,7 @@ function HumanUi() {
|
||||
const endMarker = useRef<Group>(null);
|
||||
const outerGroup = useRef<Group>(null);
|
||||
const prevMousePos = useRef({ x: 0, y: 0 });
|
||||
const { controls, raycaster } = useThree();
|
||||
const { controls, raycaster, camera } = useThree();
|
||||
const { selectedEventSphere } = useSelectedEventSphere();
|
||||
const { selectedProductStore } = useProductContext();
|
||||
const { humanStore, productStore } = useSceneContext();
|
||||
@@ -27,11 +27,12 @@ function HumanUi() {
|
||||
const { updateEvent } = productStore();
|
||||
const [startPosition, setStartPosition] = useState<[number, number, number]>([0, 1, 0]);
|
||||
const [endPosition, setEndPosition] = useState<[number, number, number]>([0, 1, 0]);
|
||||
const [startRotation, setStartRotation] = useState<[number, number, number]>([0, 0, 0]);
|
||||
const [startRotation, setStartRotation] = useState<[number, number, number]>([0, Math.PI, 0]);
|
||||
const [endRotation, setEndRotation] = useState<[number, number, number]>([0, 0, 0]);
|
||||
const { isDragging, setIsDragging } = useIsDragging();
|
||||
const { isRotating, setIsRotating } = useIsRotating();
|
||||
const plane = useRef(new Plane(new Vector3(0, 1, 0), 0));
|
||||
const dragOffset = useRef(new Vector3());
|
||||
|
||||
const [selectedHumanData, setSelectedHumanData] = useState<{
|
||||
position: [number, number, number];
|
||||
@@ -74,15 +75,15 @@ function HumanUi() {
|
||||
if (action.pickUpPoint?.position && outerGroup.current) {
|
||||
const worldPos = new Vector3(...action.pickUpPoint.position);
|
||||
const localPosition = outerGroup.current.worldToLocal(worldPos.clone());
|
||||
setStartPosition([localPosition.x, 0.5, localPosition.z]);
|
||||
setStartPosition([localPosition.x, 1, localPosition.z]);
|
||||
setStartRotation(action.pickUpPoint.rotation || [0, 0, 0]);
|
||||
}
|
||||
|
||||
if (action.dropPoint?.position && outerGroup.current) {
|
||||
const worldPos = new Vector3(...action.dropPoint.position);
|
||||
const localPosition = outerGroup.current.worldToLocal(worldPos.clone());
|
||||
setEndPosition([localPosition.x, 0.5, localPosition.z]);
|
||||
setEndRotation(action.dropPoint.rotation || [0, 0, 0]);
|
||||
setEndPosition([localPosition.x, 1, localPosition.z]);
|
||||
setEndRotation(action.dropPoint.rotation || [0, Math.PI, 0]);
|
||||
}
|
||||
}, [selectedEventSphere, outerGroup.current, selectedAction, humans]);
|
||||
|
||||
@@ -91,20 +92,39 @@ function HumanUi() {
|
||||
state: "start" | "end",
|
||||
rotation: "start" | "end"
|
||||
) => {
|
||||
if (e.object.name === "handle") {
|
||||
e.stopPropagation();
|
||||
const intersection = new Vector3();
|
||||
const pointer = new Vector2((e.clientX / window.innerWidth) * 2 - 1, -(e.clientY / window.innerHeight) * 2 + 1);
|
||||
raycaster.setFromCamera(pointer, camera);
|
||||
const intersects = raycaster.ray.intersectPlane(plane.current, intersection);
|
||||
|
||||
if (e.object.parent.name === "handle") {
|
||||
const normalizedX = (e.clientX / window.innerWidth) * 2 - 1;
|
||||
const normalizedY = -(e.clientY / window.innerHeight) * 2 + 1;
|
||||
prevMousePos.current = { x: normalizedX, y: normalizedY };
|
||||
setIsRotating(rotation);
|
||||
if (controls) (controls as any).enabled = false;
|
||||
setIsDragging(null);
|
||||
} else {
|
||||
setIsDragging(state);
|
||||
setIsRotating(null);
|
||||
if (controls) (controls as any).enabled = false;
|
||||
}
|
||||
|
||||
if (intersects) {
|
||||
let localPoint: Vector3 | null = null;
|
||||
if (outerGroup.current) {
|
||||
localPoint = outerGroup.current.worldToLocal(intersection.clone());
|
||||
}
|
||||
const marker = state === "start" ? startMarker.current : endMarker.current;
|
||||
if (marker && localPoint) {
|
||||
const markerPos = new Vector3().copy(marker.position);
|
||||
dragOffset.current.copy(markerPos.sub(localPoint));
|
||||
}
|
||||
}
|
||||
|
||||
if (controls) (controls as any).enabled = false;
|
||||
};
|
||||
|
||||
|
||||
const handlePointerUp = () => {
|
||||
(controls as any).enabled = true;
|
||||
setIsDragging(null);
|
||||
@@ -159,16 +179,15 @@ function HumanUi() {
|
||||
useFrame(() => {
|
||||
if (!isDragging || !plane.current || !raycaster || !outerGroup.current) return;
|
||||
const intersectPoint = new Vector3();
|
||||
const intersects = raycaster.ray.intersectPlane(
|
||||
plane.current,
|
||||
intersectPoint
|
||||
);
|
||||
const intersects = raycaster.ray.intersectPlane(plane.current, intersectPoint);
|
||||
if (!intersects) return;
|
||||
const localPoint = outerGroup?.current.worldToLocal(intersectPoint.clone());
|
||||
|
||||
const localPoint = outerGroup.current.worldToLocal(intersectPoint.clone()).add(dragOffset.current);
|
||||
|
||||
if (isDragging === "start") {
|
||||
setStartPosition([localPoint.x, 0.5, localPoint.z]);
|
||||
setStartPosition([localPoint.x, 1, localPoint.z]);
|
||||
} else if (isDragging === "end") {
|
||||
setEndPosition([localPoint.x, 0.5, localPoint.z]);
|
||||
setEndPosition([localPoint.x, 1, localPoint.z]);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -177,7 +196,7 @@ function HumanUi() {
|
||||
const currentPointerX = state.pointer.x;
|
||||
const deltaX = currentPointerX - prevMousePos.current.x;
|
||||
prevMousePos.current.x = currentPointerX;
|
||||
const marker =isRotating === "start" ? startMarker.current : endMarker.current;
|
||||
const marker = isRotating === "start" ? startMarker.current : endMarker.current;
|
||||
if (marker) {
|
||||
const rotationSpeed = 10;
|
||||
marker.rotation.y += deltaX * rotationSpeed;
|
||||
@@ -220,6 +239,7 @@ function HumanUi() {
|
||||
<group
|
||||
position={selectedHumanData.position}
|
||||
ref={outerGroup}
|
||||
rotation={[0, Math.PI, 0]}
|
||||
>
|
||||
<primitive
|
||||
name="startMarker"
|
||||
|
||||
Reference in New Issue
Block a user