webgl2_materials_texture2darray.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 2D texture array</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="vs" type="x-shader/x-vertex">
  10. #version 300 es
  11. uniform vec2 size;
  12. out vec2 vUv;
  13. void main() {
  14. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  15. // Convert position.xy to 1.0-0.0
  16. vUv.xy = position.xy / size + 0.5;
  17. vUv.y = 1.0 - vUv.y; // original data is upside down
  18. }
  19. </script>
  20. <script id="fs" type="x-shader/x-fragment">
  21. #version 300 es
  22. precision highp float;
  23. precision highp int;
  24. precision highp sampler2DArray;
  25. uniform sampler2DArray diffuse;
  26. in vec2 vUv;
  27. uniform int depth;
  28. out vec4 out_FragColor;
  29. void main() {
  30. vec4 color = texture( diffuse, vec3( vUv, depth ) );
  31. // lighten a bit
  32. out_FragColor = vec4( color.rrr * 1.5, 1.0 );
  33. }
  34. </script>
  35. <body>
  36. <div id="info">
  37. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 2D Texture array<br />
  38. Scanned head data by
  39. <a href="https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering" target="_blank" rel="noopener">Divine Augustine</a><br />
  40. licensed under
  41. <a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
  42. </div>
  43. <script src="js/libs/jszip.min.js"></script>
  44. <script type="module">
  45. import * as THREE from '../build/three.module.js';
  46. import Stats from './jsm/libs/stats.module.js';
  47. import { WEBGL } from './jsm/WebGL.js';
  48. if ( WEBGL.isWebGL2Available() === false ) {
  49. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  50. }
  51. var camera, scene, mesh, renderer, stats;
  52. var planeWidth = 50;
  53. var planeHeight = 50;
  54. var depthStep = 0.4;
  55. init();
  56. animate();
  57. function init() {
  58. var container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  61. camera.position.z = 70;
  62. scene = new THREE.Scene();
  63. // width 256, height 256, depth 109, 8-bit, zip archived raw data
  64. new THREE.FileLoader()
  65. .setResponseType( 'arraybuffer' )
  66. .load( 'textures/3d/head256x256x109.zip', function ( data ) {
  67. var zip = new JSZip( data );
  68. var array = zip.files[ 'head256x256x109' ].asUint8Array();
  69. var texture = new THREE.DataTexture2DArray( array, 256, 256, 109 );
  70. texture.format = THREE.RedFormat;
  71. texture.type = THREE.UnsignedByteType;
  72. var material = new THREE.ShaderMaterial( {
  73. uniforms: {
  74. diffuse: { value: texture },
  75. depth: { value: 0 },
  76. size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
  77. },
  78. vertexShader: document.getElementById( 'vs' ).textContent.trim(),
  79. fragmentShader: document.getElementById( 'fs' ).textContent.trim()
  80. } );
  81. var geometry = new THREE.PlaneBufferGeometry( planeWidth, planeHeight );
  82. mesh = new THREE.Mesh( geometry, material );
  83. scene.add( mesh );
  84. } );
  85. // 2D Texture array is available on WebGL 2.0
  86. var canvas = document.createElement( 'canvas' );
  87. var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );
  88. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. container.appendChild( renderer.domElement );
  92. stats = new Stats();
  93. container.appendChild( stats.dom );
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. if ( mesh ) {
  104. var value = mesh.material.uniforms[ "depth" ].value;
  105. value += depthStep;
  106. if ( value > 109.0 || value < 0.0 ) {
  107. if ( value > 1.0 ) value = 109.0 * 2.0 - value;
  108. if ( value < 0.0 ) value = - value;
  109. depthStep = - depthStep;
  110. }
  111. mesh.material.uniforms[ "depth" ].value = value;
  112. }
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>