webgl_postprocessing_crossfade.html 9.4 KB

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