• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

dat.gui

23.09.20 12:51 작성 조회수 269

0

react에 three.js를 따라하고 있습니다. 그런데 다른건 다 잘 되는데 dat.GUI는 똑같이 따라 하니 안되네요 혹시 다른 방법을 더 추가 해야 할까여

import * as THREE from 'three';
import dat from 'dat.gui';

export default function example() {
	 const scene = new THREE.Scene();
	 const renderer = new THREE.WebGLRenderer({
		 antialias : true
	 });
   renderer.setSize(window.innerWidth, window.innerHeight);
   renderer.setPixelRatio(window.devicePixelRatio>=1 ? 2 : 1);
   document.body.innerHTML="";
   document.body.appendChild(renderer.domElement);

	// Camera
	const camera = new THREE.PerspectiveCamera(
		75,
		window.innerWidth / window.innerHeight,
		0.1,
		1000
	);
	camera.position.y = 1;
	camera.position.z = 5;
	scene.add(camera);

	// Light
	const ambientLight = new THREE.AmbientLight('white', 0.5);
	scene.add(ambientLight);
	
	const directionalLight = new THREE.DirectionalLight('white', 1);
	directionalLight.position.x = 1;
	directionalLight.position.z = 2;
	scene.add(directionalLight);

	// Mesh
	const geometry = new THREE.BoxGeometry(1, 1, 1);
	const material = new THREE.MeshStandardMaterial({
		color: 'seagreen'
	});
	const mesh = new THREE.Mesh(geometry, material);
	scene.add(mesh);

	// Dat GUI
	const gui = new dat.GUI({ name: "My GUI" });
	console.log(gui)
	gui.add(mesh.position, 'y', -5, 5, 0.01).name('큐브 Y');
	gui.add(camera.position, 'x', -10, 10, 0.01).name('카메라 X');
	camera.lookAt(mesh.position);

	// 그리기
	const clock = new THREE.Clock();
	function draw() {
		const time = clock.getElapsedTime();
		mesh.rotation.y = time;
		camera.lookAt(mesh.position);
		renderer.render(scene, camera);
		renderer.setAnimationLoop(draw);
	}

	function setSize() {
		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();
		renderer.setSize(window.innerWidth, window.innerHeight);
		renderer.render(scene, camera);
	}
	// 이벤트
	window.addEventListener('resize', setSize);
	draw();
}

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.09.22

안녕하세요, 인프런 AI 인턴이에요. dat.GUI 사용 시 문제가 발생하는 것 같네요. 첫 번째로 확인해야 할 점은 dat.GUI의 버전입니다. 현재 사용하고 계신 버전이 React와 호환되는지 확인해보세요. React와 호환되지 않는다면, React에서 다른 상태 관리 라이브러리를 사용해야 할 수도 있습니다.

두 번째로 확인해야 할 점은 import 구문입니다. dat.GUI를 import할 때, 다음과 같이 변경해 보세요.

import dat from 'dat.gui/build/dat.gui.module';

위와 같이 변경하고 다시 시도해보세요.

만약 이렇게 변경해도 문제가 해결되지 않는다면, 해당 문제에 대한 에러 메시지나 콘솔 로그를 공유해주시면 더 자세한 도움을 드릴 수 있을 것 같아요.