webgpu_materials_transmission.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgpu - transmission</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="threejs webgpu - transmission">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_transmission.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_materials_transmission.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Transmission</span>
  19. </div>
  20. <small>
  21. Transmissive PBR material.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js';
  39. const params = {
  40. color: 0xffffff,
  41. transmission: 1,
  42. opacity: 1,
  43. metalness: 0,
  44. roughness: 0,
  45. ior: 1.5,
  46. thickness: 0.01,
  47. specularIntensity: 1,
  48. specularColor: 0xffffff,
  49. envMapIntensity: 1,
  50. lightIntensity: 1,
  51. exposure: 1
  52. };
  53. let camera, scene, renderer;
  54. let mesh;
  55. const hdrEquirect = new UltraHDRLoader()
  56. .setPath( 'textures/equirectangular/' )
  57. .load( 'royal_esplanade_2k.hdr.jpg', function () {
  58. hdrEquirect.mapping = THREE.EquirectangularReflectionMapping;
  59. init();
  60. } );
  61. function init() {
  62. renderer = new THREE.WebGPURenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( animate );
  66. renderer.inspector = new Inspector();
  67. document.body.appendChild( renderer.domElement );
  68. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  69. renderer.toneMappingExposure = params.exposure;
  70. scene = new THREE.Scene();
  71. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  72. camera.position.set( 0, 0, 120 );
  73. //
  74. scene.background = hdrEquirect;
  75. //
  76. const geometry = new THREE.SphereGeometry( 20, 64, 32 );
  77. const texture = new THREE.CanvasTexture( generateTexture() );
  78. texture.magFilter = THREE.NearestFilter;
  79. texture.wrapT = THREE.RepeatWrapping;
  80. texture.wrapS = THREE.RepeatWrapping;
  81. texture.repeat.set( 1, 3.5 );
  82. const material = new THREE.MeshPhysicalMaterial( {
  83. color: params.color,
  84. metalness: params.metalness,
  85. roughness: params.roughness,
  86. ior: params.ior,
  87. alphaMap: texture,
  88. envMap: hdrEquirect,
  89. envMapIntensity: params.envMapIntensity,
  90. transmission: params.transmission, // use material.transmission for glass materials
  91. specularIntensity: params.specularIntensity,
  92. specularColor: params.specularColor,
  93. opacity: params.opacity,
  94. side: THREE.DoubleSide,
  95. transparent: true
  96. } );
  97. mesh = new THREE.Mesh( geometry, material );
  98. scene.add( mesh );
  99. //
  100. const controls = new OrbitControls( camera, renderer.domElement );
  101. controls.minDistance = 10;
  102. controls.maxDistance = 150;
  103. window.addEventListener( 'resize', onWindowResize );
  104. //
  105. const gui = renderer.inspector.createParameters( 'Settings' );
  106. gui.addColor( params, 'color' )
  107. .onChange( function () {
  108. material.color.set( params.color );
  109. } );
  110. gui.add( params, 'transmission', 0, 1, 0.01 )
  111. .onChange( function () {
  112. material.transmission = params.transmission;
  113. } );
  114. gui.add( params, 'opacity', 0, 1, 0.01 )
  115. .onChange( function () {
  116. material.opacity = params.opacity;
  117. } );
  118. gui.add( params, 'metalness', 0, 1, 0.01 )
  119. .onChange( function () {
  120. material.metalness = params.metalness;
  121. } );
  122. gui.add( params, 'roughness', 0, 1, 0.01 )
  123. .onChange( function () {
  124. material.roughness = params.roughness;
  125. } );
  126. gui.add( params, 'ior', 1, 2, 0.01 )
  127. .onChange( function () {
  128. material.ior = params.ior;
  129. } );
  130. gui.add( params, 'thickness', 0, 5, 0.01 )
  131. .onChange( function () {
  132. material.thickness = params.thickness;
  133. } );
  134. gui.add( params, 'specularIntensity', 0, 1, 0.01 )
  135. .name( 'specular intensity' )
  136. .onChange( function () {
  137. material.specularIntensity = params.specularIntensity;
  138. } );
  139. gui.addColor( params, 'specularColor' )
  140. .name( 'specular color' )
  141. .onChange( function () {
  142. material.specularColor.set( params.specularColor );
  143. } );
  144. gui.add( params, 'envMapIntensity', 0, 1, 0.01 )
  145. .name( 'envMap intensity' )
  146. .onChange( function () {
  147. material.envMapIntensity = params.envMapIntensity;
  148. } );
  149. gui.add( params, 'exposure', 0, 1, 0.01 )
  150. .onChange( function () {
  151. renderer.toneMappingExposure = params.exposure;
  152. } );
  153. }
  154. function onWindowResize() {
  155. const width = window.innerWidth;
  156. const height = window.innerHeight;
  157. camera.aspect = width / height;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( width, height );
  160. }
  161. //
  162. function generateTexture() {
  163. const canvas = document.createElement( 'canvas' );
  164. canvas.width = 2;
  165. canvas.height = 2;
  166. const context = canvas.getContext( '2d' );
  167. context.fillStyle = 'white';
  168. context.fillRect( 0, 1, 2, 1 );
  169. return canvas;
  170. }
  171. function animate() {
  172. renderer.render( scene, camera );
  173. }
  174. </script>
  175. </body>
  176. </html>
粤ICP备19079148号