webgpu_backdrop.html 4.9 KB

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