webgl_materials_transparency.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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="http://threejs.org" target="_blank" rel="noopener">threejs</a> - Transparency with Premultiplied Alpha (right) and without (left)<br /> using RGBA8 Buffers by <a href="http://clara.io/" target="_blank" rel="noopener">Ben Houston</a>.</div>
  12. <script type="module">
  13. import {
  14. Mesh,
  15. MeshStandardMaterial,
  16. PerspectiveCamera,
  17. PlaneBufferGeometry,
  18. Scene,
  19. SphereBufferGeometry,
  20. SpotLight,
  21. TextureLoader,
  22. WebGLRenderer,
  23. } from "../build/three.module.js";
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/dat.gui.module.js';
  26. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  27. var params = { opacity: 0.25 };
  28. var container, stats;
  29. var camera, scene, renderer;
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  36. camera.position.set( 0.0, 40, 40 * 3.5 );
  37. scene = new Scene();
  38. //
  39. var geometry = new SphereBufferGeometry( 18, 30, 30 );
  40. var material1 = new MeshStandardMaterial( {
  41. opacity: params.opacity,
  42. transparent: true
  43. } );
  44. var material2 = new MeshStandardMaterial( {
  45. opacity: params.opacity,
  46. premultipliedAlpha: true,
  47. transparent: true
  48. } );
  49. var textureLoader = new TextureLoader();
  50. textureLoader.load( "textures/hardwood2_diffuse.jpg", function ( map ) {
  51. map.anisotropy = 8;
  52. material1.map = map;
  53. material1.needsUpdate = true;
  54. material2.map = map;
  55. material2.needsUpdate = true;
  56. } );
  57. var textureLoader = new TextureLoader();
  58. textureLoader.load( "textures/hardwood2_roughness.jpg", function ( map ) {
  59. map.anisotropy = 8;
  60. material1.roughnessMap = map;
  61. material1.needsUpdate = true;
  62. material2.roughnessMap = map;
  63. material2.needsUpdate = true;
  64. } );
  65. var mesh = new Mesh( geometry, material1 );
  66. mesh.position.x = - 25.0;
  67. scene.add( mesh );
  68. var mesh = new Mesh( geometry, material2 );
  69. mesh.position.x = 25.0;
  70. scene.add( mesh );
  71. //
  72. var geometry = new PlaneBufferGeometry( 800, 800 );
  73. var material = new MeshStandardMaterial( { color: 0x333333 } );
  74. var mesh = new Mesh( geometry, material );
  75. mesh.position.y = - 50;
  76. mesh.rotation.x = - Math.PI * 0.5;
  77. scene.add( mesh );
  78. // Lights
  79. var spotLight = new SpotLight( 0xff8888 );
  80. spotLight.position.set( 100, 200, 100 );
  81. spotLight.angle = Math.PI / 6;
  82. spotLight.penumbra = 0.9;
  83. scene.add( spotLight );
  84. var spotLight = new SpotLight( 0x8888ff );
  85. spotLight.position.set( - 100, - 200, - 100 );
  86. spotLight.angle = Math.PI / 6;
  87. spotLight.penumbra = 0.9;
  88. scene.add( spotLight );
  89. //
  90. renderer = new WebGLRenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.shadowMap.enabled = true;
  94. container.appendChild( renderer.domElement );
  95. renderer.gammaInput = true;
  96. renderer.gammaOutput = true;
  97. stats = new Stats();
  98. container.appendChild( stats.dom );
  99. var controls = new OrbitControls( camera, renderer.domElement );
  100. window.addEventListener( 'resize', onWindowResize, false );
  101. var gui = new GUI();
  102. gui.add( params, 'opacity', 0, 1 ).onChange( function () {
  103. material1.opacity = params.opacity;
  104. material2.opacity = params.opacity;
  105. } );
  106. gui.open();
  107. }
  108. function onWindowResize() {
  109. var width = window.innerWidth;
  110. var height = window.innerHeight;
  111. camera.aspect = width / height;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( width, height );
  114. }
  115. //
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. stats.begin();
  119. render();
  120. stats.end();
  121. }
  122. function render() {
  123. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  124. var object = scene.children[ i ];
  125. if ( object.geometry instanceof SphereBufferGeometry ) {
  126. object.rotation.x = performance.now() * 0.0002;
  127. object.rotation.y = - performance.now() * 0.0002;
  128. }
  129. }
  130. renderer.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>