webgl_framebuffer_texture.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - framebuffer - texture</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. <style>
  8. body {
  9. background:#777;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family:Monospace;
  21. font-size:13px;
  22. text-align:center;
  23. }
  24. a {
  25. color: #ffffff;
  26. }
  27. #overlay {
  28. position: fixed;
  29. display: flex;
  30. flex-direction: column;
  31. justify-content: center;
  32. align-items: center;
  33. height: 100%;
  34. width: 100%;
  35. top: 0;
  36. z-index: 999;
  37. }
  38. #overlay > div {
  39. height: 256px;
  40. width: 256px;
  41. border: 1px solid white;
  42. }
  43. </style>
  44. <script src="../build/three.js"></script>
  45. <script src="js/controls/OrbitControls.js"></script>
  46. <script src="js/Detector.js"></script>
  47. </head>
  48. <body>
  49. <div id="container"></div>
  50. <div id="info">
  51. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> framebuffer to texture <br/>
  52. The area of the white square is copied from the framebuffer to a texture (shown in the top-left corner).
  53. </div>
  54. <div id="overlay">
  55. <div>
  56. </div>
  57. </div>
  58. <script>
  59. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  60. var camera, scene, renderer;
  61. var mesh, sprite, texture;
  62. var cameraOrtho, sceneOrtho;
  63. var dpr = window.devicePixelRatio;
  64. var imageWidth = 256 * dpr;
  65. var imageHeight = 256 * dpr;
  66. init();
  67. animate();
  68. function init() {
  69. //
  70. var width = window.innerWidth;
  71. var height = window.innerHeight;
  72. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  73. camera.position.z = 20;
  74. cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
  75. cameraOrtho.position.z = 10;
  76. scene = new THREE.Scene();
  77. scene.background = new THREE.Color( 0x20252f );
  78. sceneOrtho = new THREE.Scene();
  79. //
  80. var geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
  81. var material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
  82. mesh = new THREE.Mesh( geometry, material );
  83. scene.add( mesh );
  84. //
  85. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  86. scene.add( ambientLight );
  87. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  88. camera.add( pointLight );
  89. scene.add( camera );
  90. //
  91. texture = new THREE.Texture();
  92. texture.image = new Image( imageWidth, imageHeight );
  93. texture.format = THREE.RGBFormat;
  94. texture.minFilter = texture.magFilter = THREE.NearestFilter;
  95. //
  96. var spriteMaterial = new THREE.SpriteMaterial( { map: texture } );
  97. sprite = new THREE.Sprite( spriteMaterial );
  98. sprite.scale.set( imageWidth, imageHeight, 1 );
  99. sceneOrtho.add( sprite );
  100. updateSpritePosition();
  101. //
  102. renderer = new THREE.WebGLRenderer( { antialias: true } );
  103. renderer.setPixelRatio( window.devicePixelRatio );
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. renderer.autoClear = false;
  106. document.body.appendChild( renderer.domElement );
  107. //
  108. var overlay = document.getElementById( 'overlay' );
  109. var controls = new THREE.OrbitControls( camera, overlay );
  110. //
  111. window.addEventListener( 'resize', onWindowResize, false );
  112. }
  113. function onWindowResize() {
  114. var width = window.innerWidth;
  115. var height = window.innerHeight;
  116. camera.aspect = width / height;
  117. camera.updateProjectionMatrix();
  118. cameraOrtho.left = - width / 2;
  119. cameraOrtho.right = width / 2;
  120. cameraOrtho.top = height / 2;
  121. cameraOrtho.bottom = - height / 2;
  122. cameraOrtho.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. updateSpritePosition();
  125. }
  126. function updateSpritePosition() {
  127. var halfWidth = window.innerWidth / 2;
  128. var halfHeight = window.innerHeight / 2;
  129. var halfImageWidth = imageWidth / 2;
  130. var halfImageHeight = imageHeight / 2;
  131. sprite.position.set( - halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1 );
  132. }
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. mesh.rotation.x += 0.005;
  136. mesh.rotation.y += 0.01;
  137. renderer.clear();
  138. renderer.render( scene, camera );
  139. // calculate start position for copying data
  140. var x = ( window.innerWidth * dpr / 2 ) - ( imageWidth / 2 );
  141. var y = ( window.innerHeight * dpr / 2 ) - ( imageHeight / 2 );
  142. renderer.copyFramebufferToTexture( x, y, texture );
  143. renderer.clearDepth();
  144. renderer.render( sceneOrtho, cameraOrtho );
  145. }
  146. </script>
  147. </body>
  148. </html>