forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransition.js
More file actions
161 lines (103 loc) · 3.64 KB
/
transition.js
File metadata and controls
161 lines (103 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 = [];
for ( var i = 0; i < 6; i ++ )
this.textures[ i ] = new THREE.TextureLoader().load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
this.quadmaterial = new THREE.ShaderMaterial( {
uniforms: {
tDiffuse1: {
value: null
},
tDiffuse2: {
value: null
},
mixRatio: {
value: 0.0
},
threshold: {
value: 0.1
},
useTexture: {
value: 1
},
tMixTexture: {
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" )
} );
quadgeometry = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
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.texture;
this.quadmaterial.uniforms.tDiffuse2.value = sceneB.fbo.texture;
this.needChange = false;
this.setTextureThreshold = function ( value ) {
this.quadmaterial.uniforms.threshold.value = value;
};
this.useTexture = function ( value ) {
this.quadmaterial.uniforms.useTexture.value = value ? 1 : 0;
};
this.setTexture = function ( i ) {
this.quadmaterial.uniforms.tMixTexture.value = this.textures[ i ];
};
this.render = function( delta ) {
// Transition animation
if ( transitionParams.animateTransition ) {
var t = ( 1 + Math.sin( transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI ) ) / 2;
transitionParams.transition = THREE.Math.smoothstep( t, 0.3, 0.7 );
// Change the current alpha texture after each transition
if ( transitionParams.loopTexture && ( transitionParams.transition == 0 || transitionParams.transition == 1 ) ) {
if ( this.needChange ) {
transitionParams.texture = ( transitionParams.texture + 1 ) % this.textures.length;
this.quadmaterial.uniforms.tMixTexture.value = this.textures[ transitionParams.texture ];
this.needChange = false;
}
} else
this.needChange = true;
}
this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;
// Prevent render both scenes when it's not necessary
if ( transitionParams.transition == 0 ) {
this.sceneB.render( delta, false );
} else if ( transitionParams.transition == 1 ) {
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 );
}
}
}