webgl_postprocessing_procedural.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. <!-- Import maps polyfill -->
  48. <!-- Remove this when import maps will be widely supported -->
  49. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  50. <script type="importmap">
  51. {
  52. "imports": {
  53. "three": "../build/three.module.js",
  54. "three/addons/": "./jsm/"
  55. }
  56. }
  57. </script>
  58. <script type="module">
  59. import * as THREE from 'three';
  60. import Stats from 'three/addons/libs/stats.module.js';
  61. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  62. let postCamera, postScene, renderer;
  63. let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
  64. let stats;
  65. const params = { procedure: 'noiseRandom3D' };
  66. init();
  67. animate();
  68. initGui();
  69. // Init gui
  70. function initGui() {
  71. const gui = new GUI();
  72. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  73. }
  74. function init() {
  75. const container = document.getElementById( 'container' );
  76. renderer = new THREE.WebGLRenderer();
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. document.body.appendChild( renderer.domElement );
  80. stats = new Stats();
  81. container.appendChild( stats.dom );
  82. // Setup post processing stage
  83. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  84. noiseRandom1DMaterial = new THREE.ShaderMaterial( {
  85. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  86. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  87. } );
  88. noiseRandom2DMaterial = new THREE.ShaderMaterial( {
  89. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  90. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  91. } );
  92. noiseRandom3DMaterial = new THREE.ShaderMaterial( {
  93. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  94. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  95. } );
  96. postMaterial = noiseRandom3DMaterial;
  97. const postPlane = new THREE.PlaneGeometry( 2, 2 );
  98. postQuad = new THREE.Mesh( postPlane, postMaterial );
  99. postScene = new THREE.Scene();
  100. postScene.add( postQuad );
  101. window.addEventListener( 'resize', onWindowResize );
  102. }
  103. function onWindowResize() {
  104. const width = window.innerWidth;
  105. const height = window.innerHeight;
  106. postCamera.aspect = width / height;
  107. postCamera.updateProjectionMatrix();
  108. renderer.setSize( width, height );
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. switch ( params.procedure ) {
  113. case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
  114. case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
  115. case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
  116. }
  117. postQuad.material = postMaterial;
  118. // render post FX
  119. renderer.render( postScene, postCamera );
  120. stats.update();
  121. }
  122. </script>
  123. </body>
  124. </html>