webgl_materials_physical_transparency.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgl - materials - transparency</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> - Transparency 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. var params = {
  19. color: 0xffffff,
  20. transparency: 0.90,
  21. envMapIntensity: 1,
  22. lightIntensity: 1,
  23. exposure: 1
  24. };
  25. var container, stats;
  26. var camera, scene, renderer;
  27. var hdrCubeRenderTarget;
  28. var spotLight1, spotLight2;
  29. var mesh1, mesh2;
  30. var hdrEquirect = new RGBELoader()
  31. .setDataType( THREE.UnsignedByteType )
  32. .setPath( 'textures/equirectangular/' )
  33. .load( 'royal_esplanade_1k.hdr', function ( texture ) {
  34. init();
  35. animate();
  36. } );
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. renderer = new THREE.WebGLRenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. renderer.shadowMap.enabled = true;
  44. container.appendChild( renderer.domElement );
  45. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  46. renderer.toneMappingExposure = params.exposure;
  47. renderer.outputEncoding = THREE.sRGBEncoding;
  48. scene = new THREE.Scene();
  49. scene.background = hdrEquirect;
  50. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  51. camera.position.set( 0, 0, 120 );
  52. //
  53. var pmremGenerator = new THREE.PMREMGenerator( renderer );
  54. hdrCubeRenderTarget = pmremGenerator.fromEquirectangular( hdrEquirect );
  55. hdrEquirect.dispose();
  56. pmremGenerator.dispose();
  57. scene.background = hdrCubeRenderTarget.texture;
  58. //
  59. var geometry = new THREE.SphereBufferGeometry( 20, 64, 32 );
  60. var texture = new THREE.CanvasTexture( generateTexture() );
  61. texture.magFilter = THREE.NearestFilter;
  62. texture.wrapT = THREE.RepeatWrapping;
  63. texture.wrapS = THREE.RepeatWrapping;
  64. texture.repeat.set( 1, 3.5 );
  65. var material = new THREE.MeshPhysicalMaterial( {
  66. color: params.color,
  67. metalness: 0,
  68. roughness: 0,
  69. alphaMap: texture,
  70. alphaTest: 0.5,
  71. envMap: hdrCubeRenderTarget.texture,
  72. envMapIntensity: params.envMapIntensity,
  73. depthTest: false,
  74. transparency: params.transparency,
  75. transparent: true
  76. } );
  77. var material1 = new THREE.MeshPhysicalMaterial().copy( material );
  78. var material1b = new THREE.MeshPhysicalMaterial().copy( material );
  79. material1b.side = THREE.BackSide;
  80. var material2 = new THREE.MeshPhysicalMaterial().copy( material );
  81. material2.premultipliedAlpha = true;
  82. var material2b = new THREE.MeshPhysicalMaterial().copy( material );
  83. material2b.premultipliedAlpha = true;
  84. material2b.side = THREE.BackSide;
  85. mesh1 = new THREE.Mesh( geometry, material1 );
  86. mesh1.position.x = - 30.0;
  87. scene.add( mesh1 );
  88. var mesh = new THREE.Mesh( geometry, material1b );
  89. mesh.renderOrder = - 1;
  90. mesh1.add( mesh );
  91. mesh2 = new THREE.Mesh( geometry, material2 );
  92. mesh2.position.x = 30.0;
  93. scene.add( mesh2 );
  94. var mesh = new THREE.Mesh( geometry, material2b );
  95. mesh.renderOrder = - 1;
  96. mesh2.add( mesh );
  97. //
  98. spotLight1 = new THREE.SpotLight( 0xffffff, params.lightIntensity );
  99. spotLight1.position.set( 100, 200, 100 );
  100. spotLight1.angle = Math.PI / 6;
  101. scene.add( spotLight1 );
  102. spotLight2 = new THREE.SpotLight( 0xffffff, params.lightIntensity );
  103. spotLight2.position.set( - 100, - 200, - 100 );
  104. spotLight2.angle = Math.PI / 6;
  105. scene.add( spotLight2 );
  106. //
  107. stats = new Stats();
  108. container.appendChild( stats.dom );
  109. var controls = new OrbitControls( camera, renderer.domElement );
  110. controls.minDistance = 10;
  111. controls.maxDistance = 150;
  112. window.addEventListener( 'resize', onWindowResize, false );
  113. //
  114. var gui = new GUI();
  115. gui.addColor( params, 'color' )
  116. .onChange( function () {
  117. material1.color.set( params.color );
  118. material2.color.set( params.color );
  119. material1b.color.set( params.color );
  120. material2b.color.set( params.color );
  121. } );
  122. gui.add( params, 'transparency', 0, 1 )
  123. .onChange( function () {
  124. material1.transparency = material2.transparency = params.transparency;
  125. material1b.transparency = material2b.transparency = params.transparency;
  126. } );
  127. gui.add( params, 'envMapIntensity', 0, 1 )
  128. .name( 'envMap intensity' )
  129. .onChange( function () {
  130. material1.envMapIntensity = material2.envMapIntensity = params.envMapIntensity;
  131. material1b.envMapIntensity = material2b.envMapIntensity = params.envMapIntensity;
  132. } );
  133. gui.add( params, 'lightIntensity', 0, 1 )
  134. .name( 'light intensity' )
  135. .onChange( function () {
  136. spotLight1.intensity = spotLight2.intensity = params.lightIntensity;
  137. } );
  138. gui.add( params, 'exposure', 0, 1 )
  139. .onChange( function () {
  140. renderer.toneMappingExposure = params.exposure;
  141. } );
  142. gui.open();
  143. }
  144. function onWindowResize() {
  145. var width = window.innerWidth;
  146. var height = window.innerHeight;
  147. camera.aspect = width / height;
  148. camera.updateProjectionMatrix();
  149. renderer.setSize( width, height );
  150. }
  151. //
  152. function generateTexture() {
  153. var canvas = document.createElement( 'canvas' );
  154. canvas.width = 2;
  155. canvas.height = 2;
  156. var context = canvas.getContext( '2d' );
  157. context.fillStyle = 'white';
  158. context.fillRect( 0, 1, 2, 1 );
  159. return canvas;
  160. }
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. var t = performance.now();
  164. mesh1.rotation.x = mesh2.rotation.x = t * 0.0002;
  165. mesh1.rotation.z = mesh2.rotation.z = - t * 0.0002;
  166. stats.begin();
  167. renderer.render( scene, camera );
  168. stats.end();
  169. }
  170. </script>
  171. </body>
  172. </html>