webgpu_sandbox_nodes.html 4.8 KB

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