webgpu_reflection.html 5.9 KB

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