webgpu_portal.html 4.5 KB

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