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>