webgl_postprocessing_crossfade.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/dat.gui.module.js';
  19. let container, stats;
  20. let renderer;
  21. let transition;
  22. const transitionParams = {
  23. 'useTexture': true,
  24. 'transition': 0,
  25. 'transitionSpeed': 2.0,
  26. 'texture': 5,
  27. 'loopTexture': true,
  28. 'animateTransition': true,
  29. 'textureThreshold': 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( 1, 1, 1 );
  44. const geometryB = new THREE.IcosahedronGeometry( 1, 1 );
  45. const sceneA = new FXScene( geometryA, 5000, 1200, 120, new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  46. const sceneB = new FXScene( geometryB, 500, 2000, 50, 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, 'useTexture' ).onChange( function ( value ) {
  57. transition.useTexture( value );
  58. } );
  59. gui.add( transitionParams, 'loopTexture' );
  60. gui.add( transitionParams, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } ).onChange( function ( value ) {
  61. transition.setTexture( value );
  62. } ).listen();
  63. gui.add( transitionParams, 'textureThreshold', 0, 1, 0.01 ).onChange( function ( value ) {
  64. transition.setTextureThreshold( value );
  65. } );
  66. gui.add( transitionParams, 'animateTransition' );
  67. gui.add( transitionParams, 'transition', 0, 1, 0.01 ).listen();
  68. gui.add( transitionParams, 'transitionSpeed', 0.5, 5, 0.01 );
  69. }
  70. function render() {
  71. transition.render( clock.getDelta() );
  72. }
  73. function generateInstancedMesh( geometry, material, count ) {
  74. const mesh = new THREE.InstancedMesh( geometry, material, count );
  75. const dummy = new THREE.Object3D();
  76. const color = new THREE.Color();
  77. for ( let i = 0; i < count; i ++ ) {
  78. dummy.position.x = Math.random() * 10000 - 5000;
  79. dummy.position.y = Math.random() * 6000 - 3000;
  80. dummy.position.z = Math.random() * 8000 - 4000;
  81. dummy.rotation.x = Math.random() * 2 * Math.PI;
  82. dummy.rotation.y = Math.random() * 2 * Math.PI;
  83. dummy.rotation.z = Math.random() * 2 * Math.PI;
  84. dummy.scale.x = Math.random() * 200 + 100;
  85. if ( geometry.type === 'BoxGeometry' ) {
  86. dummy.scale.y = Math.random() * 200 + 100;
  87. dummy.scale.z = Math.random() * 200 + 100;
  88. } else {
  89. dummy.scale.y = dummy.scale.x;
  90. dummy.scale.z = dummy.scale.x;
  91. }
  92. dummy.updateMatrix();
  93. mesh.setMatrixAt( i, dummy.matrix );
  94. mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );
  95. }
  96. return mesh;
  97. }
  98. function FXScene( geometry, numObjects, cameraZ, fov, rotationSpeed, clearColor ) {
  99. this.clearColor = clearColor;
  100. const camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 10000 );
  101. camera.position.z = cameraZ;
  102. // Setup scene
  103. const scene = new THREE.Scene();
  104. scene.add( new THREE.AmbientLight( 0x555555 ) );
  105. const light = new THREE.SpotLight( 0xffffff, 1.5 );
  106. light.position.set( 0, 500, 2000 );
  107. scene.add( light );
  108. this.rotationSpeed = rotationSpeed;
  109. const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
  110. const material = new THREE.MeshPhongMaterial( { color: color, flatShading: true } );
  111. const mesh = generateInstancedMesh( geometry, material, numObjects );
  112. scene.add( mesh );
  113. const renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  114. this.fbo = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, renderTargetParameters );
  115. this.render = function ( delta, rtt ) {
  116. mesh.rotation.x += delta * this.rotationSpeed.x;
  117. mesh.rotation.y += delta * this.rotationSpeed.y;
  118. mesh.rotation.z += delta * this.rotationSpeed.z;
  119. renderer.setClearColor( this.clearColor );
  120. if ( rtt ) {
  121. renderer.setRenderTarget( this.fbo );
  122. renderer.clear();
  123. renderer.render( scene, camera );
  124. } else {
  125. renderer.setRenderTarget( null );
  126. renderer.render( scene, camera );
  127. }
  128. };
  129. }
  130. function Transition( sceneA, sceneB ) {
  131. const scene = new THREE.Scene();
  132. const width = window.innerWidth;
  133. const height = window.innerHeight;
  134. const camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, - 10, 10 );
  135. const textures = [];
  136. const loader = new THREE.TextureLoader();
  137. for ( let i = 0; i < 6; i ++ ) {
  138. textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  139. }
  140. const material = new THREE.ShaderMaterial( {
  141. uniforms: {
  142. tDiffuse1: {
  143. value: null
  144. },
  145. tDiffuse2: {
  146. value: null
  147. },
  148. mixRatio: {
  149. value: 0.0
  150. },
  151. threshold: {
  152. value: 0.1
  153. },
  154. useTexture: {
  155. value: 1
  156. },
  157. tMixTexture: {
  158. value: textures[ 0 ]
  159. }
  160. },
  161. vertexShader: [
  162. 'varying vec2 vUv;',
  163. 'void main() {',
  164. 'vUv = vec2( uv.x, uv.y );',
  165. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  166. '}'
  167. ].join( '\n' ),
  168. fragmentShader: [
  169. 'uniform float mixRatio;',
  170. 'uniform sampler2D tDiffuse1;',
  171. 'uniform sampler2D tDiffuse2;',
  172. 'uniform sampler2D tMixTexture;',
  173. 'uniform int useTexture;',
  174. 'uniform float threshold;',
  175. 'varying vec2 vUv;',
  176. 'void main() {',
  177. ' vec4 texel1 = texture2D( tDiffuse1, vUv );',
  178. ' vec4 texel2 = texture2D( tDiffuse2, vUv );',
  179. ' if (useTexture==1) {',
  180. ' vec4 transitionTexel = texture2D( tMixTexture, vUv );',
  181. ' float r = mixRatio * (1.0 + threshold * 2.0) - threshold;',
  182. ' float mixf=clamp((transitionTexel.r - r)*(1.0/threshold), 0.0, 1.0);',
  183. ' gl_FragColor = mix( texel1, texel2, mixf );',
  184. ' } else {',
  185. ' gl_FragColor = mix( texel2, texel1, mixRatio );',
  186. ' }',
  187. '}'
  188. ].join( '\n' )
  189. } );
  190. const geometry = new THREE.PlaneGeometry( window.innerWidth, window.innerHeight );
  191. const mesh = new THREE.Mesh( geometry, material );
  192. scene.add( mesh );
  193. material.uniforms.tDiffuse1.value = sceneA.fbo.texture;
  194. material.uniforms.tDiffuse2.value = sceneB.fbo.texture;
  195. this.needChange = false;
  196. this.setTextureThreshold = function ( value ) {
  197. material.uniforms.threshold.value = value;
  198. };
  199. this.useTexture = function ( value ) {
  200. material.uniforms.useTexture.value = value ? 1 : 0;
  201. };
  202. this.setTexture = function ( i ) {
  203. material.uniforms.tMixTexture.value = textures[ i ];
  204. };
  205. this.render = function ( delta ) {
  206. // Transition animation
  207. if ( transitionParams.animateTransition ) {
  208. const t = ( 1 + Math.sin( transitionParams.transitionSpeed * clock.getElapsedTime() / Math.PI ) ) / 2;
  209. transitionParams.transition = THREE.MathUtils.smoothstep( t, 0.3, 0.7 );
  210. // Change the current alpha texture after each transition
  211. if ( transitionParams.loopTexture && ( transitionParams.transition == 0 || transitionParams.transition == 1 ) ) {
  212. if ( this.needChange ) {
  213. transitionParams.texture = ( transitionParams.texture + 1 ) % textures.length;
  214. material.uniforms.tMixTexture.value = textures[ transitionParams.texture ];
  215. this.needChange = false;
  216. }
  217. } else
  218. this.needChange = true;
  219. }
  220. material.uniforms.mixRatio.value = transitionParams.transition;
  221. // Prevent render both scenes when it's not necessary
  222. if ( transitionParams.transition == 0 ) {
  223. sceneB.render( delta, false );
  224. } else if ( transitionParams.transition == 1 ) {
  225. sceneA.render( delta, false );
  226. } else {
  227. // When 0<transition<1 render transition between two scenes
  228. sceneA.render( delta, true );
  229. sceneB.render( delta, true );
  230. renderer.setRenderTarget( null );
  231. renderer.clear();
  232. renderer.render( scene, camera );
  233. }
  234. };
  235. }
  236. </script>
  237. </body>
  238. </html>