webgpu_procedural_texture.html 2.8 KB

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