인프런 커뮤니티 질문&답변
box3.position.x 위치
작성
·
6
·
수정됨
0
box3 position이 box2가 아니라 box1 기준 x축만큼 이동이 됩니다 ㅜㅜ
뭐가 씌인건지 코드와 강의를 몇 번을 확인해 봐도 이유를 모르겠는데 알려주시면 감사드려요 ....
(강의 기준 box3.position.x = 0.5 로 하면 box3가 box1과 겹쳐져 잘 안보여서 일단 1로 해뒀습니다)
import * as THREE from "three";
import dat from "dat.gui";
// ----- 주제: 그룹 만들기(Scene Graph)
export default function example() {
// Renderer
const canvas = document.querySelector("#three-canvas");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio > 1 ? 2 : 1);
// Scene
const scene = new THREE.Scene();
// Camera
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.y = 1.5;
camera.position.z = 4;
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: "hotpink",
});
const group1 = new THREE.Group();
const box1 = new THREE.Mesh(geometry, material);
const group2 = new THREE.Group();
const box2 = box1.clone(); // box1 메쉬 복제
box2.scale.set(0.3, 0.3, 0.3);
box2.position.x = 2;
// const group3 = new THREE.Object3D();
const group3 = new THREE.Group();
const box3 = box2.clone();
box3.scale.set(0.15, 0.15, 0.15);
box3.position.x = 1;
group3.add(box3);
group2.add(box2, group3);
group1.add(box1, group2);
scene.add(group1);
// AxesHelper
// const axesHelper = new THREE.AxesHelper(3);
// scene.add(axesHelper);
// Dat GUI
const gui = new dat.GUI();
gui.add(camera.position, "x", -5, 5, 0.1).name("카메라 X");
gui.add(camera.position, "y", -5, 5, 0.1).name("카메라 Y");
gui.add(camera.position, "z", 2, 10, 0.1).name("카메라 Z");
// 그리기
const clock = new THREE.Clock();
function draw() {
const delta = clock.getDelta();
// group1.rotation.y += delta;
// group2.rotation.y += delta;
// group3.rotation.y += delta;
renderer.render(scene, camera);
window.requestAnimationFrame(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 인턴입니다.
현재 box3의 위치가 box1을 기준으로 이동하고 있다고 문의주셨는데요. Three.js에서 그룹(group)을 활용하는 경우, 해당 메쉬의 좌표는 자신이 속한 그룹의 좌표를 기준으로 이동하게 됩니다. 따라서 box3의 위치가 이상하게 보이는 것처럼 느껴지시는 것 같습니다.
코드에서 box2와 box3을 그룹화하여 group3에 추가하고, group3을 group2에 추가하면서 좌표 위치가 중첩된 구조로 변경됩니다. 이 경우, box3.position.x = 1;은 box3이 group3 좌표계에서 x축으로 1만큼 이동한다는 것을 의미합니다.
따라서, box3의 의도된 위치를 확인하고 싶으시다면 아래처럼 box3의 위치 값을 콘솔로 찍어 보면서 이동된 좌표값을 확인해보시면 도움이 될 것입니다.
console.log('box3 Position:', box3.position);
console.log('group3 Position:', group3.position);
console.log('group2 Position:', group2.position);
이렇게 하면 중첩된 그룹 구조에서도 box3이 정확히 어떤 위치로 이동하고 있는지 확인할 수 있습니다. 좀 더 세밀한 컨트롤을 원하시면, 그룹 간의 상대적 위치를 조정하면 됩니다.
질문과 관련된 내용은 다음 URL에서 확인 가능합니다:
- “Three.js 그룹과 위치 설정”: 그룹과 위치 설정에 대한 답변을 포함하고 있어 도움이 될 수 있습니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.





