webgl_materials_physical_transmission.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - 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> - Transmission with Premultiplied Alpha (right) and without (left)</div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { RGBELoader } from './jsm/loaders/RGBELoader.js';
  18. const params = {
  19. color: 0xffffff,
  20. transmission: 1,
  21. opacity: 1,
  22. metalness: 0,
  23. roughness: 0,
  24. envMapIntensity: 1,
  25. lightIntensity: 1,
  26. exposure: 1
  27. };
  28. let container, stats;
  29. let camera, scene, renderer;
  30. let hdrCubeRenderTarget;
  31. let mesh1, mesh2;
  32. const hdrEquirect = new RGBELoader()
  33. .setDataType( THREE.UnsignedByteType )
  34. .setPath( 'textures/equirectangular/' )
  35. .load( 'royal_esplanade_1k.hdr', function () {
  36. init();
  37. animate();
  38. } );
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. renderer = new THREE.WebGLRenderer( { antialias: true } );
  43. renderer.setPixelRatio( window.devicePixelRatio );
  44. renderer.setSize( window.innerWidth, window.innerHeight );
  45. renderer.shadowMap.enabled = true;
  46. container.appendChild( renderer.domElement );
  47. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  48. renderer.toneMappingExposure = params.exposure;
  49. renderer.outputEncoding = THREE.sRGBEncoding;
  50. scene = new THREE.Scene();
  51. scene.background = hdrEquirect;
  52. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  53. camera.position.set( 0, 0, 120 );
  54. //
  55. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  56. hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  57. hdrEquirect.dispose();
  58. pmremGenerator.dispose();
  59. scene.background = hdrCubeRenderTarget.texture;
  60. //
  61. const geometry = new THREE.SphereGeometry( 20, 64, 32 );
  62. const texture = new THREE.CanvasTexture( generateTexture() );
  63. texture.magFilter = THREE.NearestFilter;
  64. texture.wrapT = THREE.RepeatWrapping;
  65. texture.wrapS = THREE.RepeatWrapping;
  66. texture.repeat.set( 1, 3.5 );
  67. const material = new THREE.MeshPhysicalMaterial( {
  68. color: params.color,
  69. metalness: params.metalness,
  70. roughness: params.roughness,
  71. alphaMap: texture,
  72. envMap: hdrCubeRenderTarget.texture,
  73. envMapIntensity: params.envMapIntensity,
  74. depthWrite: false,
  75. transmission: params.transmission, // use material.transmission for glass materials
  76. opacity: params.opacity,
  77. side: THREE.DoubleSide,
  78. transparent: true
  79. } );
  80. mesh1 = new THREE.Mesh( geometry, material );
  81. mesh1.position.x = - 30.0;
  82. scene.add( mesh1 );
  83. mesh2 = new THREE.Mesh( geometry, material );
  84. mesh2.position.x = 30.0;
  85. scene.add( mesh2 );
  86. //
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. const controls = new OrbitControls( camera, renderer.domElement );
  90. controls.minDistance = 10;
  91. controls.maxDistance = 150;
  92. window.addEventListener( 'resize', onWindowResize );
  93. //
  94. const gui = new GUI();
  95. gui.addColor( params, 'color' )
  96. .onChange( function () {
  97. material.color.set( params.color );
  98. } );
  99. gui.add( params, 'transmission', 0, 1, 0.01 )
  100. .onChange( function () {
  101. material.transmission = params.transmission;
  102. } );
  103. gui.add( params, 'opacity', 0, 1, 0.01 )
  104. .onChange( function () {
  105. material.opacity = params.opacity;
  106. } );
  107. gui.add( params, 'metalness', 0, 1, 0.01 )
  108. .onChange( function () {
  109. material.metalness = params.metalness;
  110. } );
  111. gui.add( params, 'roughness', 0, 1, 0.01 )
  112. .onChange( function () {
  113. material.roughness = params.roughness;
  114. } );
  115. gui.add( params, 'envMapIntensity', 0, 1, 0.01 )
  116. .name( 'envMap intensity' )
  117. .onChange( function () {
  118. material.envMapIntensity = params.envMapIntensity;
  119. } );
  120. gui.add( params, 'exposure', 0, 1, 0.01 )
  121. .onChange( function () {
  122. renderer.toneMappingExposure = params.exposure;
  123. } );
  124. gui.open();
  125. }
  126. function onWindowResize() {
  127. const width = window.innerWidth;
  128. const height = window.innerHeight;
  129. camera.aspect = width / height;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( width, height );
  132. }
  133. //
  134. function generateTexture() {
  135. const canvas = document.createElement( 'canvas' );
  136. canvas.width = 2;
  137. canvas.height = 2;
  138. const context = canvas.getContext( '2d' );
  139. context.fillStyle = 'white';
  140. context.fillRect( 0, 1, 2, 1 );
  141. return canvas;
  142. }
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. const t = performance.now();
  146. mesh1.rotation.x = mesh2.rotation.x = t * 0.0002;
  147. mesh1.rotation.z = mesh2.rotation.z = - t * 0.0002;
  148. stats.begin();
  149. renderer.render( scene, camera );
  150. stats.end();
  151. }
  152. </script>
  153. </body>
  154. </html>