webgpu_reflection_blurred.html 7.1 KB

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