Viewport.Pathtracer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. const background = scene.background;
  55. if ( background ) {
  56. if ( background.isTexture ) {
  57. ptMaterial.backgroundMap = background;
  58. } else if ( background.isColor ) {
  59. ptMaterial.backgroundMap = buildColorTexture( background );
  60. }
  61. } else {
  62. ptMaterial.backgroundMap = buildColorTexture( new THREE.Color( 0 ) );
  63. }
  64. //
  65. const environment = scene.environment;
  66. if ( environment && environment.isDataTexture === true ) {
  67. // Avoid calling envMapInfo() with the same hdr
  68. if ( scene.environment !== hdr ) {
  69. ptMaterial.envMapInfo.updateFrom( scene.environment );
  70. hdr = scene.environment;
  71. }
  72. } else {
  73. ptMaterial.envMapInfo.updateFrom( buildColorTexture( new THREE.Color( 0 ) ) );
  74. }
  75. }
  76. function setSize( width, height ) {
  77. if ( pathtracer === null ) return;
  78. pathtracer.setSize( width, height );
  79. pathtracer.reset();
  80. }
  81. function update() {
  82. if ( pathtracer === null ) return;
  83. pathtracer.update();
  84. if ( pathtracer.samples >= 1 ) {
  85. renderer.autoClear = false;
  86. quad.render( renderer );
  87. renderer.autoClear = true;
  88. }
  89. }
  90. function reset() {
  91. if ( pathtracer === null ) return;
  92. pathtracer.reset();
  93. }
  94. return {
  95. init: init,
  96. setSize: setSize,
  97. update: update,
  98. reset: reset
  99. };
  100. }
  101. export { ViewportPathtracer };