webgpu_textures_2d-array.html 3.4 KB

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