首页 > 其他分享 >【Three.JS零基础入门教程】第八篇:材质详解

【Three.JS零基础入门教程】第八篇:材质详解

时间:2024-08-19 11:26:59浏览次数:9  
标签:const 第八篇 入门教程 THREE Three window renderer new scene

 前期回顾:

【Three.JS零基础入门教程】第一篇:搭建开发环境

【Three.JS零基础入门教程】第二篇:起步案例

【Three.JS零基础入门教程】第三篇:开发辅助

【Three.JS零基础入门教程】第四篇:基础变换

【Three.JS零基础入门教程】第五篇:项目规划

【Three.JS零基础入门教程】第六篇:物体详解

【Three.JS零基础入门教程】第七篇:材质详解

上一期我们介绍了threejs中的材质,本期主要介绍纹理。

1) 基本概念

three.js中, 使用图片来模拟纹理效果, 将一张二维的图片包裹到三维的物体上

比如

  • 世界地图的图片包裹到球体上就可以模拟出地球的效果
  • 篮球的图片包裹到球体上就可以模拟出篮球的效果

2) 纹理加载器

three.js中通过纹理加载器加载图片

示例:

// 加载纹理贴图
const texture = new THREE.TextureLoader().load('/src/assets/texture/world.jpeg')
// 在设置材质时指定纹理贴图
const sphereMaterial = new THREE.MeshStandardMaterial({
  map: texture,
})

纹理贴图又可以细分为:

  • 普通纹理贴图
  • 凹凸纹理贴图
  • 法向纹理贴图
  • 环境纹理贴图

3) 普通纹理贴图

完整示例:

import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

// 导入封装的MeshGui
import { MeshGui } from '../gui'

// 一. 创建场景
const scene = new THREE.Scene()

// 二. 创建相机
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight
)
camera.position.set(0, 0, 5)

// 三. 创建物体
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32)
// 加载纹理贴图
const texture = new THREE.TextureLoader().load('/src/assets/texture/world.jpeg')
// 在设置材质时指定纹理贴图
const sphereMaterial = new THREE.MeshStandardMaterial({
  map: texture,
})
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
scene.add(sphere)

new MeshGui({
  target: sphere,
})

// 创建灯光
const ambientLight = new THREE.AmbientLight(0xffffff, 1)
scene.add(ambientLight)

// 四. 创建渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setAnimationLoop(animation)
// 将渲染的canvas添加到body元素中
document.body.appendChild(renderer.domElement)

// 五. 辅助工具
const control = new OrbitControls(camera, renderer.domElement)

const axesHelper = new THREE.AxesHelper(10)
scene.add(axesHelper)

const gridHelper = new THREE.GridHelper(20, 20, 0xffffff, 0xffffff)
gridHelper.material.transparent = true
gridHelper.material.opacity = 0.5
scene.add(gridHelper)

function animation() {
  renderer.render(scene, camera)
}

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()

  renderer.setSize(window.innerWidth, window.innerHeight)
})

效果:

4) 凹凸纹理贴图

为了模拟表面的粗糙凹凸效果, 可以多加一组贴图来模拟高度上的凹凸效果

注意

只有在可以产生阴影的光线环境下, 才可以观察到凹凸效果

完整示例:

import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

// 导入封装的MeshGui
import { MeshGui } from '../gui'

// 一. 创建场景
const scene = new THREE.Scene()

// 二. 创建相机
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight
)
camera.position.set(0, 0, 5)

// 三. 创建物体
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32)

// 加载纹理贴图
const texture = new THREE.TextureLoader().load('/src/assets/texture/world.jpeg')

// 加载法向贴图
const map = new THREE.TextureLoader().load(
  '/src/assets/texture/world_normal.jpg'
)
// 在设置材质时指定纹理贴图
const sphereMaterial = new THREE.MeshStandardMaterial({
  map: texture,
  bumpMap: map,
})
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
scene.add(sphere)

new MeshGui({
  target: sphere,
})

// 添加环境灯光源
// const ambientLight = new THREE.AmbientLight(0xffffff, 1)
// scene.add(ambientLight)

// 添加点光源
const pointLight = new THREE.PointLight(0xffffff, 1)
pointLight.position.set(10, 10, 10)
scene.add(pointLight)

// 四. 创建渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setAnimationLoop(animation)
// 将渲染的canvas添加到body元素中
document.body.appendChild(renderer.domElement)

// 五. 辅助工具
const control = new OrbitControls(camera, renderer.domElement)

const axesHelper = new THREE.AxesHelper(10)
scene.add(axesHelper)

