webgpu_portal.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass, color, mx_worley_noise_float, timerLocal, viewportTopLeft, vec2, uv, normalWorld, mx_fractal_noise_vec3, toneMapping, MeshBasicNodeMaterial } from 'three/nodes';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, sceneMain, scenePortal, renderer;
  29. let clock;
  30. const mixers = [];
  31. init();
  32. function init() {
  33. //
  34. sceneMain = new THREE.Scene();
  35. sceneMain.background = new THREE.Color( 0x222222 );
  36. sceneMain.backgroundNode = normalWorld.y.mix( color( 0x0066ff ), color( 0xff0066 ) );
  37. scenePortal = new THREE.Scene();
  38. scenePortal.backgroundNode = mx_worley_noise_float( normalWorld.mul( 20 ).add( vec2( 0, timerLocal().oneMinus() ) ) ).mul( color( 0x0066ff ) );
  39. //
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 30 );
  41. camera.position.set( 2.5, 1, 3 );
  42. camera.position.multiplyScalar( .8 );
  43. camera.lookAt( 0, 1, 0 );
  44. clock = new THREE.Clock();
  45. // lights
  46. const light = new THREE.PointLight( 0xffffff, 1 );
  47. light.position.set( 0, 1, 5 );
  48. light.power = 17000;
  49. sceneMain.add( new THREE.HemisphereLight( 0xff0066, 0x0066ff, 7 ) );
  50. sceneMain.add( light );
  51. scenePortal.add( light.clone() );
  52. // models
  53. const loader = new GLTFLoader();
  54. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  55. const createModel = ( colorNode = null ) => {
  56. let object;
  57. if ( mixers.length === 0 ) {
  58. object = gltf.scene;
  59. } else {
  60. object = gltf.scene.clone();
  61. const children = object.children[ 0 ].children;
  62. const applyFX = ( index ) => {
  63. children[ index ].material = children[ index ].material.clone();
  64. children[ index ].material.colorNode = colorNode;
  65. children[ index ].material.wireframe = true;
  66. };
  67. applyFX( 0 );
  68. applyFX( 1 );
  69. }
  70. const mixer = new THREE.AnimationMixer( object );
  71. const action = mixer.clipAction( gltf.animations[ 6 ] );
  72. action.play();
  73. mixers.push( mixer );
  74. return object;
  75. };
  76. const colorNode = mx_fractal_noise_vec3( uv().mul( 20 ).add( timerLocal() ) );
  77. const modelMain = createModel();
  78. const modelPortal = createModel( colorNode );
  79. // model portal
  80. sceneMain.add( modelMain );
  81. scenePortal.add( modelPortal );
  82. } );
  83. // portal
  84. const geometry = new THREE.PlaneGeometry( 1.7, 2 );
  85. const material = new MeshBasicNodeMaterial();
  86. material.colorNode = pass( scenePortal, camera ).context( { getUV: () => viewportTopLeft } );
  87. material.opacityNode = uv().distance( .5 ).remapClamp( .3, .5 ).oneMinus();
  88. material.side = THREE.DoubleSide;
  89. material.transparent = true;
  90. const plane = new THREE.Mesh( geometry, material );
  91. plane.position.set( 0, 1, .8 );
  92. sceneMain.add( plane );
  93. // renderer
  94. renderer = new WebGPURenderer( { antialias: true } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. renderer.setAnimationLoop( animate );
  98. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .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>