transition.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.PlaneGeometry(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. {
  85. var t=(1+Math.sin(transitionParams.transitionSpeed*clock.getElapsedTime()/Math.PI))/2;
  86. transitionParams.transition=THREE.Math.smoothstep(t,0.3,0.7);
  87. // Change the current alpha texture after each transition
  88. if (transitionParams.loopTexture && (transitionParams.transition==0 || transitionParams.transition==1))
  89. {
  90. if (this.needChange)
  91. {
  92. transitionParams.texture=(transitionParams.texture+1)%this.textures.length;
  93. this.quadmaterial.uniforms.tMixTexture.value = this.textures[transitionParams.texture];
  94. this.needChange=false;
  95. }
  96. }
  97. else
  98. this.needChange=true;
  99. }
  100. this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;
  101. // Prevent render both scenes when it's not necessary
  102. if (transitionParams.transition==0) {
  103. this.sceneB.render( delta, false );
  104. } else if (transitionParams.transition==1) {
  105. this.sceneA.render( delta, false );
  106. } else {
  107. // When 0<transition<1 render transition between two scenes
  108. this.sceneA.render( delta, true );
  109. this.sceneB.render( delta, true );
  110. renderer.render( this.scene, this.cameraOrtho, null, true );
  111. }
  112. }
  113. }