webgl_postprocessing_transition.html 7.6 KB

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