three.js使用情况贴图或者加载hdr图

打印 上一主题 下一主题

主题 506|帖子 506|积分 1518

1、three.js使用情况贴图

1.1、效果视频


     情况贴图
  
1.2、使用步调(个人以为)

(1)导入引入相关方法
(2)创建场景
(3)创建相机
(4)添加物体材质
(5)添加光源
(6)渲染
1.3、代码

  1. // 环境贴图代码
  2. import * as THREE from 'three'
  3. //目标:使用环境贴图放在球上展示
  4. //导入轨道控制器
  5. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
  6. //1、创建场景
  7. const scene = new THREE.Scene()
  8. //2、创建相机
  9. const camera = new THREE.PerspectiveCamera(
  10.     75,
  11.     window.innerWidth / window.innerHeight,
  12.     0.1,
  13.     1000
  14. ) // 参数分别代表,相机角度、屏幕宽高比、近端点,远端点
  15. //设置相机位置
  16. camera.position.set(2, 2, 2)
  17. scene.add(camera)
  18. //导入纹理
  19. const cubeTextureLoader = new THREE.CubeTextureLoader()
  20. //一下代码是因为物体有上下左右前后六个面,所以设置6个方向的贴图
  21. const envMapTexture = cubeTextureLoader.load([
  22.     '/static/texture/environmentMaps/1/px.jpg',
  23.     '/static/texture/environmentMaps/1/nx.jpg',
  24.     '/static/texture/environmentMaps/1/py.jpg',
  25.     '/static/texture/environmentMaps/1/ny.jpg',
  26.     '/static/texture/environmentMaps/1/pz.jpg',
  27.     '/static/texture/environmentMaps/1/nz.jpg',
  28. ])
  29. //添加物体
  30. const sphereGeometry = new THREE.SphereGeometry(1, 20, 20)
  31. const material = new THREE.MeshStandardMaterial({
  32.     metalness: 0.7, //金属度
  33.     roughness: 0.1, //粗糙度,设置为0表面会非常光滑,可以折射出太阳光
  34.     // envMap: envMapTexture, //环境贴图
  35. })
  36. const mesh = new THREE.Mesh(sphereGeometry, material)
  37. scene.add(mesh)
  38. //给场景添加背景
  39. scene.background = envMapTexture
  40. //给场景中所有的物体添加默认的贴图
  41. scene.environment = envMapTexture
  42. //添加环境光
  43. const light = new THREE.AmbientLight(0xffffff, 0.5)
  44. scene.add(light)
  45. //添加直线光源
  46. const directionLight = new THREE.DirectionalLight(0xffffff, 1)
  47. directionLight.position.set(10, 10, 10)
  48. scene.add(directionLight)
  49. //初始化渲染器
  50. const renderer = new THREE.WebGLRenderer()
  51. //设置渲染的尺寸大小
  52. renderer.setSize(window.innerWidth, window.innerHeight)
  53. //将webGL渲染的canvas添加到app中
  54. document.getElementById('app').appendChild(renderer.domElement)
  55. //创建控制器
  56. const controls = new OrbitControls(camera, renderer.domElement)
  57. //设置控制器阻尼,让滑动更有真实感
  58. controls.enableDamping = true
  59. //创建坐标轴
  60. const axesHelper = new THREE.AxesHelper(5)
  61. scene.add(axesHelper)
  62. render()
  63. //渲染函数
  64. function render(time) {
  65.     controls.update()
  66.     renderer.render(scene, camera)
  67.     //下一帧渲染完毕再次执行,保证每一帧都渲染
  68.     requestAnimationFrame(render)
  69. }
复制代码
2、three.js加载hdr图

2.1、效果视频


     加载hdr图
  
2.2、代码

  1. // three.js加载hdr图
  2. import * as THREE from 'three'
  3. //目标:加载hdr图
  4. //导入轨道控制器
  5. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
  6. //导入hdr加载器
  7. import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader'
  8. //1、创建场景
  9. const scene = new THREE.Scene()
  10. //2、创建相机
  11. const camera = new THREE.PerspectiveCamera(
  12.     75,
  13.     window.innerWidth / window.innerHeight,
  14.     0.1,
  15.     1000
  16. ) // 参数分别代表,相机角度、屏幕宽高比、近端点,远端点
  17. //设置相机位置
  18. camera.position.set(2, 2, 2)
  19. scene.add(camera)
  20. const rgbeLoader = new RGBELoader()
  21. rgbeLoader.loadAsync('static/texture/hdr/003.hdr').then((texture) => {
  22.     texture.mapping = THREE.EquirectangularReflectionMapping //正常只是一张图平铺,设置这个可以让图包围环绕整个环境
  23.     scene.background = texture //设置环境贴图
  24.     scene.environment = texture
  25. })
  26. //添加物体
  27. const sphereGeometry = new THREE.SphereGeometry(1, 20, 20)
  28. const material = new THREE.MeshStandardMaterial({
  29.     metalness: 0.7, //金属度
  30.     roughness: 0.1, //粗糙度,设置为0表面会非常光滑,可以折射出太阳光
  31. })
  32. const mesh = new THREE.Mesh(sphereGeometry, material)
  33. scene.add(mesh)
  34. //添加环境光
  35. const light = new THREE.AmbientLight(0xffffff, 0.5)
  36. scene.add(light)
  37. //添加直线光源
  38. const directionLight = new THREE.DirectionalLight(0xffffff, 1)
  39. directionLight.position.set(10, 10, 10)
  40. scene.add(directionLight)
  41. //初始化渲染器
  42. const renderer = new THREE.WebGLRenderer()
  43. //设置渲染的尺寸大小
  44. renderer.setSize(window.innerWidth, window.innerHeight)
  45. //将webGL渲染的canvas添加到app中
  46. document.getElementById('app').appendChild(renderer.domElement)
  47. //创建控制器
  48. const controls = new OrbitControls(camera, renderer.domElement)
  49. //设置控制器阻尼,让滑动更有真实感
  50. controls.enableDamping = true
  51. //创建坐标轴
  52. const axesHelper = new THREE.AxesHelper(5)
  53. scene.add(axesHelper)
  54. render()
  55. //渲染函数
  56. function render(time) {
  57.     controls.update()
  58.     renderer.render(scene, camera)
  59.     //下一帧渲染完毕再次执行,保证每一帧都渲染
  60.     requestAnimationFrame(render)
  61. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万万哇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表