threepipe
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

script.ts 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. _testFinish,
  3. IObject3D,
  4. LinearToneMapping,
  5. LoadingScreenPlugin,
  6. Mesh,
  7. PerspectiveCamera2,
  8. PlaneGeometry,
  9. PopmotionPlugin,
  10. ProgressivePlugin,
  11. Texture,
  12. ThreeViewer,
  13. ToneMapping,
  14. TonemapPlugin,
  15. UnlitMaterial,
  16. VirtualCamerasPlugin,
  17. } from 'threepipe'
  18. async function init() {
  19. const viewer = new ThreeViewer({
  20. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  21. debug: true,
  22. plugins: [new ProgressivePlugin(16), LoadingScreenPlugin],
  23. })
  24. const virtualCameras = viewer.addPluginSync(VirtualCamerasPlugin)
  25. const popmotion = viewer.addPluginSync(PopmotionPlugin)
  26. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  27. await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  28. autoCenter: true,
  29. autoScale: true,
  30. })
  31. const aspect = 2
  32. const plane = new Mesh(
  33. new PlaneGeometry(5 * aspect, 5)
  34. .translate(0, 0, -3),
  35. new UnlitMaterial({
  36. color: '#ffffff',
  37. })
  38. )
  39. plane.castShadow = false
  40. plane.receiveShadow = true
  41. viewer.scene.addObject(plane)
  42. const camera = new PerspectiveCamera2('', viewer.canvas, false, 45, aspect)
  43. camera.position.set(0, 0, 5)
  44. camera.target.set(0, 0.25, 0)
  45. camera.userData.autoLookAtTarget = true
  46. camera.near = 1
  47. camera.far = 10
  48. camera.setDirty()
  49. const vCam = virtualCameras.addCamera(camera)
  50. plane.material.map = vCam.target.texture as Texture
  51. popmotion.animate({
  52. from: 0,
  53. to: 1,
  54. repeat: Infinity,
  55. duration: 6000,
  56. onUpdate: (v)=>{
  57. // Set camera position xz in a circle around the target
  58. const angle = v * Math.PI * 2 + Math.PI / 2
  59. const radius = 5
  60. camera.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius)
  61. camera.setDirty()
  62. viewer.setDirty() // since camera is not in the scene
  63. },
  64. })
  65. // We need to disable tonemapping when rendering the virtual camera, otherwise the tonemapping will be applied multiple times.
  66. let lastTonemapping: ToneMapping = LinearToneMapping
  67. const tonemap = viewer.getPlugin(TonemapPlugin)!
  68. virtualCameras.addEventListener('preRenderCamera', ()=>{
  69. lastTonemapping = tonemap.toneMapping
  70. // Comment this and see what happens to the color in the plane
  71. tonemap.toneMapping = LinearToneMapping
  72. })
  73. virtualCameras.addEventListener('postRenderCamera', ()=>{
  74. tonemap.toneMapping = lastTonemapping
  75. })
  76. }
  77. init().finally(_testFinish)