webgl_postprocessing_crossfade.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl scene transitions<br/>
  12. by <a href="https://twitter.com/fernandojsg">fernandojsg</a> - <a href="https://github.com/kile/three.js-demos">github</a>
  13. </div>
  14. <div id="container"></div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  19. import { TWEEN } from './jsm/libs/tween.module.min.js';
  20. let container, stats;
  21. let renderer;
  22. let transition;
  23. const transitionParams = {
  24. 'useTexture': true,
  25. 'transition': 0,
  26. 'texture': 5,
  27. 'cycle': true,
  28. 'animate': true,
  29. 'threshold': 0.3
  30. };
  31. const clock = new THREE.Clock();
  32. init();
  33. animate();
  34. function init() {
  35. initGUI();
  36. container = document.getElementById( 'container' );
  37. renderer = new THREE.WebGLRenderer( { antialias: true } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. container.appendChild( renderer.domElement );
  41. stats = new Stats();
  42. container.appendChild( stats.dom );
  43. const geometryA = new THREE.BoxGeometry( 2, 2, 2 );
  44. const geometryB = new THREE.IcosahedronGeometry( 1, 1 );
  45. const sceneA = new FXScene( geometryA, new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  46. const sceneB = new FXScene( geometryB, new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );
  47. transition = new Transition( sceneA, sceneB );
  48. }
  49. function animate() {
  50. requestAnimationFrame( animate );
  51. render();
  52. stats.update();
  53. }
  54. function initGUI() {
  55. const gui = new GUI();
  56. gui.add( transitionParams, 'animate' );
  57. gui.add( transitionParams, 'transition', 0, 1, 0.01 ).listen();
  58. gui.add( transitionParams, 'useTexture' ).onChange( function ( value ) {
  59. transition.useTexture( value );
  60. } );
  61. gui.add( transitionParams, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } ).onChange( function ( value ) {
  62. transition.setTexture( value );
  63. } ).listen();
  64. gui.add( transitionParams, 'cycle' );
  65. gui.add( transitionParams, 'threshold', 0, 1, 0.01 ).onChange( function ( value ) {
  66. transition.setTextureThreshold( value );
  67. } );
  68. }
  69. function render() {
  70. transition.render( clock.getDelta() );
  71. }
  72. function generateInstancedMesh( geometry, material, count ) {
  73. const mesh = new THREE.InstancedMesh( geometry, material, count );
  74. const dummy = new THREE.Object3D();
  75. const color = new THREE.Color();
  76. for ( let i = 0; i < count; i ++ ) {
  77. dummy.position.x = Math.random() * 10000 - 5000;
  78. dummy.position.y = Math.random() * 6000 - 3000;
  79. dummy.position.z = Math.random() * 8000 - 4000;
  80. dummy.rotation.x = Math.random() * 2 * Math.PI;
  81. dummy.rotation.y = Math.random() * 2 * Math.PI;
  82. dummy.rotation.z = Math.random() * 2 * Math.PI;
  83. dummy.scale.x = Math.random() * 200 + 100;
  84. if ( geometry.type === 'BoxGeometry' ) {
  85. dummy.scale.y = Math.random() * 200 + 100;
  86. dummy.scale.z = Math.random() * 200 + 100;
  87. } else {
  88. dummy.scale.y = dummy.scale.x;
  89. dummy.scale.z = dummy.scale.x;
  90. }
  91. dummy.updateMatrix();
  92. mesh.setMatrixAt( i, dummy.matrix );
  93. mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );
  94. }
  95. return mesh;
  96. }
  97. function FXScene( geometry, rotationSpeed, clearColor ) {
  98. this.clearColor = clearColor;
  99. const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  100. camera.position.z = 2000;
  101. // Setup scene
  102. const scene = new THREE.Scene();
  103. scene.add( new THREE.AmbientLight( 0x555555 ) );
  104. const light = new THREE.SpotLight( 0xffffff, 1.5 );
  105. light.position.set( 0, 500, 2000 );
  106. scene.add( light );
  107. this.rotationSpeed = rotationSpeed;
  108. const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
  109. const material = new THREE.MeshPhongMaterial( { color: color, flatShading: true } );
  110. const mesh = generateInstancedMesh( geometry, material, 500 );
  111. scene.add( mesh );
  112. const renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  113. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
  114. this.render = function ( delta, rtt ) {
  115. mesh.rotation.x += delta * this.rotationSpeed.x;
  116. mesh.rotation.y += delta * this.rotationSpeed.y;
  117. mesh.rotation.z += delta * this.rotationSpeed.z;
  118. renderer.setClearColor( this.clearColor );
  119. if ( rtt ) {
  120. renderer.setRenderTarget( this.fbo );
  121. renderer.clear();
  122. renderer.render( scene, camera );
  123. } else {
  124. renderer.setRenderTarget( null );
  125. renderer.render( scene, camera );
  126. }
  127. };
  128. }
  129. function Transition( sceneA, sceneB ) {
  130. const scene = new THREE.Scene();
  131. const width = window.innerWidth;
  132. const height = window.innerHeight;
  133. const camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, - 10, 10 );
  134. const textures = [];
  135. const loader = new THREE.TextureLoader();
  136. for ( let i = 0; i < 6; i ++ ) {
  137. textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  138. }
  139. const material = new THREE.ShaderMaterial( {
  140. uniforms: {
  141. tDiffuse1: {
  142. value: null
  143. },
  144. tDiffuse2: {
  145. value: null
  146. },
  147. mixRatio: {
  148. value: 0.0
  149. },
  150. threshold: {
  151. value: 0.1
  152. },
  153. useTexture: {
  154. value: 1
  155. },
  156. tMixTexture: {
  157. value: textures[ 0 ]
  158. }
  159. },
  160. vertexShader: [
  161. 'varying vec2 vUv;',
  162. 'void main() {',
  163. 'vUv = vec2( uv.x, uv.y );',
  164. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  165. '}'
  166. ].join( '\n' ),
  167. fragmentShader: [
  168. 'uniform float mixRatio;',
  169. 'uniform sampler2D tDiffuse1;',
  170. 'uniform sampler2D tDiffuse2;',
  171. 'uniform sampler2D tMixTexture;',
  172. 'uniform int useTexture;',
  173. 'uniform float threshold;',
  174. 'varying vec2 vUv;',
  175. 'void main() {',
  176. ' vec4 texel1 = texture2D( tDiffuse1, vUv );',
  177. ' vec4 texel2 = texture2D( tDiffuse2, vUv );',
  178. ' if (useTexture==1) {',
  179. ' vec4 transitionTexel = texture2D( tMixTexture, vUv );',
  180. ' float r = mixRatio * (1.0 + threshold * 2.0) - threshold;',
  181. ' float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);',
  182. ' gl_FragColor = mix( texel1, texel2, mixf );',
  183. ' } else {',
  184. ' gl_FragColor = mix( texel2, texel1, mixRatio );',
  185. ' }',
  186. '}'
  187. ].join( '\n' )
  188. } );
  189. const geometry = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight );
  190. const mesh = new THREE.Mesh( geometry, material );
  191. scene.add( mesh );
  192. material.uniforms.tDiffuse1.value = sceneA.fbo.texture;
  193. material.uniforms.tDiffuse2.value = sceneB.fbo.texture;
  194. new TWEEN.Tween( transitionParams )
  195. .to( { transition: 1 }, 1500 )
  196. .repeat( Infinity )
  197. .delay( 2000 )
  198. .yoyo( true )
  199. .start();
  200. this.needsTextureChange = false;
  201. this.setTextureThreshold = function ( value ) {
  202. material.uniforms.threshold.value = value;
  203. };
  204. this.useTexture = function ( value ) {
  205. material.uniforms.useTexture.value = value ? 1 : 0;
  206. };
  207. this.setTexture = function ( i ) {
  208. material.uniforms.tMixTexture.value = textures[ i ];
  209. };
  210. this.render = function ( delta ) {
  211. // Transition animation
  212. if ( transitionParams.animate ) {
  213. TWEEN.update();
  214. // Change the current alpha texture after each transition
  215. if ( transitionParams.cycle ) {
  216. if ( transitionParams.transition == 0 || transitionParams.transition == 1 ) {
  217. if ( this.needsTextureChange ) {
  218. transitionParams.texture = ( transitionParams.texture + 1 ) % textures.length;
  219. material.uniforms.tMixTexture.value = textures[ transitionParams.texture ];
  220. this.needsTextureChange = false;
  221. }
  222. } else {
  223. this.needsTextureChange = true;
  224. }
  225. } else {
  226. this.needsTextureChange = true;
  227. }
  228. }
  229. material.uniforms.mixRatio.value = transitionParams.transition;
  230. // Prevent render both scenes when it's not necessary
  231. if ( transitionParams.transition == 0 ) {
  232. sceneB.render( delta, false );
  233. } else if ( transitionParams.transition == 1 ) {
  234. sceneA.render( delta, false );
  235. } else {
  236. // When 0<transition<1 render transition between two scenes
  237. sceneA.render( delta, true );
  238. sceneB.render( delta, true );
  239. renderer.setRenderTarget( null );
  240. renderer.clear();
  241. renderer.render( scene, camera );
  242. }
  243. };
  244. }
  245. </script>
  246. </body>
  247. </html>