webgpu_mirror.html 6.7 KB

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