webgpu_backdrop.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - WebGPU - Backdrop</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 - Backdrop
  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 { float, vec3, color, viewportSharedTexture, hue, overlay, posterize, grayscale, saturation, viewportSafeUV, viewportUV, checker, uv, timerLocal, oscSine, output } from 'three/tsl';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let camera, scene, renderer;
  28. let portals, rotate = true;
  29. let mixer, clock;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  33. camera.position.set( 1, 2, 3 );
  34. scene = new THREE.Scene();
  35. scene.backgroundNode = viewportUV.y.mix( color( 0x66bbff ), color( 0x4466ff ) );
  36. camera.lookAt( 0, 1, 0 );
  37. clock = new THREE.Clock();
  38. // lights
  39. const light = new THREE.SpotLight( 0xffffff, 1 );
  40. light.power = 2000;
  41. camera.add( light );
  42. scene.add( camera );
  43. const loader = new GLTFLoader();
  44. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  45. const object = gltf.scene;
  46. mixer = new THREE.AnimationMixer( object );
  47. const material = object.children[ 0 ].children[ 0 ].material;
  48. material.outputNode = oscSine( timerLocal( .1 ) ).mix( output, posterize( output.add( .1 ), 4 ).mul( 2 ) );
  49. const action = mixer.clipAction( gltf.animations[ 0 ] );
  50. action.play();
  51. scene.add( object );
  52. } );
  53. // portals
  54. const geometry = new THREE.SphereGeometry( .3, 32, 16 );
  55. portals = new THREE.Group();
  56. scene.add( portals );
  57. function addBackdropSphere( backdropNode, backdropAlphaNode = null ) {
  58. const distance = 1;
  59. const id = portals.children.length;
  60. const rotation = THREE.MathUtils.degToRad( id * 45 );
  61. const material = new THREE.MeshStandardNodeMaterial( { color: 0x0066ff } );
  62. material.roughnessNode = float( .2 );
  63. material.metalnessNode = float( 0 );
  64. material.backdropNode = backdropNode;
  65. material.backdropAlphaNode = backdropAlphaNode;
  66. material.transparent = true;
  67. const mesh = new THREE.Mesh( geometry, material );
  68. mesh.position.set(
  69. Math.cos( rotation ) * distance,
  70. 1,
  71. Math.sin( rotation ) * distance
  72. );
  73. portals.add( mesh );
  74. }
  75. addBackdropSphere( hue( viewportSharedTexture().bgr, oscSine().mul( Math.PI ) ) );
  76. addBackdropSphere( viewportSharedTexture().rgb.oneMinus() );
  77. addBackdropSphere( grayscale( viewportSharedTexture().rgb ) );
  78. addBackdropSphere( saturation( viewportSharedTexture().rgb, 10 ), oscSine() );
  79. addBackdropSphere( overlay( viewportSharedTexture().rgb, checker( uv().mul( 10 ) ) ) );
  80. addBackdropSphere( viewportSharedTexture( viewportSafeUV( viewportUV.mul( 40 ).floor().div( 40 ) ) ) );
  81. addBackdropSphere( viewportSharedTexture( viewportSafeUV( viewportUV.mul( 80 ).floor().div( 80 ) ) ).add( color( 0x0033ff ) ) );
  82. addBackdropSphere( vec3( 0, 0, viewportSharedTexture().b ) );
  83. // renderer
  84. renderer = new THREE.WebGPURenderer( { antialias: false } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.setAnimationLoop( animate );
  88. renderer.toneMapping = THREE.NeutralToneMapping;
  89. renderer.toneMappingExposure = 0.3;
  90. document.body.appendChild( renderer.domElement );
  91. const controls = new OrbitControls( camera, renderer.domElement );
  92. controls.target.set( 0, 1, 0 );
  93. controls.addEventListener( 'start', () => rotate = false );
  94. controls.addEventListener( 'end', () => rotate = true );
  95. controls.update();
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. const delta = clock.getDelta();
  105. if ( mixer ) mixer.update( delta );
  106. if ( rotate ) portals.rotation.y += delta * 0.5;
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>