three.js坐标轴辅助器AxesHelper

一、效果图

在这里插入图片描述

二、添加坐标轴辅助器

使用three.js,通过以下代码可以添加坐标轴辅助器:

// 创建坐标轴辅助器
var axesHelper = new THREE.AxesHelper( 5 );
// 添加到场景中
scene.add( axesHelper );

在这里,我们创建了一个坐标轴辅助器并将其添加到场景中。THREE.AxesHelper()函数使用一个数值参数,该数字指定辅助器的大小。在这个例子中,我们设置大小为5。

坐标轴辅助器可协助您在three.js场景中定位和定向对象,使调试和所有其他工作更加轻松。

三、完整代码

// 导入three
import * as THREE from 'three'

// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

// 创建场景
const scene = new THREE.Scene()

// 创建相机
// 参数:视野角度,长宽比,近平面,远平面
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)

// 设置相机位置
// 参数:x轴,y轴,z轴
camera.position.set(0, 0, 5)

// 将相机添加到场景中
scene.add(camera)

// 创建几何体
const geometry = new THREE.BoxGeometry(1, 1, 1)
// 创建材质
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })
// 根据几何体和材质创建物体
const cube = new THREE.Mesh(geometry, material)
// 将物体添加到场景中
scene.add(cube)

// 创建渲染器
const renderer = new THREE.WebGLRenderer()
// 设置渲染器大小
renderer.setSize(window.innerWidth, window.innerHeight)
console.log(renderer)
// 将渲染器添加到页面中
document.body.appendChild(renderer.domElement)

// 创建轨道控制器
// 参数:相机,渲染器dom元素
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true; // 开启阻尼效果
controls.rotateSpeed = 0.5; // 旋转速度
controls.target.set(0, 0, 0); // 查看物体时的中心点

// 添加坐标轴辅助器
// 参数:坐标轴长度
const axesHelper = new THREE.AxesHelper(5)
scene.add(axesHelper)

// 使用渲染器,通过相机渲染场景
function animate() {
    requestAnimationFrame( animate ); //帧动画
    controls.update() //更新控制器
    cube.rotation.x += 0.01; //旋转物体
    cube.rotation.y += 0.01; 
    renderer.render( scene, camera ); //渲染
}
animate();


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值