import {useState,type CSSProperties} from "react"; export type CubeDirection = [number,number,number]; export interface NavigationCubeProps { /** Camera world quaternion, Y up. Directions point from target toward camera. */ quaternion: readonly [number,number,number,number]; onSelect: (direction:CubeDirection)=>void; size?:number; face?:string; text?:string; edge?:string; hover?:string; hoverText?:string; opacity?:number; edgeOpacity?:number; faceOpacity?:number; disabled?:boolean; label?:string; } const faces = [ {axis:0,sign:1,label:"RIGHT"},{axis:0,sign:-1,label:"LEFT"}, {axis:1,sign:1,label:"TOP"},{axis:1,sign:-1,label:"BOTTOM"}, {axis:2,sign:1,label:"FRONT"},{axis:2,sign:-1,label:"BACK"}, ]; const cuts=[-1,-.55,.55,1]; /** Inverse quaternion rotation; avoids a renderer or additional WebGL context. */ function view(p:number[],q:readonly number[]) { const n=Math.hypot(...q)||1; const [x,y,z,w]=[-q[0]/n,-q[1]/n,-q[2]/n,q[3]/n], [a,b,c]=p; const tx=2*(y*c-z*b),ty=2*(z*a-x*c),tz=2*(x*b-y*a); return [a+w*tx+y*tz-z*ty,b+w*ty+z*tx-x*tz,c+w*tz+x*ty-y*tx]; } export function cubeRegions(quaternion:NavigationCubeProps["quaternion"]) { return faces.flatMap(face=>{ const axes=[0,1,2].filter(k=>k!==face.axis),normal=[0,0,0];normal[face.axis]=face.sign; if(view(normal,quaternion)[2]<1e-5)return []; const point=(u:number,v:number)=>{const p=[0,0,0];p[face.axis]=face.sign;p[axes[0]]=u;p[axes[1]]=v;return p;}; return Array.from({length:9},(_,i)=>{ const row=Math.floor(i/3),col=i%3,direction=point(col-1,row-1) as CubeDirection; const label=faces.filter(f=>direction[f.axis]===f.sign).map(f=>f.label).join(" · "); const polygon=[[cuts[col],cuts[row]],[cuts[col+1],cuts[row]],[cuts[col+1],cuts[row+1]],[cuts[col],cuts[row+1]]] .map(([u,v])=>view(point(u,v),quaternion)).map(p=>`${60+p[0]*30},${60-p[1]*30}`).join(" "); const p=view(point(0,0),quaternion); // Face-local right/up tangents keep labels printed on the rotating face. const right=face.axis===0?[0,0,-face.sign]:face.axis===1?[1,0,0]:[face.sign,0,0]; const up=face.axis===1?[0,0,-face.sign]:[0,1,0]; const u=view(right,quaternion),v=view(up,quaternion); const textMatrix=[u[0],-u[1],-v[0],v[1],60+p[0]*30,60-p[1]*30]; return {id:`${face.label}-${i}`,direction,label,polygon,faceLabel:i===4?face.label:null,textMatrix}; }); }); } /** BIM-style face / edge / corner navigation, independent of the scene engine. */ export function NavigationCube({quaternion,onSelect,disabled=false,label="Навигационный куб",size=66,face,text,edge,hover:highlight,hoverText,opacity=1,edgeOpacity=1,faceOpacity=1}:NavigationCubeProps) { const [hover,setHover]=useState(null); return {cubeRegions(quaternion).map(r=>setHover(r.label)} onPointerLeave={()=>setHover(null)} onClick={()=>{if(!disabled)onSelect(r.direction);}} onKeyDown={e=>{if(!disabled&&(e.key==="Enter"||e.key===" ")){e.preventDefault();onSelect(r.direction);}}}> {r.label} {r.faceLabel?{r.faceLabel}:null} )} ; }