webgl2_rendertarget_texture2darray.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 2D texture array framebuffer attachment</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. </head>
  9. <script id="vertex-postprocess" type="x-shader/x-vertex">
  10. out vec2 vUv;
  11. void main()
  12. {
  13. vUv = uv;
  14. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  15. }
  16. </script>
  17. <!--
  18. Fragment shader processing an input 2d texture array and writing the output
  19. into a framebuffer. The framebuffer should have a 2d texture array bound
  20. as color attachment.
  21. -->
  22. <script id="fragment-postprocess" type="x-shader/x-fragment">
  23. precision highp sampler2DArray;
  24. precision mediump float;
  25. in vec2 vUv;
  26. uniform sampler2DArray uTexture;
  27. uniform int uDepth;
  28. uniform float uSampleLeft;
  29. uniform float uSampleWidth;
  30. void main()
  31. {
  32. float voxel = texture(uTexture, vec3( vUv, uDepth )).r;
  33. gl_FragColor.r = (voxel - uSampleLeft) / uSampleWidth;
  34. }
  35. </script>
  36. <script id="vs" type="x-shader/x-vertex">
  37. uniform vec2 size;
  38. out vec2 vUv;
  39. void main() {
  40. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  41. // Convert position.xy to 1.0-0.0
  42. vUv.xy = position.xy / size + 0.5;
  43. vUv.y = 1.0 - vUv.y; // original data is upside down
  44. }
  45. </script>
  46. <script id="fs" type="x-shader/x-fragment">
  47. precision highp float;
  48. precision highp int;
  49. precision highp sampler2DArray;
  50. uniform sampler2DArray diffuse;
  51. in vec2 vUv;
  52. uniform int depth;
  53. void main() {
  54. vec4 color = texture( diffuse, vec3( vUv, depth ) );
  55. // lighten a bit
  56. gl_FragColor = vec4( color.rrr * 1.5, 1.0 );
  57. }
  58. </script>
  59. <body>
  60. <div id="info">
  61. <a href="https://threejs.org" target="_blank" rel="noopener">
  62. three.js
  63. </a>
  64. - 2D Texture array framebuffer color attachment
  65. <br />
  66. <p>
  67. This example shows how to render to an array of 2D texture.</br>
  68. WebGL2 allows to render to specific "layers" in 3D texture and array of textures.</br>
  69. You can use the mouse left click to change the intensity of the render target.
  70. </p>
  71. Scanned head data by
  72. <a href="https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering" target="_blank" rel="noopener">Divine Augustine</a><br />
  73. licensed under
  74. <a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
  75. </div>
  76. <script type="module">
  77. import * as THREE from '../build/three.module.js';
  78. import Stats from './jsm/libs/stats.module.js';
  79. import { unzipSync } from './jsm/libs/fflate.module.min.js';
  80. import { WEBGL } from './jsm/WebGL.js';
  81. if ( WEBGL.isWebGL2Available() === false ) {
  82. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  83. }
  84. const DIMENSIONS = {
  85. width: 256,
  86. height: 256,
  87. depth: 109
  88. };
  89. const App = {
  90. mousePrevious: new THREE.Vector2()
  91. };
  92. /** Post-processing objects */
  93. const postProcessScene = new THREE.Scene();
  94. const postProcessCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  95. const renderTargetTexture = new THREE.DataTexture2DArray();
  96. renderTargetTexture.format = THREE.RedFormat;
  97. renderTargetTexture.type = THREE.UnsignedByteType;
  98. const renderTarget = new THREE.WebGLRenderTarget( DIMENSIONS.width, DIMENSIONS.height );
  99. renderTarget.depth = DIMENSIONS.depth;
  100. renderTarget.setTexture( renderTargetTexture );
  101. const postProcessMaterial = new THREE.ShaderMaterial( {
  102. uniforms: {
  103. uTexture: { value: null },
  104. uDepth: { value: 55 },
  105. uSampleLeft: { value: 0 },
  106. uSampleWidth: { value: 1.0 },
  107. },
  108. vertexShader: document.getElementById( 'vertex-postprocess' ).textContent.trim(),
  109. fragmentShader: document.getElementById( 'fragment-postprocess' ).textContent.trim()
  110. } );
  111. var depthStep = 0.4;
  112. var camera, scene, mesh, renderer, stats;
  113. var planeWidth = 50;
  114. var planeHeight = 50;
  115. init();
  116. function init() {
  117. var container = document.createElement( 'div' );
  118. document.body.appendChild( container );
  119. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  120. camera.position.z = 70;
  121. scene = new THREE.Scene();
  122. /** Post-processing scene */
  123. const planeGeometry = new THREE.PlaneBufferGeometry( 2, 2 );
  124. const screenQuad = new THREE.Mesh( planeGeometry, postProcessMaterial );
  125. postProcessScene.add( screenQuad );
  126. // 2D Texture array is available on WebGL 2.0
  127. renderer = new THREE.WebGLRenderer();
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. container.appendChild( renderer.domElement );
  131. renderer.domElement.addEventListener( 'mousedown', onMouseDown );
  132. stats = new Stats();
  133. container.appendChild( stats.dom );
  134. window.addEventListener( 'resize', onWindowResize, false );
  135. // width 256, height 256, depth 109, 8-bit, zip archived raw data
  136. new THREE.FileLoader()
  137. .setResponseType( 'arraybuffer' )
  138. .load( 'textures/3d/head256x256x109.zip', function ( data ) {
  139. var zip = unzipSync( new Uint8Array( data ) );
  140. const array = new Uint8Array( zip[ 'head256x256x109' ].buffer );
  141. const texture = new THREE.DataTexture2DArray( array, DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth );
  142. texture.format = THREE.RedFormat;
  143. texture.type = THREE.UnsignedByteType;
  144. var material = new THREE.ShaderMaterial( {
  145. uniforms: {
  146. diffuse: { value: renderTarget.texture },
  147. depth: { value: 55 },
  148. size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
  149. },
  150. vertexShader: document.getElementById( 'vs' ).textContent.trim(),
  151. fragmentShader: document.getElementById( 'fs' ).textContent.trim()
  152. } );
  153. var geometry = new THREE.PlaneBufferGeometry( planeWidth, planeHeight );
  154. mesh = new THREE.Mesh( geometry, material );
  155. scene.add( mesh );
  156. postProcessMaterial.uniforms.uTexture.value = texture;
  157. animate();
  158. } );
  159. }
  160. function onMouseDown(e) {
  161. renderer.domElement.addEventListener('mouseup', onMouseUp);
  162. renderer.domElement.addEventListener('mousemove', onMouseMove);
  163. App.mousePrevious.set(e.clientX, e.clientY);
  164. }
  165. function onMouseUp() {
  166. renderer.domElement.removeEventListener('mousemove', onMouseMove);
  167. renderer.domElement.removeEventListener('mouseup', onMouseUp);
  168. }
  169. function onMouseMove(e) {
  170. const x = e.clientX;
  171. const y = e.clientY;
  172. const deltaX = ( x - App.mousePrevious.x ) * 0.001;
  173. const deltaY = ( y - App.mousePrevious.y ) * 0.001;
  174. postProcessMaterial.uniforms.uSampleLeft.value += deltaX;
  175. postProcessMaterial.uniforms.uSampleWidth.value += deltaY;
  176. App.mousePrevious.set( x, y );
  177. }
  178. function onWindowResize() {
  179. camera.aspect = window.innerWidth / window.innerHeight;
  180. camera.updateProjectionMatrix();
  181. renderer.setSize( window.innerWidth, window.innerHeight );
  182. }
  183. function animate() {
  184. requestAnimationFrame( animate );
  185. var value = mesh.material.uniforms[ 'depth' ].value;
  186. value += depthStep;
  187. if ( value > 109.0 || value < 0.0 ) {
  188. if ( value > 1.0 ) value = 109.0 * 2.0 - value;
  189. if ( value < 0.0 ) value = - value;
  190. depthStep = - depthStep;
  191. }
  192. mesh.material.uniforms[ 'depth' ].value = value;
  193. render();
  194. }
  195. /**
  196. * Renders the 2D array into the render target `renderTarget`.
  197. */
  198. function renderTo2DArray() {
  199. const layer = Math.floor( mesh.material.uniforms[ 'depth' ].value );
  200. postProcessMaterial.uniforms.uDepth.value = layer;
  201. renderer.setRenderTarget( renderTarget, layer );
  202. renderer.render( postProcessScene, postProcessCamera );
  203. renderer.setRenderTarget( null );
  204. }
  205. function render() {
  206. // Step 1 - Render the input DataTexture2DArray into a
  207. // DataTexture2DArray render target.
  208. renderTo2DArray();
  209. // Step 2 - Renders the scene containing the plane with a material
  210. // sampling the render target texture.
  211. renderer.render( scene, camera );
  212. }
  213. </script>
  214. </body>
  215. </html>