2
0

webgpu_sandbox.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <html lang="en">
  2. <head>
  3. <title>three.js - 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. <!-- WebGPU (For Chrome 94-101), expires 09/01/2022 -->
  8. <meta http-equiv="origin-trial" content="AoS1pSJwCV3KRe73TO0YgJkK9FZ/qhmvKeafztp0ofiE8uoGrnKzfxGVKKICvoBfL8dgE0zpkp2g/oEJNS0fDgkAAABeeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJHUFUiLCJleHBpcnkiOjE2NTI4MzE5OTksImlzU3ViZG9tYWluIjp0cnVlfQ==">
  9. </head>
  10. <body>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Sandbox
  13. </div>
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three-nodes/": "./jsm/nodes/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import * as Nodes from 'three-nodes/Nodes.js';
  26. import { DDSLoader } from './jsm/loaders/DDSLoader.js';
  27. import WebGPU from './jsm/capabilities/WebGPU.js';
  28. import WebGPURenderer from './jsm/renderers/webgpu/WebGPURenderer.js';
  29. let camera, scene, renderer;
  30. let box;
  31. init().then( animate ).catch( error );
  32. async function init() {
  33. if ( WebGPU.isAvailable() === false ) {
  34. document.body.appendChild( WebGPU.getErrorMessage() );
  35. throw new Error( 'No WebGPU support' );
  36. }
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  38. camera.position.z = 4;
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.Color( 0x222222 );
  41. // textures
  42. const textureLoader = new THREE.TextureLoader();
  43. const texture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  44. texture.wrapS = THREE.RepeatWrapping;
  45. texture.wrapT = THREE.RepeatWrapping;
  46. texture.name = 'uv_grid';
  47. const textureDisplace = textureLoader.load( './textures/transition/transition1.png' );
  48. textureDisplace.wrapS = THREE.RepeatWrapping;
  49. textureDisplace.wrapT = THREE.RepeatWrapping;
  50. // compressed texture
  51. const ddsLoader = new DDSLoader();
  52. const dxt5Texture = ddsLoader.load( './textures/compressed/explosion_dxt5_mip.dds' );
  53. // box mesh
  54. const geometryBox = new THREE.BoxGeometry();
  55. const materialBox = new Nodes.MeshBasicNodeMaterial();
  56. const timerNode = new Nodes.TimerNode();
  57. // birection speed
  58. const timerScaleNode = new Nodes.OperatorNode( '*', timerNode, new Nodes.ConstNode( new THREE.Vector2( - 0.5, 0.1 ) ) );
  59. const animateUV = new Nodes.OperatorNode( '+', new Nodes.UVNode(), timerScaleNode );
  60. const textureNode = new Nodes.TextureNode( texture, animateUV );
  61. materialBox.colorNode = new Nodes.MathNode( 'mix', textureNode, new Nodes.CheckerNode( animateUV ), new Nodes.UniformNode( .5 ) );
  62. // test uv 2
  63. //geometryBox.setAttribute( 'uv2', geometryBox.getAttribute( 'uv' ) );
  64. //materialBox.colorNode = new TextureNode( texture, new UVNode( 1 ) );
  65. box = new THREE.Mesh( geometryBox, materialBox );
  66. box.position.set( 0, 1, 0 );
  67. scene.add( box );
  68. // displace example
  69. const geometrySphere = new THREE.SphereGeometry( .5, 64, 64 );
  70. const materialSphere = new Nodes.MeshBasicNodeMaterial();
  71. const displaceAnimated = new Nodes.SplitNode( new Nodes.TextureNode( textureDisplace ), 'x' );
  72. const displaceY = new Nodes.OperatorNode( '*', displaceAnimated, new Nodes.ConstNode( .25 ) );
  73. const displace = new Nodes.OperatorNode( '*', new Nodes.NormalNode( Nodes.NormalNode.LOCAL ), displaceY );
  74. materialSphere.colorNode = displaceY;
  75. materialSphere.positionNode = new Nodes.OperatorNode( '+', new Nodes.PositionNode( Nodes.PositionNode.LOCAL ), displace );
  76. const sphere = new THREE.Mesh( geometrySphere, materialSphere );
  77. sphere.position.set( - 2, - 1, 0 );
  78. scene.add( sphere );
  79. // data texture
  80. const geometryPlane = new THREE.PlaneGeometry();
  81. const materialPlane = new Nodes.MeshBasicNodeMaterial();
  82. materialPlane.colorNode = new Nodes.OperatorNode( '+', new Nodes.TextureNode( createDataTexture() ), new Nodes.UniformNode( new THREE.Color( 0x0000FF ) ) );
  83. materialPlane.opacityNode = new Nodes.SplitNode( new Nodes.TextureNode( dxt5Texture ), 'a' );
  84. materialPlane.transparent = true;
  85. const plane = new THREE.Mesh( geometryPlane, materialPlane );
  86. plane.position.set( 0, - 1, 0 );
  87. scene.add( plane );
  88. // compressed texture
  89. const materialCompressed = new Nodes.MeshBasicNodeMaterial();
  90. materialCompressed.colorNode = new Nodes.TextureNode( dxt5Texture );
  91. materialCompressed.emissiveNode = new Nodes.UniformNode( new THREE.Color( 0x663300 ) );
  92. materialCompressed.alphaTestNode = new Nodes.OscNode();
  93. materialCompressed.transparent = true;
  94. const boxCompressed = new THREE.Mesh( geometryBox, materialCompressed );
  95. boxCompressed.position.set( - 2, 1, 0 );
  96. scene.add( boxCompressed );
  97. // points
  98. const points = [];
  99. for ( let i = 0; i < 1000; i ++ ) {
  100. const point = new THREE.Vector3().random().subScalar( 0.5 );
  101. points.push( point );
  102. }
  103. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  104. const materialPoints = new Nodes.PointsNodeMaterial();
  105. materialPoints.colorNode = new Nodes.OperatorNode( '*', new Nodes.PositionNode(), new Nodes.ConstNode( 3 ) );
  106. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  107. pointCloud.position.set( 2, - 1, 0 );
  108. scene.add( pointCloud );
  109. // lines
  110. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  111. new THREE.Vector3( - 0.5, - 0.5, 0 ),
  112. new THREE.Vector3( 0.5, - 0.5, 0 ),
  113. new THREE.Vector3( 0.5, 0.5, 0 ),
  114. new THREE.Vector3( - 0.5, 0.5, 0 )
  115. ] );
  116. geometryLine.setAttribute( 'color', geometryLine.getAttribute( 'position' ) );
  117. const materialLine = new Nodes.LineBasicNodeMaterial();
  118. materialLine.colorNode = new Nodes.AttributeNode( 'color', 'vec3' );
  119. const line = new THREE.Line( geometryLine, materialLine );
  120. line.position.set( 2, 1, 0 );
  121. scene.add( line );
  122. //
  123. renderer = new WebGPURenderer( { requiredFeatures: [ 'texture-compression-bc' ] } );
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. document.body.appendChild( renderer.domElement );
  127. window.addEventListener( 'resize', onWindowResize );
  128. return renderer.init();
  129. }
  130. function onWindowResize() {
  131. camera.aspect = window.innerWidth / window.innerHeight;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( window.innerWidth, window.innerHeight );
  134. }
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. box.rotation.x += 0.01;
  138. box.rotation.y += 0.02;
  139. renderer.render( scene, camera );
  140. }
  141. function createDataTexture() {
  142. const color = new THREE.Color( 0xff0000 );
  143. const width = 512;
  144. const height = 512;
  145. const size = width * height;
  146. const data = new Uint8Array( 4 * size );
  147. const r = Math.floor( color.r * 255 );
  148. const g = Math.floor( color.g * 255 );
  149. const b = Math.floor( color.b * 255 );
  150. for ( let i = 0; i < size; i ++ ) {
  151. const stride = i * 4;
  152. data[ stride ] = r;
  153. data[ stride + 1 ] = g;
  154. data[ stride + 2 ] = b;
  155. data[ stride + 3 ] = 255;
  156. }
  157. const texture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  158. texture.needsUpdate = true;
  159. return texture;
  160. }
  161. function error( error ) {
  162. console.error( error );
  163. }
  164. </script>
  165. </body>
  166. </html>