1
0

webgpu_postprocessing_transition.html 7.5 KB

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