webgpu_sandbox.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <html lang="en">
  2. <head>
  3. <title>WebGPU Sandbox</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - sandbox<br/>(Chrome Canary with flag: --enable-unsafe-webgpu)
  11. </div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import { DDSLoader } from './jsm/loaders/DDSLoader.js';
  15. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  16. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  17. let camera, scene, renderer;
  18. let box;
  19. init().then( animate ).catch( error );
  20. async function init() {
  21. if ( WebGPU.isAvailable() === false ) {
  22. document.body.appendChild( WebGPU.getErrorMessage() );
  23. throw 'No WebGPU support';
  24. }
  25. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  26. camera.position.z = 4;
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0x222222 );
  29. // textured mesh
  30. const textureLoader = new THREE.TextureLoader();
  31. const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  32. const geometryBox = new THREE.BoxBufferGeometry();
  33. const materialBox = new THREE.MeshBasicMaterial( { map: texture } );
  34. box = new THREE.Mesh( geometryBox, materialBox );
  35. box.position.set( - 1, 1, 0 );
  36. scene.add( box );
  37. // data texture
  38. const geometryPlane = new THREE.PlaneBufferGeometry();
  39. const materialPlane = new THREE.MeshBasicMaterial( { map: createDataTexture(), transparent: true, opacity: 0.5 } );
  40. const plane = new THREE.Mesh( geometryPlane, materialPlane );
  41. plane.position.set( - 1, - 1, 0 );
  42. scene.add( plane );
  43. // compress texture
  44. const ddsLoader = new DDSLoader();
  45. const dxt5Texture = ddsLoader.load( './textures/compressed/explosion_dxt5_mip.dds' );
  46. const materialBoxCompressed = new THREE.MeshBasicMaterial( { map: dxt5Texture, transparent: true } );
  47. box = new THREE.Mesh( geometryBox, materialBoxCompressed );
  48. box.position.set( - 3, 1, 0 );
  49. scene.add( box );
  50. // points
  51. const geometryPoints = new THREE.BufferGeometry().setFromPoints( [
  52. new THREE.Vector3( 0.5, 0.5, 0 ),
  53. new THREE.Vector3( 0.5, - 0.5, 0 ),
  54. new THREE.Vector3( - 0.5, 0.5, 0 ),
  55. new THREE.Vector3( - 0.5, - 0.5, 0 ) ] );
  56. const materialPoints = new THREE.PointsMaterial();
  57. const points = new THREE.Points( geometryPoints, materialPoints );
  58. points.position.set( 1, - 1, 0 );
  59. scene.add( points );
  60. // lines
  61. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  62. new THREE.Vector3( - 0.5, - 0.5, 0 ),
  63. new THREE.Vector3( 0.5, - 0.5, 0 ),
  64. new THREE.Vector3( 0.5, 0.5, 0 ),
  65. new THREE.Vector3( - 0.5, 0.5, 0 )
  66. ] );
  67. const materialLine = new THREE.LineBasicMaterial();
  68. const line = new THREE.Line( geometryLine, materialLine );
  69. line.position.set( 1, 1, 0 );
  70. scene.add( line );
  71. //
  72. renderer = new WebGPURenderer( { extensions: [ 'texture-compression-bc' ] } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. document.body.appendChild( renderer.domElement );
  76. window.addEventListener( 'resize', onWindowResize, false );
  77. return renderer.init();
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. box.rotation.x += 0.01;
  87. box.rotation.y += 0.02;
  88. renderer.render( scene, camera );
  89. }
  90. function createDataTexture() {
  91. const color = new THREE.Color( 0xff0000 );
  92. const width = 512;
  93. const height = 512;
  94. const size = width * height;
  95. const data = new Uint8Array( 4 * size );
  96. const r = Math.floor( color.r * 255 );
  97. const g = Math.floor( color.g * 255 );
  98. const b = Math.floor( color.b * 255 );
  99. for ( let i = 0; i < size; i ++ ) {
  100. const stride = i * 4;
  101. data[ stride ] = r;
  102. data[ stride + 1 ] = g;
  103. data[ stride + 2 ] = b;
  104. data[ stride + 3 ] = 255;
  105. }
  106. return new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  107. }
  108. function error( error ) {
  109. console.error( error );
  110. }
  111. </script>
  112. </body>
  113. </html>