webgl_framebuffer_texture.html 5.4 KB

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