/examples/webgl_postprocessing_procedural.html

https://github.com/nosy-b/three.js · HTML · 165 lines · 121 code · 42 blank · 2 comment · 0 complexity · 288f08c712e1578390fafb3a34657f21 MD5 · raw file

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