webgl_multisampled_renderbuffers.html 4.7 KB

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