webgpu_procedural_texture.html 2.9 KB

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