【Three.js基础学习】20.Environment map

打印 上一主题 下一主题

主题 985|帖子 985|积分 2955

提示:文章写完后,目次可以主动生成,怎样生成可参考右边的资助文档
  
前言

   课程回顾:
      模型的加载
      GLTFLoader
  
      环境贴图实现;
      CubeTextureLoader
  
      LDR:低动态范围
      backgroundBlurriness:设置背景含糊 (不生效 为什么?)
      backgroundIntensity: 设置背景强度,设置的是背景亮度 (不生效 为什么?)
  
      HDRI :  等距矩形环境贴图
      怎样加载使用HDRI文件?
  
      RGBE :赤色 ,绿色 ,蓝色 ,指数:存储的是亮度 (重点)
      我们需要使用RGBELoader。"RGBE"代表"红绿蓝指数",指数存储亮度
      这是“HDR”格式的编码。
  
      THREE.EquirectangularReflectionMapping // 等距型反射
      THREE.EquirectangularRefractionMapping // 折射透明
  
      HDRI 缺点
          文件加载渲染较大
      建议只对光照使用HDRI
  
      ?上面实现中有一个题目,怎样办理环境贴图对模型的影响
  
      1.blender怎样生成环田地图?
      在blender制作
  
      2.使用NVIDA Canvas生成环田地图
      https://www.nvidia.cn/studio/canvas/
  
          类型:exr后缀
          扩展名是.exr,而不是.hdr。我们导出的文件也是存储的颜色范围的“HDR”图像,但编码不同
          EXR还可以存储层,并具有alpha通道
     
      3. 环境贴图 (收费)
          https://skybox.blockadelabs.com/
  
     
      时间环田地图
      真实的光 : 怎样让甜甜圈模型 产生光?
          需要创建自己的立方体纹理,更textureLoad差不多,在每一帧中做处置惩罚;因此使用webgl立方体渲染器目的
     
      这个时间模型还是黑色
  
      需要一个特殊的相机。 如果想要渲染就需要一个相机进行拍摄
      WebGLCubeRenderTarget
  
      分成办理相机渲染,决定显示什么
      object.layers.enable(..)将添加一个图层
      object.layers.disable(...)将移除一个图层
      object.layers.set(...)将主动启用一个图层并禁用全部其他图层
  
  一、代码

  1. import * as THREE from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. import GUI from 'lil-gui'
  4. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
  5. import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js'
  6. import {EXRLoader} from 'three/examples/jsm/loaders/EXRLoader.js'
  7. import { Mapping, Mesh } from 'three'
  8. /*
  9. * Loaders
  10. */
  11. const gltfLoader = new GLTFLoader()
  12. const cubeTextrueLoader = new THREE.CubeTextureLoader()
  13. const rgbeLoader = new RGBELoader()
  14. const exrLoader = new EXRLoader()
  15. const textureLoader = new THREE.TextureLoader()
  16. /**
  17. * gui
  18. */
  19. const gui = new GUI()
  20. const global = {
  21. }
  22. // Canvas
  23. const canvas = document.querySelector('canvas.webgl')
  24. /**
  25. * scene
  26. */
  27. const scene = new THREE.Scene()
  28. /*
  29.     Updata all materials
  30. */
  31. const updateAllMaterials = () => {
  32.     scene.traverse((child)=>{ //  遍历
  33.         if(child.isMesh && child.material.isMeshStandardMaterial){
  34.             child.material.envMapIntensity = global.envMapIntensity
  35.         }
  36.     })
  37. }
  38. /*
  39.     environmentMap 环境贴图
  40. */
  41. scene.backgroundBlurriness = 0
  42. scene.backgroundIntensity = 1
  43. // global intensity
  44. global.envMapIntensity = 1 // 环境贴图强度
  45. gui
  46.     .add(global,'envMapIntensity')
  47.     .min(0)
  48.     .max(10)
  49.     .step(0.001)
  50.     .onChange(updateAllMaterials)
  51. gui
  52.     .add(scene, 'backgroundBlurriness')
  53.     .min(0)
  54.     .max(1)
  55.     .step(0.001)
  56. gui
  57.     .add(scene, 'backgroundIntensity')
  58.     .min(0)
  59.     .max(10)
  60.     .step(0.001)
  61.    
  62. // LDR cube texture  环境贴图的实现
  63. // const environmentMap = cubeTextrueLoader.load([
  64. //     '/environmentMaps/2/px.png',
  65. //     '/environmentMaps/2/nx.png',
  66. //     '/environmentMaps/2/py.png',
  67. //     '/environmentMaps/2/ny.png',
  68. //     '/environmentMaps/2/pz.png',
  69. //     '/environmentMaps/2/nz.png',
  70. // ])
  71. // scene.environment = environmentMap  // 可以实现场景中每一个网格材质对于场景起到作用
  72. // scene.background = environmentMap // 将环境贴图应用到场景中
  73. // HDR (RGBA) equirectangular
  74. // rgbeLoader.load(
  75. //     '/environmentMaps/测试hdr生成全景3.hdr',
  76. //     (environmentMap)=>{
  77. //         environmentMap.mapping = THREE.EquirectangularReflectionMapping
  78. //         scene.background = environmentMap
  79. //         scene.environment = environmentMap
  80. //     }
  81. // )
  82. // HDR (EXR) equirectangular
  83. // exrLoader.load('/environmentMaps/nvidiaCanvas-4k.exr',(environmentMap)=>{
  84. //     environmentMap.mapping = THREE.EquirectangularReflectionMapping
  85. //     scene.background = environmentMap
  86. //     scene.environment = environmentMap
  87. // })
  88. // LDR equirectangular
  89. // const environmentMap = textureLoader.load('') //没资源
  90. // environmentMap.mapping = THREE.EquirectangularReflectionMapping
  91. // environmentMap.colorSpace = THREE.SRGBColorSpace
  92. // scene.background = environmentMap
  93. // scene.environment = environmentMap
  94. // Ground projected skybox
  95. // rgbeLoader.load('/environmentMaps/1/little_paris_eiffel_tower_8k.hdr',(environmentMap)=>{
  96. //     environmentMap.mapping = THREE.EquirectangularReflectionMapping
  97. //     scene.environment = environmentMap
  98. //     scene.background = environmentMap
  99. // })
  100. /*
  101.     Real time environment map
  102. */
  103. const environmentMap = textureLoader.load('/environmentMaps/blockadesLabsSkybox/interior_views_cozy_wood_cabin_with_cauldron_and_p.jpg')
  104. environmentMap.mapping = THREE.EquirectangularReflectionMapping
  105. environmentMap.colorSpace = THREE.SRGBColorSpace
  106. scene.background = environmentMap
  107. /*
  108.     holyDount
  109. */
  110. const holyDonut = new THREE.Mesh(
  111.     new THREE.TorusGeometry(8,0.5),
  112.     new THREE.MeshBasicMaterial({color:new THREE.Color(10, 4, 2)})
  113. )
  114. holyDonut.layers.enable(1) // 分层启用
  115. holyDonut.position.y = 3.5
  116. scene.add(holyDonut)
  117. /*
  118.     cube render target
  119. */
  120. const cubeRenderTarget = new THREE.WebGLCubeRenderTarget(
  121.     256, // 分辨率
  122.     { // 渲染目标的选项
  123.         type:THREE.FloatType  // 类型
  124.     }
  125. )
  126. scene.environment = cubeRenderTarget.texture // 使用自己的纹理
  127. // Cube Camera
  128. const cubeCamera = new THREE.CubeCamera(0.1,100,cubeRenderTarget) // 要每一帧渲染
  129. cubeCamera.layers.set(1) // 设置相机分成
  130. /**
  131. * torus knot
  132. */
  133. const torusKnot = new THREE.Mesh(
  134.     new THREE.TorusKnotGeometry(1, 0.4, 100, 16),
  135.     new THREE.MeshStandardMaterial({
  136.         roughness: 0,
  137.         metalness: 1,
  138.         color: 0xaaaaaa
  139.     })
  140. )
  141. torusKnot.position.x = -4
  142. torusKnot.position.y = 4
  143. scene.add(torusKnot)
  144. /*
  145. * Models
  146. */
  147. gltfLoader.load(
  148.     '/models/FlightHelmet/glTF/FlightHelmet.gltf',
  149.     (gltf)=>{
  150.         gltf.scene.scale.set(10,10,10)
  151.         scene.add(gltf.scene)
  152.         updateAllMaterials()
  153.     }
  154. )
  155. /**
  156. * Sizes
  157. */
  158. const sizes = {
  159.     width: window.innerWidth,
  160.     height: window.innerHeight
  161. }
  162. window.addEventListener('resize', () => {
  163.     // Update sizes
  164.     sizes.width = window.innerWidth
  165.     sizes.height = window.innerHeight
  166.     // Update camera
  167.     camera.aspect = sizes.width / sizes.height
  168.     camera.updateProjectionMatrix()
  169.     // Update renderer
  170.     renderer.setSize(sizes.width, sizes.height)
  171.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  172. })
  173. /**
  174. * camera
  175. */
  176. const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  177. camera.position.set(- 8, 4, 8)
  178. scene.add(camera)
  179. /**
  180. * renderer
  181. */
  182. const renderer = new THREE.WebGLRenderer({
  183.     canvas: canvas
  184. })
  185. // renderer.shadowMap.enabled = true
  186. // renderer.shadowMap.type = THREE.PCFSoftShadowMap
  187. renderer.setSize(sizes.width, sizes.height)
  188. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  189. /**
  190. * control
  191. */
  192. const controls = new OrbitControls(camera, canvas)
  193. controls.target.y = 3.5
  194. controls.enableDamping = true
  195. /**
  196. * tick
  197. */
  198. const clock = new THREE.Clock()
  199. const tick = () => {
  200.     // Time
  201.     const elapsedTime = clock.getElapsedTime()
  202.     if(holyDonut){
  203.         holyDonut.rotation.x = Math.sin(elapsedTime) * 2
  204.         cubeCamera.update(renderer, scene)
  205.     }
  206.     controls.update()
  207.     renderer.render(scene, camera)
  208.     requestAnimationFrame(tick)
  209. }
  210. tick()
