123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - 2D texture array</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <script id="vs" type="x-shader/x-vertex">
- #version 300 es
- uniform vec2 size;
- out vec2 vUv;
- void main() {
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
- // Convert position.xy to 1.0-0.0
- vUv.xy = position.xy / size + 0.5;
- vUv.y = 1.0 - vUv.y; // original data is upside down
- }
- </script>
- <script id="fs" type="x-shader/x-fragment">
- #version 300 es
- precision highp float;
- precision highp int;
- precision highp sampler2DArray;
- uniform sampler2DArray diffuse;
- in vec2 vUv;
- uniform int depth;
- out vec4 out_FragColor;
- void main() {
- vec4 color = texture( diffuse, vec3( vUv, depth ) );
- // lighten a bit
- out_FragColor = vec4( color.rrr * 1.5, 1.0 );
- }
- </script>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 2D Texture array<br />
- Scanned head data by
- <a href="https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering" target="_blank" rel="noopener">Divine Augustine</a><br />
- licensed under
- <a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
- </div>
- <script src="js/libs/jszip.min.js"></script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { WEBGL } from './jsm/WebGL.js';
- if ( WEBGL.isWebGL2Available() === false ) {
- document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
- }
- var camera, scene, mesh, renderer, stats;
- var planeWidth = 50;
- var planeHeight = 50;
- var depthStep = 0.4;
- init();
- animate();
- function init() {
- var container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
- camera.position.z = 70;
- scene = new THREE.Scene();
- // width 256, height 256, depth 109, 8-bit, zip archived raw data
- new THREE.FileLoader()
- .setResponseType( 'arraybuffer' )
- .load( 'textures/3d/head256x256x109.zip', function ( data ) {
- var zip = new JSZip( data );
- var array = zip.files[ 'head256x256x109' ].asUint8Array();
- var texture = new THREE.DataTexture2DArray( array, 256, 256, 109 );
- texture.format = THREE.RedFormat;
- texture.type = THREE.UnsignedByteType;
- var material = new THREE.ShaderMaterial( {
- uniforms: {
- diffuse: { value: texture },
- depth: { value: 0 },
- size: { value: new THREE.Vector2( planeWidth, planeHeight ) }
- },
- vertexShader: document.getElementById( 'vs' ).textContent.trim(),
- fragmentShader: document.getElementById( 'fs' ).textContent.trim()
- } );
- var geometry = new THREE.PlaneBufferGeometry( planeWidth, planeHeight );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- } );
- // 2D Texture array is available on WebGL 2.0
- var canvas = document.createElement( 'canvas' );
- var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );
- renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- if ( mesh ) {
- var value = mesh.material.uniforms[ "depth" ].value;
- value += depthStep;
- if ( value > 109.0 || value < 0.0 ) {
- if ( value > 1.0 ) value = 109.0 * 2.0 - value;
- if ( value < 0.0 ) value = - value;
- depthStep = - depthStep;
- }
- mesh.material.uniforms[ "depth" ].value = value;
- }
- render();
- stats.update();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|