使用vue学习three.js之高级几何体-SVG拉伸几何体

1.SVG拉伸体使用步骤

通过SVG拉伸创建几何体实际上使用的还是ExtrudeGeometry几何体。只不创建SVG拉伸体使用的底图是一个SVG图形,而普通的拉伸体的底图是一个二维图形;接下来介绍一下创建SVG拉伸几何体的步骤

1.1svg的初始化

在示例中我们的svg是在vue文件中的html部分中使用标签初始化的,具体如下:

<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>
</div>

1.2svg内容获取与转换成Shape类型

通过操作DOM元素获取的标签内容,继而获取到svg的内容,比较棘手一点的是如何把获取到的svg内容转换为Shape,此示例中,为了在vue中正常使用d3-threeD.js库,把此文件稍作调整,并放在示例文件相同目录下,在工作文件引入,然后调用transformSVGPathExposed方法转换svg,具体过程如下:

  1. 调整d3-threeD.js文件。在该文件的开头引入three,结尾输出transformSVGPathExposed方法
import * as THREE from 'three'
...
...
export {
    
    
  transformSVGPathExposed
}

也可以直接在这里下载d3-threeD.js

  1. 引入transformSVGPathExposed方法
import {
    
     transformSVGPathExposed } from './d3-threeD.js'
  1. 获取svg并转换
// 获取svg内容 转换为THREE.Shape对象
svgChangeToShape() {
    
       
  const svgString = document.querySelector('#batman-path').getAttribute('d')
  const shape = transformSVGPathExposed(svgString)
  return shape
}

1.3创建几何体,网格对象,添加到场景

// 拉伸几何体配置项
const options = {
    
    
  depth: this.properties.depth.value,
  bevelThickness: this.properties.bevelThickness.value,
  bevelSize: this.properties.bevelSize.value,
  bevelSegments: this.properties.bevelSegments.value,
  bevelEnabled: this.properties.bevelEnabled,
  curveSegments: this.properties.curveSegments.value,
  steps: this.properties.steps.value
}

// 创建拉伸几何体
const geom = new THREE.ExtrudeGeometry(this.svgChangeToShape(), options)

geom.applyMatrix4(new THREE.Matrix4().makeTranslation(-370, -74, 0))

// 创建材质
const meshMaterial = new THREE.MeshPhongMaterial({
    
    
  color: 0x003333
})
// 创建网格对象
this.mesh = new THREE.Mesh(geom, meshMaterial)
this.mesh.scale.x = 0.05
this.mesh.scale.y = 0.05

this.mesh.rotation.z = Math.PI
this.mesh.rotation.x = -1.1
// 网格对象添加到场景中
this.scene.add(this.mesh)

2.demo说明

在这里插入图片描述

如上图,该示例支持以下功能

  1. 通过depth属性调整SVG拉伸几何体拉伸的高度
  2. 通过bevelThickness属性指定SVG拉伸几何体斜角的厚度
  3. 通过bevelSize属性指定SVG拉伸几何体斜角的高度
  4. 通过bevelSegments属性指定SVG拉伸几何体斜角的分段数
  5. 通过bevelEnabled属性设置SVG拉伸几何体是否开启斜角
  6. 通过curveSegments属性指定SVG拉伸几何体拉伸曲线的分段数
  7. 通过steps属性指定SVG拉伸几何体的分段数

3.demo代码

<template>
  <div>
    <div id="container"></div>
    <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>
    </div>
    <div class="controls-box">
      <section>
        <el-row>
          <div v-for="(item,key) in properties" :key="key">
            <div v-if="item&&item.name!=undefined">
              <el-col :span="8">
                <span class="vertice-span">{
    
    {
    
    item.name}}</span>
              </el-col>
              <el-col :span="13">
                <el-slider v-model="item.value" :min="item.min" :max="item.max" :step="item.step" :format-tooltip="formatTooltip" @change="redraw"></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{
    
    {
    
    item.value}}</span>
              </el-col>
            </div>
          </div>
        </el-row>
        <el-row>
          <el-checkbox v-model="properties.bevelEnabled" @change="redraw">bevelEnabled</el-checkbox>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
import {
    
     transformSVGPathExposed } from './d3-threeD.js'
