webgpu_mirror.html 6.7 KB

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