2
0

webgpu_sandbox.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 { timerLocal, vec2, uv, texture, mix, checker, normalLocal, positionLocal, color, oscSine, attribute, MeshBasicNodeMaterial, PointsNodeMaterial, LineBasicNodeMaterial } from 'three/nodes';
  25. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.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. async 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. //
  41. renderer = new WebGPURenderer();
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. document.body.appendChild( renderer.domElement );
  46. await renderer.init();
  47. // textures
  48. const textureLoader = new THREE.TextureLoader();
  49. const uvTexture = textureLoader.load( './textures/uv_grid_opengl.jpg' );
  50. uvTexture.wrapS = THREE.RepeatWrapping;
  51. uvTexture.wrapT = THREE.RepeatWrapping;
  52. uvTexture.name = 'uv_grid';
  53. const textureDisplace = textureLoader.load( './textures/transition/transition1.png' );
  54. textureDisplace.wrapS = THREE.RepeatWrapping;
  55. textureDisplace.wrapT = THREE.RepeatWrapping;
  56. const ktxLoader = new KTX2Loader()
  57. .setTranscoderPath( 'jsm/libs/basis/' )
  58. .detectSupport( renderer );
  59. const ktxTexture = await ktxLoader.loadAsync( './textures/compressed/sample_uastc_zstd.ktx2' );
  60. // box mesh
  61. const geometryBox = new THREE.BoxGeometry();
  62. const materialBox = new MeshBasicNodeMaterial();
  63. // birection speed
  64. const timerScaleNode = timerLocal().mul( vec2( - 0.5, 0.1 ) );
  65. const animateUV = uv().add( timerScaleNode );
  66. const textureNode = texture( uvTexture, animateUV );
  67. materialBox.colorNode = mix( textureNode, checker( animateUV ), 0.5 );
  68. // test uv 2
  69. //geometryBox.setAttribute( 'uv1', geometryBox.getAttribute( 'uv' ) );
  70. //materialBox.colorNode = texture( uvTexture, uv( 1 ) );
  71. box = new THREE.Mesh( geometryBox, materialBox );
  72. box.position.set( 0, 1, 0 );
  73. scene.add( box );
  74. // displace example
  75. const geometrySphere = new THREE.SphereGeometry( .5, 64, 64 );
  76. const materialSphere = new MeshBasicNodeMaterial();
  77. const displaceY = texture( textureDisplace ).x.mul( 0.25 );
  78. const displace = normalLocal.mul( displaceY );
  79. materialSphere.colorNode = displaceY;
  80. materialSphere.positionNode = positionLocal.add( displace );
  81. const sphere = new THREE.Mesh( geometrySphere, materialSphere );
  82. sphere.position.set( - 2, - 1, 0 );
  83. scene.add( sphere );
  84. // data texture
  85. const geometryPlane = new THREE.PlaneGeometry();
  86. const materialPlane = new MeshBasicNodeMaterial();
  87. materialPlane.colorNode = texture( createDataTexture() ).add( color( 0x0000FF ) );
  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 MeshBasicNodeMaterial();
  94. materialCompressed.colorNode = texture( ktxTexture );
  95. materialCompressed.emissiveNode = oscSine().mix( color( 0x663300 ), color( 0x0000FF ) );
  96. materialCompressed.alphaTestNode = oscSine();
  97. materialCompressed.transparent = true;
  98. const geo = flipY( new THREE.PlaneGeometry() );
  99. const planeCompressed = new THREE.Mesh( geo, materialCompressed );
  100. planeCompressed.position.set( - 2, 1, 0 );
  101. scene.add( planeCompressed );
  102. // points
  103. const points = [];
  104. for ( let i = 0; i < 1000; i ++ ) {
  105. const point = new THREE.Vector3().random().subScalar( 0.5 );
  106. points.push( point );
  107. }
  108. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  109. const materialPoints = new PointsNodeMaterial();
  110. materialPoints.colorNode = positionLocal.mul( 3 );
  111. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  112. pointCloud.position.set( 2, - 1, 0 );
  113. scene.add( pointCloud );
  114. // lines
  115. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  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. new THREE.Vector3( - 0.5, 0.5, 0 )
  120. ] );
  121. geometryLine.setAttribute( 'color', geometryLine.getAttribute( 'position' ) );
  122. const materialLine = new LineBasicNodeMaterial();
  123. materialLine.colorNode = attribute( 'color' );
  124. const line = new THREE.Line( geometryLine, materialLine );
  125. line.position.set( 2, 1, 0 );
  126. scene.add( line );
  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. if ( box ) {
  136. box.rotation.x += 0.01;
  137. box.rotation.y += 0.02;
  138. }
  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. /** Correct UVs to be compatible with `flipY=false` textures. */
  162. function flipY( geometry ) {
  163. const uv = geometry.attributes.uv;
  164. for ( let i = 0; i < uv.count; i ++ ) {
  165. uv.setY( i, 1 - uv.getY( i ) );
  166. }
  167. return geometry;
  168. }
  169. </script>
  170. </body>
  171. </html>