webgl_postprocessing_procedural.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <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/>
  12. </div>
  13. <script id="procedural-vert" type="x-shader/x-vertex">
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  18. }
  19. </script>
  20. <script id="noiseRandom1D-frag" type="x-shader/x-fragment">
  21. #include <common>
  22. varying vec2 vUv;
  23. void main() {
  24. gl_FragColor.xyz = vec3( rand( vUv ) );
  25. gl_FragColor.w = 1.0;
  26. }
  27. </script>
  28. <script id="noiseRandom2D-frag" type="x-shader/x-fragment">
  29. #include <common>
  30. varying vec2 vUv;
  31. void main() {
  32. vec2 rand2 = vec2( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ) );
  33. 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 );
  34. gl_FragColor.w = 1.0;
  35. }
  36. </script>
  37. <script id="noiseRandom3D-frag" type="x-shader/x-fragment">
  38. #include <common>
  39. varying vec2 vUv;
  40. void main() {
  41. vec3 rand3 = vec3( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ), rand( vUv + vec2( 0.6, 0.4 ) ) );
  42. gl_FragColor.xyz = rand3;
  43. gl_FragColor.w = 1.0;
  44. }
  45. </script>
  46. <div id="container"></div>
  47. <script type="module">
  48. import * as THREE from '../build/three.module.js';
  49. import Stats from './jsm/libs/stats.module.js';
  50. import { GUI } from './jsm/libs/dat.gui.module.js';
  51. var postCamera, postScene, renderer;
  52. var postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
  53. var stats;
  54. var params = { procedure: 'noiseRandom3D' };
  55. init();
  56. animate();
  57. initGui();
  58. // Init gui
  59. function initGui() {
  60. var gui = new GUI();
  61. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  62. }
  63. function init() {
  64. var container = document.getElementById( "container" );
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. // Setup post processing stage
  72. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  73. noiseRandom1DMaterial = new THREE.ShaderMaterial( {
  74. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  75. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  76. } );
  77. noiseRandom2DMaterial = new THREE.ShaderMaterial( {
  78. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  79. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  80. } );
  81. noiseRandom3DMaterial = new THREE.ShaderMaterial( {
  82. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  83. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  84. } );
  85. postMaterial = noiseRandom3DMaterial;
  86. var postPlane = new THREE.PlaneBufferGeometry( 2, 2 );
  87. postQuad = new THREE.Mesh( postPlane, postMaterial );
  88. postScene = new THREE.Scene();
  89. postScene.add( postQuad );
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. var width = window.innerWidth;
  94. var height = window.innerHeight;
  95. postCamera.aspect = width / height;
  96. postCamera.updateProjectionMatrix();
  97. renderer.setSize( width, height );
  98. }
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. switch ( params.procedure ) {
  102. case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
  103. case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
  104. case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
  105. }
  106. postQuad.material = postMaterial;
  107. // render post FX
  108. renderer.render( postScene, postCamera );
  109. stats.update();
  110. }
  111. </script>
  112. </body>
  113. </html>