webgpu_textures_2d-array.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - 2D Texture array<br />
  12. Scanned head data by
  13. <a href="https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering" target="_blank" rel="noopener">Divine Augustine</a><br />
  14. licensed under
  15. <a href="https://www.codeproject.com/info/cpol10.aspx" target="_blank" rel="noopener">CPOL</a>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.webgpu.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { texture, uv, oscTriangle, timerLocal } from 'three/tsl';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { unzipSync } from 'three/addons/libs/fflate.module.js';
  31. let camera, scene, mesh, renderer, stats;
  32. const planeWidth = 50;
  33. const planeHeight = 50;
  34. init();
  35. function init() {
  36. const container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  39. camera.position.z = 70;
  40. scene = new THREE.Scene();
  41. // width 256, height 256, depth 109, 8-bit, zip archived raw data
  42. new THREE.FileLoader()
  43. .setResponseType( 'arraybuffer' )
  44. .load( 'textures/3d/head256x256x109.zip', function ( data ) {
  45. const zip = unzipSync( new Uint8Array( data ) );
  46. const array = new Uint8Array( zip[ 'head256x256x109' ].buffer );
  47. const map = new THREE.DataArrayTexture( array, 256, 256, 109 );
  48. map.format = THREE.RedFormat;
  49. map.needsUpdate = true;
  50. let coord = uv();
  51. coord = coord.setY( coord.y.oneMinus() ); // flip y
  52. let oscLayers = oscTriangle( timerLocal( .5 ) ); // [ /\/ ] triangle osc animation
  53. oscLayers = oscLayers.add( 1 ).mul( .5 ); // convert osc range of [ -1, 1 ] to [ 0, 1 ]
  54. oscLayers = oscLayers.mul( map.image.depth ); // scale osc range to texture depth
  55. const material = new THREE.MeshBasicNodeMaterial();
  56. material.colorNode = texture( map, coord ).depth( oscLayers ).r.remap( 0, 1, - .1, 1.8 ); // remap to make it more visible
  57. const geometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
  58. mesh = new THREE.Mesh( geometry, material );
  59. scene.add( mesh );
  60. } );
  61. renderer = new THREE.WebGPURenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.setAnimationLoop( animate );
  65. container.appendChild( renderer.domElement );
  66. stats = new Stats();
  67. container.appendChild( stats.dom );
  68. window.addEventListener( 'resize', onWindowResize );
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. function animate() {
  76. render();
  77. stats.update();
  78. }
  79. function render() {
  80. renderer.render( scene, camera );
  81. }
  82. </script>
  83. </body>
  84. </html>