webgpu_reflection.html 5.6 KB

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