webgpu_refraction.html 5.4 KB

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