webgl2_multisampled_renderbuffers.html 4.9 KB

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