👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 1.1 ☘️拉伸几何体THREE.TubeGeometry管道概念
- 二、🍀生成拉伸几何体THREE.TubeGeometry管道
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中实现生成拉伸几何体THREE.TubeGeometry管道,亲测可用。希望能帮助到您。一起学习,加油!加油!
1.1 ☘️拉伸几何体THREE.TubeGeometry管道概念
THREE.TubeGeometry 沿着一条三维的样条曲线拉伸出一根管。
创建方法:
构造函数 new THREE.TubeGeometry(new THREE.CatmullRomCurve3(points), segments, radius, radiusSegments, closed)
参数说明
path:该属性用一个 THREE.SplineCurve3 对象来指定管道应当遵循的路径。
segments:该属性指定构建这个管所用的分段数。默认值为 64.路径越长,指定的分段数应该越多。
radius:该属性指定管的半径。默认值为 1.
radiusSegments:该属性指定管道圆周的分段数。默认值为 8,分段数越多,管道看上去越圆。
closed:如果该属性设置为 true,管道的头和尾会连起来,默认值为 false。
二、🍀生成拉伸几何体THREE.TubeGeometry管道
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set
- 4、初始化THREE.AmbientLight环境光源,scene场景加入环境光源,初始化THREE.DirectionalLight平行光源,设置平行光源位置,scene添加平行光源。
- 5、加载几何模型:生成管道路径THREE.CatmullRomCurve3,并设置segments、radius、radiusSegments、closed参数生成THREE.TubeGeometry管道,创建THREE.MeshBasicMaterial基本材质,生成mesh物体,scene场景加入mesh。
- 6、加入controls、gui控制,加入stats监控器,监控帧数信息。
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>learn24(拉伸几何体THREE.TubeGeometry管道)</title>
<script src="lib/threejs/127/three.js-master/build/three.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/utils/SceneUtils.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/geometries/ConvexGeometry.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/math/ConvexHull.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/controls/OrbitControls.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/libs/stats.min.js"></script>
<script src="lib/threejs/127/three.js-master/examples/js/libs/dat.gui.min.js"></script>
</head>
<style>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%;
display: block;
}
</style>
<body onload="draw()">
</body>
<script>
var renderer
var initRender = () => {
renderer = new THREE.WebGLRenderer({antialias: true})
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
}
var camera
var initCamera = () => {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000)
camera.position.set(0, 0, 100)
}
var scene
var initScene = () => {
scene = new THREE.Scene()
}
var light
var initLight = () => {
scene.add(new THREE.AmbientLight(0x404040))
light = new THREE.DirectionalLight(0xffffff)
light.position.set(1, 1, 1)
scene.add(light)
}
// var initModel = () => {
// var shape = new THREE.ShapeGeometry()
// }
var stats
var initStats = () => {
stats = new Stats()
document.body.appendChild(stats.dom)
}
var controls
var initControls = () => {
controls = new THREE.OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
}
var gui, spGroup, tubeMesh
var initGui = () => {
gui = {
numberOfPoints: 5,
segments: 64,
radius: 1,
radiusSegments: 8,
closed: false,
points: [],
newPoints: function () {
//生成一些随机点放置到数组当中
var points = []
for (var i = 0; i < gui.numberOfPoints; i++) {
var randomX = -20 + Math.round(Math.random() * 50)
var randomY = -15 + Math.round(Math.random() * 40)
var randomZ = -20 + Math.round(Math.random() * 40)
points.push(new THREE.Vector3(randomX, randomY, randomZ))
}
gui.points = points
gui.redraw()
},
redraw: function () {
//清楚掉场景中原来的模型对象
scene.remove(spGroup)
scene.remove(tubeMesh)
//重新绘制模型
generatePoints(gui.points, gui.segments, gui.radius, gui.radiusSegments, gui.closed)
}
}
var datGui = new dat.GUI()
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
datGui.add(gui, 'newPoints')
datGui.add(gui, 'numberOfPoints', 2, 15).step(1).onChange(gui.newPoints)
datGui.add(gui, 'segments', 0, 200).step(1).onChange(gui.redraw)
datGui.add(gui, 'radius', 0, 10).onChange(gui.redraw)
datGui.add(gui, 'radiusSegments', 0, 100).step(1).onChange(gui.redraw)
datGui.add(gui, 'closed').onChange(gui.redraw)
gui.newPoints()
}
var generatePoints = (points, segments, radius, radiusSegments, closed) => {
spGroup = new THREE.Object3D()
var material = new THREE.MeshBasicMaterial({color: 0xff0000, transparent: false})
points.forEach(point => {
var spGeom = new THREE.SphereGeometry(0.2)
var spMesh = new THREE.Mesh(spGeom, material)
spMesh.position.copy(point)
spGroup.add(spMesh)
})
scene.add(spGroup)
var tubeGeometry = new THREE.TubeGeometry(new THREE.CatmullRomCurve3(points), segments, radius, radiusSegments, closed)
tubeMesh = createMesh(tubeGeometry)
scene.add(tubeMesh)
}
var createMesh = (geom) => {
var meshMaterial = new THREE.MeshBasicMaterial({color: 0x00ff00, transparent: true, opacity: 0.3})
var wireFrameMat = new THREE.MeshBasicMaterial()
wireFrameMat.wireframe = true
var mesh = THREE.SceneUtils.createMultiMaterialObject(geom, [meshMaterial, wireFrameMat])
return mesh
}
var render = () => {
renderer.render(scene, camera)
}
var onWindowResize = () => {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
render()
renderer.setSize(window.innerWidth, window.innerHeight)
}
var animate = () => {
render()
stats.update()
controls.update()
requestAnimationFrame(animate)
}
var draw = () => {
initRender()
initScene()
initCamera()
initLight()
initControls()
initStats()
initGui()
animate()
window.onresize = onWindowResize
}
</script>
</html>
效果如下: