webgpu_procedural_texture.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <html lang="en">
  2. <head>
  3. <title>three.js webgpu - procedural texture</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 - procedural texture
  11. </div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/tsl": "../build/three.webgpu.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { checker, uv, uniform, convertToTexture } from 'three/tsl';
  24. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. let camera, scene, renderer;
  27. init();
  28. render();
  29. function init() {
  30. const aspect = window.innerWidth / window.innerHeight;
  31. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  32. camera.position.z = 1;
  33. scene = new THREE.Scene();
  34. // procedural to texture
  35. const uvScale = uniform( 4 );
  36. const blurAmount = uniform( .5 );
  37. const procedural = checker( uv().mul( uvScale ) );
  38. const proceduralToTexture = convertToTexture( procedural, 512, 512 ); // ( node, width, height )
  39. const colorNode = gaussianBlur( proceduralToTexture, blurAmount, 10 );
  40. // extra
  41. //proceduralToTexture.autoUpdate = false; // update just once
  42. //proceduralToTexture.textureNeedsUpdate = true; // manually update
  43. // scene
  44. const material = new THREE.MeshBasicNodeMaterial();
  45. material.colorNode = colorNode;
  46. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  47. scene.add( plane );
  48. // renderer
  49. renderer = new THREE.WebGPURenderer( { antialias: true } );
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. renderer.setAnimationLoop( render );
  53. document.body.appendChild( renderer.domElement );
  54. window.addEventListener( 'resize', onWindowResize );
  55. // gui
  56. const gui = new GUI();
  57. gui.add( uvScale, 'value', 1, 10 ).name( 'uv scale ( before rtt )' );
  58. gui.add( blurAmount, 'value', 0, 2 ).name( 'blur amount ( after rtt )' );
  59. gui.add( proceduralToTexture, 'autoUpdate' ).name( 'auto update' );
  60. }
  61. function onWindowResize() {
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. const aspect = window.innerWidth / window.innerHeight;
  64. const frustumHeight = camera.top - camera.bottom;
  65. camera.left = - frustumHeight * aspect / 2;
  66. camera.right = frustumHeight * aspect / 2;
  67. camera.updateProjectionMatrix();
  68. }
  69. function render() {
  70. renderer.renderAsync( scene, camera );
  71. }
  72. </script>
  73. </body>
  74. </html>