Skip to content

Commit

Permalink
feat: 타이어 패턴에 따른 three.js 랜더링 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 committed Mar 3, 2024
1 parent f09cbd0 commit 7cf0908
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/pages/production/content/preview/index.style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import styled from 'styled-components';
import defaultImage from '@assets/image/default-image.png';

export const Wrapper = styled.div`
width: 300px;
height: 300px;
background-image: url(${defaultImage});
background-size: cover;
`;
58 changes: 57 additions & 1 deletion src/pages/production/content/preview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
import { useEffect, useRef } from 'react';
import { Wrapper } from './index.style';
import * as THREE from 'three';
import liv from '@assets/pattern/liv.png';
import lug from '@assets/pattern/lug.png';
import livlug from '@assets/pattern/livlug.png';
import block from '@assets/pattern/block.png';
import v from '@assets/pattern/v.png';
import bedeching from '@assets/pattern/bedeching.png';
import { selectedPatternState } from '@context/pattern';
import { useRecoilState } from 'recoil';

function Preview() {
return <Wrapper className="preview" />;
const mountRef = useRef<HTMLDivElement>(null);
const image = [liv, lug, livlug, block, v, bedeching];
const [selectedPattern] = useRecoilState(selectedPatternState);

useEffect(() => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor(0xffffff);
renderer.setSize(480, 300);

if (mountRef.current) {
mountRef.current.appendChild(renderer.domElement);
}

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load(image[selectedPattern]);
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.center.set(0.5, 0.5);
texture.rotation = Math.PI / 2;
texture.offset.set(0, 0);
texture.repeat.set(2, 8);

const tireGeometry = new THREE.TorusGeometry(6, 2, 16, 150);
const tireMaterial = new THREE.MeshBasicMaterial({ map: texture });
const tire = new THREE.Mesh(tireGeometry, tireMaterial);
tire.rotation.y = Math.PI / 2;
scene.add(tire);

camera.position.z = 15;

const animate = function () {
requestAnimationFrame(animate);
tire.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();

// 컴포넌트 언마운트 시 클린업 로직
return () => {
if (mountRef.current) {
mountRef.current.removeChild(renderer.domElement);
}
};
}, [selectedPattern]);

return <Wrapper ref={mountRef} className="preview" />;
}

export default Preview;

0 comments on commit 7cf0908

Please sign in to comment.