webgpu_procedural_texture.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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="example.css">
  7. </head>
  8. <body>
  9. <div id="info">
  10. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  11. <div class="title-wrapper">
  12. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Procedural Texture</span>
  13. </div>
  14. <small>
  15. GPU procedural texture generation.
  16. </small>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.webgpu.js",
  22. "three/webgpu": "../build/three.webgpu.js",
  23. "three/tsl": "../build/three.tsl.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three/webgpu';
  30. import { checker, uv, uniform, convertToTexture } from 'three/tsl';
  31. import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. let camera, scene, renderer;
  34. init();
  35. function init() {
  36. const aspect = window.innerWidth / window.innerHeight;
  37. camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
  38. camera.position.z = 1;
  39. scene = new THREE.Scene();
  40. // procedural to texture
  41. const uvScale = uniform( 4 );
  42. const blurAmount = uniform( .5 );
  43. const procedural = checker( uv().mul( uvScale ) );
  44. const proceduralToTexture = convertToTexture( procedural, 512, 512 ); // ( node, width, height )
  45. const colorNode = gaussianBlur( proceduralToTexture, blurAmount, 20 );
  46. // extra
  47. //proceduralToTexture.autoUpdate = false; // update just once
  48. //proceduralToTexture.textureNeedsUpdate = true; // manually update
  49. // scene
  50. const material = new THREE.MeshBasicNodeMaterial();
  51. material.colorNode = colorNode;
  52. const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
  53. scene.add( plane );
  54. // renderer
  55. renderer = new THREE.WebGPURenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. renderer.setAnimationLoop( render );
  59. renderer.inspector = new Inspector();
  60. document.body.appendChild( renderer.domElement );
  61. window.addEventListener( 'resize', onWindowResize );
  62. // gui
  63. const gui = renderer.inspector.createParameters( 'Procedural Texture' );
  64. gui.add( uvScale, 'value', 1, 10 ).name( 'uv scale ( before rtt )' );
  65. gui.add( blurAmount, 'value', 0, 2 ).name( 'blur amount ( after rtt )' );
  66. gui.add( proceduralToTexture, 'autoUpdate' ).name( 'auto update' );
  67. }
  68. function onWindowResize() {
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. const aspect = window.innerWidth / window.innerHeight;
  71. const frustumHeight = camera.top - camera.bottom;
  72. camera.left = - frustumHeight * aspect / 2;
  73. camera.right = frustumHeight * aspect / 2;
  74. camera.updateProjectionMatrix();
  75. }
  76. function render() {
  77. renderer.render( scene, camera );
  78. }
  79. </script>
  80. </body>
  81. </html>
粤ICP备19079148号