1
0

webgpu_reflection_blurred.html 7.1 KB

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