Skip to content

Commit

Permalink
feat(cmpts): update general canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
FradSer committed Jan 3, 2023
1 parent 90331d9 commit bdc8f37
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions components/WebXR/GeneralCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Canvas } from '@react-three/fiber';
import { PerspectiveCamera, OrbitControls } from '@react-three/drei';
import { Canvas, useFrame } from '@react-three/fiber';
import { Controllers, XR, XRButton } from '@react-three/xr';
import { ReactNode } from 'react';
import { ReactNode, useRef } from 'react';

import useMousePosition from '../../hooks/useMousePosition';
import useWindowSize from '../../hooks/useWindowSize';
import useXRDetect from '../../hooks/useXRDetect';

type IGenericCanvasProps = {
children: ReactNode;
};

function MouseMove({ children }: IGenericCanvasProps) {
const mousePosition = useMousePosition();
const windowSize = useWindowSize();

const ref = useRef<any>();

useFrame(() => {
const positionX = mousePosition.x / windowSize.width;
const positionY = mousePosition.y / windowSize.height;

const delta = 0.1;

ref.current.position.x = positionX * delta;
ref.current.position.y = positionY * delta;
});

return <mesh ref={ref}>{children}</mesh>;
}

function GenericCanvas({ children }: IGenericCanvasProps) {
const xrDetect = useXRDetect();

const deg2rad = (degrees: number) => degrees * (Math.PI / 180);

return xrDetect.isVR ? (
<>
<XRButton mode="VR" />
Expand All @@ -26,9 +44,7 @@ function GenericCanvas({ children }: IGenericCanvasProps) {
</>
) : (
<Canvas>
<PerspectiveCamera />
<OrbitControls makeDefault />
{children}
<MouseMove>{children}</MouseMove>
</Canvas>
);
}
Expand Down

0 comments on commit bdc8f37

Please sign in to comment.