[3JS] Transform Objects

Chenยท2024๋…„ 5์›” 30์ผ

Three.js

๋ชฉ๋ก ๋ณด๊ธฐ
3/4
post-thumbnail

4 Properties to transform obj.

  • position
  • scale
  • rotation
  • quaternion - ํšŒ์ „๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ (ํ›„์— ์ž์„ธํžˆ ๋‹ค๋ฃธ)

All classes that inherit from the Object3D like PerspectiveCamera or Mesh has thos props.


1์˜ ๋‹จ์œ„

  • ๋ณธ์ธ์ด ์„ค์ •ํ•˜๋ฉด ๋จ just stick to it

position

  • position ์†์„ฑ์€ Vector3์˜ ์ธ์Šคํ„ด์Šค

  • Any arbitrary ordered triplet of numbers.
  • position์€ x, y, z๋ผ๋Š” props์„ ๊ฐ€์ง
mesh.position.x = 0.7;
mesh.position.y = - 0.6;
mesh.position.z = 1;
// set์€ x,y,z ํ•œ ๋ฒˆ์— ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ, Vector3์ด๋ฏ€๋กœ ์ง€์ • ๊ฐ€๋Šฅ
mesh.position.set(0.7, -0.6, 1);

// nomarlize()๋Š” ๋ฐฉํ–ฅ์„ ์œ ์ง€ํ•˜๋ฉด์„œ length๋ฅผ 1๋กœ ์„ค์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

mesh.position.normalize();


// length()๋Š” (0, 0, 0)๋ถ€ํ„ฐ, ์ , ํ˜น์€ mesh๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ
console.log(mesh.position.length())

// mesh์™€ object์˜ ๊ฑฐ๋ฆฌ distanceTo()

console.log(mesh.position.distanceTo(camera.position))


Axes Helper

  • ์ถ•์„ ์‹œ๊ฐํ™”ํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ์†์„ฑ
  • param์œผ๋กœ ์ถ•์˜ ๊ธธ์ด ์„ค์ • ๊ฐ€๋Šฅ. default๋Š” 1
// Axes helper
const axesHelper = new THREE.AxesHelper();
scene.add(axesHelper);

์ด๋ฏธ์ง€์— z์ถ•์ด ๋ณด์ด์ง€ ์•Š๋Š” ์ด์œ ๋Š” ์นด๋ฉ”๋ผ ๊ธฐ์ค€ it's moving forward "straight"

camera.position.set(1, 1, 4); // ๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด...


Scale Objects

  • scale ๋˜ํ•œ Vector3์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ any arbitrary triplet of nums

  • ์Œ์ˆ˜๊ฐ’์„ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ, ์ถ•์ด logical direction์— ์œ„์น˜ํ•˜์ง€ ์•Š๊ฒŒ ๋˜๊ณ , ์ด๋กœ ์ธํ•ด ๋‚˜์ค‘์— ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๋Š” ํŽธ์ด ์ข‹์Œ


// Scale
mesh.scale.x = 2;
mesh.scale.y = 0.5;
mesh.scale.z = 0.5;
mesh.scale.set(2, 0.5, 0.5)


Rotate Objects

rotate๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Œ

  • rotation
  • quaternion

Rotation

  • rotation ์—ญ์‹œ x, y, z ์†์„ฑ์„ ๊ฐ–๊ณ  ์žˆ์ง€๋งŒ, ์ด๋“ค์€ Vector3๊ฐ€ ์•„๋‹ˆ๋ผ, Euler์ด๋‹ค.

๋‹จ์œ„๋Š” ๋ผ๋””์•ˆ์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ Math.PI๋ฅผ ์ด์šฉ

// Rotation
mesh.rotation.x = Math.PI * 0.25;
mesh.rotation.y = Math.PI * 0.25;

Axes order

์ฃผ์˜ : x์ถ•์„ ํšŒ์ „ํ•˜๋ฉด ๋‹ค๋ฅธ ์ถ•์˜ ๋ฐฉํ–ฅ๋„ ๋™์‹œ์— ๋ณ€ํ•จ

์ถ•์€ ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ๋ฐ–์— ์—†์–ด์„œgimbal lock ๋ฐœ์ƒ ๊ฐ€๋Šฅ (์ถ•์ด ๋‹ค๋ฅธ ์ถ•์— ์˜ํ–ฅ ๋ฐ›์•„์„œ)

์ด๋Ÿด ๋•Œ๋Š” ์ถ• ์ ์šฉ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค
์ฃผ์˜ : reorder ๋จผ์ €

// Rotation
mesh.rotation.reorder('YXZ')
mesh.rotation.x = Math.PI * 0.25;
mesh.rotation.y = Math.PI * 0.25;


Quaternion

Euler is easy to understand but this axid order can be problematic. This is why most engine sand 3D S/W use Quaternion

also expressess a rotation, but in more ์ˆ˜ํ•™์  ๋ฐฉ๋ฒ•

์„œ๋กœ ์—…๋ฐ์ดํŠธ
Quaternion updates -> rotation
rotation updates -> Quaternion


LookAt

lookAt(...) method rotates the obj. so that its -z faces the target you provided. target must be a Vector3

์ด ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์นด๋ฉ”๋ผ๊ฐ€ ์–ด๋–ค obj๋ฅผ ๋ฐ”๋ผ๋ณด๋„๋ก ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
ex. ๊ฒŒ์ž„์—์„œ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์–ด๋–ค ๋ฌผ์ฒด ๋ฐ”๋ผ๋ณด๋Š” ์‹œ์ 

์ธ์ž๋กœ Vector3๋ฅผ ๋ฐ›๋Š”๋‹ค

// ์กฐ๊ธˆ ๋ฌด์‹ํ•œ ๋ฐฉ์‹ 
camera.lookAt(new THREE.Vector3(3, 0, 0));

// posiotion๋„ vector3์ด๋ฏ€๋กœ
camera.lookAt(mesh.position);


Scene Graph

you can put objs inside groups and use position, rotation(or quaternion), and scale on those groups. To do this, use the Group class

Group์œผ๋กœ ๋ฌถ๋Š” ์Šต๊ด€ ๋“ค์ด๊ธฐ!

const group = new THREE.Group();
scene.add(group);

const cube1 = new THREE.Mesh(
    new THREE.BoxGeometry(1, 1, 1),
    new THREE.MeshBasicMaterial({ color: 0xff0000 }),
);
group.add(cube1)

const cube2 = new THREE.Mesh(
    new THREE.BoxGeometry(1, 1, 1),
    new THREE.MeshBasicMaterial({ color: 0x00ff00 }),
);
cube2.position.x = -2;
group.add(cube2)

const cube3 = new THREE.Mesh(
    new THREE.BoxGeometry(1, 1, 1),
    new THREE.MeshBasicMaterial({ color: 0x0000ff }),
);
cube3.position.x = 2;
group.add(cube3)

group.position.y = 1;

group.position.y = 1;
group.scale.y = 2;

group.position.y = 1;
group.scale.y = 2;
group.rotation.y = 1;


ํ€ด์ฆˆ ์ธ์ฆ


์ฐธ๊ณ :
9rganizedChaos๋‹˜ ๋ฒจ๋กœ๊ทธ

profile
ํ˜„์‹ค์ ์ธ ๋ชฝ์ƒ๊ฐ€

0๊ฐœ์˜ ๋Œ“๊ธ€