webgpu_sandbox.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 { 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 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. // 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 MeshBasicNodeMaterial();
  55. // birection speed
  56. const timerScaleNode = timerLocal().mul( vec2( - 0.5, 0.1 ) );
  57. const animateUV = uv().add( timerScaleNode );
  58. const textureNode = texture( uvTexture, animateUV );
  59. materialBox.colorNode = mix( textureNode, checker( animateUV ), 0.5 );
  60. // test uv 2
  61. //geometryBox.setAttribute( 'uv2', geometryBox.getAttribute( 'uv' ) );
  62. //materialBox.colorNode = texture( uvTexture, uv( 1 ) );
  63. box = new THREE.Mesh( geometryBox, materialBox );
  64. box.position.set( 0, 1, 0 );
  65. scene.add( box );
  66. // displace example
  67. const geometrySphere = new THREE.SphereGeometry( .5, 64, 64 );
  68. const materialSphere = new MeshBasicNodeMaterial();
  69. const displaceY = texture( textureDisplace ).x.mul( 0.25 );
  70. const displace = normalLocal.mul( displaceY );
  71. materialSphere.colorNode = displaceY;
  72. materialSphere.positionNode = positionLocal.add( displace );
  73. const sphere = new THREE.Mesh( geometrySphere, materialSphere );
  74. sphere.position.set( - 2, - 1, 0 );
  75. scene.add( sphere );
  76. // data texture
  77. const geometryPlane = new THREE.PlaneGeometry();
  78. const materialPlane = new MeshBasicNodeMaterial();
  79. materialPlane.colorNode = texture( createDataTexture() ).add( color( 0x0000FF ) );
  80. materialPlane.opacityNode = texture( dxt5Texture ).a;
  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 MeshBasicNodeMaterial();
  87. materialCompressed.colorNode = texture( dxt5Texture );
  88. materialCompressed.emissiveNode = oscSine().mix( color( 0x663300 ), color( 0x0000FF ) );
  89. materialCompressed.alphaTestNode = oscSine();
  90. materialCompressed.transparent = true;
  91. const boxCompressed = new THREE.Mesh( geometryBox, materialCompressed );
  92. boxCompressed.position.set( - 2, 1, 0 );
  93. scene.add( boxCompressed );
  94. // points
  95. const points = [];
  96. for ( let i = 0; i < 1000; i ++ ) {
  97. const point = new THREE.Vector3().random().subScalar( 0.5 );
  98. points.push( point );
  99. }
  100. const geometryPoints = new THREE.BufferGeometry().setFromPoints( points );
  101. const materialPoints = new PointsNodeMaterial();
  102. materialPoints.colorNode = positionLocal.mul( 3 );
  103. const pointCloud = new THREE.Points( geometryPoints, materialPoints );
  104. pointCloud.position.set( 2, - 1, 0 );
  105. scene.add( pointCloud );
  106. // lines
  107. const geometryLine = new THREE.BufferGeometry().setFromPoints( [
  108. new THREE.Vector3( - 0.5, - 0.5, 0 ),
  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. ] );
  113. geometryLine.setAttribute( 'color', geometryLine.getAttribute( 'position' ) );
  114. const materialLine = new LineBasicNodeMaterial();
  115. materialLine.colorNode = attribute( 'color' );
  116. const line = new THREE.Line( geometryLine, materialLine );
  117. line.position.set( 2, 1, 0 );
  118. scene.add( line );
  119. //
  120. renderer = new WebGPURenderer( { requiredFeatures: [ 'texture-compression-bc' ] } );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. renderer.setAnimationLoop( animate );
  124. document.body.appendChild( renderer.domElement );
  125. window.addEventListener( 'resize', onWindowResize );
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. function animate() {
  133. box.rotation.x += 0.01;
  134. box.rotation.y += 0.02;
  135. renderer.render( scene, camera );
  136. }
  137. function createDataTexture() {
  138. const color = new THREE.Color( 0xff0000 );
  139. const width = 512;
  140. const height = 512;
  141. const size = width * height;
  142. const data = new Uint8Array( 4 * size );
  143. const r = Math.floor( color.r * 255 );
  144. const g = Math.floor( color.g * 255 );
  145. const b = Math.floor( color.b * 255 );
  146. for ( let i = 0; i < size; i ++ ) {
  147. const stride = i * 4;
  148. data[ stride ] = r;
  149. data[ stride + 1 ] = g;
  150. data[ stride + 2 ] = b;
  151. data[ stride + 3 ] = 255;
  152. }
  153. const texture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat );
  154. texture.needsUpdate = true;
  155. return texture;
  156. }
  157. </script>
  158. </body>
  159. </html>