webgl_postprocessing_transition.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import TWEEN from 'three/addons/libs/tween.module.js';
  27. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  28. import { RenderTransitionPass } from 'three/addons/postprocessing/RenderTransitionPass.js';
  29. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  30. let stats;
  31. let renderer, composer, renderTransitionPass;
  32. const textures = [];
  33. const clock = new THREE.Clock();
  34. const params = {
  35. sceneAnimate: true,
  36. transitionAnimate: true,
  37. transition: 0,
  38. useTexture: true,
  39. texture: 5,
  40. cycle: true,
  41. threshold: 0.1,
  42. };
  43. const fxSceneA = new FXScene( new THREE.BoxGeometry( 2, 2, 2 ), new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  44. const fxSceneB = new FXScene( new THREE.IcosahedronGeometry( 1, 1 ), new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );
  45. init();
  46. function init() {
  47. initGUI();
  48. initTextures();
  49. renderer = new THREE.WebGLRenderer( { antialias: true } );
  50. renderer.setPixelRatio( window.devicePixelRatio );
  51. renderer.setSize( window.innerWidth, window.innerHeight );
  52. renderer.setAnimationLoop( animate );
  53. document.body.appendChild( renderer.domElement );
  54. composer = new EffectComposer( renderer );
  55. stats = new Stats();
  56. document.body.appendChild( stats.dom );
  57. renderTransitionPass = new RenderTransitionPass( fxSceneA.scene, fxSceneA.camera, fxSceneB.scene, fxSceneB.camera );
  58. renderTransitionPass.setTexture( textures[ 0 ] );
  59. composer.addPass( renderTransitionPass );
  60. const outputPass = new OutputPass();
  61. composer.addPass( outputPass );
  62. }
  63. window.addEventListener( 'resize', onWindowResize );
  64. function onWindowResize() {
  65. fxSceneA.resize();
  66. fxSceneB.resize();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. composer.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. new TWEEN.Tween( params )
  71. .to( { transition: 1 }, 1500 )
  72. .onUpdate( function () {
  73. renderTransitionPass.setTransition( params.transition );
  74. // Change the current alpha texture after each transition
  75. if ( params.cycle ) {
  76. if ( params.transition == 0 || params.transition == 1 ) {
  77. params.texture = ( params.texture + 1 ) % textures.length;
  78. renderTransitionPass.setTexture( textures[ params.texture ] );
  79. }
  80. }
  81. } )
  82. .repeat( Infinity )
  83. .delay( 2000 )
  84. .yoyo( true )
  85. .start();
  86. function animate() {
  87. // Transition animation
  88. if ( params.transitionAnimate ) TWEEN.update();
  89. const delta = clock.getDelta();
  90. fxSceneA.update( delta );
  91. fxSceneB.update( delta );
  92. render();
  93. stats.update();
  94. }
  95. function initTextures() {
  96. const loader = new THREE.TextureLoader();
  97. for ( let i = 0; i < 6; i ++ ) {
  98. textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  99. }
  100. }
  101. function initGUI() {
  102. const gui = new GUI();
  103. gui.add( params, 'sceneAnimate' ).name( 'Animate scene' );
  104. gui.add( params, 'transitionAnimate' ).name( 'Animate transition' );
  105. gui.add( params, 'transition', 0, 1, 0.01 ).onChange( function ( value ) {
  106. renderTransitionPass.setTransition( value );
  107. } ).listen();
  108. gui.add( params, 'useTexture' ).onChange( function ( value ) {
  109. renderTransitionPass.useTexture( value );
  110. } );
  111. gui.add( params, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } ).onChange( function ( value ) {
  112. renderTransitionPass.setTexture( textures[ value ] );
  113. } ).listen();
  114. gui.add( params, 'cycle' );
  115. gui.add( params, 'threshold', 0, 1, 0.01 ).onChange( function ( value ) {
  116. renderTransitionPass.setTextureThreshold( value );
  117. } );
  118. }
  119. function render() {
  120. // Prevent render both scenes when it's not necessary
  121. if ( params.transition === 0 ) {
  122. renderer.render( fxSceneB.scene, fxSceneB.camera );
  123. } else if ( params.transition === 1 ) {
  124. renderer.render( fxSceneA.scene, fxSceneA.camera );
  125. } else {
  126. // When 0 < transition < 1 render transition between two scenes
  127. composer.render();
  128. }
  129. }
  130. function FXScene( geometry, rotationSpeed, backgroundColor ) {
  131. const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  132. camera.position.z = 20;
  133. // Setup scene
  134. const scene = new THREE.Scene();
  135. scene.background = new THREE.Color( backgroundColor );
  136. scene.add( new THREE.AmbientLight( 0xaaaaaa, 3 ) );
  137. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  138. light.position.set( 0, 1, 4 );
  139. scene.add( light );
  140. this.rotationSpeed = rotationSpeed;
  141. const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
  142. const material = new THREE.MeshPhongMaterial( { color: color, flatShading: true } );
  143. const mesh = generateInstancedMesh( geometry, material, 500 );
  144. scene.add( mesh );
  145. this.scene = scene;
  146. this.camera = camera;
  147. this.mesh = mesh;
  148. this.update = function ( delta ) {
  149. if ( params.sceneAnimate ) {
  150. mesh.rotation.x += this.rotationSpeed.x * delta;
  151. mesh.rotation.y += this.rotationSpeed.y * delta;
  152. mesh.rotation.z += this.rotationSpeed.z * delta;
  153. }
  154. };
  155. this.resize = function () {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. };
  159. }
  160. function generateInstancedMesh( geometry, material, count ) {
  161. const mesh = new THREE.InstancedMesh( geometry, material, count );
  162. const dummy = new THREE.Object3D();
  163. const color = new THREE.Color();
  164. for ( let i = 0; i < count; i ++ ) {
  165. dummy.position.x = Math.random() * 100 - 50;
  166. dummy.position.y = Math.random() * 60 - 30;
  167. dummy.position.z = Math.random() * 80 - 40;
  168. dummy.rotation.x = Math.random() * 2 * Math.PI;
  169. dummy.rotation.y = Math.random() * 2 * Math.PI;
  170. dummy.rotation.z = Math.random() * 2 * Math.PI;
  171. dummy.scale.x = Math.random() * 2 + 1;
  172. if ( geometry.type === 'BoxGeometry' ) {
  173. dummy.scale.y = Math.random() * 2 + 1;
  174. dummy.scale.z = Math.random() * 2 + 1;
  175. } else {
  176. dummy.scale.y = dummy.scale.x;
  177. dummy.scale.z = dummy.scale.x;
  178. }
  179. dummy.updateMatrix();
  180. mesh.setMatrixAt( i, dummy.matrix );
  181. mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );
  182. }
  183. return mesh;
  184. }
  185. </script>
  186. </body>
  187. </html>