webgpu_reflection.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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 - reflection
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { color, pass, reflector, normalWorld, texture, uv, screenUV } from 'three/tsl';
  25. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. let camera, scene, renderer;
  30. let model, mixer, clock;
  31. let postProcessing;
  32. let controls;
  33. let stats;
  34. init();
  35. function init() {
  36. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  37. camera.position.set( 2, 2.5, 3 );
  38. scene = new THREE.Scene();
  39. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  40. scene.backgroundNode = normalWorld.y.mix( color( 0x0487e2 ), color( 0x0066ff ) );
  41. camera.lookAt( 0, 1, 0 );
  42. const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
  43. sunLight.castShadow = true;
  44. sunLight.shadow.camera.near = .1;
  45. sunLight.shadow.camera.far = 5;
  46. sunLight.shadow.camera.right = 2;
  47. sunLight.shadow.camera.left = - 2;
  48. sunLight.shadow.camera.top = 2;
  49. sunLight.shadow.camera.bottom = - 2;
  50. sunLight.shadow.mapSize.width = 2048;
  51. sunLight.shadow.mapSize.height = 2048;
  52. sunLight.shadow.bias = - 0.001;
  53. sunLight.position.set( .5, 3, .5 );
  54. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  55. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  56. scene.add( sunLight );
  57. scene.add( skyAmbientLight );
  58. scene.add( waterAmbientLight );
  59. clock = new THREE.Clock();
  60. // animated model
  61. const loader = new GLTFLoader();
  62. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  63. model = gltf.scene;
  64. model.children[ 0 ].children[ 0 ].castShadow = true;
  65. mixer = new THREE.AnimationMixer( model );
  66. const action = mixer.clipAction( gltf.animations[ 0 ] );
  67. action.play();
  68. scene.add( model );
  69. } );
  70. // textures
  71. const textureLoader = new THREE.TextureLoader();
  72. const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  73. floorColor.wrapS = THREE.RepeatWrapping;
  74. floorColor.wrapT = THREE.RepeatWrapping;
  75. floorColor.colorSpace = THREE.SRGBColorSpace;
  76. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  77. floorNormal.wrapS = THREE.RepeatWrapping;
  78. floorNormal.wrapT = THREE.RepeatWrapping;
  79. // floor
  80. const floorUV = uv().mul( 15 );
  81. const floorNormalOffset = texture( floorNormal, floorUV ).xy.mul( 2 ).sub( 1 ).mul( .02 );
  82. const reflection = reflector( { resolution: 0.5 } ); // 0.5 is half of the rendering view
  83. reflection.target.rotateX( - Math.PI / 2 );
  84. reflection.uvNode = reflection.uvNode.add( floorNormalOffset );
  85. scene.add( reflection.target );
  86. const floorMaterial = new THREE.MeshPhongNodeMaterial();
  87. floorMaterial.colorNode = texture( floorColor, floorUV ).add( reflection );
  88. const floor = new THREE.Mesh( new THREE.BoxGeometry( 50, .001, 50 ), floorMaterial );
  89. floor.receiveShadow = true;
  90. floor.position.set( 0, 0, 0 );
  91. scene.add( floor );
  92. // renderer
  93. renderer = new THREE.WebGPURenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.setAnimationLoop( animate );
  97. renderer.shadowMap.enabled = true;
  98. document.body.appendChild( renderer.domElement );
  99. stats = new Stats();
  100. document.body.appendChild( stats.dom );
  101. controls = new OrbitControls( camera, renderer.domElement );
  102. controls.minDistance = 1;
  103. controls.maxDistance = 10;
  104. controls.maxPolarAngle = Math.PI / 2;
  105. controls.autoRotate = true;
  106. controls.autoRotateSpeed = 1;
  107. controls.target.set( 0, .5, 0 );
  108. controls.update();
  109. // post-processing
  110. const scenePass = pass( scene, camera );
  111. const scenePassColor = scenePass.getTextureNode();
  112. const scenePassDepth = scenePass.getLinearDepthNode().remapClamp( .3, .5 );
  113. const scenePassColorBlurred = gaussianBlur( scenePassColor );
  114. scenePassColorBlurred.directionNode = scenePassDepth;
  115. const vignet = screenUV.distance( .5 ).mul( 1.35 ).clamp().oneMinus();
  116. postProcessing = new THREE.PostProcessing( renderer );
  117. postProcessing.outputNode = scenePassColorBlurred.mul( vignet );
  118. //
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function animate() {
  127. stats.update();
  128. controls.update();
  129. const delta = clock.getDelta();
  130. if ( model ) {
  131. mixer.update( delta );
  132. }
  133. postProcessing.render();
  134. }
  135. </script>
  136. </body>
  137. </html>