Skip to content

Commit

Permalink
Merge pull request #65 from TUK-MoreView/feat/object-create
Browse files Browse the repository at this point in the history
feat: drag & drop ์˜ค๋ธŒ์ ํŠธ ์กฐ์ž‘ ์ถ”๊ฐ€
  • Loading branch information
play3step authored Jun 27, 2024
2 parents 7b8069e + 8cb49db commit 4e58de0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 5 deletions.
Binary file added public/models copy/model.glb
Binary file not shown.
6 changes: 4 additions & 2 deletions src/components/EditPage/PageData/Edit3d.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ function Edit3d({ objecturl }) {
const dataSize = useRecoilValue(objectSizeState);

const [isMouseDown, setIsMouseDown] = useState(false);
const [isDragging, setIsDragging] = useState(false);

useEffect(() => {
const sensitivity = 0.002;
const euler = new Euler(0, 0, 0, 'YXZ');

const handleMouseMove = (event) => {
if (!isMouseDown) return;
if (!isMouseDown || isDragging) return;

const { movementX, movementY } = event;

Expand All @@ -44,7 +45,7 @@ function Edit3d({ objecturl }) {
window.removeEventListener('mousedown', handleMouseDown);
window.removeEventListener('mouseup', handleMouseUp);
};
}, [camera, isMouseDown]);
}, [camera, isMouseDown, isDragging]);
const objectList = Object.values(objecturl || {});

return (
Expand Down Expand Up @@ -77,6 +78,7 @@ function Edit3d({ objecturl }) {
x={url.x}
y={url.y}
z={url.z}
setIsDragging={setIsDragging}
/>
) : null,
)}
Expand Down
38 changes: 35 additions & 3 deletions src/components/EditPage/PageData/Edit3d/EditGltfLoader.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, { useRef, useEffect, useState } from 'react';
import { useFrame, useThree } from '@react-three/fiber';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { Color, Box3, Vector3 } from 'three';
import { Color, Box3, Vector3, AxesHelper } from 'three';
import { useRecoilState } from 'recoil';
import { DragControls } from 'three/examples/jsm/controls/DragControls';
import { LodingState } from '../../../../store/modalState';
import useKeyDown from '../../../../hooks/EditPage/Handlers/useKeyDown';

function EditGltfLoader({ objecturl, size, x, y, z }) {
function EditGltfLoader({ objecturl, size, x, y, z, setIsDragging }) {
const modelRef = useRef();
const { camera, scene } = useThree();
const { camera, scene, gl } = useThree();
const [loadingValue, setLoadingValue] = useRecoilState(LodingState);
const [gltf, setGltf] = useState(null);
const [initialScale, setInitialScale] = useState([1, 1, 1]);
const movement = useRef({ forward: 0, right: 0, up: 0 });
const gltfUrl = typeof objecturl === 'string' ? objecturl : objecturl.gltf;
const controlsRef = useRef();

useEffect(() => {
scene.background = new Color('#FFFFFF');
Expand Down Expand Up @@ -60,7 +62,37 @@ function EditGltfLoader({ objecturl, size, x, y, z }) {
});

useKeyDown(movement, modelRef);
useEffect(() => {
if (modelRef.current) {
controlsRef.current = new DragControls(
[modelRef.current],
camera,
gl.domElement,
);

controlsRef.current.addEventListener('dragstart', (event) => {
event.object.material.emissive.set(0xaaaaaa);
setIsDragging(true);
});

controlsRef.current.addEventListener('dragend', (event) => {
event.object.material.emissive.set(0x000000);
setIsDragging(false);
});

return () => {
controlsRef.current.dispose();
};
}
return undefined;
}, [camera, gl]);
useEffect(() => {
const axesHelper = new AxesHelper(100);
scene.add(axesHelper);
return () => {
scene.remove(axesHelper);
};
}, [scene]);
return (
<mesh ref={modelRef} visible={!loadingValue}>
{gltf ? (
Expand Down
76 changes: 76 additions & 0 deletions src/components/Modal/atom/Test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { useEffect, useRef, useState } from 'react';
import { useThree } from '@react-three/fiber';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader';
import { Color, Box3, Vector3 } from 'three';
import { useRecoilState } from 'recoil';
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader';
import { LodingState } from '../../../store/modalState';

function TestOBJ({ objecturl, x, y, z }) {
const modelRef = useRef();
const [loadingValue, setLoadingValue] = useRecoilState(LodingState);
const [object, setObject] = useState(null);

const { scene } = useThree();

useEffect(() => {
scene.background = new Color('#FFFFFF');
}, [scene]);

useEffect(() => {
if (!objecturl?.obj) {
setLoadingValue(false);
setObject(null);
return;
}
setLoadingValue(true);

const materialUrl = objecturl?.mtl || null;
const loadUrl = objecturl?.obj || null;

const loadModel = () => {
const objLoader = new OBJLoader();
const onLoad = (obj) => {
// ๋ชจ๋ธ์˜ ๊ฒฝ๊ณ„ ๊ณ„์‚ฐ
const box = new Box3().setFromObject(obj);
const size = new Vector3();
box.getSize(size);
const maxDimension = Math.max(size.x, size.y, size.z);

const scale = 5 / maxDimension;

obj.scale.set(scale, scale, scale);

setObject(obj);
setLoadingValue(false);
};

if (materialUrl) {
const mtlLoader = new MTLLoader();
mtlLoader.load(materialUrl, (materials) => {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load(loadUrl, onLoad);
});
} else {
objLoader.load(loadUrl, onLoad);
}
};

loadModel();
}, [objecturl, setLoadingValue]);

useEffect(() => {
if (object) {
setLoadingValue(false);
}
}, [object, setLoadingValue]);

return (
<mesh ref={modelRef} visible={!loadingValue}>
{object ? <primitive object={object} position={[x, y, z]} /> : null}
</mesh>
);
}

export default TestOBJ;

0 comments on commit 4e58de0

Please sign in to comment.