webgpu_materials_transmission.html 5.7 KB

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