webgpu_sandbox.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. let camera, scene, renderer;
  16. let mesh;
  17. init().then( animate );
  18. async function init() {
  19. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  20. camera.position.z = 4;
  21. scene = new THREE.Scene();
  22. scene.background = new THREE.Color( 0xffffff );
  23. // textured mesh
  24. const loader = new THREE.TextureLoader();
  25. const texture = loader.load( './textures/uv_grid_opengl.jpg' );
  26. const geometryMesh = new THREE.BoxBufferGeometry();
  27. const materialMesh = new THREE.MeshBasicMaterial( { map: texture } );
  28. mesh = new THREE.Mesh( geometryMesh, materialMesh );
  29. mesh.position.set( - 1, 0.5, 0 );
  30. scene.add( mesh );
  31. // points
  32. const geometryPoints = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, - 1, 0 ), new THREE.Vector3( 1, - 1, 0 ) ] );
  33. const materialPoints = new THREE.PointsMaterial();
  34. const points = new THREE.Points( geometryPoints, materialPoints );
  35. scene.add( points );
  36. // lines
  37. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  38. new THREE.Vector3( 1, 0, 0 ),
  39. new THREE.Vector3( 2, 0, 0 ),
  40. new THREE.Vector3( 2, 1, 0 ),
  41. new THREE.Vector3( 1, 1, 0 )
  42. ] );
  43. const materialLine = new THREE.LineBasicMaterial();
  44. const line = new THREE.Line( geometryLine, materialLine );
  45. scene.add( line );
  46. renderer = new WebGPURenderer();
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. document.body.appendChild( renderer.domElement );
  50. window.addEventListener( 'resize', onWindowResize, false );
  51. return renderer.init();
  52. }
  53. function onWindowResize() {
  54. camera.aspect = window.innerWidth / window.innerHeight;
  55. camera.updateProjectionMatrix();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. }
  58. function animate() {
  59. requestAnimationFrame( animate );
  60. mesh.rotation.x += 0.01;
  61. mesh.rotation.y += 0.02;
  62. renderer.render( scene, camera );
  63. }
  64. </script>
  65. </body>
  66. </html>