webgl_rendertarget_texture2darray.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. const DIMENSIONS = {
  88. width: 256,
  89. height: 256,
  90. depth: 109
  91. };
  92. const params = {
  93. intensity: 1
  94. };
  95. /** Post-processing objects */
  96. const postProcessScene = new THREE.Scene();
  97. const postProcessCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  98. const renderTarget = new THREE.WebGLArrayRenderTarget( DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth );
  99. renderTarget.texture.format = THREE.RedFormat;
  100. const postProcessMaterial = new THREE.ShaderMaterial( {
  101. uniforms: {
  102. uTexture: { value: null },
  103. uDepth: { value: 55 },
  104. uIntensity: { value: 1.0 }
  105. },
  106. vertexShader: document.getElementById( 'vertex-postprocess' ).textContent.trim(),
  107. fragmentShader: document.getElementById( 'fragment-postprocess' ).textContent.trim()
  108. } );
  109. let depthStep = 0.4;
  110. let camera, scene, mesh, renderer, stats;
  111. const planeWidth = 50;
  112. const planeHeight = 50;
  113. init();
  114. function init() {
  115. const container = document.createElement( 'div' );
  116. document.body.appendChild( container );
  117. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  118. camera.position.z = 70;
  119. scene = new THREE.Scene();
  120. /** Post-processing scene */
  121. const planeGeometry = new THREE.PlaneGeometry( 2, 2 );
  122. const screenQuad = new THREE.Mesh( planeGeometry, postProcessMaterial );
  123. postProcessScene.add( screenQuad );
  124. // 2D Texture array is available on WebGL 2.0
  125. renderer = new THREE.WebGLRenderer();
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. container.appendChild( renderer.domElement );
  129. stats = new Stats();
  130. container.appendChild( stats.dom );
  131. window.addEventListener( 'resize', onWindowResize );
  132. const gui = new GUI();
  133. gui.add( params, 'intensity', 0, 1 ).step( 0.01 ).onChange( value => postProcessMaterial.uniforms.uIntensity.value = value );
  134. gui.open();
  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. const zip = unzipSync( new Uint8Array( data ) );
  140. const array = new Uint8Array( zip[ 'head256x256x109' ].buffer );
  141. const texture = new THREE.DataArrayTexture( array, DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth );
  142. texture.format = THREE.RedFormat;
  143. texture.needsUpdate = true;
  144. const 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. const geometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
  154. mesh = new THREE.Mesh( geometry, material );
  155. scene.add( mesh );
  156. postProcessMaterial.uniforms.uTexture.value = texture;
  157. renderer.setAnimationLoop( animate );
  158. } );
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function animate() {
  166. let value = mesh.material.uniforms[ 'depth' ].value;
  167. value += depthStep;
  168. if ( value > 109.0 || value < 0.0 ) {
  169. if ( value > 1.0 ) value = 109.0 * 2.0 - value;
  170. if ( value < 0.0 ) value = - value;
  171. depthStep = - depthStep;
  172. }
  173. mesh.material.uniforms[ 'depth' ].value = value;
  174. render();
  175. stats.update();
  176. }
  177. /**
  178. * Renders the 2D array into the render target `renderTarget`.
  179. */
  180. function renderTo2DArray() {
  181. const layer = Math.floor( mesh.material.uniforms[ 'depth' ].value );
  182. postProcessMaterial.uniforms.uDepth.value = layer;
  183. renderer.setRenderTarget( renderTarget, layer );
  184. renderer.render( postProcessScene, postProcessCamera );
  185. renderer.setRenderTarget( null );
  186. }
  187. function render() {
  188. // Step 1 - Render the input DataArrayTexture into render target
  189. renderTo2DArray();
  190. // Step 2 - Renders the scene containing the plane with a material
  191. // sampling the render target texture.
  192. renderer.render( scene, camera );
  193. }
  194. </script>
  195. </body>
  196. </html>