transition.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. function Transition ( sceneA, sceneB ) {
  2. this.scene = new THREE.Scene();
  3. this.cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
  4. this.textures = [];
  5. var loader = new THREE.TextureLoader();
  6. for ( var i = 0; i < 6; i ++ )
  7. this.textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  8. this.quadmaterial = new THREE.ShaderMaterial( {
  9. uniforms: {
  10. tDiffuse1: {
  11. value: null
  12. },
  13. tDiffuse2: {
  14. value: null
  15. },
  16. mixRatio: {
  17. value: 0.0
  18. },
  19. threshold: {
  20. value: 0.1
  21. },
  22. useTexture: {
  23. value: 1
  24. },
  25. tMixTexture: {
  26. value: this.textures[ 0 ]
  27. }
  28. },
  29. vertexShader: [
  30. "varying vec2 vUv;",
  31. "void main() {",
  32. "vUv = vec2( uv.x, uv.y );",
  33. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  34. "}"
  35. ].join( "\n" ),
  36. fragmentShader: [
  37. "uniform float mixRatio;",
  38. "uniform sampler2D tDiffuse1;",
  39. "uniform sampler2D tDiffuse2;",
  40. "uniform sampler2D tMixTexture;",
  41. "uniform int useTexture;",
  42. "uniform float threshold;",
  43. "varying vec2 vUv;",
  44. "void main() {",
  45. " vec4 texel1 = texture2D( tDiffuse1, vUv );",
  46. " vec4 texel2 = texture2D( tDiffuse2, vUv );",
  47. " if (useTexture==1) {",
  48. " vec4 transitionTexel = texture2D( tMixTexture, vUv );",
  49. " float r = mixRatio * (1.0 + threshold * 2.0) - threshold;",
  50. " float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);",
  51. " gl_FragColor = mix( texel1, texel2, mixf );",
  52. " } else {",
  53. " gl_FragColor = mix( texel2, texel1, mixRatio );",
  54. " }",
  55. "}"
  56. ].join( "\n" )
  57. } );
  58. var quadgeometry = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
  59. this.quad = new THREE.Mesh( quadgeometry, this.quadmaterial );
  60. this.scene.add( this.quad );
  61. // Link both scenes and their FBOs
  62. this.sceneA = sceneA;
  63. this.sceneB = sceneB;
  64. this.quadmaterial.uniforms.tDiffuse1.value = sceneA.fbo.texture;
  65. this.quadmaterial.uniforms.tDiffuse2.value = sceneB.fbo.texture;
  66. this.needChange = false;
  67. this.setTextureThreshold = function ( value ) {
  68. this.quadmaterial.uniforms.threshold.value = value;
  69. };
  70. this.useTexture = function ( value ) {
  71. this.quadmaterial.uniforms.useTexture.value = value ? 1 : 0;
  72. };
  73. this.setTexture = function ( i ) {
  74. this.quadmaterial.uniforms.tMixTexture.value = this.textures[ i ];
  75. };
  76. this.render = function ( delta ) {
  77. // Transition animation
  78. if ( transitionParams.animateTransition ) {
  79. var t = ( 1 + Math.sin( transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI ) ) / 2;
  80. transitionParams.transition = THREE.Math.smoothstep( t, 0.3, 0.7 );
  81. // Change the current alpha texture after each transition
  82. if ( transitionParams.loopTexture && ( transitionParams.transition == 0 || transitionParams.transition == 1 ) ) {
  83. if ( this.needChange ) {
  84. transitionParams.texture = ( transitionParams.texture + 1 ) % this.textures.length;
  85. this.quadmaterial.uniforms.tMixTexture.value = this.textures[ transitionParams.texture ];
  86. this.needChange = false;
  87. }
  88. } else
  89. this.needChange = true;
  90. }
  91. this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;
  92. // Prevent render both scenes when it's not necessary
  93. if ( transitionParams.transition == 0 ) {
  94. this.sceneB.render( delta, false );
  95. } else if ( transitionParams.transition == 1 ) {
  96. this.sceneA.render( delta, false );
  97. } else {
  98. // When 0<transition<1 render transition between two scenes
  99. this.sceneA.render( delta, true );
  100. this.sceneB.render( delta, true );
  101. renderer.render( this.scene, this.cameraOrtho, null, true );
  102. }
  103. };
  104. }