webgpu_refraction.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - refraction</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. <meta property="og:title" content="three.js webgpu - refraction">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_refraction.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_refraction.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Refraction</span>
  18. </div>
  19. <small>
  20. Refraction using backdrop.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { viewportSafeUV, viewportSharedTexture, screenUV, texture, uv } from 'three/tsl';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. let camera, scene, renderer;
  39. let cameraControls;
  40. let smallSphere;
  41. init();
  42. function init() {
  43. // scene
  44. scene = new THREE.Scene();
  45. // camera
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  47. camera.position.set( 0, 50, 160 );
  48. //
  49. const geometry = new THREE.IcosahedronGeometry( 5, 0 );
  50. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  51. smallSphere = new THREE.Mesh( geometry, material );
  52. scene.add( smallSphere );
  53. // textures
  54. const loader = new THREE.TextureLoader();
  55. const floorNormal = loader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  56. floorNormal.wrapS = THREE.RepeatWrapping;
  57. floorNormal.wrapT = THREE.RepeatWrapping;
  58. // refractor
  59. const verticalNormalScale = 0.1;
  60. const verticalUVOffset = texture( floorNormal, uv().mul( 5 ) ).xy.mul( 2 ).sub( 1 ).mul( verticalNormalScale );
  61. const refractorUV = screenUV.add( verticalUVOffset );
  62. const verticalRefractor = viewportSharedTexture( viewportSafeUV( refractorUV ) ).toInspector( 'Viewport Texture' );
  63. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  64. const planeRefractor = new THREE.Mesh( planeGeo, new THREE.MeshBasicNodeMaterial( {
  65. backdropNode: verticalRefractor
  66. } ) );
  67. planeRefractor.material.transparent = true;
  68. planeRefractor.position.y = 50;
  69. scene.add( planeRefractor );
  70. // walls
  71. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  72. planeTop.position.y = 100;
  73. planeTop.rotateX( Math.PI / 2 );
  74. scene.add( planeTop );
  75. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  76. planeBottom.rotateX( - Math.PI / 2 );
  77. scene.add( planeBottom );
  78. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  79. planeBack.position.z = - 50;
  80. planeBack.position.y = 50;
  81. scene.add( planeBack );
  82. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  83. planeRight.position.x = 50;
  84. planeRight.position.y = 50;
  85. planeRight.rotateY( - Math.PI / 2 );
  86. scene.add( planeRight );
  87. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  88. planeLeft.position.x = - 50;
  89. planeLeft.position.y = 50;
  90. planeLeft.rotateY( Math.PI / 2 );
  91. scene.add( planeLeft );
  92. // lights
  93. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  94. mainLight.position.y = 60;
  95. scene.add( mainLight );
  96. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  97. greenLight.position.set( 550, 50, 0 );
  98. scene.add( greenLight );
  99. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  100. redLight.position.set( - 550, 50, 0 );
  101. scene.add( redLight );
  102. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  103. blueLight.position.set( 0, 50, 550 );
  104. scene.add( blueLight );
  105. // renderer
  106. renderer = new THREE.WebGPURenderer( /*{ antialias: true }*/ );
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.setAnimationLoop( animate );
  110. renderer.inspector = new Inspector();
  111. document.body.appendChild( renderer.domElement );
  112. // controls
  113. cameraControls = new OrbitControls( camera, renderer.domElement );
  114. cameraControls.target.set( 0, 50, 0 );
  115. cameraControls.maxDistance = 400;
  116. cameraControls.minDistance = 10;
  117. cameraControls.update();
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function animate() {
  126. const timer = Date.now() * 0.01;
  127. smallSphere.position.set(
  128. Math.cos( timer * 0.1 ) * 30,
  129. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  130. Math.sin( timer * 0.1 ) * 30
  131. );
  132. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  133. smallSphere.rotation.z = timer * 0.8;
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>
粤ICP备19079148号