Skip to content

Commit

Permalink
feat: 감정분석 API sentiment 사용
Browse files Browse the repository at this point in the history
sentiment 받아와서 감정에 맞는 파티클 추가
  • Loading branch information
5nxtnxtnxt committed Dec 5, 2023
1 parent b4fd904 commit 2b1d585
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 105 deletions.
Binary file added front/public/models/heart.glb
Binary file not shown.
Binary file added front/public/models/star.glb
Binary file not shown.
Binary file added front/public/models/water.glb
Binary file not shown.
3 changes: 1 addition & 2 deletions front/src/components/Msg/ListMsg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ const StyledDeleteButton = styled.button``;

const ListMsg = (props: MsgProps): JSX.Element => {
const navigate = useNavigate();
const { setSnowBallData, setUserData, userData, snowBallData } =
useContext(SnowBallContext);
const { setSnowBallData, setUserData } = useContext(SnowBallContext);

const deleteMsg = () => {
axios
Expand Down
18 changes: 11 additions & 7 deletions front/src/components/SnowGlobeCanvas/SnowGlobeCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useRef } from 'react';
import { useContext, useRef } from 'react';
import { OrbitControls } from '@react-three/drei';
import { Canvas } from '@react-three/fiber';
import * as THREE from 'three';
Expand All @@ -13,10 +13,7 @@ const SnowGlobeCanvas = () => {
const glassRadius = 7;
const glassPosition = new THREE.Vector3(0, glassRadius / 2, 0);
const { snowBallData } = useContext(SnowBallContext);
// // debugger;
useEffect(() => {
console.log(snowBallData, 'test');
}, [snowBallData]);

const snows = Array.from({ length: 100 }, (_, i) => (
<Models.Snow
key={i}
Expand All @@ -26,7 +23,6 @@ const SnowGlobeCanvas = () => {
model={Math.floor(Math.random() * 3)}
/>
));
// console.log(snowBallData, 'test');
const decos = snowBallData.message_list.map((message, index) => {
return (
<Models.Deco
Expand All @@ -42,7 +38,14 @@ const SnowGlobeCanvas = () => {
/>
);
});

const sentiments = snowBallData.message_list.map((message, i) => (
<Models.Emoji
key={i}
centerPosition={glassPosition}
rangeRadius={glassRadius}
sentiment={message.sentiment}
/>
));
return (
<PrevProvider>
<CanvasContainer>
Expand Down Expand Up @@ -96,6 +99,7 @@ const SnowGlobeCanvas = () => {
/>
{snows}
{decos}
{sentiments}
</Canvas>
</CanvasContainer>

Expand Down
84 changes: 84 additions & 0 deletions front/src/components/SnowGlobeCanvas/models/Emoji.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { useRef } from 'react';
import { useFrame } from '@react-three/fiber';
import { useGLTF } from '@react-three/drei';
import * as THREE from 'three';
import { SENTIMENT_MODEL } from '@constants';

interface SnowProps {
centerPosition: THREE.Vector3;
rangeRadius: number;
sentiment: string;
}

const randomizePosition = (
target: THREE.Mesh,
centerPosition: THREE.Vector3,
radius: number
) => {
//변경필요
target.position.set(
centerPosition.x - 3 + Math.random() * 6,
centerPosition.y + radius + Math.random() * radius * 2,
centerPosition.z - 3 + Math.random() * 6
);
};

const fallingAnimate = (
target: THREE.Mesh,
speed: number,
centerPosition: THREE.Vector3,
radius: number
) => {
if (target.position.y <= -1) {
randomizePosition(target, centerPosition, radius);
//const newScale = 0.2 + Math.random() * 0.5;
//target.scale.set(newScale, newScale, newScale);
}
target.position.y -= speed;
};

const rotateAnimate = (target: THREE.Mesh, speed: number) => {
target.rotation.y += speed;
};

const visibleInRange = (
target: THREE.Mesh,
centerPosition: THREE.Vector3,
radius: number
) => {
target.visible =
target.position.distanceTo(centerPosition) > radius ? false : true;
};

const Emoji: React.FC<SnowProps> = ({
centerPosition,
rangeRadius,
sentiment
}) => {
const snowRef = useRef<THREE.Mesh>(null);
const position = new THREE.Vector3(
centerPosition.x - rangeRadius + Math.random() * rangeRadius * 2,
centerPosition.y + rangeRadius + Math.random() * rangeRadius * 2,
centerPosition.z - rangeRadius + Math.random() * rangeRadius * 2
);
const index = sentiment === 'positive' ? 1 : sentiment === 'neutral' ? 2 : 3;
const snow = useGLTF(SENTIMENT_MODEL[index].fileName).scene.clone();

snow.scale.set(0.7, 0.7, 0.7);
snow.position.set(position.x, position.y, position.z);
snow.rotation.y = Math.random();

useFrame((_, delta) => {
const snow = snowRef.current;
const speed = 2 * delta;
if (snow) {
fallingAnimate(snow, speed, centerPosition, rangeRadius);
rotateAnimate(snow, speed);
visibleInRange(snow, centerPosition, rangeRadius - 1);
}
});

return <primitive object={snow} ref={snowRef} />;
};

export default Emoji;
1 change: 1 addition & 0 deletions front/src/components/SnowGlobeCanvas/models/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { default as Ground } from './Ground';
export { default as MainDeco } from './MainDeco';
export { default as Snow } from './Snow';
export { default as Raycaster } from './Raycaster';
export { default as Emoji } from './Emoji';
1 change: 1 addition & 0 deletions front/src/components/Song/Song.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Song = () => {

return (
<StyledMusic
id="musicController"
src={play ? '/music/music.svg' : '/music/music-slash.svg'}
onClick={toggle}
/>
Expand Down
16 changes: 16 additions & 0 deletions front/src/constants/deco.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export const SENTIMENT_MODEL = [
{ name: '선물상자', fileName: '/models/ribbonBox.glb', img: 'ribbonBox.png' },
{
name: '긍정',
fileName: '/models/heart.glb'
},
{
name: '보통',
fileName: '/models/star.glb'
},
{
name: '부정',
fileName: '/models/water.glb'
}
];

export const DECO = [
{ name: '선물상자', fileName: '/models/ribbonBox.glb', img: 'ribbonBox.png' },
{
Expand Down
2 changes: 1 addition & 1 deletion front/src/constants/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DECO, MAIN, MSG_COLOR, BOTTOM } from './deco';
export { SENTIMENT_MODEL, DECO, MAIN, MSG_COLOR, BOTTOM } from './deco';
8 changes: 6 additions & 2 deletions front/src/mockdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"opened": null,
"sender": "박민수",
"snowball_id": 1,
"user_id": 1
"user_id": 1,
"sentiment": "positive",
"confidence": 100
},
{
"content": "메리크리스마스~\n 내년에도 사이좋게 지내자!!.",
Expand All @@ -34,7 +36,9 @@
"opened": null,
"sender": "김찬우",
"snowball_id": 1,
"user_id": 1
"user_id": 1,
"sentiment": "positive",
"confidence": 100
}
]
},
Expand Down
23 changes: 11 additions & 12 deletions front/src/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ const Main = () => {
const leftArrowRef = useRef<HTMLImageElement>(null);
const rightArrowRef = useRef<HTMLImageElement>(null);

const saveCookie = () => {
const cookieToken = import.meta.env.VITE_APP_COOKIE_TOKEN;
const cookieName = 'access_token';
const cookieValue = cookieToken;
const today = new Date();
const expire = new Date();
const secure = true;
expire.setDate(today.getDate() + 1);
document.cookie = `${cookieName}=${cookieValue}; expires=${expire.toUTCString()}; secure=${secure}; path=/`;
};
// const saveCookie = () => {
// const cookieToken = import.meta.env.VITE_APP_COOKIE_TOKEN;
// const cookieName = 'access_token';
// const cookieValue = cookieToken;
// const today = new Date();
// const expire = new Date();
// const secure = true;
// expire.setDate(today.getDate() + 1);
// document.cookie = `${cookieName}=${cookieValue}; expires=${expire.toUTCString()}; secure=${secure}; path=/`;
// };

useEffect(() => {
saveCookie();
//saveCookie();
axios
.get('/api/user', {
withCredentials: true // axios 쿠키 값 전달
Expand All @@ -77,7 +77,6 @@ const Main = () => {
const snowballData = res.data.main_snowball as SnowBallData;
setSnowBallData(snowballData);
setUserData(userData);
console.log('wow');
if (
userData.nickname === null ||
userData.snowball_count === 0 ||
Expand Down
8 changes: 7 additions & 1 deletion front/src/pages/Main/MainButtonBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ const screenTime = (
);
}
});
const a = document.getElementById('musicController');
if (a) {
a.style.display = 'none';
}

setTimeout(() => {
setScreen(true);
}, 1000);

setTimeout(() => {
setScreen(false);

if (a) {
a.style.display = 'block';
}
refs.forEach(ref => {
if (ref.current) {
ref.current.style.setProperty('animation', 'none');
Expand Down
18 changes: 3 additions & 15 deletions front/src/pages/Visit/SnowBallProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, createContext, useEffect } from 'react';
import React, { useState, createContext } from 'react';
import mockData from '@mock';

interface SnowBallContextType {
Expand Down Expand Up @@ -42,6 +42,8 @@ interface Message {
sender: string | undefined;
snowball_id: number;
user_id: number;
sentiment: string;
confidence: number;
}

const SnowBallContext = createContext<SnowBallContextType>({
Expand All @@ -58,20 +60,6 @@ const SnowBallProvider: React.FC<{ children: React.ReactNode }> = ({
mockData.snowball_data
);
const [userData, setUserData] = useState<UserData>(mockData.user_data);
// const { user } = useParams();
// useEffect(() => {
// axios(`/api/user/${user}`)
// .then(res => {
// setSnowBallData(res.data.main_snowball as SnowBallData);
// setUserData(res.data.user as UserData);
// })
// .catch(e => {
// //없는 유저 조회시 wrong page로 보내버리기
// console.error(e);
// navigate('*');
// });
// }, []);

return (
<SnowBallContext.Provider
value={{
Expand Down
63 changes: 0 additions & 63 deletions front/src/router/IsLogin.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion front/src/router/IsSnowballData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const IsSnowballData: React.FC<{ children: ReactNode }> = ({ children }) => {
}
})
.catch(err => {
console.log(err);
console.error(err);
navigate('*');
});
}
Expand Down
1 change: 0 additions & 1 deletion front/src/router/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as IsLogin } from './IsLogin';
export { default as IsSnowballData } from './IsSnowballData';

0 comments on commit 2b1d585

Please sign in to comment.