webgl_postprocessing_crossfade.html 10 KB

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