webgl_framebuffer_texture.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #selection {
  10. position: fixed;
  11. display: flex;
  12. flex-direction: column;
  13. justify-content: center;
  14. align-items: center;
  15. height: 100%;
  16. width: 100%;
  17. top: 0;
  18. z-index: 999;
  19. }
  20. #selection > div {
  21. height: 128px;
  22. width: 128px;
  23. border: 1px solid white;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> framebuffer to texture
  30. </div>
  31. <div id="selection">
  32. <div></div>
  33. </div>
  34. <script type="module">
  35. import * as THREE from '../build/three.module.js';
  36. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  37. var camera, scene, renderer;
  38. var mesh, sprite, texture;
  39. var cameraOrtho, sceneOrtho;
  40. var dpr = window.devicePixelRatio;
  41. var textureSize = 128 * dpr;
  42. var vector = new THREE.Vector2();
  43. init();
  44. animate();
  45. function init() {
  46. //
  47. var width = window.innerWidth;
  48. var height = window.innerHeight;
  49. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  50. camera.position.z = 20;
  51. cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
  52. cameraOrtho.position.z = 10;
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0x20252f );
  55. sceneOrtho = new THREE.Scene();
  56. //
  57. var geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
  58. var material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
  59. mesh = new THREE.Mesh( geometry, material );
  60. scene.add( mesh );
  61. //
  62. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  63. scene.add( ambientLight );
  64. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  65. camera.add( pointLight );
  66. scene.add( camera );
  67. //
  68. var data = new Uint8Array( textureSize * textureSize * 3 );
  69. texture = new THREE.DataTexture( data, textureSize, textureSize, THREE.RGBFormat );
  70. texture.minFilter = THREE.NearestFilter;
  71. texture.magFilter = THREE.NearestFilter;
  72. //
  73. var spriteMaterial = new THREE.SpriteMaterial( { map: texture } );
  74. sprite = new THREE.Sprite( spriteMaterial );
  75. sprite.scale.set( textureSize, textureSize, 1 );
  76. sceneOrtho.add( sprite );
  77. updateSpritePosition();
  78. //
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.autoClear = false;
  83. document.body.appendChild( renderer.domElement );
  84. //
  85. var selection = document.getElementById( 'selection' );
  86. var controls = new OrbitControls( camera, selection );
  87. controls.enablePan = false;
  88. //
  89. window.addEventListener( 'resize', onWindowResize, false );
  90. }
  91. function onWindowResize() {
  92. var width = window.innerWidth;
  93. var height = window.innerHeight;
  94. camera.aspect = width / height;
  95. camera.updateProjectionMatrix();
  96. cameraOrtho.left = - width / 2;
  97. cameraOrtho.right = width / 2;
  98. cameraOrtho.top = height / 2;
  99. cameraOrtho.bottom = - height / 2;
  100. cameraOrtho.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. updateSpritePosition();
  103. }
  104. function updateSpritePosition() {
  105. var halfWidth = window.innerWidth / 2;
  106. var halfHeight = window.innerHeight / 2;
  107. var halfImageWidth = textureSize / 2;
  108. var halfImageHeight = textureSize / 2;
  109. sprite.position.set( - halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1 );
  110. }
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. mesh.rotation.x += 0.005;
  114. mesh.rotation.y += 0.01;
  115. renderer.clear();
  116. renderer.render( scene, camera );
  117. // calculate start position for copying data
  118. vector.x = ( window.innerWidth * dpr / 2 ) - ( textureSize / 2 );
  119. vector.y = ( window.innerHeight * dpr / 2 ) - ( textureSize / 2 );
  120. renderer.copyFramebufferToTexture( vector, texture );
  121. renderer.clearDepth();
  122. renderer.render( sceneOrtho, cameraOrtho );
  123. }
  124. </script>
  125. </body>
  126. </html>