webgpu_sandbox.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 mesh;
  18. init().then( animate ).catch( onInitError );
  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 geometryMesh = new THREE.BoxBufferGeometry();
  32. const materialMesh = new THREE.MeshBasicMaterial( { map: texture } );
  33. mesh = new THREE.Mesh( geometryMesh, materialMesh );
  34. mesh.position.set( - 1, 0.5, 0 );
  35. scene.add( mesh );
  36. // points
  37. const geometryPoints = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, - 1, 0 ), new THREE.Vector3( 1, - 1, 0 ) ] );
  38. const materialPoints = new THREE.PointsMaterial();
  39. const points = new THREE.Points( geometryPoints, materialPoints );
  40. scene.add( points );
  41. // lines
  42. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  43. new THREE.Vector3( 1, 0, 0 ),
  44. new THREE.Vector3( 2, 0, 0 ),
  45. new THREE.Vector3( 2, 1, 0 ),
  46. new THREE.Vector3( 1, 1, 0 )
  47. ] );
  48. const materialLine = new THREE.LineBasicMaterial();
  49. const line = new THREE.Line( geometryLine, materialLine );
  50. scene.add( line );
  51. renderer = new WebGPURenderer();
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. document.body.appendChild( renderer.domElement );
  55. window.addEventListener( 'resize', onWindowResize, false );
  56. return renderer.init();
  57. }
  58. function onWindowResize() {
  59. camera.aspect = window.innerWidth / window.innerHeight;
  60. camera.updateProjectionMatrix();
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. }
  63. function animate() {
  64. requestAnimationFrame( animate );
  65. mesh.rotation.x += 0.01;
  66. mesh.rotation.y += 0.02;
  67. renderer.render( scene, camera );
  68. }
  69. function onInitError( error ) {
  70. console.error( error );
  71. }
  72. </script>
  73. </body>
  74. </html>