webgl_framebuffer_texture.html 5.1 KB

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