1
0

webgpu_mirror.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. <meta property="og:title" content="three.js webgpu - mirror">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_mirror.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_mirror.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Mirror</span>
  18. </div>
  19. <small>Mirror reflections using procedural effects.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { reflector, uv, texture, color } from 'three/tsl';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. let camera, scene, renderer;
  37. let cameraControls;
  38. let sphereGroup, smallSphere;
  39. init();
  40. function init() {
  41. // scene
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0x000000 );
  44. // camera
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  46. camera.position.set( 0, 75, 160 );
  47. //
  48. let geometry, material;
  49. //
  50. sphereGroup = new THREE.Object3D();
  51. scene.add( sphereGroup );
  52. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  53. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x8d8d8d } );
  54. const sphereCap = new THREE.Mesh( geometry, material );
  55. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  56. sphereCap.rotateX( - Math.PI );
  57. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  58. const halfSphere = new THREE.Mesh( geometry, material );
  59. halfSphere.add( sphereCap );
  60. halfSphere.rotateX( - Math.PI / 180 * 135 );
  61. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  62. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  63. sphereGroup.add( halfSphere );
  64. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  65. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  66. smallSphere = new THREE.Mesh( geometry, material );
  67. scene.add( smallSphere );
  68. // textures
  69. const textureLoader = new THREE.TextureLoader();
  70. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  71. floorNormal.wrapS = THREE.RepeatWrapping;
  72. floorNormal.wrapT = THREE.RepeatWrapping;
  73. const decalDiffuse = textureLoader.load( 'textures/decal/decal-diffuse.png' );
  74. decalDiffuse.colorSpace = THREE.SRGBColorSpace;
  75. const decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );
  76. // reflectors / mirrors
  77. const groundReflector = reflector().toInspector( 'Ground Reflector' );
  78. const verticalReflector = reflector().toInspector( 'Vertical Reflector' );
  79. const groundNormalScale = - 0.08;
  80. const verticalNormalScale = 0.1;
  81. const groundUVOffset = texture( decalNormal ).xy.mul( 2 ).sub( 1 ).mul( groundNormalScale );
  82. const verticalUVOffset = texture( floorNormal, uv().mul( 5 ) ).xy.mul( 2 ).sub( 1 ).mul( verticalNormalScale );
  83. groundReflector.uvNode = groundReflector.uvNode.add( groundUVOffset );
  84. verticalReflector.uvNode = verticalReflector.uvNode.add( verticalUVOffset );
  85. const groundNode = texture( decalDiffuse ).a.mix( color( 0xffffff ), groundReflector );
  86. const verticalNode = color( 0x0000ff ).mul( .1 ).add( verticalReflector );
  87. // walls
  88. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  89. //
  90. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongNodeMaterial( {
  91. colorNode: groundNode
  92. } ) );
  93. planeBottom.rotateX( - Math.PI / 2 );
  94. planeBottom.add( groundReflector.target );
  95. scene.add( planeBottom );
  96. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongNodeMaterial( {
  97. colorNode: verticalNode
  98. } ) );
  99. planeBack.position.z = - 50;
  100. planeBack.position.y = 50;
  101. planeBack.add( verticalReflector.target );
  102. scene.add( planeBack );
  103. //
  104. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  105. planeTop.position.y = 100;
  106. planeTop.rotateX( Math.PI / 2 );
  107. scene.add( planeTop );
  108. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  109. planeFront.position.z = 50;
  110. planeFront.position.y = 50;
  111. planeFront.rotateY( Math.PI );
  112. scene.add( planeFront );
  113. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  114. planeRight.position.x = 50;
  115. planeRight.position.y = 50;
  116. planeRight.rotateY( - Math.PI / 2 );
  117. scene.add( planeRight );
  118. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  119. planeLeft.position.x = - 50;
  120. planeLeft.position.y = 50;
  121. planeLeft.rotateY( Math.PI / 2 );
  122. scene.add( planeLeft );
  123. // lights
  124. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  125. mainLight.position.y = 60;
  126. scene.add( mainLight );
  127. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  128. greenLight.position.set( 550, 50, 0 );
  129. scene.add( greenLight );
  130. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  131. redLight.position.set( - 550, 50, 0 );
  132. scene.add( redLight );
  133. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  134. blueLight.position.set( 0, 50, 550 );
  135. scene.add( blueLight );
  136. // renderer
  137. renderer = new THREE.WebGPURenderer( { antialias: true } );
  138. renderer.setPixelRatio( window.devicePixelRatio );
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. renderer.setAnimationLoop( animate );
  141. renderer.inspector = new Inspector();
  142. document.body.appendChild( renderer.domElement );
  143. // controls
  144. cameraControls = new OrbitControls( camera, renderer.domElement );
  145. cameraControls.target.set( 0, 40, 0 );
  146. cameraControls.maxDistance = 400;
  147. cameraControls.minDistance = 10;
  148. cameraControls.update();
  149. window.addEventListener( 'resize', onWindowResize );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. function animate() {
  157. const timer = Date.now() * 0.01;
  158. sphereGroup.rotation.y -= 0.002;
  159. smallSphere.position.set(
  160. Math.cos( timer * 0.1 ) * 30,
  161. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  162. Math.sin( timer * 0.1 ) * 30
  163. );
  164. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  165. smallSphere.rotation.z = timer * 0.8;
  166. renderer.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>
粤ICP备19079148号