webgl_framebuffer_texture.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. import { GeometryUtils } from './jsm/utils/GeometryUtils.js';
  38. let camera, scene, renderer;
  39. let line, sprite, texture;
  40. let cameraOrtho, sceneOrtho;
  41. let offset = 0;
  42. const dpr = window.devicePixelRatio;
  43. const textureSize = 128 * dpr;
  44. const vector = new THREE.Vector2();
  45. const color = new THREE.Color();
  46. init();
  47. animate();
  48. function init() {
  49. //
  50. const width = window.innerWidth;
  51. const height = window.innerHeight;
  52. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  53. camera.position.z = 20;
  54. cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
  55. cameraOrtho.position.z = 10;
  56. scene = new THREE.Scene();
  57. sceneOrtho = new THREE.Scene();
  58. //
  59. const points = GeometryUtils.gosper( 8 );
  60. const geometry = new THREE.BufferGeometry();
  61. const positionAttribute = new THREE.Float32BufferAttribute( points, 3 );
  62. geometry.setAttribute( 'position', positionAttribute );
  63. geometry.center();
  64. const colorAttribute = new THREE.BufferAttribute( new Float32Array( positionAttribute.array.length ), 3 );
  65. colorAttribute.setUsage( THREE.DynamicDrawUsage );
  66. geometry.setAttribute( 'color', colorAttribute );
  67. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  68. line = new THREE.Line( geometry, material );
  69. line.scale.setScalar( 0.05 );
  70. scene.add( line );
  71. //
  72. const data = new Uint8Array( textureSize * textureSize * 3 );
  73. texture = new THREE.DataTexture( data, textureSize, textureSize, THREE.RGBFormat );
  74. texture.minFilter = THREE.NearestFilter;
  75. texture.magFilter = THREE.NearestFilter;
  76. //
  77. const spriteMaterial = new THREE.SpriteMaterial( { map: texture } );
  78. sprite = new THREE.Sprite( spriteMaterial );
  79. sprite.scale.set( textureSize, textureSize, 1 );
  80. sceneOrtho.add( sprite );
  81. updateSpritePosition();
  82. //
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.autoClear = false;
  87. document.body.appendChild( renderer.domElement );
  88. //
  89. const selection = document.getElementById( 'selection' );
  90. const controls = new OrbitControls( camera, selection );
  91. controls.enablePan = false;
  92. //
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function onWindowResize() {
  96. const width = window.innerWidth;
  97. const height = window.innerHeight;
  98. camera.aspect = width / height;
  99. camera.updateProjectionMatrix();
  100. cameraOrtho.left = - width / 2;
  101. cameraOrtho.right = width / 2;
  102. cameraOrtho.top = height / 2;
  103. cameraOrtho.bottom = - height / 2;
  104. cameraOrtho.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. updateSpritePosition();
  107. }
  108. function updateSpritePosition() {
  109. const halfWidth = window.innerWidth / 2;
  110. const halfHeight = window.innerHeight / 2;
  111. const halfImageWidth = textureSize / 2;
  112. const halfImageHeight = textureSize / 2;
  113. sprite.position.set( - halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1 );
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. const colorAttribute = line.geometry.getAttribute( 'color' );
  118. updateColors( colorAttribute );
  119. // scene rendering
  120. renderer.clear();
  121. renderer.render( scene, camera );
  122. // calculate start position for copying data
  123. vector.x = ( window.innerWidth * dpr / 2 ) - ( textureSize / 2 );
  124. vector.y = ( window.innerHeight * dpr / 2 ) - ( textureSize / 2 );
  125. renderer.copyFramebufferToTexture( vector, texture );
  126. renderer.clearDepth();
  127. renderer.render( sceneOrtho, cameraOrtho );
  128. }
  129. function updateColors( colorAttribute ) {
  130. const l = colorAttribute.count;
  131. for ( let i = 0; i < l; i ++ ) {
  132. const h = ( ( offset + i ) % l ) / l;
  133. color.setHSL( h, 1, 0.5 );
  134. colorAttribute.setX( i, color.r );
  135. colorAttribute.setY( i, color.g );
  136. colorAttribute.setZ( i, color.b );
  137. }
  138. colorAttribute.needsUpdate = true;
  139. offset -= 25;
  140. }
  141. </script>
  142. </body>
  143. </html>