webgl_postprocessing_procedural.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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="http://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 {
  49. Mesh,
  50. OrthographicCamera,
  51. PlaneBufferGeometry,
  52. Scene,
  53. ShaderMaterial,
  54. WebGLRenderer
  55. } from "../build/three.module.js";
  56. import Stats from './jsm/libs/stats.module.js';
  57. import { GUI } from './jsm/libs/dat.gui.module.js';
  58. var postCamera, postScene, renderer;
  59. var postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
  60. var stats;
  61. var params = { procedure: 'noiseRandom3D' };
  62. init();
  63. animate();
  64. initGui();
  65. // Init gui
  66. function initGui() {
  67. var gui = new GUI();
  68. gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
  69. }
  70. function init() {
  71. var container = document.getElementById( "container" );
  72. renderer = new WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. document.body.appendChild( renderer.domElement );
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. // Setup post processing stage
  79. postCamera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  80. noiseRandom1DMaterial = new ShaderMaterial( {
  81. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  82. fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
  83. } );
  84. noiseRandom2DMaterial = new ShaderMaterial( {
  85. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  86. fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
  87. } );
  88. noiseRandom3DMaterial = new ShaderMaterial( {
  89. vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
  90. fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
  91. } );
  92. postMaterial = noiseRandom3DMaterial;
  93. var postPlane = new PlaneBufferGeometry( 2, 2 );
  94. postQuad = new Mesh( postPlane, postMaterial );
  95. postScene = new Scene();
  96. postScene.add( postQuad );
  97. window.addEventListener( 'resize', onWindowResize, false );
  98. }
  99. function onWindowResize() {
  100. var width = window.innerWidth;
  101. var height = window.innerHeight;
  102. postCamera.aspect = width / height;
  103. postCamera.updateProjectionMatrix();
  104. renderer.setSize( width, height );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. switch ( params.procedure ) {
  109. case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
  110. case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
  111. case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
  112. }
  113. postQuad.material = postMaterial;
  114. // render post FX
  115. renderer.render( postScene, postCamera );
  116. stats.update();
  117. }
  118. </script>
  119. </body>
  120. </html>