webgl_framebuffer_texture.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 * as 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. texture = new THREE.FramebufferTexture( textureSize, textureSize, THREE.RGBFormat );
  73. texture.minFilter = THREE.NearestFilter;
  74. texture.magFilter = THREE.NearestFilter;
  75. //
  76. const spriteMaterial = new THREE.SpriteMaterial( { map: texture } );
  77. sprite = new THREE.Sprite( spriteMaterial );
  78. sprite.scale.set( textureSize, textureSize, 1 );
  79. sceneOrtho.add( sprite );
  80. updateSpritePosition();
  81. //
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.autoClear = false;
  86. document.body.appendChild( renderer.domElement );
  87. //
  88. const selection = document.getElementById( 'selection' );
  89. const controls = new OrbitControls( camera, selection );
  90. controls.enablePan = false;
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. const width = window.innerWidth;
  96. const height = window.innerHeight;
  97. camera.aspect = width / height;
  98. camera.updateProjectionMatrix();
  99. cameraOrtho.left = - width / 2;
  100. cameraOrtho.right = width / 2;
  101. cameraOrtho.top = height / 2;
  102. cameraOrtho.bottom = - height / 2;
  103. cameraOrtho.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. updateSpritePosition();
  106. }
  107. function updateSpritePosition() {
  108. const halfWidth = window.innerWidth / 2;
  109. const halfHeight = window.innerHeight / 2;
  110. const halfImageWidth = textureSize / 2;
  111. const halfImageHeight = textureSize / 2;
  112. sprite.position.set( - halfWidth + halfImageWidth, halfHeight - halfImageHeight, 1 );
  113. }
  114. function animate() {
  115. requestAnimationFrame( animate );
  116. const colorAttribute = line.geometry.getAttribute( 'color' );
  117. updateColors( colorAttribute );
  118. // scene rendering
  119. renderer.clear();
  120. renderer.render( scene, camera );
  121. // calculate start position for copying data
  122. vector.x = ( window.innerWidth * dpr / 2 ) - ( textureSize / 2 );
  123. vector.y = ( window.innerHeight * dpr / 2 ) - ( textureSize / 2 );
  124. renderer.copyFramebufferToTexture( vector, texture );
  125. renderer.clearDepth();
  126. renderer.render( sceneOrtho, cameraOrtho );
  127. }
  128. function updateColors( colorAttribute ) {
  129. const l = colorAttribute.count;
  130. for ( let i = 0; i < l; i ++ ) {
  131. const h = ( ( offset + i ) % l ) / l;
  132. color.setHSL( h, 1, 0.5 );
  133. colorAttribute.setX( i, color.r );
  134. colorAttribute.setY( i, color.g );
  135. colorAttribute.setZ( i, color.b );
  136. }
  137. colorAttribute.needsUpdate = true;
  138. offset -= 25;
  139. }
  140. </script>
  141. </body>
  142. </html>