webgl2_rendertarget_texture2darray.html 7.2 KB

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