当前位置: 首页 > news >正文

Three.js

Three.js是一款基于原生WebGL封装的Web 3D库,向外提供了许多的接口。
它可以运用在在小游戏、产品展示、物联网、数字孪生、智慧城市园区、机械、建筑、全景看房、GIS等各个领域。
npm install three
https://threejs.org/docs/index.html#manual/en/introduction/Installation
创建第一个three.js 3D页面
在这里插入图片描述

<script setup lang="ts">
import * as THREE from 'three';
//创建场景
//场景能够让你在什么地方,摆放什么东西来交给three.js来渲染,这是你放置物体、灯光和摄像机的地方
const scene = new THREE.Scene();
//创建相机
const camera = new THREE.PerspectiveCamera();
camera.position.y = 2;
camera.position.z = 10;
//创建立方体 
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
//网格
const cube = new THREE.Mesh( geometry, material );
cube.position.set(0,3,0)
scene.add( cube );
//添加网格地面
const gridHelper = new THREE.GridHelper( 10, 10 );
scene.add( gridHelper );
//创建渲染器
const renderer = new THREE.WebGLRenderer();
//调整渲染器窗口大小
renderer.setSize( window.innerWidth, window.innerHeight );
// renderer.setAnimationLoop( animate );
//将渲染器添加到页面
document.body.appendChild( renderer.domElement );
//让立方体动起来
function animate() {requestAnimationFrame(animate)cube.rotation.x += 0.01;cube.rotation.y += 0.01;//进行渲染renderer.render( scene, camera );
}
animate()
</script>
<template>
</template>
<style scoped>
</style>

05 OrbitControls 轨道控制器
使得相机围绕目标进行轨道运动
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<script setup lang="ts">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
//创建场景
//场景能够让你在什么地方,摆放什么东西来交给three.js来渲染,这是你放置物体、灯光和摄像机的地方
const scene = new THREE.Scene();
//创建相机
const camera = new THREE.PerspectiveCamera();
camera.position.y = 2;
camera.position.z = 10;
//创建立方体 
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
//网格
const cube = new THREE.Mesh( geometry, material );
cube.position.set(0,3,0)
scene.add( cube );
//添加网格地面
const gridHelper = new THREE.GridHelper( 10, 10 );
scene.add( gridHelper );
//创建渲染器
const renderer = new THREE.WebGLRenderer();
//调整渲染器窗口大小
renderer.setSize( window.innerWidth, window.innerHeight );
// renderer.setAnimationLoop( animate );
//将渲染器添加到页面
document.body.appendChild( renderer.domElement );
//添加轨道控制器
const controls = new OrbitControls( camera, renderer.domElement );
//对轨道控制器改变时侯进行监听
controls.addEventListener('change',function(){console.log('触发')
})
//添加阻尼
controls.enableDamping =true
controls.dampingFactor =0.01
//自动旋转
controls.autoRotate =true
//改变速率
controls.autoRotateSpeed =0.5
//让立方体动起来
function animate() {requestAnimationFrame(animate)cube.rotation.x += 0.01;cube.rotation.y += 0.01;//轨道控制器更新controls.update();//进行渲染renderer.render( scene, camera );
}
animate()
</script><template></template><style scoped></style>

06AxesHelper
三维坐标轴
在这里插入图片描述

在这里插入图片描述

<script setup lang="ts">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
//创建场景
//场景能够让你在什么地方,摆放什么东西来交给three.js来渲染,这是你放置物体、灯光和摄像机的地方
const scene = new THREE.Scene();
//创建相机
const camera = new THREE.PerspectiveCamera();
camera.position.y = 2;
camera.position.z = 10;
//创建立方体 
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
//网格
const cube = new THREE.Mesh( geometry, material );
cube.position.set(0,3,0)
scene.add( cube );
//添加网格地面
const gridHelper = new THREE.GridHelper( 10, 10 );
scene.add( gridHelper );
//创建渲染器
const renderer = new THREE.WebGLRenderer();
//调整渲染器窗口大小
renderer.setSize( window.innerWidth, window.innerHeight );
// renderer.setAnimationLoop( animate );
//将渲染器添加到页面
document.body.appendChild( renderer.domElement );
//添加轨道控制器
const controls = new OrbitControls( camera, renderer.domElement );
//对轨道控制器改变时侯进行监听
controls.addEventListener('change',function(){console.log('触发')
})
//添加阻尼
controls.enableDamping =true
controls.dampingFactor =0.01
//自动旋转
controls.autoRotate =true
controls.autoRotateSpeed =0.5
//添加坐标轴
const axesHelper = new THREE.AxesHelper( 5 );
axesHelper.position.y= 3
scene.add( axesHelper );//让立方体动起来
function animate() {requestAnimationFrame(animate)// cube.rotation.x += 0.01;

http://www.mrgr.cn/news/29761.html

相关文章:

  • ChatGPT:强大的人工智能聊天机器人
  • 《深度学习》PyTorch框架 优化器、激活函数讲解
  • C/C++内存分布
  • [数据集][目标检测]智慧养殖场肉鸡目标检测数据集VOC+YOLO格式3548张1类别
  • 41. 如何在MyBatis-Plus中实现批量操作?批量插入和更新的最佳实践是什么?
  • 自监督的主要学习方法
  • C++list的使用:尾插、头插、insert、erase、reverse、sort等的介绍
  • Axure中后台管理信息系统通用原型方案
  • WSL中使用GPU加速AMBER MD--测试
  • Aegisub字幕自动化及函数篇(图文教程附有gif动图展示)(一)
  • 十八,Spring Boot 整合 MyBatis-Plus 的详细配置
  • 麒麟操作系统 xxl-job集群搭建
  • 一家CRO企业终止,业绩成长性遭质疑
  • 字幕编辑用什么软件好?盘点国内外7款视频加字幕软件,简单高效!
  • 传输层协议 —— UDP协议
  • C++: 高效使用智能指针的8个建议
  • vue3中如何拿到vue2中的this
  • 【电源专题】一张图理解电源的类型与转换关系
  • 数据库基础
  • 设计模式-依赖注入