webgl2_multisampled_renderbuffers.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. let camera, renderer, clock, group, container;
  50. let composer1, composer2;
  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, 1, 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. clock = new THREE.Clock();
  64. //
  65. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 1.5 );
  66. hemiLight.position.set( 1, 1, 1 );
  67. scene.add( hemiLight );
  68. //
  69. group = new THREE.Group();
  70. const geometry = new THREE.SphereGeometry( 10, 64, 40 );
  71. const material = new THREE.MeshLambertMaterial( { color: 0xee0808 } );
  72. const material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  73. for ( let i = 0; i < 10; i ++ ) {
  74. const mesh = new THREE.Mesh( geometry, material );
  75. mesh.position.x = Math.random() * 600 - 300;
  76. mesh.position.y = Math.random() * 600 - 300;
  77. mesh.position.z = Math.random() * 600 - 300;
  78. mesh.rotation.x = Math.random();
  79. mesh.rotation.z = Math.random();
  80. mesh.scale.setScalar( Math.random() * 5 + 5 );
  81. group.add( mesh );
  82. const mesh2 = new THREE.Mesh( geometry, material2 );
  83. mesh2.position.copy( mesh.position );
  84. mesh2.rotation.copy( mesh.rotation );
  85. mesh2.scale.copy( mesh.scale );
  86. group.add( mesh2 );
  87. }
  88. scene.add( group );
  89. //
  90. renderer = new THREE.WebGLRenderer();
  91. renderer.autoClear = false;
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( container.offsetWidth, container.offsetHeight );
  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 } );
  98. const renderPass = new RenderPass( scene, camera );
  99. const copyPass = new ShaderPass( CopyShader );
  100. //
  101. composer1 = new EffectComposer( renderer );
  102. composer1.addPass( renderPass );
  103. composer1.addPass( copyPass );
  104. //
  105. composer2 = new EffectComposer( renderer, renderTarget );
  106. composer2.addPass( renderPass );
  107. composer2.addPass( copyPass );
  108. //
  109. window.addEventListener( 'resize', onWindowResize );
  110. animate();
  111. }
  112. function onWindowResize() {
  113. camera.aspect = container.offsetWidth / container.offsetHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( container.offsetWidth, container.offsetHeight );
  116. composer1.setSize( container.offsetWidth, container.offsetHeight );
  117. composer2.setSize( container.offsetWidth, container.offsetHeight );
  118. }
  119. function animate() {
  120. requestAnimationFrame( animate );
  121. const halfWidth = container.offsetWidth / 2;
  122. group.rotation.y += clock.getDelta() * 0.1;
  123. renderer.setScissorTest( true );
  124. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  125. composer1.render();
  126. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  127. composer2.render();
  128. renderer.setScissorTest( false );
  129. }
  130. </script>
  131. </body>
  132. </html>