webgpu_postprocessing_transition.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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> webgpu scene transitions.<br/>
  12. Original implementation by <a href="https://twitter.com/fernandojsg">fernandojsg</a> - <a href="https://github.com/kile/three.js-demos">github</a>
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.webgpu.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import TWEEN from 'three/addons/libs/tween.module.js';
  27. import { uniform, pass } from 'three/tsl';
  28. import { transition } from 'three/addons/tsl/display/TransitionNode.js';
  29. let renderer, postProcessing, transitionController, transitionPass;
  30. const textures = [];
  31. const clock = new THREE.Clock();
  32. const effectController = {
  33. animateScene: true,
  34. animateTransition: true,
  35. transition: 0,
  36. _transition: uniform( 0 ),
  37. useTexture: true,
  38. _useTexture: uniform( 1 ),
  39. texture: 5,
  40. cycle: true,
  41. threshold: uniform( 0.1 ),
  42. };
  43. function generateInstancedMesh( geometry, material, count ) {
  44. const mesh = new THREE.InstancedMesh( geometry, material, count );
  45. const dummy = new THREE.Object3D();
  46. const color = new THREE.Color();
  47. for ( let i = 0; i < count; i ++ ) {
  48. dummy.position.x = Math.random() * 100 - 50;
  49. dummy.position.y = Math.random() * 60 - 30;
  50. dummy.position.z = Math.random() * 80 - 40;
  51. dummy.rotation.x = Math.random() * 2 * Math.PI;
  52. dummy.rotation.y = Math.random() * 2 * Math.PI;
  53. dummy.rotation.z = Math.random() * 2 * Math.PI;
  54. dummy.scale.x = Math.random() * 2 + 1;
  55. if ( geometry.type === 'BoxGeometry' ) {
  56. dummy.scale.y = Math.random() * 2 + 1;
  57. dummy.scale.z = Math.random() * 2 + 1;
  58. } else {
  59. dummy.scale.y = dummy.scale.x;
  60. dummy.scale.z = dummy.scale.x;
  61. }
  62. dummy.updateMatrix();
  63. mesh.setMatrixAt( i, dummy.matrix );
  64. mesh.setColorAt( i, color.setScalar( 0.1 + 0.9 * Math.random() ) );
  65. }
  66. return mesh;
  67. }
  68. function FXScene( geometry, rotationSpeed, backgroundColor ) {
  69. const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  70. camera.position.z = 20;
  71. // Setup scene
  72. const scene = new THREE.Scene();
  73. scene.background = new THREE.Color( backgroundColor );
  74. scene.add( new THREE.AmbientLight( 0xaaaaaa, 3 ) );
  75. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  76. light.position.set( 0, 1, 4 );
  77. scene.add( light );
  78. this.rotationSpeed = rotationSpeed;
  79. const color = geometry.type === 'BoxGeometry' ? 0x0000ff : 0xff0000;
  80. const material = new THREE.MeshPhongNodeMaterial( { color: color, flatShading: true } );
  81. const mesh = generateInstancedMesh( geometry, material, 500 );
  82. scene.add( mesh );
  83. this.scene = scene;
  84. this.camera = camera;
  85. this.mesh = mesh;
  86. this.update = function ( delta ) {
  87. if ( effectController.animateScene ) {
  88. mesh.rotation.x += this.rotationSpeed.x * delta;
  89. mesh.rotation.y += this.rotationSpeed.y * delta;
  90. mesh.rotation.z += this.rotationSpeed.z * delta;
  91. }
  92. };
  93. this.resize = function () {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. };
  97. }
  98. const fxSceneA = new FXScene( new THREE.BoxGeometry( 2, 2, 2 ), new THREE.Vector3( 0, - 0.4, 0 ), 0xffffff );
  99. const fxSceneB = new FXScene( new THREE.IcosahedronGeometry( 1, 1 ), new THREE.Vector3( 0, 0.2, 0.1 ), 0x000000 );
  100. function init() {
  101. // Initialize textures
  102. const loader = new THREE.TextureLoader();
  103. for ( let i = 0; i < 6; i ++ ) {
  104. textures[ i ] = loader.load( 'textures/transition/transition' + ( i + 1 ) + '.png' );
  105. }
  106. renderer = new THREE.WebGPURenderer( { antialias: true } );
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.setAnimationLoop( animate );
  110. document.body.appendChild( renderer.domElement );
  111. postProcessing = new THREE.PostProcessing( renderer );
  112. const scenePassA = pass( fxSceneA.scene, fxSceneA.camera );
  113. const scenePassB = pass( fxSceneB.scene, fxSceneB.camera );
  114. transitionPass = transition( scenePassA, scenePassB, new THREE.TextureNode( textures[ effectController.texture ] ), effectController._transition, effectController.threshold, effectController._useTexture );
  115. postProcessing.outputNode = transitionPass;
  116. const gui = new GUI();
  117. gui.add( effectController, 'animateScene' ).name( 'Animate Scene' );
  118. gui.add( effectController, 'animateTransition' ).name( 'Animate Transition' );
  119. transitionController = gui.add( effectController, 'transition', 0, 1, 0.01 ).name( 'transition' ).onChange( () => {
  120. effectController._transition.value = effectController.transition;
  121. } );
  122. gui.add( effectController, 'useTexture' ).onChange( () => {
  123. const value = effectController.useTexture ? 1 : 0;
  124. effectController._useTexture.value = value;
  125. } );
  126. gui.add( effectController, 'texture', { Perlin: 0, Squares: 1, Cells: 2, Distort: 3, Gradient: 4, Radial: 5 } );
  127. gui.add( effectController, 'cycle' );
  128. gui.add( effectController.threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );
  129. }
  130. window.addEventListener( 'resize', onWindowResize );
  131. function onWindowResize() {
  132. fxSceneA.resize();
  133. fxSceneB.resize();
  134. renderer.setSize( window.innerWidth, window.innerHeight );
  135. }
  136. new TWEEN.Tween( effectController )
  137. .to( { transition: 1 }, 1500 )
  138. .onUpdate( function ( ) {
  139. transitionController.setValue( effectController.transition );
  140. // Change the current alpha texture after each transition
  141. if ( effectController.cycle ) {
  142. if ( effectController.transition == 0 || effectController.transition == 1 ) {
  143. effectController.texture = ( effectController.texture + 1 ) % textures.length;
  144. }
  145. }
  146. } )
  147. .repeat( Infinity )
  148. .delay( 2000 )
  149. .yoyo( true )
  150. .start();
  151. function animate() {
  152. if ( effectController.animateTransition ) TWEEN.update();
  153. if ( textures[ effectController.texture ] ) {
  154. const mixTexture = textures[ effectController.texture ];
  155. transitionPass.mixTextureNode.value = mixTexture;
  156. }
  157. const delta = clock.getDelta();
  158. fxSceneA.update( delta );
  159. fxSceneB.update( delta );
  160. render();
  161. }
  162. function render() {
  163. // Prevent render both scenes when it's not necessary
  164. if ( effectController.transition === 0 ) {
  165. renderer.render( fxSceneB.scene, fxSceneB.camera );
  166. } else if ( effectController.transition === 1 ) {
  167. renderer.render( fxSceneA.scene, fxSceneA.camera );
  168. } else {
  169. postProcessing.render();
  170. }
  171. }
  172. init();
  173. </script>
  174. </body>
  175. </html>