webgl2_multisampled_renderbuffers.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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="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 / 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, 0x222222, 1.5 );
  55. hemiLight.position.set( 1, 1, 1 );
  56. scene.add( hemiLight );
  57. //
  58. group = new THREE.Group();
  59. var geometry = new THREE.SphereBufferGeometry( 10, 64, 40 );
  60. var material = new THREE.MeshLambertMaterial( { color: 0xee0808 } );
  61. var material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  62. for ( var i = 0; i < 10; i ++ ) {
  63. var mesh = new THREE.Mesh( geometry, material );
  64. mesh.position.x = Math.random() * 600 - 300;
  65. mesh.position.y = Math.random() * 600 - 300;
  66. mesh.position.z = Math.random() * 600 - 300;
  67. mesh.rotation.x = Math.random();
  68. mesh.rotation.z = Math.random();
  69. mesh.scale.setScalar( Math.random() * 5 + 5 );
  70. group.add( mesh );
  71. var mesh2 = new THREE.Mesh( geometry, material2 );
  72. mesh2.position.copy( mesh.position );
  73. mesh2.rotation.copy( mesh.rotation );
  74. mesh2.scale.copy( mesh.scale );
  75. group.add( mesh2 );
  76. }
  77. scene.add( group );
  78. //
  79. var canvas = document.createElement( 'canvas' );
  80. var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );
  81. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  82. renderer.autoClear = false;
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( container.offsetWidth, container.offsetHeight );
  85. container.appendChild( renderer.domElement );
  86. //
  87. var parameters = {
  88. format: THREE.RGBFormat,
  89. stencilBuffer: false
  90. };
  91. var size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  92. var renderTarget = new THREE.WebGLMultisampleRenderTarget( size.width, size.height, parameters );
  93. var renderPass = new RenderPass( scene, camera );
  94. var copyPass = new ShaderPass( CopyShader );
  95. //
  96. composer1 = new EffectComposer( renderer );
  97. composer1.addPass( renderPass );
  98. composer1.addPass( copyPass );
  99. //
  100. composer2 = new EffectComposer( renderer, renderTarget );
  101. composer2.addPass( renderPass );
  102. composer2.addPass( copyPass );
  103. //
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = container.offsetWidth / container.offsetHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( container.offsetWidth, container.offsetHeight );
  110. composer1.setSize( container.offsetWidth, container.offsetHeight );
  111. composer2.setSize( container.offsetWidth, container.offsetHeight );
  112. }
  113. function animate() {
  114. requestAnimationFrame( animate );
  115. var halfWidth = container.offsetWidth / 2;
  116. group.rotation.y += clock.getDelta() * 0.1;
  117. renderer.setScissorTest( true );
  118. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  119. composer1.render();
  120. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  121. composer2.render();
  122. renderer.setScissorTest( false );
  123. }
  124. </script>
  125. </body>
  126. </html>