webgpu_sandbox_nodes.html 4.7 KB

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