webgpu_postprocessing_transition.html 7.9 KB

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