1
0

webgpu_mirror.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - mirror</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 - mirror
  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 { reflector, uv, texture, color } from 'three/tsl';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. let camera, scene, renderer;
  27. let cameraControls;
  28. let sphereGroup, 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, 75, 160 );
  36. //
  37. let geometry, material;
  38. //
  39. sphereGroup = new THREE.Object3D();
  40. scene.add( sphereGroup );
  41. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  42. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x8d8d8d } );
  43. const sphereCap = new THREE.Mesh( geometry, material );
  44. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  45. sphereCap.rotateX( - Math.PI );
  46. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  47. const halfSphere = new THREE.Mesh( geometry, material );
  48. halfSphere.add( sphereCap );
  49. halfSphere.rotateX( - Math.PI / 180 * 135 );
  50. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  51. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  52. sphereGroup.add( halfSphere );
  53. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  54. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  55. smallSphere = new THREE.Mesh( geometry, material );
  56. scene.add( smallSphere );
  57. // textures
  58. const textureLoader = new THREE.TextureLoader();
  59. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  60. floorNormal.wrapS = THREE.RepeatWrapping;
  61. floorNormal.wrapT = THREE.RepeatWrapping;
  62. const decalDiffuse = textureLoader.load( 'textures/decal/decal-diffuse.png' );
  63. decalDiffuse.colorSpace = THREE.SRGBColorSpace;
  64. const decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
  65. // reflectors / mirrors
  66. const groundReflector = reflector();
  67. const verticalReflector = reflector();
  68. const groundNormalScale = - 0.08;
  69. const verticalNormalScale = 0.1;
  70. const groundUVOffset = texture( decalNormal ).xy.mul( 2 ).sub( 1 ).mul( groundNormalScale );
  71. const verticalUVOffset = texture( floorNormal, uv().mul( 5 ) ).xy.mul( 2 ).sub( 1 ).mul( verticalNormalScale );
  72. groundReflector.uvNode = groundReflector.uvNode.add( groundUVOffset );
  73. verticalReflector.uvNode = verticalReflector.uvNode.add( verticalUVOffset );
  74. const groundNode = texture( decalDiffuse ).a.mix( color( 0xffffff ), groundReflector );
  75. const verticalNode = color( 0x0000ff ).mul( .1 ).add( verticalReflector );
  76. // walls
  77. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  78. //
  79. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongNodeMaterial( {
  80. colorNode: groundNode
  81. } ) );
  82. planeBottom.rotateX( - Math.PI / 2 );
  83. planeBottom.add( groundReflector.target );
  84. scene.add( planeBottom );
  85. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongNodeMaterial( {
  86. colorNode: verticalNode
  87. } ) );
  88. planeBack.position.z = - 50;
  89. planeBack.position.y = 50;
  90. planeBack.add( verticalReflector.target );
  91. scene.add( planeBack );
  92. //
  93. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  94. planeTop.position.y = 100;
  95. planeTop.rotateX( Math.PI / 2 );
  96. scene.add( planeTop );
  97. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  98. planeFront.position.z = 50;
  99. planeFront.position.y = 50;
  100. planeFront.rotateY( Math.PI );
  101. scene.add( planeFront );
  102. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  103. planeRight.position.x = 50;
  104. planeRight.position.y = 50;
  105. planeRight.rotateY( - Math.PI / 2 );
  106. scene.add( planeRight );
  107. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  108. planeLeft.position.x = - 50;
  109. planeLeft.position.y = 50;
  110. planeLeft.rotateY( Math.PI / 2 );
  111. scene.add( planeLeft );
  112. // lights
  113. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  114. mainLight.position.y = 60;
  115. scene.add( mainLight );
  116. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  117. greenLight.position.set( 550, 50, 0 );
  118. scene.add( greenLight );
  119. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  120. redLight.position.set( - 550, 50, 0 );
  121. scene.add( redLight );
  122. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  123. blueLight.position.set( 0, 50, 550 );
  124. scene.add( blueLight );
  125. // renderer
  126. renderer = new THREE.WebGPURenderer( { antialias: true } );
  127. renderer.setPixelRatio( window.devicePixelRatio );
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. renderer.setAnimationLoop( animate );
  130. document.body.appendChild( renderer.domElement );
  131. // controls
  132. cameraControls = new OrbitControls( camera, renderer.domElement );
  133. cameraControls.target.set( 0, 40, 0 );
  134. cameraControls.maxDistance = 400;
  135. cameraControls.minDistance = 10;
  136. cameraControls.update();
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. function animate() {
  145. const timer = Date.now() * 0.01;
  146. sphereGroup.rotation.y -= 0.002;
  147. smallSphere.position.set(
  148. Math.cos( timer * 0.1 ) * 30,
  149. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  150. Math.sin( timer * 0.1 ) * 30
  151. );
  152. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  153. smallSphere.rotation.z = timer * 0.8;
  154. renderer.render( scene, camera );
  155. }
  156. </script>
  157. </body>
  158. </html>