复制代码
二、知识点

1.LDR

  1. import * as THREE from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. import GUI from 'lil-gui'
  4. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
  5. import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js'
  6. import {EXRLoader} from 'three/examples/jsm/loaders/EXRLoader.js'
  7. import { Mapping, Mesh } from 'three'
  8. /*
  9. * Loaders
  10. */
  11. const gltfLoader = new GLTFLoader()
  12. const cubeTextrueLoader = new THREE.CubeTextureLoader()
  13. const rgbeLoader = new RGBELoader()
  14. const exrLoader = new EXRLoader()
  15. const textureLoader = new THREE.TextureLoader()
  16. /**
  17. * gui
  18. */
  19. const gui = new GUI()
  20. const global = {
  21. }
  22. // Canvas
  23. const canvas = document.querySelector('canvas.webgl')
  24. /**
  25. * scene
  26. */
  27. const scene = new THREE.Scene()
  28. /*
  29.     Updata all materials
  30. */
  31. const updateAllMaterials = () => {
  32.     scene.traverse((child)=>{ //  遍历
  33.         if(child.isMesh && child.material.isMeshStandardMaterial){
  34.             child.material.envMapIntensity = global.envMapIntensity
  35.         }
  36.     })
  37. }
  38. /*
  39.     environmentMap 环境贴图
  40. */
  41. scene.backgroundBlurriness = 0
  42. scene.backgroundIntensity = 1
  43. // global intensity
  44. global.envMapIntensity = 1 // 环境贴图强度
  45. gui
  46.     .add(global,'envMapIntensity')
  47.     .min(0)
  48.     .max(10)
  49.     .step(0.001)
  50.     .onChange(updateAllMaterials)
  51. gui
  52.     .add(scene, 'backgroundBlurriness')
  53.     .min(0)
  54.     .max(1)
  55.     .step(0.001)
  56. gui
  57.     .add(scene, 'backgroundIntensity')
  58.     .min(0)
  59.     .max(10)
  60.     .step(0.001)
  61.    
  62. // LDR cube texture  环境贴图的实现
  63. const environmentMap = cubeTextrueLoader.load([
  64.     '/environmentMaps/2/px.png',
  65.     '/environmentMaps/2/nx.png',
  66.     '/environmentMaps/2/py.png',
  67.     '/environmentMaps/2/ny.png',
  68.     '/environmentMaps/2/pz.png',
  69.     '/environmentMaps/2/nz.png',
  70. ])
  71. scene.environment = environmentMap  // 可以实现场景中每一个网格材质对于场景起到作用
  72. scene.background = environmentMap // 将环境贴图应用到场景中
  73. /**
  74. * torus knot
  75. */
  76. const torusKnot = new THREE.Mesh(
  77.     new THREE.TorusKnotGeometry(1, 0.4, 100, 16),
  78.     new THREE.MeshStandardMaterial({
  79.         roughness: 0,
  80.         metalness: 1,
  81.         color: 0xaaaaaa
  82.     })
  83. )
  84. torusKnot.position.x = -4
  85. torusKnot.position.y = 4
  86. scene.add(torusKnot)
  87. /*
  88. * Models
  89. */
  90. gltfLoader.load(
  91.     '/models/FlightHelmet/glTF/FlightHelmet.gltf',
  92.     (gltf)=>{
  93.         gltf.scene.scale.set(10,10,10)
  94.         scene.add(gltf.scene)
  95.         updateAllMaterials()
  96.     }
  97. )
  98. /**
  99. * Sizes
  100. */
  101. const sizes = {
  102.     width: window.innerWidth,
  103.     height: window.innerHeight
  104. }
  105. window.addEventListener('resize', () => {
  106.     // Update sizes
  107.     sizes.width = window.innerWidth
  108.     sizes.height = window.innerHeight
  109.     // Update camera
  110.     camera.aspect = sizes.width / sizes.height
  111.     camera.updateProjectionMatrix()
  112.     // Update renderer
  113.     renderer.setSize(sizes.width, sizes.height)
  114.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  115. })
  116. /**
  117. * camera
  118. */
  119. const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  120. camera.position.set(- 8, 4, 8)
  121. scene.add(camera)
  122. /**
  123. * renderer
  124. */
  125. const renderer = new THREE.WebGLRenderer({
  126.     canvas: canvas
  127. })
  128. // renderer.shadowMap.enabled = true
  129. // renderer.shadowMap.type = THREE.PCFSoftShadowMap
  130. renderer.setSize(sizes.width, sizes.height)
  131. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  132. /**
  133. * control
  134. */
  135. const controls = new OrbitControls(camera, canvas)
  136. controls.target.y = 3.5
  137. controls.enableDamping = true
  138. /**
  139. * tick
  140. */
  141. const clock = new THREE.Clock()
  142. const tick = () => {
  143.     // Time
  144.     const elapsedTime = clock.getElapsedTime()
  145.     controls.update()
  146.     renderer.render(scene, camera)
  147.     requestAnimationFrame(tick)
  148. }
  149. tick()
