6.纹理偏移-旋转-重复
这里cue一下上一节用到的纹理加载器TextureLoader,通过它来得到一个纹理对象Texture。
首先尝试一下offset偏移设置:
//导入纹理
const textureLoader = new THREE.TextureLoader()
const stoneColorTexture = textureLoader.load("./textures/stone.jpg")
// stoneColorTexture.offset.x = 0.5
// stoneColorTexture.offset.y = 0.5
stoneColorTexture.offset.set(0.5, 0.5)//相当于上述两条代码总和
下面是纹理的rotation旋转设置:(记住它是弧度制)
//纹理旋转
//设置旋转中心点,默认是(0,0)左下角,(0.5,0.5)对应纹理的正中心
stoneColorTexture.center.set(0.5, 0.5)
//旋转45°(默认围绕左下角)
stoneColorTexture.rotation = Math.PI / 4
下面是纹理的repeat重复设置:
wrapS:这个值定义了纹理贴图在水平方向上将如何包裹,在UV映射中对应于U。默认值是ClampToEdgeWrapping,即纹理边缘将被推到外部边缘的纹素。其他两个选项分别是:RepeatWrapping无限重复,MirroredRepeatWrapping镜像重复。
wrapT:这个值定义了纹理贴图在垂直方向上将如何包裹,在UV映射中对应于V。选项与wrapS相同。
//纹理重复
//水平方向重复2次,垂直方向重复3次
stoneColorTexture.repeat.set(2, 3)
//设置纹理重复的模式
stoneColorTexture.wrapS = THREE.MirroredRepeatWrapping
stoneColorTexture.wrapT = THREE.RepeatWrapping
7.设置纹理显示算法与mipmap
magFilter:当一个纹素覆盖大于一个像素时(被拉长),贴图将如何采样。默认值为LinearFilter,它将获取四个最接近的纹素,并在它们之间进行双线性插值。另一个是NearestFilter,它将使用最接近的纹素的值。
minFilter:当一个纹素覆盖小于一个像素时(被压缩),贴图将如何采样。默认值为LinearMipmapLinearFilter,它将使用mipmapping以及三次线性滤镜。
//导入纹理
const textureLoader = new THREE.TextureLoader()
const stoneColorTexture = textureLoader.load("./textures/stone.jpg")
//texture纹理显示设置,会很高清
stoneColorTexture.magFilter = THREE.NearestFilter
stoneColorTexture.minFilter = THREE.NearestFilter
8.透明材质与透明纹理
如果玩我的世界的话应该知道,铁栅栏,一般会比较常用在铁栅栏上。
用到的是 alphaMap 属性,alpha贴图是一张灰度纹理,用于控制整个表面的不透明度。(黑色:完全透明;白色:完全不透明;可以有灰色),默认是null。
当然在设置材质的时候需要设置transparent:true,才能将材质设置为透明。
//导入纹理
const textureLoader = new THREE.TextureLoader()
const stoneColorTexture = textureLoader.load("./textures/stone.jpg")
const stoneAlphaTexture = textureLoader.load("./textures/alpha.jpg")//一张黑白的图,白色部分不透明,黑色部分变为透明
//几何体
const cubeGeometry = new THREE.BoxBufferGeometry(1, 1, 1)
//材质
const basicMaterial = new THREE.MeshBasicMaterial({
map: stoneColorTexture,
alphaMap: stoneAlphaTexture,
transparent: true //设置为true,材质才能为透明
})
const cube = new THREE.Mesh(cubeGeometry, basicMaterial)
scene.add(cube)
当然我们还可以所以个平面,因为现在这样是一个立方体,六个面都会有整个图案。假设我们要做的是一个门之类的东西,我们要做一个平面出来。
//添加平面
const plane = new THREE.Mesh(
new THREE.PlaneBufferGeometry(1, 1), basicMaterial
)
plane.position.set(3, 0, 0)
scene.add(plane)
ok,那么在之前的时候我们遇到了一个情况,就是一个物体呢,在正面的时候显示,在背面却不显示了。是有一个属性可以设置的。
side:定义将要渲染哪一面-正面、背面或两面。默认是FrontSide为了节省性能,其他选项有BackSide和DoubleSide。
//材质
const basicMaterial = new THREE.MeshBasicMaterial({
map: stoneColorTexture,
alphaMap: stoneAlphaTexture,
transparent: true,
side: THREE.DoubleSide //设置两面可见
})
//也可以在外面设置
basicMaterial.side = THREE.DoubleSide