webgpu_mirror.html 7.0 KB

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