webgpu_portal.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - portal</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 - portal">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_portal.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_portal.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>Portal</span>
  18. </div>
  19. <small>
  20. The portal displays a different scene when viewed through it.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. import { pass, color, mx_worley_noise_float, time, screenUV, vec2, uv, normalWorld, mx_fractal_noise_vec3 } from 'three/tsl';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { Inspector } from 'three/addons/inspector/Inspector.js';
  39. let camera, sceneMain, scenePortal, renderer;
  40. let timer;
  41. const mixers = [];
  42. init();
  43. function init() {
  44. //
  45. sceneMain = new THREE.Scene();
  46. sceneMain.background = new THREE.Color( 0x222222 );
  47. sceneMain.backgroundNode = normalWorld.y.mix( color( 0x0066ff ), color( 0xff0066 ) );
  48. scenePortal = new THREE.Scene();
  49. scenePortal.backgroundNode = mx_worley_noise_float( normalWorld.mul( 20 ).add( vec2( 0, time.oneMinus() ) ) ).mul( color( 0x0066ff ) );
  50. scenePortal.name = 'Portal Scene';
  51. //
  52. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 30 );
  53. camera.position.set( 2.5, 1, 3 );
  54. camera.position.multiplyScalar( .8 );
  55. camera.lookAt( 0, 1, 0 );
  56. timer = new THREE.Timer();
  57. timer.connect( document );
  58. // lights
  59. const light = new THREE.PointLight( 0xffffff, 1 );
  60. light.position.set( 0, 1, 5 );
  61. light.power = 17000;
  62. sceneMain.add( new THREE.HemisphereLight( 0xff0066, 0x0066ff, 7 ) );
  63. sceneMain.add( light );
  64. scenePortal.add( light.clone() );
  65. // models
  66. const loader = new GLTFLoader();
  67. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  68. const createModel = ( colorNode = null ) => {
  69. let object;
  70. if ( mixers.length === 0 ) {
  71. object = gltf.scene;
  72. } else {
  73. object = gltf.scene.clone();
  74. const children = object.children[ 0 ].children;
  75. const applyFX = ( index ) => {
  76. children[ index ].material = children[ index ].material.clone();
  77. children[ index ].material.colorNode = colorNode;
  78. children[ index ].material.wireframe = true;
  79. };
  80. applyFX( 0 );
  81. applyFX( 1 );
  82. }
  83. const mixer = new THREE.AnimationMixer( object );
  84. const action = mixer.clipAction( gltf.animations[ 6 ] );
  85. action.play();
  86. mixers.push( mixer );
  87. return object;
  88. };
  89. const colorNode = mx_fractal_noise_vec3( uv().mul( 20 ).add( time ) );
  90. const modelMain = createModel();
  91. const modelPortal = createModel( colorNode );
  92. // model portal
  93. sceneMain.add( modelMain );
  94. scenePortal.add( modelPortal );
  95. } );
  96. // portal
  97. const geometry = new THREE.PlaneGeometry( 1.7, 2 );
  98. const material = new THREE.MeshBasicNodeMaterial();
  99. material.colorNode = pass( scenePortal, camera ).context( { getUV: () => screenUV } );
  100. material.opacityNode = uv().distance( .5 ).remapClamp( .3, .5 ).oneMinus();
  101. material.side = THREE.DoubleSide;
  102. material.transparent = true;
  103. const plane = new THREE.Mesh( geometry, material );
  104. plane.position.set( 0, 1, .8 );
  105. sceneMain.add( plane );
  106. // renderer
  107. renderer = new THREE.WebGPURenderer( { antialias: true } );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. renderer.setAnimationLoop( animate );
  111. renderer.toneMapping = THREE.LinearToneMapping;
  112. renderer.toneMappingExposure = 0.15;
  113. renderer.inspector = new Inspector();
  114. document.body.appendChild( renderer.domElement );
  115. //
  116. const controls = new OrbitControls( camera, renderer.domElement );
  117. controls.target.set( 0, 1, 0 );
  118. controls.update();
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function animate() {
  127. timer.update();
  128. const delta = timer.getDelta();
  129. for ( const mixer of mixers ) {
  130. mixer.update( delta );
  131. }
  132. renderer.render( sceneMain, camera );
  133. }
  134. </script>
  135. </body>
  136. </html>
粤ICP备19079148号