欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Three.js使用THREE.ExtrudeGeometry拉伸SVG为三维图形(代码实例)

程序员文章站 2022-06-22 23:19:08
简介 svg是可缩放矢量图(scalable vector graphics)。它基于xml的标准,用来在网页上创建二维矢量图。该标准是一个所有现代都支持的开房标准。我们可以使用...

简介

svg是可缩放矢量图(scalable vector graphics)。它基于xml的标准,用来在网页上创建二维矢量图。该标准是一个所有现代都支持的开房标准。我们可以使用three.js的three.extrudegeometry来将svg格式的图形转换成三维图形。

实现方式

首先,将库文件引入到页面当中

<script src="https://johnson2heng.github.io/three.js-demo/lib/js/libs/d3-threed.js"></script>

然后,从svg元素中获取到svg字符串,通过库里面transformsvgpathexposed函数,将字符串转换成three.extrudegeometry所需要的three.shape对象。 最后,通过转换出来的three.shape对象创建模型即可。

案例代码

我们将下面的一个svg图形转换成three.js格式的三维图形

Three.js使用THREE.ExtrudeGeometry拉伸SVG为三维图形(代码实例)

转换为

Three.js使用THREE.ExtrudeGeometry拉伸SVG为三维图形(代码实例)

以下是全部代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">
<p id="batman" style="display:none;">
    <svg version="1.0" xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://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>
</p>
</body>
<script src="https://johnson2heng.github.io/three.js-demo/lib/three.js"></script>
<script src="https://johnson2heng.github.io/three.js-demo/lib/js/libs/d3-threed.js"></script>
<script src="https://johnson2heng.github.io/three.js-demo/lib/js/controls/orbitcontrols.js"></script>
<script src="https://johnson2heng.github.io/three.js-demo/lib/js/libs/stats.min.js"></script>
<script src="https://johnson2heng.github.io/three.js-demo/lib/js/libs/dat.gui.min.js"></script>
<script>
    var renderer;
    function initrender() {
        renderer = new three.webglrenderer({antialias:true});
        //renderer.setclearcolor(new three.color(0xeeeeee, 1.0)); //设置背景颜色
        renderer.setsize(window.innerwidth, window.innerheight);
        document.body.appendchild(renderer.domelement);
    }

    var camera;
    function initcamera() {
        camera = new three.perspectivecamera(45, window.innerwidth/window.innerheight, 1, 10000);
        camera.position.set(0, 0, 1500);
    }

    var scene;
    function initscene() {
        scene = new three.scene();
    }

    var light;
    function initlight() {
        scene.add(new three.ambientlight(0x404040));

        light = new three.directionallight(0xffffff);
        light.position.set(1,1,1);
        scene.add(light);
    }

    function initmodel() {
        var shape = new three.shapegeometry(drawshape());
        var material = new three.meshphongmaterial({color:0xff00ff});
        material.side = three.doubleside;//设置成两面都可见
        var mesh = new three.mesh(shape,material);
        scene.add(mesh);
        /*此方法是创建两种纹理的方法
        * var shape = new three.shapegeometry(drawshape());
        var mesh = createmesh(shape);
        scene.add(mesh);
        * */
    }

    //生成2d图形
    function drawshape() {


        var svgstring = document.queryselector("#batman-path").getattribute("d");

        var shape = transformsvgpathexposed(svgstring);

        // 返回shape
        return shape;
    }

    //生成模型
    function createmesh(geom) {

        //设置当前的模型矩阵沿xy轴偏移,让图片处于显示中心
        geom.applymatrix(new three.matrix4().maketranslation(-450, -300, 0));

        // 创建法向量纹理
        var meshmaterial = new three.meshnormalmaterial({
            flatshading: three.flatshading,
            transparent: true,
            opacity: 0.7
        });

        //  创建一个线框纹理
        var wireframemat = new three.meshbasicmaterial();
        wireframemat.wireframe = true;

        // 创建模型
        var mesh = three.sceneutils.createmultimaterialobject(geom, [meshmaterial,wireframemat]);

        //由于图形时反的,让图形翻个个
        mesh.rotation.z = math.pi;

        return mesh;
    }

    //初始化性能插件
    var stats;
    function initstats() {
        stats = new stats();
        document.body.appendchild(stats.dom);
    }

    //用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放
    var controls;
    function initcontrols() {

        controls = new three.orbitcontrols( camera, renderer.domelement );

        // 如果使用animate方法时,将此函数删除
        //controls.addeventlistener( 'change', render );
        // 使动画循环使用时阻尼或自转 意思是否有惯性
        controls.enabledamping = true;
        //动态阻尼系数 就是鼠标拖拽旋转灵敏度
        //controls.dampingfactor = 0.25;
        //是否可以缩放
        controls.enablezoom = true;
        //是否自动旋转
        controls.autorotate = false;
        //设置相机距离原点的最远距离
        controls.mindistance  = 20;
        //设置相机距离原点的最远距离
        controls.maxdistance  = 10000;
        //是否开启右键拖拽
        controls.enablepan = true;
    }

    //生成gui设置配置项
    var gui,shape;
    function initgui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            amount:2,
            bevelthickness:2,
            bevelsize:0.5,
            bevelenabled:true,
            bevelsegments:3,
            curvesegments:12,
            steps:1,
            asgeom:function () {
                // 删除旧的模型
                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();
    }

    function render() {
        renderer.render( scene, camera );
    }

    //窗口变动触发的函数
    function onwindowresize() {
        camera.aspect = window.innerwidth / window.innerheight;
        camera.updateprojectionmatrix();
        render();
        renderer.setsize( window.innerwidth, window.innerheight );

    }

    function animate() {
        //更新控制器
        controls.update();
        render();

        //更新性能插件
        stats.update();
        requestanimationframe(animate);
    }

    function draw() {
        initrender();
        initscene();
        initcamera();
        initlight();
        //initmodel();
        initcontrols();
        initstats();
        initgui();

        animate();
        window.onresize = onwindowresize;
    }
</script>
</html>