- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
三、四元数(Quaternions)
四元数是一种高效的旋转表示方法,避免了万向锁问题。Three.js 的 Quaternion
类提供了简单易用的接口来管理旋转。
使用四元数进行旋转
const quaternion = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI / 4);
object.quaternion.copy(quaternion);
const targetQuaternion = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI / 2);
quaternion.slerp(targetQuaternion, 0.5);
object.quaternion.copy(quaternion);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
四、欧拉角(Euler Angles)
欧拉角提供了一种直观的方式来指定旋转顺序,尽管它容易导致万向锁的问题。Three.js 的 Euler
类允许你定义绕 X、Y 和 Z 轴的旋转角度。
使用欧拉角进行旋转
const euler = new THREE.Euler(Math.PI / 4, 0, 0, 'XYZ');
object.rotation.setFromEuler(euler);
euler.x += Math.PI / 8;
object.rotation.setFromEuler(euler);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
五、颜色(Colors)
颜色不仅是视觉效果的重要组成部分,而且在材质和光照中也起着关键作用。Three.js 的 Color
类简化了颜色的创建和操作。
创建和操作颜色
const color = new THREE.Color(0xff0000);
color.setRGB(0, 1, 0);
color.setHSL(0.5, 1, 0.5);
material.color.copy(color);
console.log(`颜色的十六进制表示: ${color.getHexString()}`);
material.opacity = 0.5;
material.transparent = true;
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
六、几何体生成器(Geometry Generators)
Three.js 提供了一系列内置的几何体生成器,如 BoxGeometry
, SphereGeometry
等,用于快速创建常见形状。此外,还可以通过自定义顶点和面来构建复杂的几何体。
创建几何体
const geometry = new THREE.BoxGeometry(1, 1, 1);
const sphereGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const customGeometry = new THREE.Geometry();
customGeometry.vertices.push(
new THREE.Vector3(-1, -1, 0),
new THREE.Vector3(1, -1, 0),
new THREE.Vector3(0, 1, 0)
);
customGeometry.faces.push(new THREE.Face3(0, 1, 2));
const mesh = new THREE.Mesh(customGeometry, material);
scene.add(mesh);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
七、随机数生成(Random Number Generation)
虽然 Three.js 没有直接提供随机数生成函数,但可以结合 JavaScript 的 Math.random()
来实现这一功能,这对于创建程序化内容非常有用。
生成随机数
function getRandomFloat(min, max) {
return (max - min) * Math.random() + min;
}
for (let i = 0; i < 100; i++) {
const sphere = new THREE.Mesh(
new THREE.SphereGeometry(0.1, 16, 16),
new THREE.MeshBasicMaterial({ color: 0xffffff * Math.random() })
);
sphere.position.set(
getRandomFloat(-5, 5),
getRandomFloat(-5, 5),
getRandomFloat(-5, 5)
);
scene.add(sphere);
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
八、时间和动画(Time and Animation)
时间管理对于创建流畅的动画至关重要。Three.js 提供了 Clock
类来跟踪时间,并结合 requestAnimationFrame
实现平滑的动画更新。
管理时间和动画
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
object.rotation.x += delta * 0.1;
renderer.render(scene, camera);
}
animate();
import { Tween } from '@tweenjs/tween.js';
new Tween(object.position)
.to({ x: 5 }, 1000)
.start();
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
九、光线追踪与碰撞检测(Ray Tracing and Collision Detection)
光线追踪用于模拟光的行为,而碰撞检测则用于确定物体是否相交。Three.js 提供了 Raycaster
类来实现这两种功能。
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
window.addEventListener('click', (event) => {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0) {
console.log('点击的对象:', intersects[0].object);
}
});
function checkCollisions(objectA, objectB) {
const distance = objectA.position.distanceTo(objectB.position);
return distance < (objectA.geometry.boundingSphere.radius + objectB.geometry.boundingSphere.radius);
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
十、曲线与路径(Curves and Paths)
曲线和路径用于定义物体的运动轨迹或其他复杂形状。Three.js 提供了多种曲线类型,如 LineCurve3
, CubicBezierCurve3
和 SplineCurve
。
创建和使用曲线
const curve = new THREE.CubicBezierCurve3(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(-0.5, 1, 0),
new THREE.Vector3(0.5, 1, 0),
new THREE.Vector3(1, 0, 0)
);
const point = curve.getPoint(0.5);
const path = new THREE.Path(curve.getPoints(50));
const geometry = path.createPointsGeometry();
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
const line = new THREE.Line(geometry, material);
scene.add(line);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">![](https://img1.iyenn.com/thumb01/c4a7c45da7i3a7f8/65811691243225911151.jpeg)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
十一、数学常量与实用函数(Math Constants and Utility Functions)
Three.js 提供了一些常用的数学常量和实用函数,如 MathUtils
,方便进行各种数学运算。
使用数学常量和实用函数
const pi = Math.PI;
const radians = THREE.MathUtils.degToRad(90);
const degrees = THREE.MathUtils.radToDeg(radians);
const distance = THREE.MathUtils.euclideanModulo(pointA.x - pointB.x, 10);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
结语
Three.js 的数学工具不仅限于上述几种方式,还包括更多高级特性,如光线追踪、碰撞检测等。掌握这些数学工具,可以帮助你在创建3D内容时提供更加精确和灵活的操作。无论你是希望构建一个教育性的演示文稿,还是开发一款复杂的游戏,Three.js 的数学能力都能为你提供强有力的支持。
data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/chaosweet/article/details/145032304","extend1":"pc","ab":"new"}">>
评论记录:
回复评论: