1
0

webgpu_refraction.html 5.1 KB

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