webgpu_postprocessing_transition.html 7.2 KB

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