复制代码



2.HDR (RGBA)

  1. import * as THREE from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. import GUI from 'lil-gui'
  4. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
  5. import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js'
  6. import {EXRLoader} from 'three/examples/jsm/loaders/EXRLoader.js'
  7. import { Mapping, Mesh } from 'three'
  8. /*
  9. * Loaders
  10. */
  11. const gltfLoader = new GLTFLoader()
  12. const cubeTextrueLoader = new THREE.CubeTextureLoader()
  13. const rgbeLoader = new RGBELoader()
  14. const exrLoader = new EXRLoader()
  15. const textureLoader = new THREE.TextureLoader()
  16. /**
  17. * gui
  18. */
  19. const gui = new GUI()
  20. const global = {
  21. }
  22. // Canvas
  23. const canvas = document.querySelector('canvas.webgl')
  24. /**
  25. * scene
  26. */
  27. const scene = new THREE.Scene()
  28. /*
  29.     Updata all materials
  30. */
  31. const updateAllMaterials = () => {
  32.     scene.traverse((child)=>{ //  遍历
  33.         if(child.isMesh && child.material.isMeshStandardMaterial){
  34.             child.material.envMapIntensity = global.envMapIntensity
  35.         }
  36.     })
  37. }
  38. /*
  39.     environmentMap 环境贴图
  40. */
  41. scene.backgroundBlurriness = 0
  42. scene.backgroundIntensity = 1
  43. // global intensity
  44. global.envMapIntensity = 1 // 环境贴图强度
  45. gui
  46.     .add(global,'envMapIntensity')
  47.     .min(0)
  48.     .max(10)
  49.     .step(0.001)
  50.     .onChange(updateAllMaterials)
  51. gui
  52.     .add(scene, 'backgroundBlurriness')
  53.     .min(0)
  54.     .max(1)
  55.     .step(0.001)
  56. gui
  57.     .add(scene, 'backgroundIntensity')
  58.     .min(0)
  59.     .max(10)
  60.     .step(0.001)
  61.    
  62. // HDR (RGBA) equirectangular
  63. rgbeLoader.load(
  64.     '/environmentMaps/测试hdr生成全景3.hdr',
  65.     (environmentMap)=>{
  66.         environmentMap.mapping = THREE.EquirectangularReflectionMapping
  67.         scene.background = environmentMap
  68.         scene.environment = environmentMap
  69.     }
  70. )
  71. /**
  72. * torus knot
  73. */
  74. const torusKnot = new THREE.Mesh(
  75.     new THREE.TorusKnotGeometry(1, 0.4, 100, 16),
  76.     new THREE.MeshStandardMaterial({
  77.         roughness: 0,
  78.         metalness: 1,
  79.         color: 0xaaaaaa
  80.     })
  81. )
  82. torusKnot.position.x = -4
  83. torusKnot.position.y = 4
  84. scene.add(torusKnot)
  85. /*
  86. * Models
  87. */
  88. gltfLoader.load(
  89.     '/models/FlightHelmet/glTF/FlightHelmet.gltf',
  90.     (gltf)=>{
  91.         gltf.scene.scale.set(10,10,10)
  92.         scene.add(gltf.scene)
  93.         updateAllMaterials()
  94.     }
  95. )
  96. /**
  97. * Sizes
  98. */
  99. const sizes = {
  100.     width: window.innerWidth,
  101.     height: window.innerHeight
  102. }
  103. window.addEventListener('resize', () => {
  104.     // Update sizes
  105.     sizes.width = window.innerWidth
  106.     sizes.height = window.innerHeight
  107.     // Update camera
  108.     camera.aspect = sizes.width / sizes.height
  109.     camera.updateProjectionMatrix()
  110.     // Update renderer
  111.     renderer.setSize(sizes.width, sizes.height)
  112.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  113. })
  114. /**
  115. * camera
  116. */
  117. const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  118. camera.position.set(- 8, 4, 8)
  119. scene.add(camera)
  120. /**
  121. * renderer
  122. */
  123. const renderer = new THREE.WebGLRenderer({
  124.     canvas: canvas
  125. })
  126. // renderer.shadowMap.enabled = true
  127. // renderer.shadowMap.type = THREE.PCFSoftShadowMap
  128. renderer.setSize(sizes.width, sizes.height)
  129. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  130. /**
  131. * control
  132. */
  133. const controls = new OrbitControls(camera, canvas)
  134. controls.target.y = 3.5
  135. controls.enableDamping = true
  136. /**
  137. * tick
  138. */
  139. const clock = new THREE.Clock()
  140. const tick = () => {
  141.     // Time
  142.     const elapsedTime = clock.getElapsedTime()
  143.     controls.update()
  144.     renderer.render(scene, camera)
  145.     requestAnimationFrame(tick)
  146. }
  147. tick()
