webgpu_postprocessing_transition.html 7.1 KB

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