Mesh 뿐만 아니라 Line, Points 등이 있음
Mesh: 삼각형 면으로 구성된 객체
Line: 선형 객체를 표현
Points: 점을 표현
3차원 객체는 3차원 공간상에 놓여지게 됨
scale값을 xyz축에 대해 모두 2로 지정하면 3D객체는 2배크기로 표시됨
예제: 태양 - 지구 - 달
- 태양은 자전
- 지구는 태양 중심으로 공전하며 자전
- 달은 지구 중심으로 공전
_setupModel() {
// Object3D 타입의 solarSystem 객체 생성
const solarSystem = new THREE.Object3D();
this._scene.add(solarSystem);
// 구 모양 지오메트리 생성
const radius = 1;
const widthSegments = 12;
const heightSegments = 12;
const sphereGeometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
// 태양 재질 생성
const sunMaterial = new THREE.MeshPhongMaterial({ emissive: 0xffff00, flatShading: true });
// sunMesh 객체 생성
const sunMesh = new THREE.Mesh(sphereGeometry, sunMaterial);
// xyz에 대해 3,3,3 설정 -> 원래 지오메트리 크기보다 3배의 크기로 표시
sunMesh.scale.set(3, 3, 3);
solarSystem.add(sunMesh);
// Object3D 타입의 earthOrbit 객체 생성
const earthOrbit = new THREE.Object3D();
// 생성한 earthOrbit 객체를 solarSystem의 자식으로 추가함
solarSystem.add(earthOrbit);
// 지구 재질 생성
const earthMaterial = new THREE.MeshPhongMaterial({ color: 0x2233ff, emissive: 0x112244, flatShading: true });
// earthMesh 생성
const earthMesh = new THREE.Mesh(sphereGeometry, earthMaterial);
// 태양에서 x축으로 거리 10만큼 떨어진 위치에 지구가 배치되도록 하기 위함
earthOrbit.position.x = 10;
// earthMesh 객체를 earthOrbit의 자식으로 추가
earthOrbit.add(earthMesh);
// Object3D 타입의 moonOrbit 객체 생성
const moonOrbit = new THREE.Object3D();
/*
moonorbit은 earthOrbit의 자식이므로, earthOrbit 기준으로 x축 거리 2만큼 떨어진 곳에 배치됨
태양 관점에서 보면 moonOrbit은 거리 12만큼 떨어진 곳에 배치됨
*/
moonOrbit.position.x = 2;
// 생성한 moonOrbit 객체를 earthOrbit의 자식으로 추가함
earthOrbit.add(moonOrbit);
// 달 재질 생성
const moonMaterial = new THREE.MeshPhongMaterial({ color: 0x888888, emissive: 0x222222, flatShading: true });
// moonMesh 생성
const moonMesh = new THREE.Mesh(sphereGeometry, moonMaterial);
// 원래 구 반지름 절반 크기로 달이 생성됨
moonMesh.scale.set(0.5, 0.5, 0.5);
// moonMesh를 moonOrbit의 자식으로 추가
moonOrbit.add(moonMesh);
// 객체를 다른 메서드에서 참조할수 있도록함
this._solarSystem = solarSystem;
this._earthOrbit = earthOrbit;
this._moonOrbit = moonOrbit;
}
camera.position.z=25;
- 현재 카메라가 태양 내부에 있기 때문에 카메라 포지션을 수정해주어야함
생성한 객체들을 회전시키기 위해(공전, 자전) update 함수 수정 필요
update(time) {
time *= 0.001;
// solarSystem은 y축에 대해 계속 회전
this._solarSystem.rotation.y = time / 2;
// 지구의 자전
this._earthOrbit.rotation.y = time * 2;
// 달의 자전
this._moonOrbit.rotation.y = time * 5;
}