复制代码


3.HDR (EXR)

  1. import * as THREE from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. import GUI from 'lil-gui'
  4. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
  5. import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js'
  6. import {EXRLoader} from 'three/examples/jsm/loaders/EXRLoader.js'
  7. import { Mapping, Mesh } from 'three'
  8. /*
  9. * Loaders
  10. */
  11. const gltfLoader = new GLTFLoader()
  12. const cubeTextrueLoader = new THREE.CubeTextureLoader()
  13. const rgbeLoader = new RGBELoader()
  14. const exrLoader = new EXRLoader()
  15. const textureLoader = new THREE.TextureLoader()
  16. /**
  17. * gui
  18. */
  19. const gui = new GUI()
  20. const global = {
  21. }
  22. // Canvas
  23. const canvas = document.querySelector('canvas.webgl')
  24. /**
  25. * scene
  26. */
  27. const scene = new THREE.Scene()
  28. /*
  29.     Updata all materials
  30. */
  31. const updateAllMaterials = () => {
  32.     scene.traverse((child)=>{ //  遍历
  33.         if(child.isMesh && child.material.isMeshStandardMaterial){
  34.             child.material.envMapIntensity = global.envMapIntensity
  35.         }
  36.     })
  37. }
  38. /*
  39.     environmentMap 环境贴图
  40. */
  41. scene.backgroundBlurriness = 0
  42. scene.backgroundIntensity = 1
  43. // global intensity
  44. global.envMapIntensity = 1 // 环境贴图强度
  45. gui
  46.     .add(global,'envMapIntensity')
  47.     .min(0)
  48.     .max(10)
  49.     .step(0.001)
  50.     .onChange(updateAllMaterials)
  51. gui
  52.     .add(scene, 'backgroundBlurriness')
  53.     .min(0)
  54.     .max(1)
  55.     .step(0.001)
  56. gui
  57.     .add(scene, 'backgroundIntensity')
  58.     .min(0)
  59.     .max(10)
  60.     .step(0.001)
  61.    
  62. // HDR (EXR) equirectangular
  63. exrLoader.load('/environmentMaps/nvidiaCanvas-4k.exr',(environmentMap)=>{
  64.     environmentMap.mapping = THREE.EquirectangularReflectionMapping
  65.     scene.background = environmentMap
  66.     scene.environment = environmentMap
  67. })
  68. /**
  69. * torus knot
  70. */
  71. const torusKnot = new THREE.Mesh(
  72.     new THREE.TorusKnotGeometry(1, 0.4, 100, 16),
  73.     new THREE.MeshStandardMaterial({
  74.         roughness: 0,
  75.         metalness: 1,
  76.         color: 0xaaaaaa
  77.     })
  78. )
  79. torusKnot.position.x = -4
  80. torusKnot.position.y = 4
  81. scene.add(torusKnot)
  82. /*
  83. * Models
  84. */
  85. gltfLoader.load(
  86.     '/models/FlightHelmet/glTF/FlightHelmet.gltf',
  87.     (gltf)=>{
  88.         gltf.scene.scale.set(10,10,10)
  89.         scene.add(gltf.scene)
  90.         updateAllMaterials()
  91.     }
  92. )
  93. /**
  94. * Sizes
  95. */
  96. const sizes = {
  97.     width: window.innerWidth,
  98.     height: window.innerHeight
  99. }
  100. window.addEventListener('resize', () => {
  101.     // Update sizes
  102.     sizes.width = window.innerWidth
  103.     sizes.height = window.innerHeight
  104.     // Update camera
  105.     camera.aspect = sizes.width / sizes.height
  106.     camera.updateProjectionMatrix()
  107.     // Update renderer
  108.     renderer.setSize(sizes.width, sizes.height)
  109.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  110. })
  111. /**
  112. * camera
  113. */
  114. const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  115. camera.position.set(- 8, 4, 8)
  116. scene.add(camera)
  117. /**
  118. * renderer
  119. */
  120. const renderer = new THREE.WebGLRenderer({
  121.     canvas: canvas
  122. })
  123. // renderer.shadowMap.enabled = true
  124. // renderer.shadowMap.type = THREE.PCFSoftShadowMap
  125. renderer.setSize(sizes.width, sizes.height)
  126. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  127. /**
  128. * control
  129. */
  130. const controls = new OrbitControls(camera, canvas)
  131. controls.target.y = 3.5
  132. controls.enableDamping = true
  133. /**
  134. * tick
  135. */
  136. const clock = new THREE.Clock()
  137. const tick = () => {
  138.     // Time
  139.     const elapsedTime = clock.getElapsedTime()
  140.     controls.update()
  141.     renderer.render(scene, camera)
  142.     requestAnimationFrame(tick)
  143. }
  144. tick()
