webgpu_sandbox.html 2.6 KB

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