webgpu_reflection_blurred.html 6.7 KB

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