👨⚕️ 主页: gis分享者
👨⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨⚕️ 收录于专栏:threejs gis工程师
文章目录
- 一、🍀前言
- 1.1 ☘️THREE.ExtrudeGeometry拉伸几何体概念
- 二、🍀利用THREE.ExtrudeGeometry拉伸几何体实现svg的拉伸
- 1. ☘️实现思路
- 2. ☘️代码样例
一、🍀前言
本文详细介绍如何基于threejs在三维场景中利用THREE.ExtrudeGeometry拉伸几何体实现svg的拉伸,亲测可用。希望能帮助到您。一起学习,加油!加油!
1.1 ☘️THREE.ExtrudeGeometry拉伸几何体概念
THREE.ExtrudeGeometry 是 Three.js 中用于创建挤出几何体的一个类。挤出几何体是从一个二维路径(轮廓)通过沿着某个方向(通常是 Z 轴)“挤出”一定的距离来生成三维几何体。这种方法非常适合创建各种三维模型,如字母、符号、标志等。
创建方法:
构造函数 new THREE.ExtrudeGeometry(shape, extrudeSettings) 接受两个参数来定义挤出几何体的形状和挤出设置。
参数说明
shape:一个 THREE.Shape 对象,定义了要挤出的二维轮廓。
extrudeSettings:一个包含挤出参数的对象,可选参数包括:
amount:挤出的距离,默认为 100。
bevelEnabled:是否启用倒角,默认为 true。
bevelThickness:倒角厚度,默认为 10。
bevelSize:倒角大小,默认为 8。
bevelSegments:倒角分割数量,默认为 1。
steps:挤出路径分割的数量,默认为 1。
curveSegments:曲线分割数量,默认为 12。
uvGenerator:UV 坐标生成器,默认为 THREE.ExtrudeGeometryUVGenerator。
二、🍀利用THREE.ExtrudeGeometry拉伸几何体实现svg的拉伸
1. ☘️实现思路
- 1、初始化renderer渲染器
- 2、初始化Scene三维场景
- 3、初始化camera相机,定义相机位置 camera.position.set
- 4、初始化THREE.AmbientLight环境光源,scene场景加入环境光源,初始化THREE.DirectionalLight平行光源,设置平行光源位置,scene添加平行光源。
- 5、加载几何模型:根据svg数据生成THREE.ShapeGeometry几何体,使用THREE.MeshPhongMaterial反射材质,生成mesh物体,scene场景加入mesh。
- 6、加入controls、gui控制,gui控制中加入asGeom函数用于生成基于svg数据的THREE.ExtrudeGeometry拉伸几何体,加入stats监控器,监控帧数信息。
2. ☘️代码样例
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>learn26(svg拉高)</title><script src="lib/threejs/127/three.js-master/build/three.js"></script><script src="https://johnson2heng.github.io/three.js-demo/lib/js/libs/d3-threeD.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/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>html, body {margin: 0;height: 100%;}canvas {display: block;}
</style>
<body onload="draw()">
<div id="batman" style="display:none;"><!--<svg version="1.0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1152px" height="1152px" xml:space="preserve"><g><path id="batman-path" style="fill:rgb(0,0,0);" d="M 261.135 114.535 C 254.906 116.662 247.491 118.825 244.659 119.344 C 229.433 122.131 177.907 142.565 151.973 156.101 C 111.417 177.269 78.9808 203.399 49.2992 238.815 C 41.0479 248.66 26.5057 277.248 21.0148 294.418 C 14.873 313.624 15.3588 357.341 21.9304 376.806 C 29.244 398.469 39.6107 416.935 52.0865 430.524 C 58.2431 437.23 63.3085 443.321 63.3431 444.06 C 63.4748 446.883 102.278 479.707 120.51 492.418 C 131.003 499.734 148.168 509.93 158.654 515.075 C 169.139 520.22 179.431 525.34 181.524 526.454 C 187.725 529.754 187.304 527.547 179.472 515.713 C 164.806 493.553 158.448 464.659 164.322 446.861 C 169.457 431.303 192.013 421.501 214.324 425.132 C 234.042 428.341 252.142 439.186 270.958 459.064 C 286.677 475.67 292.133 482.967 295.31 491.634 C 297.466 497.514 298.948 495.91 304.862 481.293 C 313.673 459.519 329.808 445.735 346.35 445.851 C 367.654 446 399.679 478.239 412.801 512.745 C 414.093 516.144 416.593 522.632 418.355 527.163 C 420.118 531.695 423.604 542.319 426.103 550.773 C 430.848 566.832 432.355 566.851 434.872 550.88 C 436.395 541.215 451.403 502.522 455.655 497.298 C 457.038 495.599 460.63 489.896 463.636 484.625 C 471.696 470.498 492.318 452.688 505.387 448.568 C 514.602 445.663 517.533 445.549 525.51 447.782 C 539.676 451.749 553.43 467.773 560.706 488.788 L 563.242 496.114 L 567.096 490.012 C 577.709 473.208 593.665 453.899 602.47 447.206 C 607.884 443.09 613.378 438.825 614.679 437.729 C 615.98 436.632 622.927 433.259 630.118 430.233 C 655.159 419.693 681.195 423.407 693.273 439.241 C 697.957 445.382 698.932 448.971 699.538 462.294 C 700.174 476.284 699.51 479.864 693.686 493.854 C 690.073 502.533 684.912 512.883 682.217 516.854 C 679.523 520.825 678.172 524.074 679.215 524.074 C 681.932 524.074 718.787 504.481 732.525 495.734 C 760.018 478.228 788.909 452.599 803.9 432.418 C 807.266 427.886 810.569 423.715 811.239 423.149 C 814.498 420.395 828.253 393.099 833.17 379.627 C 838.223 365.782 838.713 361.822 838.741 334.582 C 838.776 300.425 836.431 291.124 820.154 260.873 C 810.649 243.207 807.498 239.005 788.417 218.543 C 751.511 178.968 688.147 142.549 621.582 122.654 C 581.7 110.734 580.388 110.465 580.388 114.195 C 580.388 115.328 581.302 116.255 582.418 116.255 C 584.279 116.255 587.705 122.106 603.399 152.085 C 613.977 172.29 618.077 189.427 618.264 214.21 C 618.42 234.928 617.88 238.368 612.285 252.269 C 604.327 272.04 590.066 286.889 572.829 293.352 C 558.526 298.714 549.193 297.86 535.704 289.955 C 526.777 284.723 512.304 267.644 509.816 259.404 C 509.132 257.138 507.129 251.358 505.366 246.558 C 503.602 241.759 501.646 231.564 501.018 223.902 C 500.39 216.24 498.491 198.402 496.797 184.261 C 495.104 170.121 493.307 152.047 492.803 144.097 C 492.299 136.147 491.292 125.625 490.565 120.715 L 489.242 111.787 L 483.323 118.267 C 480.067 121.832 477.404 125.618 477.404 126.681 C 477.404 127.744 476.603 128.613 475.624 128.613 C 474.645 128.613 471.275 132.321 468.135 136.852 L 462.426 145.091 L 431.038 145.091 L 399.65 145.091 L 386.811 128.494 C 379.749 119.365 373.509 112.36 372.943 112.926 C 372.377 113.491 371.57 118.875 371.15 124.888 C 370.73 130.902 368.94 147.744 367.172 162.315 C 365.405 176.887 363.523 195.424 362.99 203.509 C 360.283 244.622 352.784 266.044 335.323 282.544 C 326.456 290.923 312.488 297.497 303.508 297.518 C 294.864 297.539 278.732 290.063 269.473 281.748 C 246.952 261.521 238.846 229.614 245.481 187.314 C 247.894 171.928 266.562 131.612 275.927 121.56 C 277.987 119.348 279.673 116.786 279.673 115.867 C 279.673 114.947 279.905 113.593 280.188 112.856 C 281.28 110.017 271.977 110.837 261.136 114.536 L 261.135 114.535 "/></g></svg>--><svg t="1626938111428" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3505" width="22" height="22"><path id="batman-path" d="M959.4 234.6v80.6c0 39.3-32 71.2-71.2 71.2-12.4 0-24-3.2-34.2-8.8l-2 461.7c0 53.4-43.6 97-97.2 97H634.6c-18.3 0-35.6-7.1-48.6-20.1-9.3-9.3-15.6-20.8-18.4-33.3-12.5-2.8-24-9.1-33.3-18.4-13-13-20.1-30.2-20.1-48.6 0-18.3 7.2-35 19-47.4-12.4-4.3-24.8-9.2-37.1-14.6v107.7c0 41.2-33.5 74.7-74.7 74.7-33.2 0-61.5-21.9-71.1-51.9-11.2-3.6-21.6-9.9-30-18.3-14.1-14.1-21.9-32.9-21.9-52.8V600.7c-28.4-37.9-47.4-77.6-55.3-115.6-37.9-25.5-61.6-68.1-63.3-113.8-0.1-0.1-0.1-0.2-0.1-0.2-5 0.8-10.2 1.2-15.5 1.2-54.6 0-99.1-44.4-99.1-99.1 0-54.6 44.5-99.1 99.1-99.1 2.5 0 4.9 0.1 7.3 0.3 32.1-52.8 89.1-85.1 151.6-85.1s119.4 32.3 151.6 85.1c2.4-0.2 4.8-0.3 7.4-0.3 54.6 0 99.1 44.5 99.1 99.1 0 15.5-3.6 30.5-10.4 44.1 29 10.8 57.5 24.7 85.1 41.2 19.6 11.8 38.3 24.6 55.8 38.5V234.6c0-68.3 55.6-123.9 123.9-123.9 68.3 0 123.8 55.6 123.8 123.9z" fill="#663333" p-id="3506"></path><path d="M922.4 234.6v80.6c0 18.8-15.3 34.2-34.2 34.2s-34.1-15.3-34.1-34.2v-80.6c0-10.2-8.3-18.5-18.5-18.5s-18.5 8.3-18.5 18.5v279.9c-17.3-29.3-40.3-58.1-68.3-85.2V234.6c0-47.9 39-86.8 86.8-86.8s86.8 39 86.8 86.8z" fill="#CA5F38" p-id="3507"></path><path d="M816.9 630.4l-2 208.7c0 33.1-27 60.1-60.1 60.1H634.6c-8.5 0-16.4-3.3-22.4-9.3-6-5.9-9.2-13.9-9.2-22.3v-1c0.5-17.1 14.4-30.6 31.6-30.6h97.8c10.2 0 18.5-8.3 18.5-18.5s-8.3-18.5-18.5-18.5c-49 0-88.9-39.9-88.9-88.9 0-10.2-8.3-18.5-18.5-18.5s-18.5 8.3-18.5 18.5c0 13 2 25.4 5.6 37.2h-18.2c-32.4-7-65.4-18.5-97.8-34v-73c0-10.2-8.3-18.5-18.5-18.5s-18.5 8.3-18.5 18.5v221.4c0 20.7-16.9 37.6-37.6 37.6-20.7 0-37.6-16.9-37.6-37.6V606.1c0-10.2-8.3-18.5-18.5-18.5-8.9 0-16.2 6.2-18.1 14.5-5.5-6.2-10.7-12.5-15.6-18.9-20-25.8-35-52.4-44.4-78.3 11.7 3 23.7 4.6 35.9 4.6 77.3 0 140.6-61.6 143.3-138.2 0.1-0.1 0.1-0.1 0.1-0.2 5 0.8 10.2 1.2 15.5 1.2 23.9 0 46.9-8.7 64.8-24.2 30.6 10.6 60.8 24.7 90 42.2 29.6 17.7 56.7 38.1 80.7 60.5 60.3 56.4 97 123 99.3 179.6z" fill="#CA5F38" p-id="3508"></path><path d="M746.5 567.2c4.8 9 1.4 20.3-7.6 25.1-2.8 1.5-5.8 2.2-8.7 2.2-6.6 0-13-3.6-16.4-9.8-12.7-23.7-32-48-55.7-70.3-19.5-18.3-41.8-34.9-66.1-49.5-8.8-5.3-11.6-16.6-6.4-25.4 5.3-8.8 16.6-11.6 25.4-6.4 26.5 15.9 50.9 34.2 72.3 54.2 27.2 25.4 48.4 52.3 63.2 79.9z" fill="#663333" p-id="3509"></path><path d="M643.2 798.9h-8.7c-30 0-55.4 18.9-64.8 45.7-3.4-1.5-6.6-3.7-9.3-6.4-6-6-9.2-13.9-9.2-22.4 0-17.4 14.2-31.6 31.6-31.6h7.2c14.8 3.1 45.1 6.7 45.6 6.7h0.3c2.3 2.7 4.8 5.4 7.3 8zM544 273.1c0 15.3-5.6 30-15.8 41.4-10.7 11.9-25.6 19.2-41.4 20.4 2.5-5.9 4.7-12 6.5-18.1 4.8-16.2 7.2-33.1 7.2-50.1 0-18.6-3-37.1-8.7-54.8 29.6 4.7 52.2 30.4 52.2 61.2z" fill="#CA5F38" p-id="3510"></path><path d="M463.5 266.7c0 13.5-1.9 26.8-5.7 39.6-5 17.1-13.3 33-24.5 47.1-2.6 3.3-4 7.3-4 11.5 0 0.6 0 1.1 0.1 1.6-0.3 58.5-47.9 105.9-106.4 105.9-19 0-37.6-5.1-53.9-14.6-32.2-19-52.3-53.9-52.5-91.3 0-0.5 0.1-1 0.1-1.5 0-4.2-1.4-8.2-4-11.5-3.3-4.2-6.4-8.6-9.2-13.2-13.7-22.1-20.9-47.5-20.9-73.6 0-10.5 1.1-20.7 3.4-30.7 33.7 39.8 83.6 63.5 137.1 63.5s103.3-23.7 137-63.5c2.2 9.9 3.4 20.2 3.4 30.7z" fill="#F9DCB2" p-id="3511"></path><path d="M393.7 313.2c8.7 0 15.7 7 15.7 15.8 0 8.7-7 15.7-15.7 15.7s-15.8-7-15.8-15.7c0.1-8.8 7.1-15.8 15.8-15.8z" fill="#663333" p-id="3512"></path><path d="M323 126.2c50.1 0 95.7 26.3 121 69.1-26 41.6-71.5 67.1-120.9 67.1-49.5 0-95-25.5-121-67 25.2-42.9 70.8-69.2 120.9-69.2z" fill="#CA5F38" p-id="3513"></path><path d="M361.3 386.1c6.5 6.5 6.5 17.1 0 23.6l-5.8 5.8c-3.3 3.3-7.5 4.9-11.8 4.9-4.3 0-8.5-1.6-11.8-4.9-6.5-6.5-6.5-17.1 0-23.6l5.8-5.8c6.5-6.5 17.1-6.5 23.6 0z" fill="#663333" p-id="3514"></path><path d="M346.7 654.9V840l-0.3-0.3c-7.1-7.1-11-16.5-11-26.6V643.8c3.7 3.7 7.5 7.5 11.3 11.1z" fill="#CA5F38" p-id="3515"></path><path d="M314.2 391.9c6.5 6.5 6.5 17.1 0 23.6-3.2 3.3-7.5 4.9-11.8 4.9s-8.5-1.6-11.8-4.9l-5.8-5.8c-6.5-6.5-6.5-17.1 0-23.6s17.1-6.5 23.6 0l5.8 5.8zM252.4 313.2c8.7 0 15.7 7 15.7 15.8 0 8.7-7 15.7-15.7 15.7s-15.8-7-15.8-15.7c0-8.8 7.1-15.8 15.8-15.8z" fill="#663333" p-id="3516"></path><path d="M145.5 266.7c0 23.6 4.7 46.8 13.7 68.3-31.9-2.5-57.2-29.3-57.2-61.8 0-30.8 22.6-56.5 52.1-61.2-5.6 17.5-8.6 36.1-8.6 54.7z" fill="#CA5F38" p-id="3517"></path></svg>
</div>
</body>
<script>var renderervar initRender = () => {renderer = new THREE.WebGLRenderer({antialias: true})renderer.setSize(window.innerWidth, window.innerHeight)document.body.appendChild(renderer.domElement)}var scenevar initScene = () => {scene = new THREE.Scene()}var cameravar initCamera = () => {camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000)camera.position.set(0, 0, 1500)}var lightvar initLight = () => {scene.add(new THREE.AmbientLight(0x404040))light = new THREE.DirectionalLight(0xffffff)light.position.set(1, 1, 1)scene.add(light)}var drawShape = () => {var svgString = document.querySelector("#batman-path").getAttribute('d')var shape = transformSVGPathExposed(svgString)return shape}var createMesh = (geom) => {// 设置当前的模型矩阵沿xy轴偏移,让图片处于显示中心// geom.applyMatrix(new THREE.Matrix4().makeTranslation(-450, -300, 0))// 更新当前几何体的相关盒子模型数据如果设置居中会居中方法内部调用。// geom.computeBoundingBox()//让图形居中显示geom.center()var meshMaterial = new THREE.MeshNormalMaterial({flatShading: THREE.FlatShading,transparent: true,opacity: 0.7})var wireFrameMatl = new THREE.MeshBasicMaterial()wireFrameMatl.wireframe = truevar mesh = THREE.SceneUtils.createMultiMaterialObject(geom, [meshMaterial, wireFrameMatl])mesh.rotation.z = Math.PIreturn mesh}var initModel = () => {var helper = new THREE.AxisHelper(20)scene.add(helper)var shape = new THREE.ShapeGeometry(drawShape())var material = new THREE.MeshPhongMaterial({color:0xff00ff})material.side = THREE.DoubleSidevar mesh = new THREE.Mesh(shape, material)scene.add(mesh)}var statsvar initStats = () => {stats = new Stats()document.body.appendChild(stats.dom)}var controlsvar initControls = () => {controls = new THREE.OrbitControls(camera, renderer.domElement)controls.enableDamping = true}var gui,shapefunction initGui() {//声明一个保存需求修改的相关数据的对象gui = {amount:2,bevelThickness:2,bevelSize:0.5,bevelEnabled:true,bevelSegments:3,curveSegments:12,steps:1,asGeom:function () {var helper = new THREE.AxisHelper(2000)scene.add(helper)// 删除旧的模型scene.remove(shape)// 创建一个新的var options = {amount: gui.amount,bevelThickness: gui.bevelThickness,bevelSize: gui.bevelSize,bevelSegments: gui.bevelSegments,bevelEnabled: gui.bevelEnabled,curveSegments: gui.curveSegments,steps: gui.steps}shape = createMesh(new THREE.ExtrudeGeometry(drawShape(), options))// 将模型添加到场景当中scene.add(shape)}};var datGui = new dat.GUI()//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)datGui.add(gui, 'amount', 0, 200).onChange(gui.asGeom)datGui.add(gui, 'bevelThickness', 0, 10).onChange(gui.asGeom)datGui.add(gui, 'bevelSize', 0, 10).onChange(gui.asGeom)datGui.add(gui, 'bevelSegments', 0, 30).step(1).onChange(gui.asGeom)datGui.add(gui, 'bevelEnabled').onChange(gui.asGeom)datGui.add(gui, 'curveSegments', 1, 30).step(1).onChange(gui.asGeom)datGui.add(gui, 'steps', 1, 5).step(1).onChange(gui.asGeom)//调用生成一次图形gui.asGeom()}var render = () => {renderer.render(scene, camera)}var onWindowResize = () => {camera.aspect = window.innerWidth / window.innerHeightcamera.updateProjectionMatrix()render()renderer.setSize(window.innerWidth, window.innerHeight)}var animate = () => {render()stats.update()controls.update()requestAnimationFrame(animate)}var draw = () => {initRender()initScene()initCamera()initLight()// initModel()initStats()initControls()initGui()animate()window.onresize = onWindowResize}
</script>
</html>
效果如下: