webgl_postprocessing_crossfade.html 11 KB

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