three-物体运动

<script setup>
import { ref, onMounted } from 'vue'
import * as THREE from 'three';
// 导入轨道控制器
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
onMounted(() => {
  // AxesHelper:辅助观察的坐标系
  show()
})
const show = () => {
  // 1. 创建场景
const scene = new THREE.Scene()
// 2. 创建相机
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight,
  1,
  1000
)

camera.position.z = 100

// 5. 创建立方体(几何+材质)
const cubeGeometry = new THREE.BoxGeometry(10, 10, 10)
const cubeMaterial = new THREE.MeshNormalMaterial()
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
// 添加到场景
scene.add(cube)

// 6. 显示坐标轴(x轴: 红色; y轴: 绿色; z轴: 蓝色 rgb)
// x轴水平方向(右正); y轴垂直方向(上正); z轴垂直xy平面即屏幕(外正)
const axesHelper = new THREE.AxesHelper(50)
scene.add(axesHelper)

// 3. 创建渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)

// 7. 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement)

// 4. 动态渲染
function animation() {
  // cube.position.x = 20; 
  // cube.position.y = 20; 
  // cube.position.z = 20; 
  cube.position.x += 0.1;// 让立方体向右移动0.01个单位  
  if (cube.position.x > 30) { // 当位置大于5时  
    cube.position.x = 0; // 从0开始  
  }  
  controls.update()
  renderer.render(scene, camera)

  requestAnimationFrame(animation)
}
animation()
}
</script>

<template></template>

<style scoped>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值