1 function Transition ( sceneA, sceneB ) {
3 this.scene = new THREE.Scene();
5 this.cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
9 var loader = new THREE.TextureLoader();
11 for ( var i = 0; i < 6; i ++ )
12 this.textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
14 this.quadmaterial = new THREE.ShaderMaterial( {
34 value: this.textures[ 0 ]
43 "vUv = vec2( uv.x, uv.y );",
44 "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
51 "uniform float mixRatio;",
53 "uniform sampler2D tDiffuse1;",
54 "uniform sampler2D tDiffuse2;",
55 "uniform sampler2D tMixTexture;",
57 "uniform int useTexture;",
58 "uniform float threshold;",
64 "vec4 texel1 = texture2D( tDiffuse1, vUv );",
65 "vec4 texel2 = texture2D( tDiffuse2, vUv );",
67 "if (useTexture==1) {",
69 "vec4 transitionTexel = texture2D( tMixTexture, vUv );",
70 "float r = mixRatio * (1.0 + threshold * 2.0) - threshold;",
71 "float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);",
73 "gl_FragColor = mix( texel1, texel2, mixf );",
76 "gl_FragColor = mix( texel2, texel1, mixRatio );",
85 var quadgeometry = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
87 this.quad = new THREE.Mesh( quadgeometry, this.quadmaterial );
88 this.scene.add( this.quad );
90 // Link both scenes and their FBOs
94 this.quadmaterial.uniforms.tDiffuse1.value = sceneA.fbo.texture;
95 this.quadmaterial.uniforms.tDiffuse2.value = sceneB.fbo.texture;
97 this.needChange = false;
99 this.setTextureThreshold = function ( value ) {
101 this.quadmaterial.uniforms.threshold.value = value;
105 this.useTexture = function ( value ) {
107 this.quadmaterial.uniforms.useTexture.value = value ? 1 : 0;
111 this.setTexture = function ( i ) {
113 this.quadmaterial.uniforms.tMixTexture.value = this.textures[ i ];
117 this.render = function( delta ) {
119 // Transition animation
120 if ( transitionParams.animateTransition ) {
122 var t = ( 1 + Math.sin( transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI ) ) / 2;
123 transitionParams.transition = THREE.Math.smoothstep( t, 0.3, 0.7 );
125 // Change the current alpha texture after each transition
126 if ( transitionParams.loopTexture && ( transitionParams.transition == 0 || transitionParams.transition == 1 ) ) {
128 if ( this.needChange ) {
130 transitionParams.texture = ( transitionParams.texture + 1 ) % this.textures.length;
131 this.quadmaterial.uniforms.tMixTexture.value = this.textures[ transitionParams.texture ];
132 this.needChange = false;
137 this.needChange = true;
141 this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;
143 // Prevent render both scenes when it's not necessary
144 if ( transitionParams.transition == 0 ) {
146 this.sceneB.render( delta, false );
148 } else if ( transitionParams.transition == 1 ) {
150 this.sceneA.render( delta, false );
154 // When 0<transition<1 render transition between two scenes
156 this.sceneA.render( delta, true );
157 this.sceneB.render( delta, true );
158 renderer.render( this.scene, this.cameraOrtho, null, true );