const gridHelper = new THREE.GridHelper(20, 20, 0xffffff, 0xffffff)
gridHelper.material.transparent = true
gridHelper.material.opacity = 0.5
scene.add(gridHelper)

function animation() {
  renderer.render(scene, camera)
}

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()

  renderer.setSize(window.innerWidth, window.innerHeight)
})

效果:

5) 法向纹理贴图

使用法向贴图可以展示更多的细节, 更精细的控制

注意

如果设置了法向贴图, 凹凸贴图就会失效

完整示例:

import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

// 导入封装的MeshGui
import { MeshGui } from '../gui'

// 一. 创建场景
const scene = new THREE.Scene()

// 二. 创建相机
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight
)
camera.position.set(0, 0, 5)

// 三. 创建物体
// 定义几何球体
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32)

// 加载纹理贴图
const texture = new THREE.TextureLoader().load('/src/assets/texture/world.jpeg')

// 加载法向贴图
const map = new THREE.TextureLoader().load(
  '/src/assets/texture/world_normal.jpg'
)
// 在设置材质时指定纹理贴图
const sphereMaterial = new THREE.MeshStandardMaterial({
  map: texture,
  bumpMap: map,
  normalMap: map,
})
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
scene.add(sphere)

new MeshGui({
  target: sphere,
})

// 添加环境灯光源
// const ambientLight = new THREE.AmbientLight(0xffffff, 1)
// scene.add(ambientLight)

// 添加点光源
const pointLight = new THREE.PointLight(0xffffff, 1)
pointLight.position.set(10, 10, 10)
scene.add(pointLight)

// 四. 创建渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setAnimationLoop(animation)
// 将渲染的canvas添加到body元素中
document.body.appendChild(renderer.domElement)

// 五. 辅助工具
const control = new OrbitControls(camera, renderer.domElement)

const axesHelper = new THREE.AxesHelper(10)
scene.add(axesHelper)

const gridHelper = new THREE.GridHelper(20, 20, 0xffffff, 0xffffff)
gridHelper.material.transparent = true
gridHelper.material.opacity = 0.5
scene.add(gridHelper)

function animation() {
  renderer.render(scene, camera)
}

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()

  renderer.setSize(window.innerWidth, window.innerHeight)
})

效果:

6) 环境纹理贴图

对于表面光滑的物体, 是可以反射出周围环境的效果, 环境贴图主要就是用于实现这个效果

效果:

示例:

import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'

// 导入封装的MeshGui
import { MeshGui } from '../gui'

// 一. 创建场景
const scene = new THREE.Scene()

// 二. 创建相机
const camera = new THREE.PerspectiveCamera(
  45,
  window.innerWidth / window.innerHeight
)
camera.position.set(0, 0, 5)

// 三. 创建物体
// 创建一个盒子, 模拟周围环境
const envGeometry = new THREE.BoxGeometry(1000, 1000, 1000)

const loader = new THREE.TextureLoader().setPath('/src/assets/texture/bridge/')

// 分别加载6个面的图片, 构成全景图
const imgs = ['px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg']

const mats = []
for (const item of imgs) {
  mats.push(
    new THREE.MeshBasicMaterial({
      map: loader.load(item),
      side: THREE.DoubleSide,
    })
  )
}
const env = new THREE.Mesh(envGeometry, mats)

// 创建一个球体, 能更好的显示效果
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32)
const texture = new THREE.CubeTextureLoader()
  .setPath('/src/assets/texture/bridge/')
  .load(imgs)

const sphereMaterial = new THREE.MeshStandardMaterial({
  envMap: texture,
  roughness: 0.0, // 粗糙程度, 0表示镜面, 非常光滑
  metalness: 1, // 金属度
})
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)

scene.add(env)
scene.add(sphere)

new MeshGui({
  target: sphere,
})

// 添加环境灯光源
// const ambientLight = new THREE.AmbientLight(0xffffff, 1)
// scene.add(ambientLight)

// 添加点光源
const pointLight = new THREE.PointLight(0xffffff, 1)
pointLight.position.set(10, 10, 10)
scene.add(pointLight)

// 四. 创建渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setAnimationLoop(animation)
// 将渲染的canvas添加到body元素中
document.body.appendChild(renderer.domElement)

// 五. 辅助工具
const control = new OrbitControls(camera, renderer.domElement)

const axesHelper = new THREE.AxesHelper(10)
// scene.add(axesHelper)

const gridHelper = new THREE.GridHelper(20, 20, 0xffffff, 0xffffff)
gridHelper.material.transparent = true
gridHelper.material.opacity = 0.5
// scene.add(gridHelper)

