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

threejs简单创建一个几何体(一)

1.下包引入

//下包
npm install three
yarn add three//引入
import * as THREE from 'three'

在这里插入图片描述

2.创建场景,摄像机

	  // 1.创建场景const scene = new THREE.Scene()// 2.创建摄像机//第一个参数是视角,一般在60-90之间,第二个参数是场景的尺寸,一般取显示器的宽高,第三个参数是开始位置,第四个参数是结束位置const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)// 设置摄像机z轴的空间的位置camera.position.z = 3

3.创建渲染器

 	  // 3.创建渲染器const renderer = new THREE.WebGLRenderer()// 设置渲染器场景的大小renderer.setSize(window.innerWidth, window.innerHeight)// 把渲染器添加到页面中去document.body.appendChild(renderer.domElement)

4.创建几何模型,网格对象

这样创建的几何模型是纯色的方块,效果如上图

	  // 4.创建几何模型const box = new THREE.BoxGeometry(1, 1, 1)// 设置几何体的材质(纯色)const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })// 创建网格对象const cube = new THREE.Mesh(box, material)// 将网格对象添加到场景中去scene.add(cube)

添加贴图(皮肤)

import logo from '@/assets/dz.png'// 4.创建几何模型const box = new THREE.BoxGeometry(1, 1, 1)// 设置几何体皮肤(贴图)const texture = new THREE.TextureLoader().load(logo)const material = new THREE.MeshBasicMaterial({ map: texture })// 创建网格对象const cube = new THREE.Mesh(box, material)// 将网格对象添加到场景中去scene.add(cube)

在这里插入图片描述

5.场景渲染

这一步将前面的设置全部加入到画布中

	  // 5.场景渲染function animate () {requestAnimationFrame(animate)// 给网格对象添加动画cube.rotation.x += 0.01cube.rotation.y += 0.01renderer.render(scene, camera)}animate()

6.响应式布局

拖动网页,刷新后画布尺寸发生变化,再次拖动窗口,会出现空白
在这里插入图片描述

 window.addEventListener('resize', () => {// 初始化相机camera.aspect = window.innerWidth / window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth, window.innerHeight)})

7.完整代码

<template><div class="container"></div>
</template>
<script>
import * as THREE from 'three'
import logo from '@/assets/dz.png'
export default {data () {return {}},mounted () {this.init()},methods: {init () {// 1.创建场景const scene = new THREE.Scene()// 2.创建摄像机const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)// 设置摄像机z轴的空间的位置camera.position.z = 4// 3.创建渲染器const renderer = new THREE.WebGLRenderer()// 设置渲染器场景的大小renderer.setSize(window.innerWidth, window.innerHeight)// 把渲染器添加到页面中去document.querySelector('.body').appendChild(renderer.domElement)// document.body.appendChild(renderer.domElement)// 4.创建几何模型const box = new THREE.BoxGeometry(1, 1, 1)// 设置几何体皮肤(贴图)const texture = new THREE.TextureLoader().load(logo)const material = new THREE.MeshBasicMaterial({ map: texture })// 设置几何体的材质(纯色)// const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 })// 创建网格对象const cube = new THREE.Mesh(box, material)// 将网格对象添加到场景中去scene.add(cube)// 5.场景渲染function animate () {requestAnimationFrame(animate)// 给网格对象添加动画cube.rotation.x += 0.01cube.rotation.y += 0.01renderer.render(scene, camera)}animate()// 响应式布局window.addEventListener('resize', () => {// 初始化相机camera.aspect = window.innerWidth / window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth, window.innerHeight)})}}
}
</script>
<style lang='scss'  scoped></style>
http://www.lryc.cn/news/317708.html

相关文章:

  • msfconsole数据库连接不了的问题【已解决】
  • 7. Linux进程环境
  • [linux] 静态图和动态图
  • 1.Spring核心功能梳理
  • 活动预告:如何培养高质量应用型医学人才?
  • 蓝桥杯算法错题记录-基础篇
  • Java知识点之单例模式
  • Flutter第三弹:常用的Widget
  • Dynamic Wallpaper v17.4 mac版 动态视频壁纸 兼容 M1/M2
  • Windows / Mac应用程序在Linux系统中的兼容性问题 解决方案
  • Net Core 使用Mongodb操作文件(上传,下载)
  • 适用于系统版本:CentOS 6/7/8的基线安全检测脚本
  • Seata源码流程图
  • 英飞凌电源管理PMIC的安全应用
  • 快速在Linux系统安装MySQL
  • 数据库相关理论知识(有目录便于直接锁定相关知识点+期末复习)
  • NCC环境配置
  • 用python实现Dubins曲线生成
  • 智能技术上的“是”并不代表具体领域的“应该”
  • 永热爱 敢向前 | Paraverse平行云的2023 年终总结
  • c/c++的内存分配,详细说一下栈、堆和静态存储区
  • 每日构造题训练——C. Divan and bitwise operations
  • 【C++练级之路】【Lv.13】多态(你真的了解虚函数和虚函数表吗?)
  • 如何在Windows系统安装Node.js环境并制作html页面发布公网远程访问?
  • C#,数值计算,希尔伯特矩阵(Hilbert Matrix)的算法与源代码
  • 【C++教程从0到1入门编程】第八篇:STL中string类的模拟实现
  • 学生时期学习资源同步-1 第一学期结业考试题6
  • 迁移学习怎么用
  • 医疗手持智能终端读取条码二维码的难点有哪些?
  • Python小设计