webgpu_sandbox.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.BoxGeometry();
  33. const materialBox = new THREE.MeshBasicMaterial( { map: texture } );
  34. box = new THREE.Mesh( geometryBox, materialBox );
  35. box.position.set( 0, 1, 0 );
  36. scene.add( box );
  37. // data texture
  38. const geometryPlane = new THREE.PlaneGeometry();
  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( 0, - 1, 0 );
  42. scene.add( plane );
  43. // compressed texture
  44. const ddsLoader = new DDSLoader();
  45. const dxt5Texture = ddsLoader.load( './textures/compressed/explosion_dxt5_mip.dds' );
  46. const materialCompressed = new THREE.MeshBasicMaterial( { map: dxt5Texture, transparent: true } );
  47. const boxCompressed = new THREE.Mesh( geometryBox, materialCompressed );
  48. boxCompressed.position.set( - 2, 1, 0 );
  49. scene.add( boxCompressed );
  50. // points
  51. const points = [];
  52. for ( let i = 0; i < 1000; i ++ ) {
  53. const point = new THREE.Vector3().random().subScalar( 0.5 );
  54. points.push( point );
  55. }
  56. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  57. const materialPoints = new THREE.PointsMaterial();
  58. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  59. pointCloud.position.set( 2, - 1, 0 );
  60. scene.add( pointCloud );
  61. // lines
  62. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  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. new THREE.Vector3( - 0.5, 0.5, 0 )
  67. ] );
  68. const materialLine = new THREE.LineBasicMaterial();
  69. const line = new THREE.Line( geometryLine, materialLine );
  70. line.position.set( 2, 1, 0 );
  71. scene.add( line );
  72. //
  73. renderer = new WebGPURenderer( { extensions: [ 'texture-compression-bc' ] } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. document.body.appendChild( renderer.domElement );
  77. window.addEventListener( 'resize', onWindowResize, false );
  78. return renderer.init();
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. box.rotation.x += 0.01;
  88. box.rotation.y += 0.02;
  89. renderer.render( scene, camera );
  90. }
  91. function createDataTexture() {
  92. const color = new THREE.Color( 0xff0000 );
  93. const width = 512;
  94. const height = 512;
  95. const size = width * height;
  96. const data = new Uint8Array( 4 * size );
  97. const r = Math.floor( color.r * 255 );
  98. const g = Math.floor( color.g * 255 );
  99. const b = Math.floor( color.b * 255 );
  100. for ( let i = 0; i < size; i ++ ) {
  101. const stride = i * 4;
  102. data[ stride ] = r;
  103. data[ stride + 1 ] = g;
  104. data[ stride + 2 ] = b;
  105. data[ stride + 3 ] = 255;
  106. }
  107. return new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  108. }
  109. function error( error ) {
  110. console.error( error );
  111. }
  112. </script>
  113. </body>
  114. </html>