webgl_postprocessing_crossfade.html 9.4 KB

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