1
0

webgpu_postprocessing_transition.html 7.5 KB

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