webgl_framebuffer_texture.html 5.3 KB

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