transition.js 3.6 KB

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