function animation() {
  renderer.render(scene, camera)
}

window.addEventListener('resize', () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()

  renderer.setSize(window.innerWidth, window.innerHeight)
})

我们不仅提供优质课程,助你学习更进一步!新中地学生threejs作品:

三维GIS开发特训营即将开班,免fei领视频版教程戳↓↓↓

GIS资料免费领icon-default.png?t=N7T8https://www.wjx.cn/vm/Qm8Ful2.aspx

标签:const,第八篇,入门教程,THREE,Three,window,renderer,new,scene
From: https://blog.csdn.net/2401_84715637/article/details/141320932

相关文章

  • 黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
    前言这篇文章没有什么套路。就是一套自学理论和方向,具体的需要配合网络黑白去学习。毕竟是有网络才会有黑白!有自学也有培训!1.打死也不要相信什么分分钟钟教你成为大黑阔的,各种包教包会的教程,就算打不死也不要去购买那些所谓的盗号软件之类的东西。2,我之前让你们在没有......
  • 【Three.JS零基础入门教程】第六篇:物体详解
     前期回顾:【Three.JS零基础入门教程】第一篇:搭建开发环境【Three.JS零基础入门教程】第二篇:起步案例【Three.JS零基础入门教程】第三篇:开发辅助【Three.JS零基础入门教程】第四篇:基础变换【Three.JS零基础入门教程】第五篇:项目规划下面将进一步详解介绍Threejs中的常用......
  • 【Three.JS零基础入门教程】第五篇:项目规划
     前期回顾:【Three.JS零基础入门教程】第一篇:搭建开发环境【Three.JS零基础入门教程】第二篇:起步案例【Three.JS零基础入门教程】第三篇:开发辅助【Three.JS零基础入门教程】第四篇:基础变换一个three.js的项目由这么几个部分组成场景物体灯光相机渲染器工具这里我们可以......
  • Docker入门教程:10分钟掌握基础
    Docker入门教程:10分钟掌握基础1.Docker是什么?Docker是一种容器化技术,它允许开发者打包他们的应用以及应用的依赖环境到一个可移植的容器中,这个容器可以在任何支持Docker的操作系统上运行。2.Docker的基本组成镜像(Image):一个只读的模板,用于创建Docker容器。容器(Containe......
  • SpringBoot整合MyBatis,入门教程,细节无敌,不能错过
    需求SpringBoot整合MyBatis。实现步骤搭建SpringBoot工程引入mybatis起步依赖、添加mysql驱动编写DataSource和MyBatis相关配置定义表和实体类编写dao和mapper文件/纯注解开发测试惨痛的教训同一个项目里,application.*文件只能有一个,如果有多个就会出现一些神奇问题......
  • 【Three.JS零基础入门教程】第四篇:基础变换
    前期回顾:【Three.JS零基础入门教程】第一篇:搭建开发环境【Three.JS零基础入门教程】第二篇:起步案例【Three.JS零基础入门教程】第三篇:开发辅助接下来,我们通过三种基础的变换来加深对3D坐标系中坐标和单位的理解,同时也是动画的基础.分别是:移动缩放旋转效果1......
  • SMHC Three SD/MMC host controller (SMHC) interfaces
    SMHCThreeSD/MMChostcontroller(SMHC)interfaces1TheSMHC0controlsthedevicesthatcomplywiththeprotocolSecureDigitalMemory(SDmem-version3.0)2TheSMHC1controlsthedevicethatcomplieswiththeprotocolSecureDigitalI/O(SDIO-version......
  • 最新版本原生three cesium案例聚集地
    开发实时预览:http://z2586300277.github.io/three-cesium-examples/index.html开源地址github:https://github.com/z2586300277/three-cesium-examples纯原生threecesium案例由于前端得发展,three.js和cesium.js越来越被越来越多得前端接触到,由于其学习路线比较复......
  • vue3+three入门一
    <scriptsetuplang="ts">import*asTHREEfrom'three';import{onMounted,ref}from"vue";import{ArcballControls}from"three/examples/jsm/controls/ArcballControls";constcontainerRef=ref(null)const......
  • C语言入门教程——手把手教零基础/新手入门(完整C语言学习笔记整理)
    前言    作为一名拥有多年开发经验的技术人员,我的职业生涯涵盖了多种编程语言,包括C语言、C++、C#和JavaScript等。出于对编程的热爱以及希望帮助更多初学者的目的,我决定利用业余时间整理一套全面的C语言学习指南。这套指南旨在为C语言初学者和编程爱好者提供......