THREE.js学习笔记2——Transform objects

这一小节主要学的是关于物体的位置移动,旋转,缩放一系列操作。

Vector3

mesh网格物理模型中的position属性是继承于Vector3的。Vector3是一个类,用于定位空间中的东西。
Vector3有很多有用的方法。例如Vector3中介绍的

//打印模型到场景中心的距离
console.log(cube.position.length());

//打印两个不同Vector3之间的距离,这里是模型到相机的距离
//也可以是一个指定坐标的Vector3之间的距离
console.log(cube.position.distanceTo(camera.position));
console.log(cube.position.distanceTo(new THREE.Vector3(0, 1, 2)));

//使用normalize将模型的位置重置为1,无论之前位置在哪
cube.position.normalize();

//使用set更改模型位置
cube.position.set(0, 0, 0);

辅助观察坐标系

//AxesHelper:辅助观察的坐标系,参数为辅助线的长度,默认为1
//红轴为X轴,绿轴为Y轴,蓝轴为Z轴
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);

等比例缩放模型

//使用scale修改模型的大小(?),等比例缩放
cube.scale.x = 1.2;
cube.scale.y = 0.7;
cube.scale.z = 0.5;
cube.scale.set(1.2, 0.7, 0.5);

旋转模型

使用rotation旋转模型,rotation有xyz三个参数(properties),但是它不是Vector3,它是一个Euler

When we change the x,y,and z properties we can imagine putting a stick through your object's center in the axis's direction and then rotating that object on that stick.

当我们更改x、y和z属性时,我们可以想象将一根棍子沿着轴的方向穿过物体的中心,然后旋转那根棍子上的物体。

//值以弧度表示,转半圈大约是3.14159,可以使用Math.PI
cube.rotation.y = 3.14159;
cube.rotation.y = Math.PI;

//同时,为了确保旋转不出错,可以使用gimbal lock(万向节锁?),reorder,也就是调整了旋转的顺序
//下面这个是先x轴旋转,然后是y轴旋转
cube.rotation.x = 3.14159;
cube.rotation.y = Math.PI;
//如果这样写,那就是先旋转y轴,在旋转x轴
cube.rotation.reorder('YXZ')
cube.rotation.x = 3.14159;
cube.rotation.y = Math.PI;

Euler旋转是很容易理解的,但是由于不同的旋转顺序会得到不同的旋转结果,所以在THREE.js和3D软件中,都使用Quaternion来表示旋转,Quaternion是一种更加数学的表达方式。
在旋转物体(改变rotation)时,Quaternion就会更新。

还可以使用.lookAt方法实现旋转,这个方法会将物体的负z面面对你所填入的目标。

Object3D instances have a lookAt(...)method which rotates the object so that its -z faces the target you provided.The target must be a Vector3

//将摄像机朝向某个模型
camera.lookAt(0, 0, 0);
camera.lookAt(cube.position);

Group

为了方便操作,可以将很多个Object放入一个Group中,并对这个Group使用positionrotationscalelookAt这些操作

const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );

const cubeA = new THREE.Mesh( geometry, material );
cubeA.position.set( 100, 100, 0 );

const cubeB = new THREE.Mesh( geometry, material );
cubeB.position.set( -100, -100, 0 );

//create a group and add the two cubes
//These cubes can now be rotated / scaled etc as a group
const group = new THREE.Group();
group.add( cubeA );
group.add( cubeB );

//do something about group
group.scale.set(5, 0.5, 1);
group.rotation.y = Math.PI * 0.25;
group.rotation.x = Math.PI * 0.25;

scene.add( group );
posted @   xxxichenjq  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示