1
0

webgl_multisampled_renderbuffers.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. <meta property="og:title" content="three.js WebGL 2 - Multisampled Renderbuffers">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_multisampled_renderbuffers.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_multisampled_renderbuffers.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #fff;
  15. color: #222;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. #container {
  21. position: absolute;
  22. top: 70px;
  23. width: 100%;
  24. bottom: 0px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Multisampled Renderbuffers<br />
  31. Left: WebGLRenderTarget, Right: WebGLRenderTarget (multisampled).
  32. </div>
  33. <div id="container">
  34. </div>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../build/three.module.js",
  39. "three/addons/": "./jsm/"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  46. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  47. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  48. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  49. let camera, renderer, group, container;
  50. let composer1, composer2;
  51. const params = {
  52. animate: true,
  53. };
  54. init();
  55. function init() {
  56. container = document.getElementById( 'container' );
  57. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 10, 2000 );
  58. camera.position.z = 500;
  59. const scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0xffffff );
  61. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  62. //
  63. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 5 );
  64. hemiLight.position.set( 1, 1, 1 );
  65. scene.add( hemiLight );
  66. //
  67. group = new THREE.Group();
  68. const geometry = new THREE.SphereGeometry( 10, 64, 40 );
  69. const material = new THREE.MeshLambertMaterial( {
  70. color: 0xee0808,
  71. polygonOffset: true,
  72. polygonOffsetFactor: 1, // positive value pushes polygon further away
  73. polygonOffsetUnits: 1
  74. } );
  75. const material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  76. for ( let i = 0; i < 50; i ++ ) {
  77. const mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.x = Math.random() * 600 - 300;
  79. mesh.position.y = Math.random() * 600 - 300;
  80. mesh.position.z = Math.random() * 600 - 300;
  81. mesh.rotation.x = Math.random();
  82. mesh.rotation.z = Math.random();
  83. mesh.scale.setScalar( Math.random() * 5 + 5 );
  84. group.add( mesh );
  85. const mesh2 = new THREE.Mesh( geometry, material2 );
  86. mesh2.position.copy( mesh.position );
  87. mesh2.rotation.copy( mesh.rotation );
  88. mesh2.scale.copy( mesh.scale );
  89. group.add( mesh2 );
  90. }
  91. scene.add( group );
  92. //
  93. renderer = new THREE.WebGLRenderer();
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( container.offsetWidth, container.offsetHeight );
  96. renderer.setAnimationLoop( animate );
  97. renderer.autoClear = false;
  98. container.appendChild( renderer.domElement );
  99. //
  100. const size = renderer.getDrawingBufferSize( new THREE.Vector2() );
  101. const renderTarget = new THREE.WebGLRenderTarget( size.width, size.height, { samples: 4, type: THREE.HalfFloatType } );
  102. const renderPass = new RenderPass( scene, camera );
  103. const outputPass = new OutputPass();
  104. //
  105. composer1 = new EffectComposer( renderer );
  106. composer1.addPass( renderPass );
  107. composer1.addPass( outputPass );
  108. //
  109. composer2 = new EffectComposer( renderer, renderTarget );
  110. composer2.addPass( renderPass );
  111. composer2.addPass( outputPass );
  112. //
  113. const gui = new GUI();
  114. gui.add( params, 'animate' );
  115. //
  116. window.addEventListener( 'resize', onWindowResize );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = container.offsetWidth / container.offsetHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( container.offsetWidth, container.offsetHeight );
  122. composer1.setSize( container.offsetWidth, container.offsetHeight );
  123. composer2.setSize( container.offsetWidth, container.offsetHeight );
  124. }
  125. function animate() {
  126. const halfWidth = container.offsetWidth / 2;
  127. if ( params.animate ) {
  128. group.rotation.y += 0.002;
  129. }
  130. renderer.setScissorTest( true );
  131. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  132. composer1.render();
  133. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  134. composer2.render();
  135. renderer.setScissorTest( false );
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号