webgpu_sandbox.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/",
  17. "three/nodes": "./jsm/nodes/Nodes.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { timerLocal, vec2, uv, texture, mix, checker, normalLocal, positionLocal, color, oscSine, attribute, MeshBasicNodeMaterial, PointsNodeMaterial, LineBasicNodeMaterial } from 'three/nodes';
  24. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. let camera, scene, renderer;
  28. let box;
  29. init();
  30. async function init() {
  31. if ( WebGPU.isAvailable() === false ) {
  32. document.body.appendChild( WebGPU.getErrorMessage() );
  33. throw new Error( 'No WebGPU support' );
  34. }
  35. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  36. camera.position.z = 4;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x222222 );
  39. //
  40. renderer = new WebGPURenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. renderer.setAnimationLoop( animate );
  44. document.body.appendChild( renderer.domElement );
  45. // textures
  46. const textureLoader = new THREE.TextureLoader();
  47. const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  48. uvTexture.wrapS = THREE.RepeatWrapping;
  49. uvTexture.wrapT = THREE.RepeatWrapping;
  50. uvTexture.name = 'uv_grid';
  51. const textureDisplace = textureLoader.load( './textures/transition/transition1.png' );
  52. textureDisplace.wrapS = THREE.RepeatWrapping;
  53. textureDisplace.wrapT = THREE.RepeatWrapping;
  54. const ktxLoader = await new KTX2Loader()
  55. .setTranscoderPath( 'jsm/libs/basis/' )
  56. .detectSupportAsync( renderer );
  57. const ktxTexture = await ktxLoader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
  58. // box mesh
  59. const geometryBox = new THREE.BoxGeometry();
  60. const materialBox = new MeshBasicNodeMaterial();
  61. // birection speed
  62. const timerScaleNode = timerLocal().mul( vec2( - 0.5, 0.1 ) );
  63. const animateUV = uv().add( timerScaleNode );
  64. const textureNode = texture( uvTexture, animateUV );
  65. materialBox.colorNode = mix( textureNode, checker( animateUV ), 0.5 );
  66. // test uv 2
  67. //geometryBox.setAttribute( 'uv1', geometryBox.getAttribute( 'uv' ) );
  68. //materialBox.colorNode = texture( uvTexture, uv( 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 MeshBasicNodeMaterial();
  75. const displaceY = texture( textureDisplace ).x.mul( 0.25 );
  76. const displace = normalLocal.mul( displaceY );
  77. materialSphere.colorNode = displaceY;
  78. materialSphere.positionNode = positionLocal.add( displace );
  79. const sphere = new THREE.Mesh( geometrySphere, materialSphere );
  80. sphere.position.set( - 2, - 1, 0 );
  81. scene.add( sphere );
  82. // data texture
  83. const geometryPlane = new THREE.PlaneGeometry();
  84. const materialPlane = new MeshBasicNodeMaterial();
  85. materialPlane.colorNode = texture( createDataTexture() ).add( color( 0x0000FF ) );
  86. materialPlane.transparent = true;
  87. const plane = new THREE.Mesh( geometryPlane, materialPlane );
  88. plane.position.set( 0, - 1, 0 );
  89. scene.add( plane );
  90. // compressed texture
  91. const materialCompressed = new MeshBasicNodeMaterial();
  92. materialCompressed.colorNode = texture( ktxTexture );
  93. materialCompressed.emissiveNode = oscSine().mix( color( 0x663300 ), color( 0x0000FF ) );
  94. materialCompressed.alphaTestNode = oscSine();
  95. materialCompressed.transparent = true;
  96. const geo = flipY( new THREE.PlaneGeometry() );
  97. const planeCompressed = new THREE.Mesh( geo, materialCompressed );
  98. planeCompressed.position.set( - 2, 1, 0 );
  99. scene.add( planeCompressed );
  100. // points
  101. const points = [];
  102. for ( let i = 0; i < 1000; i ++ ) {
  103. const point = new THREE.Vector3().random().subScalar( 0.5 );
  104. points.push( point );
  105. }
  106. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  107. const materialPoints = new PointsNodeMaterial();
  108. materialPoints.colorNode = positionLocal.mul( 3 );
  109. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  110. pointCloud.position.set( 2, - 1, 0 );
  111. scene.add( pointCloud );
  112. // lines
  113. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  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. new THREE.Vector3( - 0.5, 0.5, 0 ),
  118. new THREE.Vector3( - 0.5, - 0.5, 0 )
  119. ] );
  120. geometryLine.setAttribute( 'color', geometryLine.getAttribute( 'position' ) );
  121. const materialLine = new LineBasicNodeMaterial();
  122. materialLine.colorNode = attribute( 'color' );
  123. const line = new THREE.Line( geometryLine, materialLine );
  124. line.position.set( 2, 1, 0 );
  125. scene.add( line );
  126. window.addEventListener( 'resize', onWindowResize );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. function animate() {
  134. if ( box ) {
  135. box.rotation.x += 0.01;
  136. box.rotation.y += 0.02;
  137. }
  138. renderer.render( scene, camera );
  139. }
  140. function createDataTexture() {
  141. const color = new THREE.Color( 0xff0000 );
  142. const width = 512;
  143. const height = 512;
  144. const size = width * height;
  145. const data = new Uint8Array( 4 * size );
  146. const r = Math.floor( color.r * 255 );
  147. const g = Math.floor( color.g * 255 );
  148. const b = Math.floor( color.b * 255 );
  149. for ( let i = 0; i < size; i ++ ) {
  150. const stride = i * 4;
  151. data[ stride ] = r;
  152. data[ stride + 1 ] = g;
  153. data[ stride + 2 ] = b;
  154. data[ stride + 3 ] = 255;
  155. }
  156. const texture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  157. texture.needsUpdate = true;
  158. return texture;
  159. }
  160. /** Correct UVs to be compatible with `flipY=false` textures. */
  161. function flipY( geometry ) {
  162. const uv = geometry.attributes.uv;
  163. for ( let i = 0; i < uv.count; i ++ ) {
  164. uv.setY( i, 1 - uv.getY( i ) );
  165. }
  166. return geometry;
  167. }
  168. </script>
  169. </body>
  170. </html>