webgpu_materials_transmission.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. render();
  57. } );
  58. function init() {
  59. renderer = new THREE.WebGPURenderer( { antialias: true } );
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.setAnimationLoop( render );
  63. renderer.inspector = new Inspector();
  64. document.body.appendChild( renderer.domElement );
  65. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  66. renderer.toneMappingExposure = params.exposure;
  67. scene = new THREE.Scene();
  68. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  69. camera.position.set( 0, 0, 120 );
  70. //
  71. scene.background = hdrEquirect;
  72. //
  73. const geometry = new THREE.SphereGeometry( 20, 64, 32 );
  74. const texture = new THREE.CanvasTexture( generateTexture() );
  75. texture.magFilter = THREE.NearestFilter;
  76. texture.wrapT = THREE.RepeatWrapping;
  77. texture.wrapS = THREE.RepeatWrapping;
  78. texture.repeat.set( 1, 3.5 );
  79. const material = new THREE.MeshPhysicalMaterial( {
  80. color: params.color,
  81. metalness: params.metalness,
  82. roughness: params.roughness,
  83. ior: params.ior,
  84. alphaMap: texture,
  85. envMap: hdrEquirect,
  86. envMapIntensity: params.envMapIntensity,
  87. transmission: params.transmission, // use material.transmission for glass materials
  88. specularIntensity: params.specularIntensity,
  89. specularColor: params.specularColor,
  90. opacity: params.opacity,
  91. side: THREE.DoubleSide,
  92. transparent: true
  93. } );
  94. mesh = new THREE.Mesh( geometry, material );
  95. scene.add( mesh );
  96. //
  97. const controls = new OrbitControls( camera, renderer.domElement );
  98. controls.minDistance = 10;
  99. controls.maxDistance = 150;
  100. window.addEventListener( 'resize', onWindowResize );
  101. //
  102. const gui = renderer.inspector.createParameters( 'Settings' );
  103. gui.addColor( params, 'color' )
  104. .onChange( function () {
  105. material.color.set( params.color );
  106. } );
  107. gui.add( params, 'transmission', 0, 1, 0.01 )
  108. .onChange( function () {
  109. material.transmission = params.transmission;
  110. } );
  111. gui.add( params, 'opacity', 0, 1, 0.01 )
  112. .onChange( function () {
  113. material.opacity = params.opacity;
  114. } );
  115. gui.add( params, 'metalness', 0, 1, 0.01 )
  116. .onChange( function () {
  117. material.metalness = params.metalness;
  118. } );
  119. gui.add( params, 'roughness', 0, 1, 0.01 )
  120. .onChange( function () {
  121. material.roughness = params.roughness;
  122. } );
  123. gui.add( params, 'ior', 1, 2, 0.01 )
  124. .onChange( function () {
  125. material.ior = params.ior;
  126. } );
  127. gui.add( params, 'thickness', 0, 5, 0.01 )
  128. .onChange( function () {
  129. material.thickness = params.thickness;
  130. } );
  131. gui.add( params, 'specularIntensity', 0, 1, 0.01 )
  132. .name( 'specular intensity' )
  133. .onChange( function () {
  134. material.specularIntensity = params.specularIntensity;
  135. } );
  136. gui.addColor( params, 'specularColor' )
  137. .name( 'specular color' )
  138. .onChange( function () {
  139. material.specularColor.set( params.specularColor );
  140. } );
  141. gui.add( params, 'envMapIntensity', 0, 1, 0.01 )
  142. .name( 'envMap intensity' )
  143. .onChange( function () {
  144. material.envMapIntensity = params.envMapIntensity;
  145. } );
  146. gui.add( params, 'exposure', 0, 1, 0.01 )
  147. .onChange( function () {
  148. renderer.toneMappingExposure = params.exposure;
  149. } );
  150. }
  151. function onWindowResize() {
  152. const width = window.innerWidth;
  153. const height = window.innerHeight;
  154. camera.aspect = width / height;
  155. camera.updateProjectionMatrix();
  156. renderer.setSize( width, height );
  157. }
  158. //
  159. function generateTexture() {
  160. const canvas = document.createElement( 'canvas' );
  161. canvas.width = 2;
  162. canvas.height = 2;
  163. const context = canvas.getContext( '2d' );
  164. context.fillStyle = 'white';
  165. context.fillRect( 0, 1, 2, 1 );
  166. return canvas;
  167. }
  168. function render() {
  169. renderer.renderAsync( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>
粤ICP备19079148号