webgl2_multisampled_renderbuffers.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: 80px;
  19. width: 100%;
  20. bottom: 0px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="info">
  26. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Multisampled Renderbuffers<br />
  27. Left scene is multi-sampled, right scene is rendered without anti-aliasing.
  28. </div>
  29. <div id="container">
  30. </div>
  31. <script type="module">
  32. import * as THREE from '../build/three.module.js';
  33. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  34. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  35. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  36. import { CopyShader } from './jsm/shaders/CopyShader.js';
  37. import { WEBGL } from './jsm/WebGL.js';
  38. if ( WEBGL.isWebGL2Available() === false ) {
  39. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  40. }
  41. var camera, scene, renderer, clock, group, container;
  42. var composer1, composer2;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.getElementById( 'container' );
  47. camera = new THREE.PerspectiveCamera( 45, ( container.offsetWidth * 0.5 ) / container.offsetHeight, 1, 2000 );
  48. camera.position.z = 500;
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0xffffff );
  51. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  52. clock = new THREE.Clock();
  53. //
  54. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  55. hemiLight.position.set( 0, 1000, 0 );
  56. scene.add( hemiLight );
  57. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
  58. dirLight.position.set( - 3000, 1000, - 1000 );
  59. scene.add( dirLight );
  60. //
  61. group = new THREE.Group();
  62. var geometry = new THREE.IcosahedronBufferGeometry( 10, 2 );
  63. var material = new THREE.MeshStandardMaterial( { color: 0xee0808, flatShading: true } );
  64. for ( var i = 0; i < 100; i ++ ) {
  65. var mesh = new THREE.Mesh( geometry, material );
  66. mesh.position.x = Math.random() * 500 - 250;
  67. mesh.position.y = Math.random() * 500 - 250;
  68. mesh.position.z = Math.random() * 500 - 250;
  69. mesh.scale.setScalar( Math.random() * 2 + 1 );
  70. group.add( mesh );
  71. }
  72. scene.add( group );
  73. //
  74. var canvas = document.createElement( 'canvas' );
  75. var context = canvas.getContext( 'webgl2', { antialias: false } );
  76. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  77. renderer.autoClear = false;
  78. renderer.setSize( container.offsetWidth, container.offsetHeight );
  79. container.appendChild( renderer.domElement );
  80. //
  81. var parameters = {
  82. format: THREE.RGBFormat,
  83. stencilBuffer: false
  84. };
  85. var size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  86. var renderTarget = new THREE.WebGLMultisampleRenderTarget( size.width, size.height, parameters );
  87. var renderPass = new RenderPass( scene, camera );
  88. var copyPass = new ShaderPass( CopyShader );
  89. //
  90. composer1 = new EffectComposer( renderer, renderTarget );
  91. composer1.addPass( renderPass );
  92. composer1.addPass( copyPass );
  93. //
  94. composer2 = new EffectComposer( renderer );
  95. composer2.addPass( renderPass );
  96. composer2.addPass( copyPass );
  97. //
  98. window.addEventListener( 'resize', onWindowResize, false );
  99. }
  100. function onWindowResize() {
  101. camera.aspect = ( container.offsetWidth * 0.5 ) / container.offsetHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( container.offsetWidth, container.offsetHeight );
  104. composer1.setSize( container.offsetWidth, container.offsetHeight );
  105. composer2.setSize( container.offsetWidth, container.offsetHeight );
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. var halfWidth = container.offsetWidth / 2;
  110. group.rotation.y += clock.getDelta() * 0.1;
  111. renderer.setViewport( 0, 0, halfWidth, container.offsetHeight );
  112. composer1.render();
  113. renderer.setViewport( halfWidth, 0, halfWidth, container.offsetHeight );
  114. composer2.render();
  115. }
  116. </script>
  117. </body>
  118. </html>