webgpu_sandbox.html 6.6 KB

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