webgpu_portal.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 WebGPU from 'three/addons/capabilities/WebGPU.js';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. let camera, sceneMain, scenePortal, renderer;
  30. let clock;
  31. const mixers = [];
  32. init();
  33. function init() {
  34. if ( WebGPU.isAvailable() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU support' );
  37. }
  38. //
  39. sceneMain = new THREE.Scene();
  40. sceneMain.background = new THREE.Color( 0x222222 );
  41. sceneMain.backgroundNode = normalWorld.y.mix( color( 0x0066ff ), color( 0xff0066 ) );
  42. scenePortal = new THREE.Scene();
  43. scenePortal.backgroundNode = mx_worley_noise_float( normalWorld.mul( 20 ).add( vec2( 0, timerLocal().oneMinus() ) ) ).mul( color( 0x0066ff ) );
  44. //
  45. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 30 );
  46. camera.position.set( 2.5, 1, 3 );
  47. camera.position.multiplyScalar( .8 );
  48. camera.lookAt( 0, 1, 0 );
  49. clock = new THREE.Clock();
  50. // lights
  51. const light = new THREE.PointLight( 0xffffff, 1 );
  52. light.position.set( 0, 1, 5 );
  53. light.power = 17000;
  54. sceneMain.add( new THREE.HemisphereLight( 0xff0066, 0x0066ff, 7 ) );
  55. sceneMain.add( light );
  56. scenePortal.add( light.clone() );
  57. // models
  58. const loader = new GLTFLoader();
  59. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  60. const createModel = ( colorNode = null ) => {
  61. let object;
  62. if ( mixers.length === 0 ) {
  63. object = gltf.scene;
  64. } else {
  65. object = gltf.scene.clone();
  66. const children = object.children[ 0 ].children;
  67. const applyFX = ( index ) => {
  68. children[ index ].material = children[ index ].material.clone();
  69. children[ index ].material.colorNode = colorNode;
  70. children[ index ].material.wireframe = true;
  71. };
  72. applyFX( 0 );
  73. applyFX( 1 );
  74. }
  75. const mixer = new THREE.AnimationMixer( object );
  76. const action = mixer.clipAction( gltf.animations[ 6 ] );
  77. action.play();
  78. mixers.push( mixer );
  79. return object;
  80. };
  81. const colorNode = mx_fractal_noise_vec3( uv().mul( 20 ).add( timerLocal() ) );
  82. const modelMain = createModel();
  83. const modelPortal = createModel( colorNode );
  84. //
  85. sceneMain.add( modelMain );
  86. scenePortal.add( modelPortal );
  87. } );
  88. //
  89. const geometry = new THREE.PlaneGeometry( 1.7, 2 );
  90. const material = new MeshBasicNodeMaterial();
  91. material.colorNode = pass( scenePortal, camera ).context( { getUV: () => viewportTopLeft } );
  92. material.opacityNode = uv().distance( .5 ).remapClamp( .3, .5 ).oneMinus();
  93. material.side = THREE.DoubleSide;
  94. material.transparent = true;
  95. const plane = new THREE.Mesh( geometry, material );
  96. plane.position.set( 0, 1, .8 );
  97. sceneMain.add( plane );
  98. // renderer
  99. renderer = new WebGPURenderer( { antialias: true } );
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.setAnimationLoop( animate );
  103. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  104. document.body.appendChild( renderer.domElement );
  105. //
  106. const controls = new OrbitControls( camera, renderer.domElement );
  107. controls.target.set( 0, 1, 0 );
  108. controls.update();
  109. window.addEventListener( 'resize', onWindowResize );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function animate() {
  117. const delta = clock.getDelta();
  118. for ( const mixer of mixers ) {
  119. mixer.update( delta );
  120. }
  121. renderer.render( sceneMain, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>