webgpu_reflection.html 5.4 KB

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