webgpu_backdrop.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. <meta property="og:title" content="three.js - WebGPU - Backdrop">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_backdrop.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_backdrop.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Backdrop</span>
  18. </div>
  19. <small>Backdrop objects can apply blend, overlay or filter effects on occluded 3D objects.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { float, vec3, color, viewportSharedTexture, hue, blendOverlay, posterize, grayscale, saturation, viewportSafeUV, screenUV, checker, uv, time, oscSine, output } from 'three/tsl';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. let camera, scene, renderer;
  37. let portals, rotate = true;
  38. let mixer, timer;
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  42. camera.position.set( 1, 2, 3 );
  43. scene = new THREE.Scene();
  44. scene.backgroundNode = screenUV.y.mix( color( 0x66bbff ), color( 0x4466ff ) );
  45. camera.lookAt( 0, 1, 0 );
  46. timer = new THREE.Timer();
  47. timer.connect( document );
  48. // lights
  49. const light = new THREE.SpotLight( 0xffffff, 1 );
  50. light.power = 2000;
  51. camera.add( light );
  52. scene.add( camera );
  53. const loader = new GLTFLoader();
  54. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  55. const object = gltf.scene;
  56. mixer = new THREE.AnimationMixer( object );
  57. const material = object.children[ 0 ].children[ 0 ].material;
  58. material.outputNode = oscSine( time.mul( .1 ) ).mix( output, posterize( output.add( .1 ), 4 ).mul( 2 ) );
  59. const action = mixer.clipAction( gltf.animations[ 0 ] );
  60. action.play();
  61. scene.add( object );
  62. } );
  63. // portals
  64. const geometry = new THREE.SphereGeometry( .3, 32, 16 );
  65. portals = new THREE.Group();
  66. scene.add( portals );
  67. function addBackdropSphere( backdropNode, backdropAlphaNode = null ) {
  68. const distance = 1;
  69. const id = portals.children.length;
  70. const rotation = THREE.MathUtils.degToRad( id * 45 );
  71. const material = new THREE.MeshStandardNodeMaterial( { color: 0x0066ff } );
  72. material.roughnessNode = float( .2 );
  73. material.metalnessNode = float( 0 );
  74. material.backdropNode = backdropNode;
  75. material.backdropAlphaNode = backdropAlphaNode;
  76. material.transparent = true;
  77. const mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.set(
  79. Math.cos( rotation ) * distance,
  80. 1,
  81. Math.sin( rotation ) * distance
  82. );
  83. portals.add( mesh );
  84. }
  85. addBackdropSphere( hue( viewportSharedTexture().bgr, oscSine().mul( Math.PI ) ) );
  86. addBackdropSphere( viewportSharedTexture().rgb.oneMinus() );
  87. addBackdropSphere( grayscale( viewportSharedTexture().rgb ) );
  88. addBackdropSphere( saturation( viewportSharedTexture().rgb, 10 ), oscSine() );
  89. addBackdropSphere( blendOverlay( viewportSharedTexture().rgb, checker( uv().mul( 10 ) ) ) );
  90. addBackdropSphere( viewportSharedTexture( viewportSafeUV( screenUV.mul( 40 ).floor().div( 40 ) ) ) );
  91. addBackdropSphere( viewportSharedTexture( viewportSafeUV( screenUV.mul( 80 ).floor().div( 80 ) ) ).add( color( 0x0033ff ) ) );
  92. addBackdropSphere( vec3( 0, 0, viewportSharedTexture().b ) );
  93. // renderer
  94. renderer = new THREE.WebGPURenderer( { antialias: false } );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. renderer.setAnimationLoop( animate );
  98. renderer.toneMapping = THREE.NeutralToneMapping;
  99. renderer.toneMappingExposure = 0.3;
  100. document.body.appendChild( renderer.domElement );
  101. const controls = new OrbitControls( camera, renderer.domElement );
  102. controls.target.set( 0, 1, 0 );
  103. controls.addEventListener( 'start', () => rotate = false );
  104. controls.addEventListener( 'end', () => rotate = true );
  105. controls.update();
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. function animate() {
  114. timer.update();
  115. const delta = timer.getDelta();
  116. if ( mixer ) mixer.update( delta );
  117. if ( rotate ) portals.rotation.y += delta * 0.5;
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号