webgpu_reflection_blurred.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - blurred reflection</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. <meta property="og:title" content="three.js webgpu - blurred reflection">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_reflection_blurred.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_reflection_blurred.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Blurred Reflection</span>
  18. </div>
  19. <small>
  20. Reflection with a blurred effect using a hash blur algorithm.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { Fn, vec4, fract, sample, abs, uniform, pow, color, max, length, rangeFogFactor, sub, reflector, normalWorld, hue, time, mix, positionWorld } from 'three/tsl';
  36. import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { Inspector } from 'three/addons/inspector/Inspector.js';
  40. let camera, scene, renderer;
  41. let model, mixer, timer;
  42. let controls;
  43. let gui;
  44. init();
  45. async function init() {
  46. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  47. camera.position.set( - 2.5, 2, 2.5 );
  48. camera.lookAt( 0, .4, 0 );
  49. scene = new THREE.Scene();
  50. scene.backgroundNode = hue( normalWorld.y.mix( 0, color( 0x0066ff ) ).mul( .1 ), time );
  51. const waterAmbientLight = new THREE.HemisphereLight( 0xffffff, 0x0066ff, 10 );
  52. scene.add( waterAmbientLight );
  53. timer = new THREE.Timer();
  54. timer.connect( document );
  55. // animated model
  56. const gltfLoader = new GLTFLoader();
  57. gltfLoader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  58. model = gltf.scene;
  59. model.children[ 0 ].children[ 0 ].castShadow = true;
  60. mixer = new THREE.AnimationMixer( model );
  61. const action = mixer.clipAction( gltf.animations[ 0 ] );
  62. action.play();
  63. scene.add( model );
  64. } );
  65. // textures
  66. const textureLoader = new THREE.TextureLoader();
  67. const uvMap = textureLoader.load( 'textures/uv_grid_directx.jpg' );
  68. uvMap.colorSpace = THREE.SRGBColorSpace;
  69. // uv map for debugging
  70. const uvMaterial = new THREE.MeshStandardNodeMaterial( {
  71. map: uvMap,
  72. side: THREE.DoubleSide
  73. } );
  74. const uvMesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), uvMaterial );
  75. uvMesh.position.set( 0, 1, - 3 );
  76. scene.add( uvMesh );
  77. // circle effect
  78. const drawCircle = Fn( ( [ pos, radius, width, power, color, timer = time.mul( .5 ) ] ) => {
  79. // https://www.shadertoy.com/view/3tdSRn
  80. const dist1 = length( pos );
  81. dist1.assign( fract( dist1.mul( 5.0 ).sub( fract( timer ) ) ) );
  82. const dist2 = dist1.sub( radius );
  83. const intensity = pow( radius.div( abs( dist2 ) ), width );
  84. const col = color.rgb.mul( intensity ).mul( power ).mul( max( sub( 0.8, abs( dist2 ) ), 0.0 ) );
  85. return col;
  86. } );
  87. const circleFadeY = positionWorld.y.mul( .7 ).oneMinus().max( 0 );
  88. const animatedColor = mix( color( 0x74ccf4 ), color( 0x7f00c5 ), positionWorld.xz.distance( 0 ).div( 10 ).clamp() );
  89. const animatedCircle = hue( drawCircle( positionWorld.xz.mul( .1 ), 0.5, 0.8, .01, animatedColor ).mul( circleFadeY ), time );
  90. const floorLight = new THREE.PointLight( 0xffffff );
  91. floorLight.colorNode = animatedCircle.mul( 50 );
  92. scene.add( floorLight );
  93. // reflection
  94. const roughness = uniform( .9 );
  95. const radius = uniform( 0.2 );
  96. const reflection = reflector( { resolutionScale: .5, depth: true, bounces: false } ); // 0.5 is half of the rendering view
  97. const reflectionDepth = reflection.getDepthNode();
  98. reflection.target.rotateX( - Math.PI / 2 );
  99. scene.add( reflection.target );
  100. const floorMaterial = new THREE.MeshStandardNodeMaterial();
  101. floorMaterial.transparent = true;
  102. floorMaterial.colorNode = Fn( () => {
  103. // ranges adjustment
  104. const radiusRange = mix( 0.01, 0.1, radius ); // range [ 0.01, 0.1 ]
  105. const roughnessRange = mix( 0.3, 0.03, roughness ); // range [ 0.03, 0.3 ]
  106. // mask the sample
  107. const maskReflection = sample( ( uv ) => {
  108. const sample = reflection.sample( uv );
  109. const mask = reflectionDepth.sample( uv );
  110. return vec4( sample.rgb, sample.a.mul( mask.r ) );
  111. }, reflection.uvNode );
  112. // blur the reflection
  113. const reflectionBlurred = hashBlur( maskReflection, radiusRange, {
  114. repeats: 40,
  115. premultipliedAlpha: true
  116. } );
  117. // reflection composite
  118. const reflectionMask = reflectionBlurred.a.mul( reflectionDepth ).remapClamp( 0, roughnessRange );
  119. const reflectionIntensity = .1;
  120. const reflectionMixFactor = reflectionMask.mul( roughness.mul( 2 ).min( 1 ) );
  121. const reflectionFinal = mix( reflection.rgb, reflectionBlurred.rgb, reflectionMixFactor ).mul( reflectionIntensity );
  122. // mix reflection with animated circle
  123. const output = animatedCircle.add( reflectionFinal );
  124. // falloff opacity by distance like an opacity-fog
  125. const opacity = rangeFogFactor( 7, 25 ).oneMinus();
  126. // final output
  127. return vec4( output, opacity );
  128. } )();
  129. const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
  130. floor.position.set( 0, 0, 0 );
  131. scene.add( floor );
  132. // renderer
  133. renderer = new THREE.WebGPURenderer( { antialias: true } );
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. renderer.setAnimationLoop( animate );
  137. renderer.toneMapping = THREE.NeutralToneMapping;
  138. renderer.toneMappingExposure = 1.3;
  139. renderer.inspector = new Inspector();
  140. document.body.appendChild( renderer.domElement );
  141. gui = renderer.inspector.createParameters( 'Settings' );
  142. gui.add( roughness, 'value', 0, 1 ).name( 'roughness' );
  143. gui.add( radius, 'value', 0, 1 ).name( 'radius' );
  144. gui.add( reflection.reflector, 'resolutionScale', .25, 1 ).name( 'resolution scale' );
  145. controls = new OrbitControls( camera, renderer.domElement );
  146. controls.minDistance = 1;
  147. controls.maxDistance = 10;
  148. controls.maxPolarAngle = Math.PI / 2;
  149. //controls.autoRotate = true;
  150. controls.autoRotateSpeed = - .1;
  151. controls.target.set( 0, .5, 0 );
  152. controls.update();
  153. // events
  154. window.addEventListener( 'resize', onWindowResize );
  155. }
  156. function onWindowResize() {
  157. camera.aspect = window.innerWidth / window.innerHeight;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  160. }
  161. function animate() {
  162. timer.update();
  163. controls.update();
  164. const delta = timer.getDelta();
  165. if ( model ) {
  166. mixer.update( delta );
  167. }
  168. renderer.render( scene, camera );
  169. }
  170. </script>
  171. </body>
  172. </html>
粤ICP备19079148号