three.js之材质-MeshFaceMaterial介绍和使用(vue中使用three.js15)

MeshFaceMaterial材质介绍和使用

1.MeshFaceMaterial材质介绍

MeshFaceMaterial并不是真正的材质,更像一种材质容器,可以为几何体的每个面指定不同的材质。

使用的时候只需要创建一个数组,然后给数组中填充每个面材质,然后new一个面材质,之后几何体就可以使用这个面材质了,请参考如下代码:

const faceMaterialArray = []
// 给每个面填充不同的材质
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x009e60 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x0051ba }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffd500 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xff5800 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xc41e3a }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffffff }))
const faceMaterial = new THREE.MeshFaceMaterial(faceMaterialArray)
// 创建魔方
for (let x = 0; x < 3; x++) {
  for (let y = 0; y < 3; y++) {
    for (let z = 0; z < 3; z++) {
      const cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9)
      const cube = new THREE.Mesh(cubeGeom, faceMaterial)
      cube.position.set(x * 3 - 3, y * 3, z * 3 - 3)
      this.cubeGroup.add(cube)
    }
  }
}

通过THREE.MeshFaceMaterial创建出来的材质也可直接用数组替换也会呈现一样的效果

const faceMaterialArray = []
// 给每个面填充不同的材质
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x009e60 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x0051ba }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffd500 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xff5800 }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xc41e3a }))
faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffffff }))
// 创建魔方
for (let x = 0; x < 3; x++) {
  for (let y = 0; y < 3; y++) {
    for (let z = 0; z < 3; z++) {
      const cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9)
      const cube = new THREE.Mesh(cubeGeom, faceMaterialArray)
      cube.position.set(x * 3 - 3, y * 3, z * 3 - 3)
      this.cubeGroup.add(cube)
    }
  }
}

2.demo说明

在这里插入图片描述

如上图,该示例创建出了一个六个面不同颜色的魔方,同时可以通过右上角的speed属性控制魔方的旋转速度

3.demo代码

<template>
  <div>
    <div id="container"></div>
    <div class="controls-box">
      <section>
        <el-row>
          <div v-for="(item,key) in properties" :key="key">
            <div>
              <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"></el-slider>
              </el-col>
              <el-col :span="3">
                <span class="vertice-span">{{item.value}}</span>
              </el-col>
            </div>
          </div>
        </el-row>
      </section>
    </div>
  </div>
</template>

<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
export default {
  data () {
    return {
      properties: {
        ratationSpeed: {
          name: 'speed',
          value: 0.01,
          min: 0,
          max: 0.5,
          step: 0.01
        }
      },
      cubeGroup: null,
      camera: null,
      scene: null,
      renderer: null,
      controls: null
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    formatTooltip (val) {
      return val
    },
    // 初始化
    init () {
      this.createScene() // 创建场景
      this.createMesh() // 创建网格模型
      this.createCube() // 创建方块
      this.createLight() // 创建光源
      this.createCamera() // 创建相机
      this.createRender() // 创建渲染器
      this.createControls() // 创建控件对象
      this.render() // 渲染
    },
    // 创建场景
    createScene () {
      this.scene = new THREE.Scene()
    },
    // 创建网格模型
    createMesh () {
      const planeGeometry = new THREE.PlaneGeometry(80, 60, 4, 4) // 创建一个平面对象PlaneGeometry
      const planeMaterial = new THREE.MeshLambertMaterial({
        color: 0x777777
      }) // 材质对象Material
      const plane = new THREE.Mesh(planeGeometry, planeMaterial)
      plane.receiveShadow = true

      // 设置平面位置
      plane.rotation.x = -0.5 * Math.PI
      plane.position.set(0, -5, 0)

      // 平面对象添加到场景中
      this.scene.add(plane)
    },
    // 创建方块
    createCube () {
      this.cubeGroup = new THREE.Mesh()
      const faceMaterialArray = []
      // 给每个面填充不同的材质
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x009e60 }))
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0x0051ba }))
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffd500 }))
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xff5800 }))
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xc41e3a }))
      faceMaterialArray.push(new THREE.MeshBasicMaterial({ color: 0xffffff }))

      /* const faceMaterial = new THREE.MeshFaceMaterial(faceMaterialArray)
      for (let x = 0; x < 3; x++) {
        for (let y = 0; y < 3; y++) {
          for (let z = 0; z < 3; z++) {
            const cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9)
            const cube = new THREE.Mesh(cubeGeom, faceMaterial)
            cube.position.set(x * 3 - 3, y * 3, z * 3 - 3)
            this.cubeGroup.add(cube)
          }
        }
      } */
      // 运行以上注释代码,控制台会提示THREE.MeshFaceMaterial has been removed. Use an Array instead.不过可以正常运行,

      // 创建魔方
      for (let x = 0; x < 3; x++) {
        for (let y = 0; y < 3; y++) {
          for (let z = 0; z < 3; z++) {
            const cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9)
            const cube = new THREE.Mesh(cubeGeom, faceMaterialArray) // 原来通过MeshFaceMaterial创建的面材质可以直接用数组替换
            cube.position.set(x * 3 - 3, y * 3, z * 3 - 3)
            this.cubeGroup.add(cube)
          }
        }
      }

      this.scene.add(this.cubeGroup)
    },

    // 创建光源
    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)
    },

    // 更新属性
    updateFun () {
      this.cubeGroup.rotation.y += this.properties.ratationSpeed.value
    },
    render () {
      this.updateFun()
      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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Vue-cli 快速搭建基于 Vue.js 的项目结构并在此基础上添加 element-ui 和 three.js 的依赖库。以下是相关的命令: 1. 安装 Vue-cli ``` npm install -g @vue/cli ``` 2. 创建一个基于 Vue.js 的项目 ``` vue create your-project-name ``` 3. 进入项目目录并添加 element-ui 和 three.js 的依赖库 ``` cd your-project-name npm i element-ui three ``` 4. 在 main.js 导入 element-ui 和 three.js 的样式和组件 ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import * as THREE from 'three' import App from './App.vue' Vue.config.productionTip = false Vue.use(ElementUI) Vue.prototype.$THREE = THREE new Vue({ render: h => h(App), }).$mount('#app') ``` 5. 在 App.vue 创建一个包含 three.js 场景的组件 ```vue <template> <div class="three-container"></div> </template> <script> export default { name: 'App', mounted () { // 初始化 three.js 场景 const scene = new this.$THREE.Scene() const camera = new this.$THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ) const renderer = new this.$THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) document.querySelector('.three-container').appendChild(renderer.domElement) const geometry = new this.$THREE.BoxGeometry(1, 1, 1) const material = new this.$THREE.MeshBasicMaterial({ color: 0x00ff00 }) const cube = new this.$THREE.Mesh(geometry, material) scene.add(cube) camera.position.z = 5 const animate = () => { requestAnimationFrame(animate) cube.rotation.x += 0.01 cube.rotation.y += 0.01 renderer.render(scene, camera) } animate() } } </script> <style scoped> .three-container { width: 100%; height: 100%; position: fixed; } </style> ``` 这样就可以创建一个基于 Vue.js、element-ui 和 three.js 的后台管理系统模板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值