2
0

webgl_materials_transparency.html 4.5 KB

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