webgl2_rendertarget_texture2darray.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. <!-- Import maps polyfill -->
  75. <!-- Remove this when import maps will be widely supported -->
  76. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  77. <script type="importmap">
  78. {
  79. "imports": {
  80. "three": "../build/three.module.js",
  81. "three/addons/": "./jsm/"
  82. }
  83. }
  84. </script>
  85. <script type="module">
  86. import * as THREE from 'three';
  87. import Stats from 'three/addons/libs/stats.module.js';
  88. import { unzipSync } from 'three/addons/libs/fflate.module.js';
  89. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  90. import WebGL from 'three/addons/capabilities/WebGL.js';
  91. if ( WebGL.isWebGL2Available() === false ) {
  92. document.body.appendChild( WebGL.getWebGL2ErrorMessage() );
  93. }
  94. const DIMENSIONS = {
  95. width: 256,
  96. height: 256,
  97. depth: 109
  98. };
  99. const params = {
  100. intensity: 1
  101. };
  102. /** Post-processing objects */
  103. const postProcessScene = new THREE.Scene();
  104. const postProcessCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  105. const renderTarget = new THREE.WebGLArrayRenderTarget( DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth );
  106. renderTarget.texture.format = THREE.RedFormat;
  107. const postProcessMaterial = new THREE.ShaderMaterial( {
  108. uniforms: {
  109. uTexture: { value: null },
  110. uDepth: { value: 55 },
  111. uIntensity: { value: 1.0 }
  112. },
  113. vertexShader: document.getElementById( 'vertex-postprocess' ).textContent.trim(),
  114. fragmentShader: document.getElementById( 'fragment-postprocess' ).textContent.trim()
  115. } );
  116. let depthStep = 0.4;
  117. let camera, scene, mesh, renderer, stats;
  118. const planeWidth = 50;
  119. const planeHeight = 50;
  120. init();
  121. function init() {
  122. const container = document.createElement( 'div' );
  123. document.body.appendChild( container );
  124. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  125. camera.position.z = 70;
  126. scene = new THREE.Scene();
  127. /** Post-processing scene */
  128. const planeGeometry = new THREE.PlaneGeometry( 2, 2 );
  129. const screenQuad = new THREE.Mesh( planeGeometry, postProcessMaterial );
  130. postProcessScene.add( screenQuad );
  131. // 2D Texture array is available on WebGL 2.0
  132. renderer = new THREE.WebGLRenderer();
  133. renderer.setPixelRatio( window.devicePixelRatio );
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. container.appendChild( renderer.domElement );
  136. stats = new Stats();
  137. container.appendChild( stats.dom );
  138. window.addEventListener( 'resize', onWindowResize );
  139. const gui = new GUI();
  140. gui.add( params, 'intensity', 0, 1 ).step( 0.01 ).onChange( value => postProcessMaterial.uniforms.uIntensity.value = value );
  141. gui.open();
  142. // width 256, height 256, depth 109, 8-bit, zip archived raw data
  143. new THREE.FileLoader()
  144. .setResponseType( 'arraybuffer' )
  145. .load( 'textures/3d/head256x256x109.zip', function ( data ) {
  146. const zip = unzipSync( new Uint8Array( data ) );
  147. const array = new Uint8Array( zip[ 'head256x256x109' ].buffer );
  148. const texture = new THREE.DataArrayTexture( array, DIMENSIONS.width, DIMENSIONS.height, DIMENSIONS.depth );
  149. texture.format = THREE.RedFormat;
  150. texture.needsUpdate = true;
  151. const material = new THREE.ShaderMaterial( {
  152. uniforms: {
  153. diffuse: { value: renderTarget.texture },
  154. depth: { value: 55 },
  155. size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
  156. },
  157. vertexShader: document.getElementById( 'vs' ).textContent.trim(),
  158. fragmentShader: document.getElementById( 'fs' ).textContent.trim()
  159. } );
  160. const geometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
  161. mesh = new THREE.Mesh( geometry, material );
  162. scene.add( mesh );
  163. postProcessMaterial.uniforms.uTexture.value = texture;
  164. animate();
  165. } );
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. renderer.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. let value = mesh.material.uniforms[ 'depth' ].value;
  175. value += depthStep;
  176. if ( value > 109.0 || value < 0.0 ) {
  177. if ( value > 1.0 ) value = 109.0 * 2.0 - value;
  178. if ( value < 0.0 ) value = - value;
  179. depthStep = - depthStep;
  180. }
  181. mesh.material.uniforms[ 'depth' ].value = value;
  182. render();
  183. }
  184. /**
  185. * Renders the 2D array into the render target `renderTarget`.
  186. */
  187. function renderTo2DArray() {
  188. const layer = Math.floor( mesh.material.uniforms[ 'depth' ].value );
  189. postProcessMaterial.uniforms.uDepth.value = layer;
  190. renderer.setRenderTarget( renderTarget, layer );
  191. renderer.render( postProcessScene, postProcessCamera );
  192. renderer.setRenderTarget( null );
  193. }
  194. function render() {
  195. // Step 1 - Render the input DataArrayTexture into render target
  196. renderTo2DArray();
  197. // Step 2 - Renders the scene containing the plane with a material
  198. // sampling the render target texture.
  199. renderer.render( scene, camera );
  200. }
  201. </script>
  202. </body>
  203. </html>