当前位置:网站首页>First experience of three.js: simulating the growth of a small branch
First experience of three.js: simulating the growth of a small branch
2022-07-27 11:16:00 【TaitChan】
Code effect :
Complete code :
<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() {
// camera
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() {
// Rendering : Anti aliasing
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
// camera
this.setCamera()
// scene
this.setScene()
// guide
this.setHelper()
// Rendering
this.setRender()
},
run() {
// Geometry
// const geometry = new Three.BoxGeometry(0.2, 0.2, 0.2)
const geometry = new THREE.CylinderGeometry(0, 0.1, 0.2)
// Mesh normal vector material rgb
const material = new THREE.MeshNormalMaterial()
// Grid objects
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)// Set the 3D coordinates of the geometric center of the mesh model
mesh.rotateZ(Math.PI / 4)// Around the x Shaft rotation π/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: Super Mini edition , It can't be as like as two peas. , It's irrelevant , It is far from the desired effect hahahaaa…
There is only one video reference , I don't know what to write and whether I have my own modeling , How strange .
Pre reference :
Vue The page introduce ThreeJS
ThreeJS: Model growth
Next, you may want to refer to the following , Save a file
Use Three.js Draw a three-dimensional tree model
utilize JavaScript stay canvas Draw a tree in
Three.js Simulation animation of random growth of branches
边栏推荐
- Instructions for mock platform
- JVM judges that the object is dead, and practices verify GC recycling
- Digital triangle model acwing 275. pass note
- Introduction to software vulnerability analysis (I)
- 背包模型 AcWing 423. 采药
- Analysis of C language pointer function and function pointer
- Knapsack model acwing 423. Picking herbs
- 如何创建一个带诊断工具的.NET镜像
- Ten year structure five year life-07 young and vigorous transformation
- 求组合数 AcWing 887. 求组合数 III
猜你喜欢

涌现与形态的局部差异和整体差异

The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence

Internal and external troubles of digital collection NFT "boring ape" bayc

15 design movie rental system

区间问题 AcWing 906. 区间分组

图片中非0值的数量对分类的影响

Digital triangle model acwing 275. pass note

properties文件

ACM warm-up Exercise 2 in 2022 summer vacation (summary)

Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
随机推荐
发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Today's code farmer girl learned notes about event operation and ref attribute, and did the practice of form two-way binding
Ansible
How to assemble a registry
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do
Longest ascending subsequence model acwing 1012. Sister Cities
图片中非0值的数量对分类的影响
高斯消元 AcWing 883. 高斯消元解线性方程组
最长上升子序列模型 AcWing 1017. 怪盗基德的滑翔翼
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
349两个数组的交集和01两数之和
洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
ethereum rpc
The difference of iteration number and information entropy
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
最长上升子序列模型 AcWing 1014. 登山
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
How to build a real-time development platform to deeply release the value of enterprise real-time data?
8 find subsequences with a maximum length of K