2
0

webgpu_textures_2d-array.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.module.js",
  21. "three/addons/": "./jsm/",
  22. "three/nodes": "./jsm/nodes/Nodes.js"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { MeshBasicNodeMaterial, texture, uv, oscTriangle, timerLocal } from 'three/nodes';
  29. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  30. import WebGL from 'three/addons/capabilities/WebGL.js';
  31. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. import { unzipSync } from 'three/addons/libs/fflate.module.js';
  34. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU or WebGL2 support' );
  37. }
  38. let camera, scene, mesh, renderer, stats;
  39. const planeWidth = 50;
  40. const planeHeight = 50;
  41. init();
  42. function init() {
  43. const container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
  46. camera.position.z = 70;
  47. scene = new THREE.Scene();
  48. // width 256, height 256, depth 109, 8-bit, zip archived raw data
  49. new THREE.FileLoader()
  50. .setResponseType( 'arraybuffer' )
  51. .load( 'textures/3d/head256x256x109.zip', function ( data ) {
  52. const zip = unzipSync( new Uint8Array( data ) );
  53. const array = new Uint8Array( zip[ 'head256x256x109' ].buffer );
  54. const map = new THREE.DataArrayTexture( array, 256, 256, 109 );
  55. map.format = THREE.RedFormat;
  56. map.needsUpdate = true;
  57. let coord = uv();
  58. coord = coord.setY( coord.y.oneMinus() ); // flip y
  59. let oscLayers = oscTriangle( timerLocal( .5 ) ); // [ /\/ ] triangle osc animation
  60. oscLayers = oscLayers.add( 1 ).mul( .5 ); // convert osc range of [ -1, 1 ] to [ 0, 1 ]
  61. oscLayers = oscLayers.mul( map.image.depth ); // scale osc range to texture depth
  62. const material = new MeshBasicNodeMaterial();
  63. material.colorNode = texture( map, coord ).depth( oscLayers ).r.remap( 0, 1, - .1, 1.8 ); // remap to make it more visible
  64. const geometry = new THREE.PlaneGeometry( planeWidth, planeHeight );
  65. mesh = new THREE.Mesh( geometry, material );
  66. scene.add( mesh );
  67. } );
  68. renderer = new WebGPURenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  72. container.appendChild( renderer.domElement );
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. window.addEventListener( 'resize', onWindowResize );
  76. }
  77. function onWindowResize() {
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function animate() {
  83. render();
  84. stats.update();
  85. }
  86. function render() {
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>