Viewport.Pathtracer.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import * as THREE from 'three';
  2. import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
  3. import {
  4. PathTracingSceneGenerator,
  5. PathTracingRenderer,
  6. PhysicalPathTracingMaterial,
  7. ProceduralEquirectTexture,
  8. } from 'three-gpu-pathtracer';
  9. function buildColorTexture( color ) {
  10. const texture = new ProceduralEquirectTexture( 4, 4 );
  11. texture.generationCallback = ( polar, uv, coord, target ) => {
  12. target.copy( color );
  13. };
  14. texture.update();
  15. return texture;
  16. }
  17. function ViewportPathtracer( renderer ) {
  18. let generator = null;
  19. let pathtracer = null;
  20. let quad = null;
  21. let hdr = null;
  22. function init( scene, camera ) {
  23. if ( pathtracer === null ) {
  24. generator = new PathTracingSceneGenerator();
  25. pathtracer = new PathTracingRenderer( renderer );
  26. pathtracer.setSize( renderer.domElement.offsetWidth, renderer.domElement.offsetHeight );
  27. pathtracer.alpha = true;
  28. pathtracer.camera = camera;
  29. pathtracer.material = new PhysicalPathTracingMaterial();
  30. pathtracer.tiles.set( 3, 4 );
  31. quad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
  32. map: pathtracer.target.texture,
  33. blending: THREE.CustomBlending
  34. } ) );
  35. }
  36. pathtracer.material.backgroundBlur = scene.backgroundBlurriness;
  37. pathtracer.reset();
  38. const { bvh, textures, materials, lights } = generator.generate( scene );
  39. const ptGeometry = bvh.geometry;
  40. const ptMaterial = pathtracer.material;
  41. ptMaterial.bvh.updateFrom( bvh );
  42. ptMaterial.attributesArray.updateFrom(
  43. ptGeometry.attributes.normal,
  44. ptGeometry.attributes.tangent,
  45. ptGeometry.attributes.uv,
  46. ptGeometry.attributes.color,
  47. );
  48. ptMaterial.materialIndexAttribute.updateFrom( ptGeometry.attributes.materialIndex );
  49. ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
  50. ptMaterial.materials.updateFrom( materials, textures );
  51. ptMaterial.lights.updateFrom( lights );
  52. ptMaterial.filterGlossyFactor = 0.5;
  53. //
  54. setBackground( scene.background );
  55. //
  56. setEnvironment( scene.environment );
  57. }
  58. function setSize( width, height ) {
  59. if ( pathtracer === null ) return;
  60. pathtracer.setSize( width, height );
  61. pathtracer.reset();
  62. }
  63. function setBackground( background ) {
  64. if ( pathtracer === null ) return;
  65. const ptMaterial = pathtracer.material;
  66. if ( background ) {
  67. if ( background.isTexture ) {
  68. ptMaterial.backgroundMap = background;
  69. } else if ( background.isColor ) {
  70. ptMaterial.backgroundMap = buildColorTexture( background );
  71. }
  72. } else {
  73. ptMaterial.backgroundMap = buildColorTexture( new THREE.Color( 0 ) );
  74. }
  75. pathtracer.reset();
  76. }
  77. function setEnvironment( environment ) {
  78. if ( pathtracer === null ) return;
  79. const ptMaterial = pathtracer.material;
  80. if ( environment && environment.isDataTexture === true ) {
  81. // Avoid calling envMapInfo() with the same hdr
  82. if ( environment !== hdr ) {
  83. ptMaterial.envMapInfo.updateFrom( environment );
  84. hdr = environment;
  85. }
  86. } else {
  87. ptMaterial.envMapInfo.updateFrom( buildColorTexture( new THREE.Color( 0 ) ) );
  88. }
  89. pathtracer.reset();
  90. }
  91. function update() {
  92. if ( pathtracer === null ) return;
  93. pathtracer.update();
  94. if ( pathtracer.samples >= 1 ) {
  95. renderer.autoClear = false;
  96. quad.render( renderer );
  97. renderer.autoClear = true;
  98. }
  99. }
  100. function reset() {
  101. if ( pathtracer === null ) return;
  102. pathtracer.reset();
  103. }
  104. return {
  105. init: init,
  106. setSize: setSize,
  107. setBackground: setBackground,
  108. setEnvironment: setEnvironment,
  109. update: update,
  110. reset: reset
  111. };
  112. }
  113. export { ViewportPathtracer };