webgpu_cubemap_dynamic.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - dynamic cube reflection</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 - dynamic cube reflection">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_cubemap_dynamic.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_cubemap_dynamic.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. <style>
  13. body {
  14. touch-action: none;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  21. <div class="title-wrapper">
  22. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Dynamic Cube Reflections</span>
  23. </div>
  24. <small>
  25. Realtime reflections rendered with a cube camera.
  26. </small>
  27. </div>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.webgpu.js",
  32. "three/webgpu": "../build/three.webgpu.js",
  33. "three/tsl": "../build/three.tsl.js",
  34. "three/addons/": "./jsm/"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three/webgpu';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { HDRCubeTextureLoader } from 'three/addons/loaders/HDRCubeTextureLoader.js';
  42. import { Inspector } from 'three/addons/inspector/Inspector.js';
  43. let camera, scene, renderer;
  44. let cube, sphere, torus, material;
  45. let cubeCamera, cubeRenderTarget;
  46. let controls;
  47. init();
  48. async function init() {
  49. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  50. camera.position.z = 75;
  51. scene = new THREE.Scene();
  52. const uvTexture = new THREE.TextureLoader().load( './textures/uv_grid_opengl.jpg' );
  53. const hdrUrls = [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ];
  54. const texture = await new HDRCubeTextureLoader()
  55. .setPath( './textures/cube/pisaHDR/' )
  56. .loadAsync( hdrUrls );
  57. texture.name = 'pisaHDR';
  58. texture.minFilter = THREE.LinearMipmapLinearFilter;
  59. texture.magFilter = THREE.LinearFilter;
  60. scene.background = texture;
  61. scene.environment = texture;
  62. //
  63. cubeRenderTarget = new THREE.CubeRenderTarget( 256 );
  64. cubeRenderTarget.texture.type = THREE.HalfFloatType;
  65. cubeRenderTarget.texture.minFilter = THREE.LinearMipmapLinearFilter;
  66. cubeRenderTarget.texture.magFilter = THREE.LinearFilter;
  67. cubeRenderTarget.texture.generateMipmaps = true;
  68. cubeCamera = new THREE.CubeCamera( 1, 1000, cubeRenderTarget );
  69. //
  70. material = new THREE.MeshStandardNodeMaterial( {
  71. envMap: cubeRenderTarget.texture,
  72. roughness: 0.05,
  73. metalness: 1
  74. } );
  75. sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 15, 8 ), material );
  76. scene.add( sphere );
  77. const material1 = new THREE.MeshStandardNodeMaterial( {
  78. map: uvTexture,
  79. roughness: 0.1,
  80. metalness: 0,
  81. } );
  82. const material2 = new THREE.MeshStandardNodeMaterial( {
  83. map: uvTexture,
  84. roughness: 0.1,
  85. metalness: 0,
  86. envMap: texture,
  87. } );
  88. cube = new THREE.Mesh( new THREE.BoxGeometry( 15, 15, 15 ), material1 );
  89. scene.add( cube );
  90. torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 8, 3, 128, 16 ), material2 );
  91. scene.add( torus );
  92. //
  93. renderer = new THREE.WebGPURenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.setAnimationLoop( animation );
  97. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  98. renderer.inspector = new Inspector();
  99. document.body.appendChild( renderer.domElement );
  100. window.addEventListener( 'resize', onWindowResized );
  101. controls = new OrbitControls( camera, renderer.domElement );
  102. controls.autoRotate = true;
  103. const gui = renderer.inspector.createParameters( 'Settings' );
  104. gui.add( material, 'roughness', 0, 1 );
  105. gui.add( material, 'metalness', 0, 1 );
  106. gui.add( renderer, 'toneMappingExposure', 0, 2 ).name( 'exposure' );
  107. gui.add( scene, 'environmentIntensity', 0, 1 );
  108. gui.add( material2, 'envMapIntensity', 0, 1 );
  109. }
  110. function onWindowResized() {
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. }
  115. function animation( msTime ) {
  116. const time = msTime / 1000;
  117. cube.position.x = Math.cos( time ) * 30;
  118. cube.position.y = Math.sin( time ) * 30;
  119. cube.position.z = Math.sin( time ) * 30;
  120. cube.rotation.x += 0.02;
  121. cube.rotation.y += 0.03;
  122. torus.position.x = Math.cos( time + 10 ) * 30;
  123. torus.position.y = Math.sin( time + 10 ) * 30;
  124. torus.position.z = Math.sin( time + 10 ) * 30;
  125. torus.rotation.x += 0.02;
  126. torus.rotation.y += 0.03;
  127. material.visible = false;
  128. cubeCamera.update( renderer, scene );
  129. material.visible = true;
  130. controls.update();
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>
粤ICP备19079148号