webgl2_multisampled_renderbuffers.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <script type="importmap">
  32. {
  33. "imports": {
  34. "three": "../build/three.module.js",
  35. "three/addons/": "./jsm/"
  36. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three';
  41. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  42. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  43. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  44. import WebGL from 'three/addons/capabilities/WebGL.js';
  45. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  46. let camera, renderer, group, container;
  47. let composer1, composer2;
  48. const params = {
  49. animate: true,
  50. };
  51. init();
  52. function init() {
  53. if ( WebGL.isWebGL2Available() === false ) {
  54. document.body.appendChild( WebGL.getWebGL2ErrorMessage() );
  55. return;
  56. }
  57. container = document.getElementById( 'container' );
  58. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 10, 2000 );
  59. camera.position.z = 500;
  60. const scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0xffffff );
  62. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  63. //
  64. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 5 );
  65. hemiLight.position.set( 1, 1, 1 );
  66. scene.add( hemiLight );
  67. //
  68. group = new THREE.Group();
  69. const geometry = new THREE.SphereGeometry( 10, 64, 40 );
  70. const material = new THREE.MeshLambertMaterial( {
  71. color: 0xee0808,
  72. polygonOffset: true,
  73. polygonOffsetFactor: 1, // positive value pushes polygon further away
  74. polygonOffsetUnits: 1
  75. } );
  76. const material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  77. for ( let i = 0; i < 50; i ++ ) {
  78. const mesh = new THREE.Mesh( geometry, material );
  79. mesh.position.x = Math.random() * 600 - 300;
  80. mesh.position.y = Math.random() * 600 - 300;
  81. mesh.position.z = Math.random() * 600 - 300;
  82. mesh.rotation.x = Math.random();
  83. mesh.rotation.z = Math.random();
  84. mesh.scale.setScalar( Math.random() * 5 + 5 );
  85. group.add( mesh );
  86. const mesh2 = new THREE.Mesh( geometry, material2 );
  87. mesh2.position.copy( mesh.position );
  88. mesh2.rotation.copy( mesh.rotation );
  89. mesh2.scale.copy( mesh.scale );
  90. group.add( mesh2 );
  91. }
  92. scene.add( group );
  93. //
  94. renderer = new THREE.WebGLRenderer();
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( container.offsetWidth, container.offsetHeight );
  97. renderer.autoClear = false;
  98. container.appendChild( renderer.domElement );
  99. //
  100. const size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  101. const renderTarget = new THREE.WebGLRenderTarget( size.width, size.height, { samples: 4, type: THREE.HalfFloatType } );
  102. const renderPass = new RenderPass( scene, camera );
  103. const outputPass = new OutputPass();
  104. //
  105. composer1 = new EffectComposer( renderer );
  106. composer1.addPass( renderPass );
  107. composer1.addPass( outputPass );
  108. //
  109. composer2 = new EffectComposer( renderer, renderTarget );
  110. composer2.addPass( renderPass );
  111. composer2.addPass( outputPass );
  112. //
  113. const gui = new GUI();
  114. gui.add( params, 'animate' );
  115. //
  116. window.addEventListener( 'resize', onWindowResize );
  117. animate();
  118. }
  119. function onWindowResize() {
  120. camera.aspect = container.offsetWidth / container.offsetHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( container.offsetWidth, container.offsetHeight );
  123. composer1.setSize( container.offsetWidth, container.offsetHeight );
  124. composer2.setSize( container.offsetWidth, container.offsetHeight );
  125. }
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. const halfWidth = container.offsetWidth / 2;
  129. if ( params.animate ) {
  130. group.rotation.y += 0.002;
  131. }
  132. renderer.setScissorTest( true );
  133. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  134. composer1.render();
  135. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  136. composer2.render();
  137. renderer.setScissorTest( false );
  138. }
  139. </script>
  140. </body>
  141. </html>