webgpu_mirror.html 7.1 KB

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