webgpu_materials_transmission.html 5.3 KB

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