-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from TUK-MoreView/feat/object-create
feat: drag & drop ์ค๋ธ์ ํธ ์กฐ์ ์ถ๊ฐ
- Loading branch information
Showing
4 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |