123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - postprocessing procedural effects</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <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/>
- </div>
- <script id="procedural-vert" type="x-shader/x-vertex">
- varying vec2 vUv;
- void main() {
- vUv = uv;
- gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
- }
- </script>
- <script id="noiseRandom1D-frag" type="x-shader/x-fragment">
- #include <common>
- varying vec2 vUv;
- void main() {
- gl_FragColor.xyz = vec3( rand( vUv ) );
- gl_FragColor.w = 1.0;
- }
- </script>
- <script id="noiseRandom2D-frag" type="x-shader/x-fragment">
- #include <common>
- varying vec2 vUv;
- void main() {
- vec2 rand2 = vec2( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ) );
- 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 );
- gl_FragColor.w = 1.0;
- }
- </script>
- <script id="noiseRandom3D-frag" type="x-shader/x-fragment">
- #include <common>
- varying vec2 vUv;
- void main() {
- vec3 rand3 = vec3( rand( vUv ), rand( vUv + vec2( 0.4, 0.6 ) ), rand( vUv + vec2( 0.6, 0.4 ) ) );
- gl_FragColor.xyz = rand3;
- gl_FragColor.w = 1.0;
- }
- </script>
- <div id="container"></div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.module.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import Stats from 'three/addons/libs/stats.module.js';
- import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
- let postCamera, postScene, renderer;
- let postMaterial, noiseRandom1DMaterial, noiseRandom2DMaterial, noiseRandom3DMaterial, postQuad;
- let stats;
- const params = { procedure: 'noiseRandom3D' };
- init();
- function init() {
- const container = document.getElementById( 'container' );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- // Setup post processing stage
- postCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
- noiseRandom1DMaterial = new THREE.ShaderMaterial( {
- vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
- fragmentShader: document.querySelector( '#noiseRandom1D-frag' ).textContent.trim()
- } );
- noiseRandom2DMaterial = new THREE.ShaderMaterial( {
- vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
- fragmentShader: document.querySelector( '#noiseRandom2D-frag' ).textContent.trim()
- } );
- noiseRandom3DMaterial = new THREE.ShaderMaterial( {
- vertexShader: document.querySelector( '#procedural-vert' ).textContent.trim(),
- fragmentShader: document.querySelector( '#noiseRandom3D-frag' ).textContent.trim()
- } );
- postMaterial = noiseRandom3DMaterial;
- const postPlane = new THREE.PlaneGeometry( 2, 2 );
- postQuad = new THREE.Mesh( postPlane, postMaterial );
- postScene = new THREE.Scene();
- postScene.add( postQuad );
- window.addEventListener( 'resize', onWindowResize );
- //
- const gui = new GUI();
- gui.add( params, 'procedure', [ 'noiseRandom1D', 'noiseRandom2D', 'noiseRandom3D' ] );
- }
- function onWindowResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- switch ( params.procedure ) {
- case 'noiseRandom1D': postMaterial = noiseRandom1DMaterial; break;
- case 'noiseRandom2D': postMaterial = noiseRandom2DMaterial; break;
- case 'noiseRandom3D': postMaterial = noiseRandom3DMaterial; break;
- }
- postQuad.material = postMaterial;
- // render post FX
- renderer.render( postScene, postCamera );
- stats.update();
- }
- </script>
- </body>
- </html>
|