webgl2_multisampled_renderbuffers.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGL 2 - Multisampled Renderbuffers</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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #222;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. #container {
  17. position: absolute;
  18. top: 70px;
  19. width: 100%;
  20. bottom: 0px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="info">
  26. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Multisampled Renderbuffers<br />
  27. Left: WebGLRenderTarget, Right: WebGLMultisampleRenderTarget.
  28. </div>
  29. <div id="container">
  30. </div>
  31. <!-- Import maps polyfill -->
  32. <!-- Remove this when import maps will be widely supported -->
  33. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  34. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  45. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  46. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  47. import WebGL from 'three/addons/capabilities/WebGL.js';
  48. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  49. let camera, renderer, group, container;
  50. let composer1, composer2;
  51. const params = {
  52. animate: true,
  53. };
  54. init();
  55. function init() {
  56. if ( WebGL.isWebGL2Available() === false ) {
  57. document.body.appendChild( WebGL.getWebGL2ErrorMessage() );
  58. return;
  59. }
  60. container = document.getElementById( 'container' );
  61. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 10, 2000 );
  62. camera.position.z = 500;
  63. const scene = new THREE.Scene();
  64. scene.background = new THREE.Color( 0xffffff );
  65. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  66. //
  67. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 5 );
  68. hemiLight.position.set( 1, 1, 1 );
  69. scene.add( hemiLight );
  70. //
  71. group = new THREE.Group();
  72. const geometry = new THREE.SphereGeometry( 10, 64, 40 );
  73. const material = new THREE.MeshLambertMaterial( {
  74. color: 0xee0808,
  75. polygonOffset: true,
  76. polygonOffsetFactor: 1, // positive value pushes polygon further away
  77. polygonOffsetUnits: 1
  78. } );
  79. const material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  80. for ( let i = 0; i < 50; i ++ ) {
  81. const mesh = new THREE.Mesh( geometry, material );
  82. mesh.position.x = Math.random() * 600 - 300;
  83. mesh.position.y = Math.random() * 600 - 300;
  84. mesh.position.z = Math.random() * 600 - 300;
  85. mesh.rotation.x = Math.random();
  86. mesh.rotation.z = Math.random();
  87. mesh.scale.setScalar( Math.random() * 5 + 5 );
  88. group.add( mesh );
  89. const mesh2 = new THREE.Mesh( geometry, material2 );
  90. mesh2.position.copy( mesh.position );
  91. mesh2.rotation.copy( mesh.rotation );
  92. mesh2.scale.copy( mesh.scale );
  93. group.add( mesh2 );
  94. }
  95. scene.add( group );
  96. //
  97. renderer = new THREE.WebGLRenderer();
  98. renderer.setPixelRatio( window.devicePixelRatio );
  99. renderer.setSize( container.offsetWidth, container.offsetHeight );
  100. renderer.autoClear = false;
  101. container.appendChild( renderer.domElement );
  102. //
  103. const size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  104. const renderTarget = new THREE.WebGLRenderTarget( size.width, size.height, { samples: 4 } );
  105. const renderPass = new RenderPass( scene, camera );
  106. const outputPass = new OutputPass();
  107. //
  108. composer1 = new EffectComposer( renderer );
  109. composer1.addPass( renderPass );
  110. composer1.addPass( outputPass );
  111. //
  112. composer2 = new EffectComposer( renderer, renderTarget );
  113. composer2.addPass( renderPass );
  114. composer2.addPass( outputPass );
  115. //
  116. const gui = new GUI();
  117. gui.add( params, 'animate' );
  118. //
  119. window.addEventListener( 'resize', onWindowResize );
  120. animate();
  121. }
  122. function onWindowResize() {
  123. camera.aspect = container.offsetWidth / container.offsetHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( container.offsetWidth, container.offsetHeight );
  126. composer1.setSize( container.offsetWidth, container.offsetHeight );
  127. composer2.setSize( container.offsetWidth, container.offsetHeight );
  128. }
  129. function animate() {
  130. requestAnimationFrame( animate );
  131. const halfWidth = container.offsetWidth / 2;
  132. if ( params.animate ) {
  133. group.rotation.y += 0.002;
  134. }
  135. renderer.setScissorTest( true );
  136. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  137. composer1.render();
  138. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  139. composer2.render();
  140. renderer.setScissorTest( false );
  141. }
  142. </script>
  143. </body>
  144. </html>