webgpu_refraction.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. <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 - refraction
  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 { viewportSafeUV, viewportSharedTexture, screenUV, texture, uv } from 'three/tsl';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. let camera, scene, renderer;
  27. let cameraControls;
  28. let smallSphere;
  29. init();
  30. function init() {
  31. // scene
  32. scene = new THREE.Scene();
  33. // camera
  34. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  35. camera.position.set( 0, 50, 160 );
  36. //
  37. const geometry = new THREE.IcosahedronGeometry( 5, 0 );
  38. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  39. smallSphere = new THREE.Mesh( geometry, material );
  40. scene.add( smallSphere );
  41. // textures
  42. const loader = new THREE.TextureLoader();
  43. const floorNormal = loader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  44. floorNormal.wrapS = THREE.RepeatWrapping;
  45. floorNormal.wrapT = THREE.RepeatWrapping;
  46. // refractor
  47. const verticalNormalScale = 0.1;
  48. const verticalUVOffset = texture( floorNormal, uv().mul( 5 ) ).xy.mul( 2 ).sub( 1 ).mul( verticalNormalScale );
  49. const refractorUV = screenUV.add( verticalUVOffset );
  50. const verticalRefractor = viewportSharedTexture( viewportSafeUV( refractorUV ) );
  51. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  52. const planeRefractor = new THREE.Mesh( planeGeo, new THREE.MeshBasicNodeMaterial( {
  53. backdropNode: verticalRefractor
  54. } ) );
  55. planeRefractor.material.transparent = true;
  56. planeRefractor.position.y = 50;
  57. scene.add( planeRefractor );
  58. // walls
  59. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  60. planeTop.position.y = 100;
  61. planeTop.rotateX( Math.PI / 2 );
  62. scene.add( planeTop );
  63. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  64. planeBottom.rotateX( - Math.PI / 2 );
  65. scene.add( planeBottom );
  66. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  67. planeBack.position.z = - 50;
  68. planeBack.position.y = 50;
  69. scene.add( planeBack );
  70. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  71. planeRight.position.x = 50;
  72. planeRight.position.y = 50;
  73. planeRight.rotateY( - Math.PI / 2 );
  74. scene.add( planeRight );
  75. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  76. planeLeft.position.x = - 50;
  77. planeLeft.position.y = 50;
  78. planeLeft.rotateY( Math.PI / 2 );
  79. scene.add( planeLeft );
  80. // lights
  81. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  82. mainLight.position.y = 60;
  83. scene.add( mainLight );
  84. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  85. greenLight.position.set( 550, 50, 0 );
  86. scene.add( greenLight );
  87. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  88. redLight.position.set( - 550, 50, 0 );
  89. scene.add( redLight );
  90. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  91. blueLight.position.set( 0, 50, 550 );
  92. scene.add( blueLight );
  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. document.body.appendChild( renderer.domElement );
  99. // controls
  100. cameraControls = new OrbitControls( camera, renderer.domElement );
  101. cameraControls.target.set( 0, 50, 0 );
  102. cameraControls.maxDistance = 400;
  103. cameraControls.minDistance = 10;
  104. cameraControls.update();
  105. window.addEventListener( 'resize', onWindowResize );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. const timer = Date.now() * 0.01;
  114. smallSphere.position.set(
  115. Math.cos( timer * 0.1 ) * 30,
  116. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  117. Math.sin( timer * 0.1 ) * 30
  118. );
  119. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  120. smallSphere.rotation.z = timer * 0.8;
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>