webgl_postprocessing_procedural.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing procedural effects</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js webgl - postprocessing procedural effects">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_procedural.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_procedural.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Procedural Effects Example by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  16. </div>
  17. <script id="procedural-vert" type="x-shader/x-vertex">
  18. varying vec2 vUv;
  19. void main() {
  20. vUv = uv;
  21. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  22. }
  23. </script>
  24. <script id="noiseRandom1D-frag" type="x-shader/x-fragment">
  25. #include <common>
  26. varying vec2 vUv;
  27. void main() {
  28. gl_FragColor.xyz = vec3( rand( vUv ) );
  29. gl_FragColor.w = 1.0;
  30. }
  31. </script>
  32. <script id="noiseRandom2D-frag" type="x-shader/x-fragment">
  33. #include <common>
  34. varying vec2 vUv;
  35. void main() {
  36. vec2 rand2 = vec2( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ) );
  37. gl_FragColor.xyz = mix( mix( vec3( 1.0, 1.0, 1.0 ), vec3( 0.0, 0.0, 1.0 ), rand2.x ), vec3( 0.0 ), rand2.y );
  38. gl_FragColor.w = 1.0;
  39. }
  40. </script>
  41. <script id="noiseRandom3D-frag" type="x-shader/x-fragment">
  42. #include <common>
  43. varying vec2 vUv;
  44. void main() {
  45. vec3 rand3 = vec3( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ), rand( vUv + vec2( 0.6, 0.4 ) ) );
  46. gl_FragColor.xyz = rand3;
  47. gl_FragColor.w = 1.0;
  48. }
  49. </script>
  50. <div id="container"></div>
  51. <script type="importmap">
  52. {
  53. "imports": {
  54. "three": "../build/three.module.js",
  55. "three/addons/": "./jsm/"
  56. }
  57. }
  58. </script>
  59. <script type="module">
  60. import * as THREE from 'three';
  61. import Stats from 'three/addons/libs/stats.module.js';
  62. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  63. let postCamera, postScene, renderer;
  64. let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
  65. let stats;
  66. const params = { procedure: 'noiseRandom3D' };
  67. init();
  68. function init() {
  69. const container = document.getElementById( 'container' );
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.setAnimationLoop( animate );
  74. document.body.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. // Setup post processing stage
  78. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  79. noiseRandom1DMaterial = new THREE.ShaderMaterial( {
  80. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  81. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  82. } );
  83. noiseRandom2DMaterial = new THREE.ShaderMaterial( {
  84. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  85. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  86. } );
  87. noiseRandom3DMaterial = new THREE.ShaderMaterial( {
  88. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  89. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  90. } );
  91. postMaterial = noiseRandom3DMaterial;
  92. const postPlane = new THREE.PlaneGeometry( 2, 2 );
  93. postQuad = new THREE.Mesh( postPlane, postMaterial );
  94. postScene = new THREE.Scene();
  95. postScene.add( postQuad );
  96. window.addEventListener( 'resize', onWindowResize );
  97. //
  98. const gui = new GUI();
  99. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  100. }
  101. function onWindowResize() {
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. switch ( params.procedure ) {
  106. case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
  107. case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
  108. case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
  109. }
  110. postQuad.material = postMaterial;
  111. // render post FX
  112. renderer.render( postScene, postCamera );
  113. stats.update();
  114. }
  115. </script>
  116. </body>
  117. </html>
粤ICP备19079148号