ThreeJs学习-纹理贴图、顶点UV坐标

1、纹理贴图

javascript 复制代码
import * as THREE from 'three'

const loader = new THREE.TextureLoader(); //纹理加载器
const texture = loader.load('./earth.png'); .load()方法加载图像,返回一个纹理对象Texture
const material = new THREE.MeshLambertMaterial({
  map: texture
});
texture.colorSpace = THREE.SRGBColorSpace;//设置为SRGB颜色空间

const geometry = new THREE.SphereGeometry(100)
const mesh = new THREE.Mesh(geometry, material);

export default mesh

效果:把一个地球的图片贴到一个球体上

2、纹理对象阵列,实现地面效果。

javascript 复制代码
主要使用的是设置阵列模式和uv两个方向纹理重复数量
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(30, 30)
javascript 复制代码
import * as THREE from 'three';

const geometry = new THREE.PlaneGeometry(100, 100);

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('./floor.png');
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(30, 30)
const material = new THREE.MeshBasicMaterial({
  map: texture,
  side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.rotateX(Math.PI / 2)
export default mesh;

3、UV坐标

UV坐标的作用是从纹理贴图上提取像素映射到网格模型mesh的表面

4、背景透明png贴图

设置材质的transparent: true,这样png图片背景完全透明。

5、通过纹理的offset偏移属性实现动画

javascript 复制代码
import * as THREE from 'three';

const textureLoader = new THREE.TextureLoader(); //创建纹理加载器
const texture = textureLoader.load('./line.jpg');//加载图片
texture.wrapS = THREE.RepeatWrapping; //设置阵列
texture.repeat.x = 30; // uv两个方向纹理重复数量
const geometry = new THREE.PlaneGeometry(100, 100);

const material = new THREE.MeshBasicMaterial({
  map: texture
});

const mesh1 = new THREE.Mesh(geometry, material);
mesh1.rotateX(-Math.PI / 2)
export { mesh1, texture }



// //渲染
function animate() {
  texture.offset.x += 0.1 // 纹理偏移属性
  requestAnimationFrame(animate);
  stats.update();
  renderer.render(scene, camera);
}

const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', function () {
  // renderer.render(scene, camera); //执行渲染操作
});//监听鼠标、键盘事件

animate();
相关推荐
撩得Android一次心动1 小时前
Jetpack Compose 知识点整理2【个人用】
android·笔记·学习·kotlin·compose
dadaobusi2 小时前
学习:灵犀指令集
学习
xhyu614 小时前
【学习笔记】图像质量评估方法 (5.生成图像是否符合用户要求)
笔记·学习·计算机视觉
卿卿的产品经理日记5 小时前
【AI产品经理实战】Day 38|try-except 踩坑实录:报错是数据
人工智能·笔记·python·学习·产品经理
keyipatience5 小时前
TypeScript学习(1)
前端·javascript·学习·typescript·html·css3
lazy H6 小时前
【计算机网络】第 2 篇:一条数据如何穿过网络?彻底理解数据封装与解封装
网络·经验分享·后端·学习·计算机网络
我命由我123457 小时前
软件工程概念 - 可插拔性
java·后端·学习·java-ee·系统架构·软件工程·学习方法
kkkkkkkkkk_Z8 小时前
新手自学嵌入式 | 学习日记:I2C总线协议、时序分析、读写流程与代码实现
学习
鬼手点金10 小时前
构建有效 Agent 的核心原则
java·人工智能·git·学习·自动化
姜九笙56110 小时前
python学习---归来仍是新人
java·python·学习