webgl_postprocessing_transition.html 7.3 KB

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