复制代码

4.时间环田地图

  1. import * as THREE from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. import GUI from 'lil-gui'
  4. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
  5. import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader.js'
  6. import {EXRLoader} from 'three/examples/jsm/loaders/EXRLoader.js'
  7. import { Mapping, Mesh } from 'three'
  8. /*
  9. * Loaders
  10. */
  11. const gltfLoader = new GLTFLoader()
  12. const cubeTextrueLoader = new THREE.CubeTextureLoader()
  13. const rgbeLoader = new RGBELoader()
  14. const exrLoader = new EXRLoader()
  15. const textureLoader = new THREE.TextureLoader()
  16. /**
  17. * gui
  18. */
  19. const gui = new GUI()
  20. const global = {
  21. }
  22. // Canvas
  23. const canvas = document.querySelector('canvas.webgl')
  24. /**
  25. * scene
  26. */
  27. const scene = new THREE.Scene()
  28. /*
  29.     Updata all materials
  30. */
  31. const updateAllMaterials = () => {
  32.     scene.traverse((child)=>{ //  遍历
  33.         if(child.isMesh && child.material.isMeshStandardMaterial){
  34.             child.material.envMapIntensity = global.envMapIntensity
  35.         }
  36.     })
  37. }
  38. /*
  39.     environmentMap 环境贴图
  40. */
  41. scene.backgroundBlurriness = 0
  42. scene.backgroundIntensity = 1
  43. // global intensity
  44. global.envMapIntensity = 1 // 环境贴图强度
  45. gui
  46.     .add(global,'envMapIntensity')
  47.     .min(0)
  48.     .max(10)
  49.     .step(0.001)
  50.     .onChange(updateAllMaterials)
  51. gui
  52.     .add(scene, 'backgroundBlurriness')
  53.     .min(0)
  54.     .max(1)
  55.     .step(0.001)
  56. gui
  57.     .add(scene, 'backgroundIntensity')
  58.     .min(0)
  59.     .max(10)
  60.     .step(0.001)
  61.    
  62. /*
  63.     Real time environment map
  64. */
  65. const environmentMap = textureLoader.load('/environmentMaps/blockadesLabsSkybox/interior_views_cozy_wood_cabin_with_cauldron_and_p.jpg')
  66. environmentMap.mapping = THREE.EquirectangularReflectionMapping
  67. environmentMap.colorSpace = THREE.SRGBColorSpace
  68. scene.background = environmentMap
  69. /*
  70.     holyDount
  71. */
  72. const holyDonut = new THREE.Mesh(
  73.     new THREE.TorusGeometry(8,0.5),
  74.     new THREE.MeshBasicMaterial({color:new THREE.Color(10, 4, 2)})
  75. )
  76. holyDonut.layers.enable(1) // 分层启用
  77. holyDonut.position.y = 3.5
  78. scene.add(holyDonut)
  79. /*
  80.     cube render target
  81. */
  82. const cubeRenderTarget = new THREE.WebGLCubeRenderTarget(
  83.     256, // 分辨率
  84.     { // 渲染目标的选项
  85.         type:THREE.FloatType  // 类型
  86.     }
  87. )
  88. scene.environment = cubeRenderTarget.texture // 使用自己的纹理
  89. // Cube Camera
  90. const cubeCamera = new THREE.CubeCamera(0.1,100,cubeRenderTarget) // 要每一帧渲染
  91. cubeCamera.layers.set(1) // 设置相机分成
  92. /**
  93. * torus knot
  94. */
  95. const torusKnot = new THREE.Mesh(
  96.     new THREE.TorusKnotGeometry(1, 0.4, 100, 16),
  97.     new THREE.MeshStandardMaterial({
  98.         roughness: 0,
  99.         metalness: 1,
  100.         color: 0xaaaaaa
  101.     })
  102. )
  103. torusKnot.position.x = -4
  104. torusKnot.position.y = 4
  105. scene.add(torusKnot)
  106. /*
  107. * Models
  108. */
  109. gltfLoader.load(
  110.     '/models/FlightHelmet/glTF/FlightHelmet.gltf',
  111.     (gltf)=>{
  112.         gltf.scene.scale.set(10,10,10)
  113.         scene.add(gltf.scene)
  114.         updateAllMaterials()
  115.     }
  116. )
  117. /**
  118. * Sizes
  119. */
  120. const sizes = {
  121.     width: window.innerWidth,
  122.     height: window.innerHeight
  123. }
  124. window.addEventListener('resize', () => {
  125.     // Update sizes
  126.     sizes.width = window.innerWidth
  127.     sizes.height = window.innerHeight
  128.     // Update camera
  129.     camera.aspect = sizes.width / sizes.height
  130.     camera.updateProjectionMatrix()
  131.     // Update renderer
  132.     renderer.setSize(sizes.width, sizes.height)
  133.     renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  134. })
  135. /**
  136. * camera
  137. */
  138. const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
  139. camera.position.set(- 8, 4, 8)
  140. scene.add(camera)
  141. /**
  142. * renderer
  143. */
  144. const renderer = new THREE.WebGLRenderer({
  145.     canvas: canvas
  146. })
  147. // renderer.shadowMap.enabled = true
  148. // renderer.shadowMap.type = THREE.PCFSoftShadowMap
  149. renderer.setSize(sizes.width, sizes.height)
  150. renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
  151. /**
  152. * control
  153. */
  154. const controls = new OrbitControls(camera, canvas)
  155. controls.target.y = 3.5
  156. controls.enableDamping = true
  157. /**
  158. * tick
  159. */
  160. const clock = new THREE.Clock()
  161. const tick = () => {
  162.     // Time
  163.     const elapsedTime = clock.getElapsedTime()
  164.     if(holyDonut){
  165.         holyDonut.rotation.x = Math.sin(elapsedTime) * 2
  166.         cubeCamera.update(renderer, scene)
  167.     }
  168.     controls.update()
  169.     renderer.render(scene, camera)
  170.     requestAnimationFrame(tick)
  171. }
  172. tick()
复制代码
    真实时间环田地图
  
总结

环田地图多种加载方式,以及用法。尚有关于环田地图资源怎样探求,怎么自己在blender制作环田地图等

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

何小豆儿在此

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表