当前位置:网站首页>three.js初体验:模拟一个小树杈的生长
three.js初体验:模拟一个小树杈的生长
2022-07-17 00:05:00 【TaitChan】
代码效果:
完整代码:
<template>
<div>
<button @click="run" class="btn">run</button>
<div class="container" ref="container" />
</div>
</template>
<script>
import * as THREE from 'three'
export default {
name: 'ThreeTest',
data() {
return {
container: null,
camera: null,
scene: null,
renderer: null,
mesh: null,
timer: null
}
},
mounted() {
this.init()
// this.run()
},
methods: {
setScene() {
this.scene = new THREE.Scene()
},
setCamera() {
// 照相机
this.camera = new THREE.PerspectiveCamera(70, this.container.clientWidth / this.container.clientHeight, 0.01, 10)
this.camera.position.z = 2
this.camera.position.y = 1
// this.camera.lookAt(new Three.Vector3(0, -0.01, 0.1))
},
setHelper() {
this.scene.add(new THREE.AxesHelper(30))
const gridHelper = new THREE.GridHelper(100, 30, 0x2C2C2C, 0x888888)
gridHelper.position.y = -1
this.scene.add(gridHelper)
},
setRender() {
// 渲染:反锯齿
this.renderer = new THREE.WebGLRenderer({
antialias: true })
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight)
this.container.appendChild(this.renderer.domElement)
},
init() {
this.container = this.$refs.container
// 照相机
this.setCamera()
// 场景
this.setScene()
// 辅助线
this.setHelper()
// 渲染
this.setRender()
},
run() {
// 几何体
// const geometry = new Three.BoxGeometry(0.2, 0.2, 0.2)
const geometry = new THREE.CylinderGeometry(0, 0.1, 0.2)
// 网格法向量材质 rgb
const material = new THREE.MeshNormalMaterial()
// 网格对象
this.mesh = new THREE.Mesh(geometry, material)
this.scene.add(this.mesh)
this.timer = setInterval(() => {
if (this.mesh.scale.y < 3) {
this.mesh.scale.y += 0.1
this.mesh.position.y += 0.01
} else {
this.stop()
this.run2()
}
this.renderer.render(this.scene, this.camera)
}, 30)
},
run2() {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(0, 0.1 * 0.7, 0.2), new THREE.MeshNormalMaterial())
mesh.position.set(-0.05, 0.05, 0)// 设置网格模型几何中心三维坐标
mesh.rotateZ(Math.PI / 4)// 绕x轴旋转π/4
this.scene.add(mesh)
this.timer = setInterval(() => {
if (mesh.scale.y < 2) {
mesh.scale.y += 0.1
mesh.position.x -= 0.01
mesh.position.y += 0.01
} else {
this.stop()
this.run3()
}
this.renderer.render(this.scene, this.camera)
}, 30)
},
run3() {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(0, 0.1 * 0.5, 0.2), new THREE.MeshNormalMaterial())
mesh.position.set(0.05, 0.2, 0)
mesh.rotateZ(-Math.PI / 4)
this.scene.add(mesh)
this.timer = setInterval(() => {
if (mesh.scale.y < 1.5) {
mesh.scale.y += 0.1
mesh.position.x += 0.01
mesh.position.y += 0.01
} else {
this.stop()
}
this.renderer.render(this.scene, this.camera)
}, 30)
},
stop() {
clearInterval(this.timer)
this.timer = null
}
// animate() {
// requestAnimationFrame(this.animate)
// this.mesh.rotation.x += 0.01
// this.mesh.rotation.y += 0.02
// this.renderer.render(this.scene, this.camera)
// }
}
}
</script>
<style scoped>
.container {
height: 80vh;
}
.btn{
position: fixed;
top: 50vh;
left: 50vw;
}
</style>
ps:超级Mini版,不能说一模一样,只能说毫不相干,跟想实现的效果差了十万八千里hahahaaa…
仅有一个视频参考,不知道拿什么写的有没有自己建模,真好奇啊。
前置参考:
Vue页面引入ThreeJS
ThreeJS:模型生长
接下去做的话可能要参考下面这些,存个档
使用 Three.js 绘制三维树模型
利用JavaScript在canvas中画一棵树
Three.js 树枝随机生长模拟动画
边栏推荐
- MoveIt2——5.场景规划
- Single page application spa and multi page application MPa
- Day16-排序,限流
- Cve-2022-34265 Django extract & TRUNC SQL injection vulnerability recurrence
- promise
- Common mode attack and shared prime of RSA
- From catf1ag two-hour AK match pwn/attack killing, summarize the common command guide of emergency response
- [elementui El date picker date selector, the end time must not be earlier than the start time, and only the date of the specified number of days from the start time can be selected]
- 智能指针(shared_ptr、unique_ptr、weak_ptr)
- Day16 sorting, current limiting
猜你喜欢

深度之眼三——(7,8)】数学:矩阵对角化及二次型2.3

Win10 vscode 代码格式化设置与远程断点调试

Cve-2022-34265 Django extract & TRUNC SQL injection vulnerability recurrence

JS get the suffix format of a file name

uni-app微信小程序——商城(6)——我的主页

Oracle automatic storage management 18C step-by-step installation -2

jsx 编译

uni-app微信小程序——商城(3)——商城主页

Single page application spa and multi page application MPa

1.互联网基础
随机推荐
Deployment of cobalstrike (with resources)
Common mode attack and shared prime of RSA
Oracle database architecture
Day12-关联序列化处理
Maker-鸿蒙应用开发培训笔记02
深度之眼三——(4,5)】数学:矩阵特征值与特征向量2
数组操作——判断、去重、合并、展开
Collection and summary of penetration test information
(10)文件包含
Summary of XML external entity injection (xxE target recurrence)
判断两个时间戳是否是同一天
JS get the suffix format of a file name
学习STM32F103时涉及的C语言知识汇总(仅链接)
MoveIt2——4.机器人模型和机器人状态
P6-day01-general attempt (P4 review)
es6 map提取数组对象
js替换字符串某个字符,js修改字符串中指定字符
鼠标右键菜单添加快速打开选项遇见的错误:
Moveit2——1.开始
C Programming Language(2nd Edition)--读书笔记--1.5