import {
    
     OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import {
    
     SceneUtils } from 'three/examples/jsm/utils/SceneUtils.js'
export default {
    
    
  data() {
    
    
    return {
    
    
      properties: {
    
    
        depth: {
    
    
          name: 'depth',
          value: 2,
          min: 0,
          max: 40,
          step: 1
        },
        bevelThickness: {
    
    
          name: 'bevelThickness',
          value: 2,
          min: 0,
          max: 10,
          step: 0.1
        },
        bevelSize: {
    
    
          name: 'bevelSize',
          value: 0.8,
          min: 0,
          max: 10,
          step: 0.1
        },
        bevelSegments: {
    
    
          name: 'bevelSegments',
          value: 3,
          min: 0,
          max: 40,
          step: 1
        },
        curveSegments: {
    
    
          name: 'curveSegments',
          value: 12,
          min: 0,
          max: 40,
          step: 1
        },
        steps: {
    
    
          name: 'steps',
          value: 1,
          min: 0,
          max: 5,
          step: 1
        },
        bevelEnabled: true
      },
      step: 0.01,
      mesh: null,
      camera: null,
      scene: null,
      renderer: null,
      controls: null
    }
  },
  mounted() {
    
    
    this.init()
  },
  methods: {
    
    
    formatTooltip(val) {
    
    
      return val
    },
    // 初始化
    init() {
    
    
      this.createScene() // 创建场景
      this.createMesh() // 创建网格模型
      this.createLight() // 创建光源
      this.createCamera() // 创建相机
      this.createRender() // 创建渲染器
      this.createControls() // 创建控件对象
      this.render() // 渲染
    },
    // 创建场景
    createScene() {
    
    
      this.scene = new THREE.Scene()
    },
    // 获取svg内容 转换为THREE.Shape对象
    svgChangeToShape() {
    
    
      const svgString = document.querySelector('#batman-path').getAttribute('d')
      const shape = transformSVGPathExposed(svgString)
      return shape
    },

    // 创建网格模型
    createMesh() {
    
    
      // 拉伸几何体配置项
      const options = {
    
    
        depth: this.properties.depth.value,
        bevelThickness: this.properties.bevelThickness.value,
        bevelSize: this.properties.bevelSize.value,
        bevelSegments: this.properties.bevelSegments.value,
        bevelEnabled: this.properties.bevelEnabled,
        curveSegments: this.properties.curveSegments.value,
        steps: this.properties.steps.value
      }

      // 创建拉伸几何体
      const geom = new THREE.ExtrudeGeometry(this.svgChangeToShape(), options)

      geom.applyMatrix4(new THREE.Matrix4().makeTranslation(-370, -74, 0))

      // 创建材质
      const meshMaterial = new THREE.MeshPhongMaterial({
    
    
        color: 0x003333
      })
      // 创建网格对象
      this.mesh = new THREE.Mesh(geom, meshMaterial)
      this.mesh.scale.x = 0.05
      this.mesh.scale.y = 0.05

      this.mesh.rotation.z = Math.PI
      this.mesh.rotation.x = -1.1
      // 网格对象添加到场景中
      this.scene.add(this.mesh)
    },

    // 创建光源
    createLight() {
    
    
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1) // 创建环境光
      this.scene.add(ambientLight) // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff) // 创建聚光灯
      spotLight.position.set(-40, 60, -10)
      spotLight.castShadow = true
      this.scene.add(spotLight)
    },
    // 创建相机
    createCamera() {
    
    
      const element = document.getElementById('container')
      const width = element.clientWidth // 窗口宽度
      const height = element.clientHeight // 窗口高度
      const k = width / height // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      this.camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000)
      this.camera.position.set(-80, 60, 40) // 设置相机位置

      this.camera.lookAt(new THREE.Vector3(10, 0, 0)) // 设置相机方向
      this.scene.add(this.camera)
    },
    // 创建渲染器
    createRender() {
    
    
      const element = document.getElementById('container')
      this.renderer = new THREE.WebGLRenderer({
    
     antialias: true, alpha: true })
      this.renderer.setSize(element.clientWidth, element.clientHeight) // 设置渲染区域尺寸
      this.renderer.shadowMap.enabled = true // 显示阴影
      this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
      this.renderer.setClearColor(0x3f3f3f, 1) // 设置背景颜色
      element.appendChild(this.renderer.domElement)
    },

    // 重新绘制
    redraw() {
    
    
      this.scene.remove(this.mesh)
      this.createMesh()
    },
    render() {
    
    
      this.mesh.rotation.y = this.step += 0.01
      this.renderer.render(this.scene, this.camera)
      requestAnimationFrame(this.render)
    },
    // 创建控件对象
    createControls() {
    
    
      this.controls = new OrbitControls(this.camera, this.renderer.domElement)
    }
  }
}
</script>
<style>
#container {
    
    
  position: absolute;
  width: 100%;
  height: 100%;
}
.controls-box {
    
    
  position: absolute;
  right: 5px;
  top: 5px;
  width: 300px;
  padding: 10px;
  background-color: #fff;
  border: 1px solid #c3c3c3;
}
.vertice-span {
    
    
  line-height: 38px;
  padding: 0 2px 0 10px;
}
</style>

猜你喜欢

转载自blog.csdn.net/qw8704149/article/details/110295955