webgpu_postprocessing_godrays.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing - godrays</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Godrays</span>
  14. </div>
  15. <small>
  16. Screen-space raymarched Godrays.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { pass, uniform, float, int, color } from 'three/tsl';
  32. import { godrays } from 'three/addons/tsl/display/GodraysNode.js';
  33. import { bilateralBlur } from 'three/addons/tsl/display/BilateralBlurNode.js';
  34. import { depthAwareBlend } from 'three/addons/tsl/display/depthAwareBlend.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let camera, controls, scene, renderer, renderPipeline;
  39. const params = {
  40. enabledBlur: true,
  41. };
  42. init();
  43. async function init() {
  44. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
  45. camera.position.set( - 175, 50, 0 );
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x000000 );
  48. // asset
  49. const loader = new GLTFLoader();
  50. const gltf = await loader.loadAsync( 'models/gltf/godrays_demo.glb' );
  51. scene.add( gltf.scene );
  52. const pillars = gltf.scene.getObjectByName( 'concrete' );
  53. pillars.material = new THREE.MeshStandardMaterial( {
  54. color: 0x333333,
  55. } );
  56. const base = gltf.scene.getObjectByName( 'base' );
  57. base.material = new THREE.MeshStandardMaterial( {
  58. color: 0x333333,
  59. side: THREE.DoubleSide,
  60. } );
  61. // lights
  62. const lightPos = new THREE.Vector3( 0, 50, 0 );
  63. const lightSphereMaterial = new THREE.MeshBasicMaterial( {
  64. color: 0xffffff,
  65. } );
  66. const lightSphere = new THREE.Mesh( new THREE.SphereGeometry( 0.5, 16, 16 ), lightSphereMaterial );
  67. lightSphere.position.copy( lightPos );
  68. scene.add( lightSphere );
  69. scene.add( new THREE.AmbientLight( 0xcccccc, 0.4 ) );
  70. const pointLight = new THREE.PointLight( 0xf6287d, 10000 );
  71. pointLight.castShadow = true;
  72. pointLight.shadow.bias = - 0.00001;
  73. pointLight.shadow.mapSize.width = 2048;
  74. pointLight.shadow.mapSize.height = 2048;
  75. pointLight.position.copy( lightPos );
  76. scene.add( pointLight );
  77. setupBackdrop();
  78. // shadow setup
  79. scene.traverse( obj => {
  80. if ( obj.isMesh === true ) {
  81. obj.castShadow = true;
  82. obj.receiveShadow = true;
  83. }
  84. } );
  85. lightSphere.castShadow = false;
  86. lightSphere.receiveShadow = false;
  87. // renderer
  88. renderer = new THREE.WebGPURenderer();
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. renderer.setAnimationLoop( animate );
  92. renderer.inspector = new Inspector();
  93. renderer.shadowMap.enabled = true;
  94. document.body.appendChild( renderer.domElement );
  95. // post processing
  96. renderPipeline = new THREE.RenderPipeline( renderer );
  97. // beauty
  98. const scenePass = pass( scene, camera );
  99. const scenePassColor = scenePass.getTextureNode( 'output' );
  100. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  101. // godrays
  102. const godraysPass = godrays( scenePassDepth, camera, pointLight );
  103. const godraysPassColor = godraysPass.getTextureNode();
  104. // blur
  105. const blurPass = bilateralBlur( godraysPassColor );
  106. const blurPassColor = blurPass.getTextureNode();
  107. // composite
  108. const blendColor = uniform( color( 0xf6287d ) );
  109. const edgeRadius = uniform( int( 2 ) );
  110. const edgeStrength = uniform( float( 2 ) );
  111. const outputBlurred = depthAwareBlend( scenePassColor, blurPassColor, scenePassDepth, camera, { blendColor, edgeRadius, edgeStrength } );
  112. const outputRaw = depthAwareBlend( scenePassColor, godraysPassColor, scenePassDepth, camera, { blendColor, edgeRadius, edgeStrength } );
  113. renderPipeline.outputNode = outputBlurred;
  114. // GUI
  115. const gui = renderer.inspector.createParameters( 'Settings' );
  116. const godraysFolder = gui.addFolder( 'Godrays' );
  117. godraysFolder.add( godraysPass.raymarchSteps, 'value', 24, 120 ).step( 1 ).name( 'raymarch steps' );
  118. godraysFolder.add( godraysPass.density, 'value', 0, 1 ).name( 'density' );
  119. godraysFolder.add( godraysPass.maxDensity, 'value', 0, 1 ).name( 'max density' );
  120. godraysFolder.add( godraysPass.distanceAttenuation, 'value', 0, 5 ).name( 'distance attenuation' );
  121. const compositeFolder = gui.addFolder( 'composite' );
  122. compositeFolder.add( edgeRadius, 'value', 0, 5, 1 ).name( 'edge radius' );
  123. compositeFolder.add( edgeStrength, 'value', 0, 5 ).name( 'edge strength' );
  124. const blurFolder = gui.addFolder( 'blur' );
  125. blurFolder.add( params, 'enabledBlur' ).name( 'enabled' ).onChange( ( value ) => {
  126. if ( value === true ) {
  127. renderPipeline.outputNode = outputBlurred;
  128. } else {
  129. renderPipeline.outputNode = outputRaw;
  130. }
  131. renderPipeline.needsUpdate = true;
  132. } );
  133. //
  134. controls = new OrbitControls( camera, renderer.domElement );
  135. controls.target.set( 0, 0.5, 0 );
  136. controls.enableDamping = true;
  137. controls.maxDistance = 200;
  138. controls.update();
  139. window.addEventListener( 'resize', onWindowResize );
  140. }
  141. function setupBackdrop() {
  142. const backdropDistance = 200;
  143. // Add backdrop walls `backdropDistance` units away from the origin
  144. const backdropGeometry = new THREE.PlaneGeometry( 400, 200 );
  145. const backdropMaterial = new THREE.MeshBasicMaterial( {
  146. color: 0x000000,
  147. side: THREE.DoubleSide,
  148. } );
  149. const backdropLeft = new THREE.Mesh( backdropGeometry, backdropMaterial );
  150. backdropLeft.position.set( - backdropDistance, 100, 0 );
  151. backdropLeft.rotateY( Math.PI / 2 );
  152. scene.add( backdropLeft );
  153. const backdropRight = new THREE.Mesh( backdropGeometry, backdropMaterial );
  154. backdropRight.position.set( backdropDistance, 100, 0 );
  155. backdropRight.rotateY( Math.PI / 2 );
  156. scene.add( backdropRight );
  157. const backdropFront = new THREE.Mesh( backdropGeometry, backdropMaterial );
  158. backdropFront.position.set( 0, 100, - backdropDistance );
  159. scene.add( backdropFront );
  160. const backdropBack = new THREE.Mesh( backdropGeometry, backdropMaterial );
  161. backdropBack.position.set( 0, 100, backdropDistance );
  162. scene.add( backdropBack );
  163. const backdropTop = new THREE.Mesh( backdropGeometry, backdropMaterial );
  164. backdropTop.position.set( 0, 200, 0 );
  165. backdropTop.rotateX( Math.PI / 2 );
  166. backdropTop.scale.set( 3, 6, 1 );
  167. scene.add( backdropTop );
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. function animate() {
  175. controls.update();
  176. renderPipeline.render();
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号