webgpu_sandbox.html 7.2 KB

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