webgl2_multisampled_renderbuffers.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. let camera, renderer, clock, group, container;
  39. let composer1, composer2;
  40. init();
  41. function init() {
  42. if ( WEBGL.isWebGL2Available() === false ) {
  43. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  44. return;
  45. }
  46. container = document.getElementById( 'container' );
  47. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 2000 );
  48. camera.position.z = 500;
  49. const 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. const 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. const geometry = new THREE.SphereGeometry( 10, 64, 40 );
  60. const material = new THREE.MeshLambertMaterial( { color: 0xee0808 } );
  61. const material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  62. for ( let i = 0; i < 10; i ++ ) {
  63. const 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. const 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. renderer = new THREE.WebGLRenderer();
  80. renderer.autoClear = false;
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( container.offsetWidth, container.offsetHeight );
  83. container.appendChild( renderer.domElement );
  84. //
  85. const parameters = {
  86. format: THREE.RGBFormat
  87. };
  88. const size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  89. const renderTarget = new THREE.WebGLMultisampleRenderTarget( size.width, size.height, parameters );
  90. const renderPass = new RenderPass( scene, camera );
  91. const copyPass = new ShaderPass( CopyShader );
  92. //
  93. composer1 = new EffectComposer( renderer );
  94. composer1.addPass( renderPass );
  95. composer1.addPass( copyPass );
  96. //
  97. composer2 = new EffectComposer( renderer, renderTarget );
  98. composer2.addPass( renderPass );
  99. composer2.addPass( copyPass );
  100. //
  101. window.addEventListener( 'resize', onWindowResize );
  102. animate();
  103. }
  104. function onWindowResize() {
  105. camera.aspect = container.offsetWidth / container.offsetHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( container.offsetWidth, container.offsetHeight );
  108. composer1.setSize( container.offsetWidth, container.offsetHeight );
  109. composer2.setSize( container.offsetWidth, container.offsetHeight );
  110. }
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. const halfWidth = container.offsetWidth / 2;
  114. group.rotation.y += clock.getDelta() * 0.1;
  115. renderer.setScissorTest( true );
  116. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  117. composer1.render();
  118. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  119. composer2.render();
  120. renderer.setScissorTest( false );
  121. }
  122. </script>
  123. </body>
  124. </html>