@@ -6,7 +6,7 @@ import { animated, useSpring } from "@react-spring/three";
66import { useTextInput } from "../../../logic/input" ;
77import { useKeypress , useShiftHold } from "../../../logic/keys" ;
88import { usePlayer } from "../../../layers/Player" ;
9- import { Mesh , MeshStandardMaterial , Raycaster } from "three" ;
9+ import { Group , Mesh , MeshStandardMaterial , Raycaster , Vector3 } from "three" ;
1010import { syncOnChange } from "./logic/sync" ;
1111import {
1212 getClickedCaret ,
@@ -19,6 +19,7 @@ import { useDragSelect } from "./logic/drag";
1919import { useLimitedFrame } from "../../../logic/limiter" ;
2020import { cache } from "../../../logic/cache" ;
2121import { HitBox } from "../../primitives/HitBox" ;
22+ import { useEnvironment } from "../../../layers/Environment" ;
2223
2324type TextProps = {
2425 value ?: string ;
@@ -51,9 +52,12 @@ export function TextInput(props: TextProps) {
5152 } = props ;
5253
5354 const clock = useThree ( ( st ) => st . clock ) ;
55+ const camera = useThree ( ( st ) => st . camera ) ;
5456 const player = usePlayer ( ) ;
57+ const { device } = useEnvironment ( ) ;
5558 const RAYCASTER = passedRaycaster || player . raycaster ;
5659
60+ const group = useRef < Group > ( null ) ;
5761 const textRef = useRef < any > ( ) ;
5862 const caret = useRef < Mesh > ( null ) ;
5963 const highlight = useRef < Mesh > ( null ) ;
@@ -67,18 +71,27 @@ export function TextInput(props: TextProps) {
6771
6872 const { input, focused, focusInput } = useTextInput ( type , val , setVal ) ;
6973
74+ // focus callback
7075 useEffect ( ( ) => {
7176 if ( ! onFocus ) return ;
7277 input . addEventListener ( "focus" , onFocus ) ;
7378 return ( ) => input . removeEventListener ( "focus" , onFocus ) ;
7479 } , [ input , onFocus ] ) ;
7580
81+ // blur callback
7682 useEffect ( ( ) => {
7783 if ( ! onBlur ) return ;
7884 input . addEventListener ( "blur" , onBlur ) ;
7985 return ( ) => input . removeEventListener ( "blur" , onBlur ) ;
8086 } , [ input , onBlur ] ) ;
8187
88+ // look at input when focused, only on mobile
89+ useEffect ( ( ) => {
90+ if ( ! group . current || ! focused || ! device . mobile ) return ;
91+ const worldpos = group . current . getWorldPosition ( new Vector3 ( ) ) ;
92+ camera . lookAt ( worldpos ) ;
93+ } , [ focused , camera , device ] ) ;
94+
8295 const { color } = useSpring ( { color : focused ? "#000" : "#828282" } ) ;
8396
8497 const highlightMat = cache . useResource (
@@ -321,7 +334,7 @@ export function TextInput(props: TextProps) {
321334 } ) ;
322335
323336 return (
324- < group name = "spacesvr-text-input" { ...rest } >
337+ < group name = "spacesvr-text-input" { ...rest } ref = { group } >
325338 < group name = "content" position-z = { DEPTH / 2 + 0.001 } >
326339 < Suspense fallback = { null } >
327340 < Text
0 commit comments