webgpu_procedural_texture.html 3.5 KB

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