webgpu_portal.html 4.9 KB

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