webgl_postprocessing_procedural.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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="importmap">
  48. {
  49. "imports": {
  50. "three": "../build/three.module.js",
  51. "three/addons/": "./jsm/"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three';
  57. import Stats from 'three/addons/libs/stats.module.js';
  58. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  59. let postCamera, postScene, renderer;
  60. let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
  61. let stats;
  62. const params = { procedure: 'noiseRandom3D' };
  63. init();
  64. function init() {
  65. const container = document.getElementById( 'container' );
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.setAnimationLoop( animate );
  70. document.body.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. container.appendChild( stats.dom );
  73. // Setup post processing stage
  74. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  75. noiseRandom1DMaterial = new THREE.ShaderMaterial( {
  76. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  77. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  78. } );
  79. noiseRandom2DMaterial = new THREE.ShaderMaterial( {
  80. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  81. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  82. } );
  83. noiseRandom3DMaterial = new THREE.ShaderMaterial( {
  84. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  85. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  86. } );
  87. postMaterial = noiseRandom3DMaterial;
  88. const postPlane = new THREE.PlaneGeometry( 2, 2 );
  89. postQuad = new THREE.Mesh( postPlane, postMaterial );
  90. postScene = new THREE.Scene();
  91. postScene.add( postQuad );
  92. window.addEventListener( 'resize', onWindowResize );
  93. //
  94. const gui = new GUI();
  95. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  96. }
  97. function onWindowResize() {
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function 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>