Skip to content

Commit

Permalink
update: edit camera
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Aug 21, 2023
1 parent 8f8346a commit 0eb1ed6
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/ThreejsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import { Made } from "@exabyte-io/made.js";
import { Box } from "@mui/material";
import { Canvas, useThree } from "@react-three/fiber";
import React, { useEffect } from "react";
import React, { useEffect, useRef } from "react";
import * as THREE from "three";

import { materialsToThreeDSceneData } from "../utils";

interface ThreejsSceneProps {
materials: Made.Material[];
}

function ThreejsScene({ materials }: ThreejsSceneProps): JSX.Element | null {
const { scene } = useThree();

Expand All @@ -21,13 +22,22 @@ function ThreejsScene({ materials }: ThreejsSceneProps): JSX.Element | null {
scene.add(...loadedScene.children);
}, [materials, scene]);

return null; // This component is just for side effects, it doesn't render anything
return null; // This component doesn't render anything visually itself
}

interface ThreejsEditorProps {
materials: Made.Material[];
}

export function ThreejsEditor({ materials }: ThreejsEditorProps): JSX.Element {
const cameraRef = useRef<THREE.PerspectiveCamera | null>(null);

useEffect(() => {
if (cameraRef.current) {
cameraRef.current.lookAt(new THREE.Vector3(0, 0, 0));
}
}, []);

useEffect(() => {
return () => {
// Cleanup actions (equivalent to componentWillUnmount)
Expand All @@ -40,7 +50,11 @@ export function ThreejsEditor({ materials }: ThreejsEditorProps): JSX.Element {
<Canvas style={{ background: "black" }}>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<camera position={[-20, 0, 10]} lookAt={} />
<perspectiveCamera
ref={cameraRef}
position={new THREE.Vector3(-20, 0, 10)}
up={new THREE.Vector3(0, 0, 1)}
/>
<ThreejsScene materials={materials} />
</Canvas>
</Box>
Expand Down

0 comments on commit 0eb1ed6

Please sign in to comment.