webgl_postprocessing_crossfade.html 9.0 KB

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