webgl_postprocessing_crossfade.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - scenes transition</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> webgl scene transitions<br/>
  12. by <a href="https://twitter.com/fernandojsg">fernandojsg</a> - <a href="https://github.com/kile/three.js-demos">github</a>
  13. </div>
  14. <div id="container"></div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import TWEEN from 'three/addons/libs/tween.module.js';
  31. let container, stats;
  32. let renderer;
  33. let transition;
  34. const transitionParams = {
  35. 'useTexture': true,
  36. 'transition': 0,
  37. 'texture': 5,
  38. 'cycle': true,
  39. 'animate': true,
  40. 'threshold': 0.3
  41. };
  42. const clock = new THREE.Clock();
  43. init();
  44. animate();
  45. function init() {
  46. initGUI();
  47. container = document.getElementById( 'container' );
  48. renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. container.appendChild( renderer.domElement );
  52. stats = new Stats();
  53. container.appendChild( stats.dom );
  54. const geometryA = new THREE.BoxGeometry( 2, 2, 2 );
  55. const geometryB = new THREE.IcosahedronGeometry( 1, 1 );
  56. const sceneA = new FXScene( geometryA, new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  57. const sceneB = new FXScene( geometryB, new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );
  58. transition = new Transition( sceneA, sceneB );
  59. }
  60. function animate() {
  61. requestAnimationFrame( animate );
  62. render();
  63. stats.update();
  64. }
  65. function initGUI() {
  66. const gui = new GUI();
  67. gui.add( transitionParams, 'animate' );
  68. gui.add( transitionParams, 'transition', 0, 1, 0.01 ).listen();
  69. gui.add( transitionParams, 'useTexture' ).onChange( function ( value ) {
  70. transition.useTexture( value );
  71. } );
  72. gui.add( transitionParams, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } ).onChange( function ( value ) {
  73. transition.setTexture( value );
  74. } ).listen();
  75. gui.add( transitionParams, 'cycle' );
  76. gui.add( transitionParams, 'threshold', 0, 1, 0.01 ).onChange( function ( value ) {
  77. transition.setTextureThreshold( value );
  78. } );
  79. }
  80. function render() {
  81. transition.render( clock.getDelta() );
  82. }
  83. function generateInstancedMesh( geometry, material, count ) {
  84. const mesh = new THREE.InstancedMesh( geometry, material, count );
  85. const dummy = new THREE.Object3D();
  86. const color = new THREE.Color();
  87. for ( let i = 0; i < count; i ++ ) {
  88. dummy.position.x = Math.random() * 100 - 50;
  89. dummy.position.y = Math.random() * 60 - 30;
  90. dummy.position.z = Math.random() * 80 - 40;
  91. dummy.rotation.x = Math.random() * 2 * Math.PI;
  92. dummy.rotation.y = Math.random() * 2 * Math.PI;
  93. dummy.rotation.z = Math.random() * 2 * Math.PI;
  94. dummy.scale.x = Math.random() * 2 + 1;
  95. if ( geometry.type === 'BoxGeometry' ) {
  96. dummy.scale.y = Math.random() * 2 + 1;
  97. dummy.scale.z = Math.random() * 2 + 1;
  98. } else {
  99. dummy.scale.y = dummy.scale.x;
  100. dummy.scale.z = dummy.scale.x;
  101. }
  102. dummy.updateMatrix();
  103. mesh.setMatrixAt( i, dummy.matrix );
  104. mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );
  105. }
  106. return mesh;
  107. }
  108. function FXScene( geometry, rotationSpeed, clearColor ) {
  109. this.clearColor = clearColor;
  110. const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  111. camera.position.z = 20;
  112. // Setup scene
  113. const scene = new THREE.Scene();
  114. scene.add( new THREE.AmbientLight( 0xaaaaaa, 3 ) );
  115. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  116. light.position.set( 0, 1, 4 );
  117. scene.add( light );
  118. this.rotationSpeed = rotationSpeed;
  119. const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
  120. const material = new THREE.MeshPhongMaterial( { color: color, flatShading: true } );
  121. const mesh = generateInstancedMesh( geometry, material, 500 );
  122. scene.add( mesh );
  123. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { type: THREE.HalfFloatType } );
  124. this.render = function ( delta, rtt ) {
  125. mesh.rotation.x += delta * this.rotationSpeed.x;
  126. mesh.rotation.y += delta * this.rotationSpeed.y;
  127. mesh.rotation.z += delta * this.rotationSpeed.z;
  128. renderer.setClearColor( this.clearColor );
  129. if ( rtt ) {
  130. renderer.setRenderTarget( this.fbo );
  131. renderer.clear();
  132. renderer.render( scene, camera );
  133. } else {
  134. renderer.setRenderTarget( null );
  135. renderer.render( scene, camera );
  136. }
  137. };
  138. }
  139. function Transition( sceneA, sceneB ) {
  140. const scene = new THREE.Scene();
  141. const width = window.innerWidth;
  142. const height = window.innerHeight;
  143. const camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, - 10, 10 );
  144. const textures = [];
  145. const loader = new THREE.TextureLoader();
  146. for ( let i = 0; i < 6; i ++ ) {
  147. textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  148. }
  149. const material = new THREE.ShaderMaterial( {
  150. uniforms: {
  151. tDiffuse1: {
  152. value: null
  153. },
  154. tDiffuse2: {
  155. value: null
  156. },
  157. mixRatio: {
  158. value: 0.0
  159. },
  160. threshold: {
  161. value: 0.1
  162. },
  163. useTexture: {
  164. value: 1
  165. },
  166. tMixTexture: {
  167. value: textures[ 0 ]
  168. }
  169. },
  170. vertexShader: [
  171. 'varying vec2 vUv;',
  172. 'void main() {',
  173. 'vUv = vec2( uv.x, uv.y );',
  174. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  175. '}'
  176. ].join( '\n' ),
  177. fragmentShader: [
  178. 'uniform float mixRatio;',
  179. 'uniform sampler2D tDiffuse1;',
  180. 'uniform sampler2D tDiffuse2;',
  181. 'uniform sampler2D tMixTexture;',
  182. 'uniform int useTexture;',
  183. 'uniform float threshold;',
  184. 'varying vec2 vUv;',
  185. 'void main() {',
  186. ' vec4 texel1 = texture2D( tDiffuse1, vUv );',
  187. ' vec4 texel2 = texture2D( tDiffuse2, vUv );',
  188. ' if (useTexture==1) {',
  189. ' vec4 transitionTexel = texture2D( tMixTexture, vUv );',
  190. ' float r = mixRatio * (1.0 + threshold * 2.0) - threshold;',
  191. ' float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);',
  192. ' gl_FragColor = mix( texel1, texel2, mixf );',
  193. ' } else {',
  194. ' gl_FragColor = mix( texel2, texel1, mixRatio );',
  195. ' }',
  196. ' #include <tonemapping_fragment>',
  197. ' #include <colorspace_fragment>',
  198. '}'
  199. ].join( '\n' )
  200. } );
  201. const geometry = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight );
  202. const mesh = new THREE.Mesh( geometry, material );
  203. scene.add( mesh );
  204. material.uniforms.tDiffuse1.value = sceneA.fbo.texture;
  205. material.uniforms.tDiffuse2.value = sceneB.fbo.texture;
  206. new TWEEN.Tween( transitionParams )
  207. .to( { transition: 1 }, 1500 )
  208. .repeat( Infinity )
  209. .delay( 2000 )
  210. .yoyo( true )
  211. .start();
  212. this.needsTextureChange = false;
  213. this.setTextureThreshold = function ( value ) {
  214. material.uniforms.threshold.value = value;
  215. };
  216. this.useTexture = function ( value ) {
  217. material.uniforms.useTexture.value = value ? 1 : 0;
  218. };
  219. this.setTexture = function ( i ) {
  220. material.uniforms.tMixTexture.value = textures[ i ];
  221. };
  222. this.render = function ( delta ) {
  223. // Transition animation
  224. if ( transitionParams.animate ) {
  225. TWEEN.update();
  226. // Change the current alpha texture after each transition
  227. if ( transitionParams.cycle ) {
  228. if ( transitionParams.transition == 0 || transitionParams.transition == 1 ) {
  229. if ( this.needsTextureChange ) {
  230. transitionParams.texture = ( transitionParams.texture + 1 ) % textures.length;
  231. material.uniforms.tMixTexture.value = textures[ transitionParams.texture ];
  232. this.needsTextureChange = false;
  233. }
  234. } else {
  235. this.needsTextureChange = true;
  236. }
  237. } else {
  238. this.needsTextureChange = true;
  239. }
  240. }
  241. material.uniforms.mixRatio.value = transitionParams.transition;
  242. // Prevent render both scenes when it's not necessary
  243. if ( transitionParams.transition == 0 ) {
  244. sceneB.render( delta, false );
  245. } else if ( transitionParams.transition == 1 ) {
  246. sceneA.render( delta, false );
  247. } else {
  248. // When 0<transition<1 render transition between two scenes
  249. sceneA.render( delta, true );
  250. sceneB.render( delta, true );
  251. renderer.setRenderTarget( null );
  252. renderer.clear();
  253. renderer.render( scene, camera );
  254. }
  255. };
  256. }
  257. </script>
  258. </body>
  259. </html>