webgpu_sandbox.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  15. import WebGPU from './jsm/renderers/webgpu/WebGPU.js';
  16. let camera, scene, renderer;
  17. let box;
  18. init().then( animate ).catch( error );
  19. async function init() {
  20. if ( WebGPU.isAvailable() === false ) {
  21. document.body.appendChild( WebGPU.getErrorMessage() );
  22. throw 'No WebGPU support';
  23. }
  24. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  25. camera.position.z = 4;
  26. scene = new THREE.Scene();
  27. scene.background = new THREE.Color( 0x222222 );
  28. // textured mesh
  29. const loader = new THREE.TextureLoader();
  30. const texture = loader.load( './textures/uv_grid_opengl.jpg' );
  31. const geometryBox = new THREE.BoxBufferGeometry();
  32. const materialBox = new THREE.MeshBasicMaterial( { map: texture } );
  33. box = new THREE.Mesh( geometryBox, materialBox );
  34. box.position.set( - 1, 1, 0 );
  35. scene.add( box );
  36. // data texture
  37. const geometryPlane = new THREE.PlaneBufferGeometry();
  38. const materialPlane = new THREE.MeshBasicMaterial( { map: createDataTexture(), transparent: true, opacity: 0.5 } );
  39. const plane = new THREE.Mesh( geometryPlane, materialPlane );
  40. plane.position.set( - 1, - 1, 0 );
  41. scene.add( plane );
  42. // points
  43. const geometryPoints = new THREE.BufferGeometry().setFromPoints( [
  44. new THREE.Vector3( 0.5, 0.5, 0 ),
  45. new THREE.Vector3( 0.5, - 0.5, 0 ),
  46. new THREE.Vector3( - 0.5, 0.5, 0 ),
  47. new THREE.Vector3( - 0.5, - 0.5, 0 ) ] );
  48. const materialPoints = new THREE.PointsMaterial();
  49. const points = new THREE.Points( geometryPoints, materialPoints );
  50. points.position.set( 1, - 1, 0 );
  51. scene.add( points );
  52. // lines
  53. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  54. new THREE.Vector3( - 0.5, - 0.5, 0 ),
  55. new THREE.Vector3( 0.5, - 0.5, 0 ),
  56. new THREE.Vector3( 0.5, 0.5, 0 ),
  57. new THREE.Vector3( - 0.5, 0.5, 0 )
  58. ] );
  59. const materialLine = new THREE.LineBasicMaterial();
  60. const line = new THREE.Line( geometryLine, materialLine );
  61. line.position.set( 1, 1, 0 );
  62. scene.add( line );
  63. //
  64. renderer = new WebGPURenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. document.body.appendChild( renderer.domElement );
  68. window.addEventListener( 'resize', onWindowResize, false );
  69. return renderer.init();
  70. }
  71. function onWindowResize() {
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. box.rotation.x += 0.01;
  79. box.rotation.y += 0.02;
  80. renderer.render( scene, camera );
  81. }
  82. function createDataTexture() {
  83. const color = new THREE.Color( 0xff0000 );
  84. const width = 512;
  85. const height = 512;
  86. const size = width * height;
  87. const data = new Uint8Array( 4 * size );
  88. const r = Math.floor( color.r * 255 );
  89. const g = Math.floor( color.g * 255 );
  90. const b = Math.floor( color.b * 255 );
  91. for ( let i = 0; i < size; i ++ ) {
  92. const stride = i * 4;
  93. data[ stride ] = r;
  94. data[ stride + 1 ] = g;
  95. data[ stride + 2 ] = b;
  96. data[ stride + 3 ] = 255;
  97. }
  98. return new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  99. }
  100. function error( error ) {
  101. console.error( error );
  102. }
  103. </script>
  104. </body>
  105. </html>