webgl_postprocessing_crossfade.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - scenes transition</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. .info {
  15. position: absolute;
  16. background-color: black;
  17. opacity: 0.8;
  18. color: white;
  19. text-align: center;
  20. top: 0px;
  21. width: 100%;
  22. }
  23. a { color: #00ffff; }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="info">
  28. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl scene transitions - by <a href="https://twitter.com/fernandojsg">fernandojsg</a> - <a href="https://github.com/kile/three.js-demos">github</a>
  29. </div>
  30. <div id="container"></div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script src="js/libs/dat.gui.min.js"></script>
  34. <script src="js/utils/BufferGeometryUtils.js"></script>
  35. <script>
  36. var container, stats;
  37. var renderer;
  38. var transition;
  39. var transitionParams = {
  40. "useTexture": true,
  41. "transition": 0.5,
  42. "transitionSpeed": 2.0,
  43. "texture": 5,
  44. "loopTexture": true,
  45. "animateTransition": true,
  46. "textureThreshold": 0.3
  47. };
  48. var clock = new THREE.Clock();
  49. init();
  50. animate();
  51. function init() {
  52. initGUI();
  53. container = document.getElementById( "container" );
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. container.appendChild( renderer.domElement );
  58. stats = new Stats();
  59. container.appendChild( stats.dom );
  60. var sceneA = new Scene( "cube", 5000, 1200, 120, new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  61. var sceneB = new Scene( "sphere", 500, 2000, 50, new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );
  62. transition = new Transition( sceneA, sceneB );
  63. }
  64. function animate() {
  65. requestAnimationFrame( animate );
  66. render();
  67. stats.update();
  68. }
  69. function initGUI() {
  70. var gui = new dat.GUI();
  71. gui.add( transitionParams, "useTexture" ).onChange( function ( value ) {
  72. transition.useTexture( value );
  73. } );
  74. gui.add( transitionParams, 'loopTexture' );
  75. gui.add( transitionParams, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } ).onChange( function ( value ) {
  76. transition.setTexture( value );
  77. } ).listen();
  78. gui.add( transitionParams, "textureThreshold", 0, 1, 0.01 ).onChange( function ( value ) {
  79. transition.setTextureThreshold( value );
  80. } );
  81. gui.add( transitionParams, "animateTransition" );
  82. gui.add( transitionParams, "transition", 0, 1, 0.01 ).listen();
  83. gui.add( transitionParams, "transitionSpeed", 0.5, 5, 0.01 );
  84. }
  85. function render() {
  86. transition.render( clock.getDelta() );
  87. }
  88. function generateGeometry( objectType, numObjects ) {
  89. function applyVertexColors( geometry, color ) {
  90. var position = geometry.attributes.position;
  91. var colors = [];
  92. for ( var i = 0; i < position.count; i ++ ) {
  93. colors.push( color.r, color.g, color.b );
  94. }
  95. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  96. }
  97. var geometries = [];
  98. var matrix = new THREE.Matrix4();
  99. var position = new THREE.Vector3();
  100. var rotation = new THREE.Euler();
  101. var quaternion = new THREE.Quaternion();
  102. var scale = new THREE.Vector3();
  103. var color = new THREE.Color();
  104. for ( var i = 0; i < numObjects; i ++ ) {
  105. position.x = Math.random() * 10000 - 5000;
  106. position.y = Math.random() * 6000 - 3000;
  107. position.z = Math.random() * 8000 - 4000;
  108. rotation.x = Math.random() * 2 * Math.PI;
  109. rotation.y = Math.random() * 2 * Math.PI;
  110. rotation.z = Math.random() * 2 * Math.PI;
  111. quaternion.setFromEuler( rotation, false );
  112. scale.x = Math.random() * 200 + 100;
  113. var geometry;
  114. if ( objectType === 'cube' ) {
  115. geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  116. geometry = geometry.toNonIndexed(); // merging needs consistent buffer geometries
  117. scale.y = Math.random() * 200 + 100;
  118. scale.z = Math.random() * 200 + 100;
  119. color.setRGB( 0, 0, 0.1 + 0.9 * Math.random() );
  120. } else if ( objectType === 'sphere' ) {
  121. geometry = new THREE.IcosahedronBufferGeometry( 1, 1 );
  122. scale.y = scale.z = scale.x;
  123. color.setRGB( 0.1 + 0.9 * Math.random(), 0, 0 );
  124. }
  125. // give the geom's vertices a random color, to be displayed
  126. applyVertexColors( geometry, color );
  127. matrix.compose( position, quaternion, scale );
  128. geometry.applyMatrix( matrix );
  129. geometries.push( geometry );
  130. }
  131. return THREE.BufferGeometryUtils.mergeBufferGeometries( geometries );
  132. }
  133. function Scene( type, numObjects, cameraZ, fov, rotationSpeed, clearColor ) {
  134. this.clearColor = clearColor;
  135. this.camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 10000 );
  136. this.camera.position.z = cameraZ;
  137. // Setup scene
  138. this.scene = new THREE.Scene();
  139. this.scene.add( new THREE.AmbientLight( 0x555555 ) );
  140. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  141. light.position.set( 0, 500, 2000 );
  142. this.scene.add( light );
  143. this.rotationSpeed = rotationSpeed;
  144. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors } );
  145. this.mesh = new THREE.Mesh( generateGeometry( type, numObjects ), defaultMaterial );
  146. this.scene.add( this.mesh );
  147. var renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  148. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
  149. this.render = function ( delta, rtt ) {
  150. this.mesh.rotation.x += delta * this.rotationSpeed.x;
  151. this.mesh.rotation.y += delta * this.rotationSpeed.y;
  152. this.mesh.rotation.z += delta * this.rotationSpeed.z;
  153. renderer.setClearColor( this.clearColor );
  154. if ( rtt ) {
  155. renderer.setRenderTarget( this.fbo );
  156. renderer.clear();
  157. renderer.render( this.scene, this.camera );
  158. } else {
  159. renderer.setRenderTarget( null );
  160. renderer.render( this.scene, this.camera );
  161. }
  162. };
  163. }
  164. function Transition( sceneA, sceneB ) {
  165. this.scene = new THREE.Scene();
  166. this.cameraOrtho = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10, 10 );
  167. this.textures = [];
  168. var loader = new THREE.TextureLoader();
  169. for ( var i = 0; i < 6; i ++ )
  170. this.textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  171. this.quadmaterial = new THREE.ShaderMaterial( {
  172. uniforms: {
  173. tDiffuse1: {
  174. value: null
  175. },
  176. tDiffuse2: {
  177. value: null
  178. },
  179. mixRatio: {
  180. value: 0.0
  181. },
  182. threshold: {
  183. value: 0.1
  184. },
  185. useTexture: {
  186. value: 1
  187. },
  188. tMixTexture: {
  189. value: this.textures[ 0 ]
  190. }
  191. },
  192. vertexShader: [
  193. "varying vec2 vUv;",
  194. "void main() {",
  195. "vUv = vec2( uv.x, uv.y );",
  196. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  197. "}"
  198. ].join( "\n" ),
  199. fragmentShader: [
  200. "uniform float mixRatio;",
  201. "uniform sampler2D tDiffuse1;",
  202. "uniform sampler2D tDiffuse2;",
  203. "uniform sampler2D tMixTexture;",
  204. "uniform int useTexture;",
  205. "uniform float threshold;",
  206. "varying vec2 vUv;",
  207. "void main() {",
  208. " vec4 texel1 = texture2D( tDiffuse1, vUv );",
  209. " vec4 texel2 = texture2D( tDiffuse2, vUv );",
  210. " if (useTexture==1) {",
  211. " vec4 transitionTexel = texture2D( tMixTexture, vUv );",
  212. " float r = mixRatio * (1.0 + threshold * 2.0) - threshold;",
  213. " float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);",
  214. " gl_FragColor = mix( texel1, texel2, mixf );",
  215. " } else {",
  216. " gl_FragColor = mix( texel2, texel1, mixRatio );",
  217. " }",
  218. "}"
  219. ].join( "\n" )
  220. } );
  221. var quadgeometry = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
  222. this.quad = new THREE.Mesh( quadgeometry, this.quadmaterial );
  223. this.scene.add( this.quad );
  224. // Link both scenes and their FBOs
  225. this.sceneA = sceneA;
  226. this.sceneB = sceneB;
  227. this.quadmaterial.uniforms.tDiffuse1.value = sceneA.fbo.texture;
  228. this.quadmaterial.uniforms.tDiffuse2.value = sceneB.fbo.texture;
  229. this.needChange = false;
  230. this.setTextureThreshold = function ( value ) {
  231. this.quadmaterial.uniforms.threshold.value = value;
  232. };
  233. this.useTexture = function ( value ) {
  234. this.quadmaterial.uniforms.useTexture.value = value ? 1 : 0;
  235. };
  236. this.setTexture = function ( i ) {
  237. this.quadmaterial.uniforms.tMixTexture.value = this.textures[ i ];
  238. };
  239. this.render = function ( delta ) {
  240. // Transition animation
  241. if ( transitionParams.animateTransition ) {
  242. var t = ( 1 + Math.sin( transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI ) ) / 2;
  243. transitionParams.transition = THREE.Math.smoothstep( t, 0.3, 0.7 );
  244. // Change the current alpha texture after each transition
  245. if ( transitionParams.loopTexture && ( transitionParams.transition == 0 || transitionParams.transition == 1 ) ) {
  246. if ( this.needChange ) {
  247. transitionParams.texture = ( transitionParams.texture + 1 ) % this.textures.length;
  248. this.quadmaterial.uniforms.tMixTexture.value = this.textures[ transitionParams.texture ];
  249. this.needChange = false;
  250. }
  251. } else
  252. this.needChange = true;
  253. }
  254. this.quadmaterial.uniforms.mixRatio.value = transitionParams.transition;
  255. // Prevent render both scenes when it's not necessary
  256. if ( transitionParams.transition == 0 ) {
  257. this.sceneB.render( delta, false );
  258. } else if ( transitionParams.transition == 1 ) {
  259. this.sceneA.render( delta, false );
  260. } else {
  261. // When 0<transition<1 render transition between two scenes
  262. this.sceneA.render( delta, true );
  263. this.sceneB.render( delta, true );
  264. renderer.setRenderTarget( null );
  265. renderer.clear();
  266. renderer.render( this.scene, this.cameraOrtho );
  267. }
  268. };
  269. }
  270. </script>
  271. </body>
  272. </html>