webgl_postprocessing_procedural.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. animate();
  65. initGui();
  66. // Init gui
  67. function initGui() {
  68. const gui = new GUI();
  69. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  70. }
  71. function init() {
  72. const container = document.getElementById( 'container' );
  73. renderer = new THREE.WebGLRenderer();
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. document.body.appendChild( renderer.domElement );
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. // Setup post processing stage
  80. postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  81. noiseRandom1DMaterial = new THREE.ShaderMaterial( {
  82. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  83. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  84. } );
  85. noiseRandom2DMaterial = new THREE.ShaderMaterial( {
  86. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  87. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  88. } );
  89. noiseRandom3DMaterial = new THREE.ShaderMaterial( {
  90. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  91. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  92. } );
  93. postMaterial = noiseRandom3DMaterial;
  94. const postPlane = new THREE.PlaneGeometry( 2, 2 );
  95. postQuad = new THREE.Mesh( postPlane, postMaterial );
  96. postScene = new THREE.Scene();
  97. postScene.add( postQuad );
  98. window.addEventListener( 'resize', onWindowResize );
  99. }
  100. function onWindowResize() {
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. requestAnimationFrame( 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>