webgpu_sandbox.html 7.6 KB

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