transition.js 3.8 KB

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