three.js加载和使⽤纹理-通过设置纹理的wrapS、wrapT、repeat属性实现。。
纹理的重复平铺、重复映射
1.demo效果
2. 纹理平铺的相关属性
在之前学习纹理对象Texture已经介绍过纹理的wrapS、wrapT、repeat属性,它们都是⽤来设置纹理重复的相关属性,在来简单说⼀下这⼏个属性
wrapS
纹理在⽔平⽅向上纹理包裹⽅式,在UV映射中对应于U,默认THREE.ClampToEdgeWrapping,表⽰纹理边缘与⽹格的边缘贴合。
中间部分等⽐缩放。还可以设置为:THREE.RepeatWrapping(重复平铺) 和 THREE.MirroredRepeatWrapping(先镜像再重复平铺)
wrapT
纹理贴图在垂直⽅向上的包裹⽅式,在UV映射中对应于V,默认也是THREE.ClampToEdgeWrapping,与wrapS属性⼀样也可以设置为:THREE.RepeatWrapping(重复平铺) 和 THREE.MirroredRepeatWrapping(先镜像再重复平铺)
repeat
⽤来设置纹理将在表⾯上,分别在U、V⽅向重复多少次。
repeat属性是Vector2类型,可以使⽤如下语句来为它赋值
this.cube.peat.set(repeatX, repeatY)
repeatX ⽤来指定在x轴⽅向上多久重复⼀次,repeatY⽤来指定在y轴⽅向上多久重复⼀次
这两个变量取值范围与对应效果如下
如果设置等于1,则纹理不会重复
如果设置为 ⼤于0⼩于1 的值,纹理会被放⼤
如果设置为 ⼩于0 的值,那么会产⽣纹理的镜像
如果设置为 ⼤于1 的值,纹理会重复平铺
3. 实现要点
属性改变时需要将新的值重新赋值给wrapS、wrapT、repeat属性,赋值过程和处理逻辑如下
//设置⽴⽅体的纹理重复数量
this.cube.peat.set(
peatX.value,
peatY.value
)
//设置球体的纹理重复数量
this.sphere.peat.set(
peatX.value,
peatY.value
)
//纹理不重复时,纹理包裹⽅式为重复平铺
if(this.properties.isRepeat){
this.cube.material.map.wrapS =THREE.RepeatWrapping
this.cube.material.map.wrapT =THREE.RepeatWrapping
this.sphere.material.map.wrapS =THREE.RepeatWrapping
this.sphere.material.map.wrapT =THREE.RepeatWrapping
}else{
//纹理不重复时,纹理包裹⽅式为纹理边缘与⽹格的边缘贴合
this.cube.material.map.wrapS =THREE.ClampToEdgeWrapping
this.cube.material.map.wrapT =THREE.ClampToEdgeWrapping
this.sphere.material.map.wrapS =THREE.ClampToEdgeWrapping
this.sphere.material.map.wrapT =THREE.ClampToEdgeWrapping
}
4. demo代码
<template>
<div>
<div id="container"/>
<div class="controls-box">
<section>
<el-row>
<el-checkbox v-model="properties.isRepeat" @change="propertiesChange">
isRepeat
</el-checkbox>
</el-row>
<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="propertiesChange" />
</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:{
repeatX:{
name:'repeatX',
value:1,
min:-4,
max:4,
step:0.1
},
repeatY:{
name:'repeatY',
value:1,
min:-4,
max:4,
step:0.1
},
isRepeat:true
},
cube:null,
sphere:null,
camera:null,
scene:null,
renderer:null,
controls:null,
step:0
}
},
mounted(){
this.init()
},
methods:{
formatTooltip(val){
return val
},
// 初始化
init(){
},
// 创建场景
createScene(){
this.scene =new THREE.Scene()
},
// 创建模型
createModels(){
//创建砖块纹理的⽴⽅体
this.cube =ateMesh(
new THREE.BoxGeometry(6,6,6),
'brick-wall.jpg'
)
this.cube.position.x =-5
this.scene.add(this.cube)
//创建⽊条纹理的球体
this.sphere =ateMesh(
new THREE.SphereGeometry(5,20,20),
'floor-wood.jpg'
)
this.scene.add(this.sphere)
},
createMesh(geom, textureName){
//加载纹理
const publicPath = v.BASE_URL
const texture =new THREE.TextureLoader().load(
`${publicPath}textures/general/`+ textureName
)
//⽔平⽅向和垂直⽅向上纹理包裹⽅式为重复平铺
texture.wrapS =THREE.RepeatWrapping
texture.wrapT =THREE.RepeatWrapping
const mat =new THREE.MeshPhongMaterial()
mat.map = texture
const mesh =new THREE.Mesh(geom, mat)
return mesh
},
// 创建光源
createLight(){
// 环境光
const ambientLight =new THREE.AmbientLight(0x242424,0.1)// 创建环境光
this.scene.add(ambientLight)// 将环境光添加到场景
const spotLight =new THREE.SpotLight(0xffffff)// 创建聚光灯
spotLight.position.set(0,30,30)
spotLight.intensity =1.2
this.scene.add(spotLight)
},
// 创建相机
createCamera(){
const element = ElementById('container')
const width = element.clientWidth // 窗⼝宽度
const height = element.clientHeight // 窗⼝⾼度
const k = width / height // 窗⼝宽⾼⽐
// PerspectiveCamera( fov, aspect, near, far )
this.camera =new THREE.PerspectiveCamera(45, k,0.1,1000)
this.camera.position.set(0,12,20)// 设置相机位置
this.camera.lookAt(new THREE.Vector3(0,0,0))// 设置相机⽅向
this.scene.add(this.camera)
},
// 创建渲染器
createRender(){
const element = ElementById('container')
// pe = THREE.PCFSoftShadowMap
element.derer.domElement)
},
propertiesChange(){
//设置⽴⽅体的纹理重复数量
this.cube.peat.set(
peatX.value,
原生js和js的区别peatY.value
)
//设置球体的纹理重复数量
this.sphere.peat.set(
peatX.value,
peatY.value
/
/纹理不重复时,纹理包裹⽅式为重复平铺
if(this.properties.isRepeat){
this.cube.material.map.wrapS =THREE.RepeatWrapping
this.cube.material.map.wrapT =THREE.RepeatWrapping
this.sphere.material.map.wrapS =THREE.RepeatWrapping
this.sphere.material.map.wrapT =THREE.RepeatWrapping
}else{
//纹理不重复时,纹理包裹⽅式为纹理边缘与⽹格的边缘贴合
this.cube.material.map.wrapS =THREE.ClampToEdgeWrapping
this.cube.material.map.wrapT =THREE.ClampToEdgeWrapping
this.sphere.material.map.wrapS =THREE.ClampToEdgeWrapping
this.sphere.material.map.wrapT =THREE.ClampToEdgeWrapping
}
//更新材质中的纹理
this.cube.dsUpdate =true
this.sphere.dsUpdate =true
},
updateRotation(){
this.step +=0.01
ation.x =this.step
ation.y =this.step
ation.x =this.step
ation.y =this.step
},
render(){
this.updateRotation()
der)
},
// 创建控件对象
createControls(){
}
}
</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;
}
.label-col {
padding:8px 5px;
}
.vertice-span {
line-height:38px;
padding:02px 010px;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。