transition.js 3.7 KB
Newer Older
1 2 3 4 5 6 7
function Transition ( sceneA, sceneB ) {

	this.scene = new THREE.Scene();
	
	this.cameraOrtho = new THREE.OrthographicCamera(window.innerWidth / -2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / -2, -10, 10);

	this.textures = [];
G
gero3 已提交
8 9
	for (var i = 0; i < 6; i ++)
		this.textures[i] = new THREE.ImageUtils.loadTexture('textures/transition/transition' + (i + 1) + '.png');
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
				
	this.quadmaterial = new THREE.ShaderMaterial({

		uniforms: {

			tDiffuse1: {
				type: "t",
				value: null
			},
			tDiffuse2: {
				type: "t",
				value: null
			},
			mixRatio: {
				type: "f",
				value: 0.0
			},
			threshold: {
				type: "f",
				value: 0.1
			},
			useTexture: {
				type: "i",
				value: 1,
			},
			tMixTexture: {
				type: "t",
				value: this.textures[0]
			}
		},
		vertexShader: [

			"varying vec2 vUv;",

			"void main() {",

			"vUv = vec2( uv.x, uv.y );",
			"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

			"}"

		].join("\n"),
		fragmentShader: [

			"uniform float mixRatio;",

			"uniform sampler2D tDiffuse1;",
			"uniform sampler2D tDiffuse2;",
			"uniform sampler2D tMixTexture;",
			
			"uniform int useTexture;",
			"uniform float threshold;",

			"varying vec2 vUv;",

			"void main() {",

			"vec4 texel1 = texture2D( tDiffuse1, vUv );",
			"vec4 texel2 = texture2D( tDiffuse2, vUv );",
			
			"if (useTexture==1) {",
				
				"vec4 transitionTexel = texture2D( tMixTexture, vUv );",
				"float r = mixRatio * (1.0 + threshold * 2.0) - threshold;",
				"float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);",
				
				"gl_FragColor = mix( texel1, texel2, mixf );",
			"} else {",
				
				"gl_FragColor = mix( texel2, texel1, mixRatio );",
				
			"}",
		"}"

		].join("\n")

86
	});
87

88
	quadgeometry = new THREE.PlaneBufferGeometry(window.innerWidth, window.innerHeight);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	
	this.quad = new THREE.Mesh(quadgeometry, this.quadmaterial);
	this.scene.add(this.quad);

	// Link both scenes and their FBOs
	this.sceneA = sceneA;
	this.sceneB = sceneB;

	this.quadmaterial.uniforms.tDiffuse1.value = sceneA.fbo;
	this.quadmaterial.uniforms.tDiffuse2.value = sceneB.fbo;

	this.needChange = false;
	
	this.setTextureThreshold = function ( value ) {
	
G
gero3 已提交
104
		this.quadmaterial.uniforms.threshold.value = value;
105 106 107 108 109

	}
	
	this.useTexture = function ( value ) {
	
G
gero3 已提交
110
		this.quadmaterial.uniforms.useTexture.value = value ? 1 : 0;
111 112 113 114 115 116 117 118 119 120 121 122 123 124

	}
	
	this.setTexture = function ( i ) {
		
		this.quadmaterial.uniforms.tMixTexture.value = this.textures[i];
		
	}
	
	this.render = function( delta ) {
		
		// Transition animation
		if (transitionParams.animateTransition)
		{
G
gero3 已提交
125 126
			var t = (1 + Math.sin(transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI)) / 2;
			transitionParams.transition = THREE.Math.smoothstep(t, 0.3, 0.7);
127 128
			
			// Change the current alpha texture after each transition
G
gero3 已提交
129
			if (transitionParams.loopTexture && (transitionParams.transition == 0 || transitionParams.transition == 1))
130 131 132
			{
				if (this.needChange)
				{
G
gero3 已提交
133
					transitionParams.texture = (transitionParams.texture + 1)%this.textures.length;
134
					this.quadmaterial.uniforms.tMixTexture.value = this.textures[transitionParams.texture];
G
gero3 已提交
135
					this.needChange = false;
136 137 138
				}
			}	
			else
G
gero3 已提交
139
				this.needChange = true;
140 141 142 143 144 145
				
		}
		
		this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;

		// Prevent render both scenes when it's not necessary
G
gero3 已提交
146
		if (transitionParams.transition == 0) {
147 148 149
			
			this.sceneB.render( delta, false );
		
G
gero3 已提交
150
		} else if (transitionParams.transition == 1) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		
			this.sceneA.render( delta, false );
			
		} else {
			
			// When 0<transition<1 render transition between two scenes
			
			this.sceneA.render( delta, true );
			this.sceneB.render( delta, true );
			renderer.render( this.scene, this.cameraOrtho, null, true );

		}

	}
}