webgl_framebuffer_texture.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: 128px;
  40. width: 128px;
  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/WebGL.js"></script>
  47. </head>
  48. <body>
  49. <div id="info">
  50. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> framebuffer to texture <br/>
  51. The area of the white square is copied from the framebuffer to a texture (shown in the top-left corner).
  52. </div>
  53. <div id="overlay">
  54. <div>
  55. </div>
  56. </div>
  57. <script>
  58. if ( WEBGL.isWebGLAvailable() === false ) {
  59. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  60. }
  61. var camera, scene, renderer;
  62. var mesh, sprite, texture;
  63. var cameraOrtho, sceneOrtho;
  64. var dpr = window.devicePixelRatio;
  65. var textureSize = 128 * dpr;
  66. var vector = new THREE.Vector2();
  67. init();
  68. animate();
  69. function init() {
  70. //
  71. var width = window.innerWidth;
  72. var height = window.innerHeight;
  73. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  74. camera.position.z = 20;
  75. cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
  76. cameraOrtho.position.z = 10;
  77. scene = new THREE.Scene();
  78. scene.background = new THREE.Color( 0x20252f );
  79. sceneOrtho = new THREE.Scene();
  80. //
  81. var geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
  82. var material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
  83. mesh = new THREE.Mesh( geometry, material );
  84. scene.add( mesh );
  85. //
  86. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  87. scene.add( ambientLight );
  88. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  89. camera.add( pointLight );
  90. scene.add( camera );
  91. //
  92. var data = new Uint8Array( textureSize * textureSize * 3 );
  93. texture = new THREE.DataTexture( data, textureSize, textureSize, THREE.RGBFormat );
  94. texture.minFilter = THREE.NearestFilter;
  95. texture.magFilter = THREE.NearestFilter;
  96. texture.needsUpdate = true;
  97. //
  98. var spriteMaterial = new THREE.SpriteMaterial( { map: texture } );
  99. sprite = new THREE.Sprite( spriteMaterial );
  100. sprite.scale.set( textureSize, textureSize, 1 );
  101. sceneOrtho.add( sprite );
  102. updateSpritePosition();
  103. //
  104. renderer = new THREE.WebGLRenderer( { antialias: true } );
  105. renderer.setPixelRatio( window.devicePixelRatio );
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. renderer.autoClear = false;
  108. document.body.appendChild( renderer.domElement );
  109. //
  110. var overlay = document.getElementById( 'overlay' );
  111. var controls = new THREE.OrbitControls( camera, overlay );
  112. controls.enablePan = false;
  113. //
  114. window.addEventListener( 'resize', onWindowResize, false );
  115. }
  116. function onWindowResize() {
  117. var width = window.innerWidth;
  118. var height = window.innerHeight;
  119. camera.aspect = width / height;
  120. camera.updateProjectionMatrix();
  121. cameraOrtho.left = - width / 2;
  122. cameraOrtho.right = width / 2;
  123. cameraOrtho.top = height / 2;
  124. cameraOrtho.bottom = - height / 2;
  125. cameraOrtho.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. updateSpritePosition();
  128. }
  129. function updateSpritePosition() {
  130. var halfWidth = window.innerWidth / 2;
  131. var halfHeight = window.innerHeight / 2;
  132. var halfImageWidth = textureSize / 2;
  133. var halfImageHeight = textureSize / 2;
  134. sprite.position.set( - halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1 );
  135. }
  136. function animate() {
  137. requestAnimationFrame( animate );
  138. mesh.rotation.x += 0.005;
  139. mesh.rotation.y += 0.01;
  140. renderer.clear();
  141. renderer.render( scene, camera );
  142. // calculate start position for copying data
  143. vector.x = ( window.innerWidth * dpr / 2 ) - ( textureSize / 2 );
  144. vector.y = ( window.innerHeight * dpr / 2 ) - ( textureSize / 2 );
  145. renderer.copyFramebufferToTexture( vector, texture );
  146. renderer.clearDepth();
  147. renderer.render( sceneOrtho, cameraOrtho );
  148. }
  149. </script>
  150. </body>
  151. </html>