webgpu_backdrop.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { float, vec3, color, toneMapping, viewportSharedTexture, viewportTopLeft, checker, uv, oscSine, MeshStandardNodeMaterial } from 'three/nodes';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  28. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. let camera, scene, renderer;
  31. let portals, rotate = true;
  32. let mixer, clock;
  33. init();
  34. function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU support' );
  38. }
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  40. camera.position.set( 1, 2, 3 );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 'lightblue' );
  43. camera.lookAt( 0, 1, 0 );
  44. clock = new THREE.Clock();
  45. //lights
  46. const light = new THREE.SpotLight( 0xffffff, 1 );
  47. light.power = 2000;
  48. camera.add( light );
  49. scene.add( camera );
  50. const loader = new GLTFLoader();
  51. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  52. const object = gltf.scene;
  53. mixer = new THREE.AnimationMixer( object );
  54. const action = mixer.clipAction( gltf.animations[ 0 ] );
  55. action.play();
  56. scene.add( object );
  57. } );
  58. // portals
  59. portals = new THREE.Group();
  60. scene.add( portals );
  61. function addBackdropSphere( backdropNode, backdropAlphaNode = null ) {
  62. const distance = 1;
  63. const id = portals.children.length;
  64. const rotation = THREE.MathUtils.degToRad( id * 45 );
  65. const geometry = new THREE.SphereGeometry( .3, 32, 16 );
  66. const material = new 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( viewportSharedTexture().bgr.hue( oscSine().mul( Math.PI ) ) );
  81. addBackdropSphere( viewportSharedTexture().rgb.oneMinus() );
  82. addBackdropSphere( viewportSharedTexture().rgb.saturation( 0 ) );
  83. addBackdropSphere( viewportSharedTexture().rgb.saturation( 10 ), oscSine() );
  84. addBackdropSphere( viewportSharedTexture().rgb.overlay( checker( uv().mul( 10 ) ) ) );
  85. addBackdropSphere( viewportSharedTexture( viewportTopLeft.mul( 40 ).floor().div( 40 ) ) );
  86. addBackdropSphere( viewportSharedTexture( viewportTopLeft.mul( 80 ).floor().div( 80 ) ).add( color( 0x0033ff ) ) );
  87. addBackdropSphere( vec3( 0, 0, viewportSharedTexture().b ) );
  88. //renderer
  89. renderer = new WebGPURenderer();
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. renderer.setAnimationLoop( animate );
  93. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  94. document.body.appendChild( renderer.domElement );
  95. const controls = new OrbitControls( camera, renderer.domElement );
  96. controls.target.set( 0, 1, 0 );
  97. controls.addEventListener( 'start', () => rotate = false );
  98. controls.addEventListener( 'end', () => rotate = true );
  99. controls.update();
  100. window.addEventListener( 'resize', onWindowResize );
  101. }
  102. function onWindowResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. function animate() {
  108. const delta = clock.getDelta();
  109. if ( mixer ) mixer.update( delta );
  110. if ( rotate ) portals.rotation.y += delta * 0.5;
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>