2
0

webgpu_backdrop.html 5.0 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. <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.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 { float, vec3, color, toneMapping, viewportSharedTexture, viewportTopLeft, checker, uv, timerLocal, oscSine, output, MeshStandardNodeMaterial } 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, scene, renderer;
  30. let portals, rotate = true;
  31. let mixer, clock;
  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. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  39. camera.position.set( 1, 2, 3 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 'lightblue' );
  42. camera.lookAt( 0, 1, 0 );
  43. clock = new THREE.Clock();
  44. //lights
  45. const light = new THREE.SpotLight( 0xffffff, 1 );
  46. light.power = 2000;
  47. camera.add( light );
  48. scene.add( camera );
  49. const loader = new GLTFLoader();
  50. loader.load( 'models/gltf/Michelle.glb', function ( gltf ) {
  51. const object = gltf.scene;
  52. mixer = new THREE.AnimationMixer( object );
  53. const material = object.children[ 0 ].children[ 0 ].material;
  54. // output material effect ( better using hsv )
  55. // ignore output.sRGBToLinear().linearTosRGB() for now
  56. material.outputNode = oscSine( timerLocal( .1 ) ).mix( output, output.add( .1 ).posterize( 4 ).mul( 2 ) );
  57. const action = mixer.clipAction( gltf.animations[ 0 ] );
  58. action.play();
  59. scene.add( object );
  60. } );
  61. // portals
  62. const geometry = new THREE.SphereGeometry( .3, 32, 16 );
  63. portals = new THREE.Group();
  64. scene.add( portals );
  65. function addBackdropSphere( backdropNode, backdropAlphaNode = null ) {
  66. const distance = 1;
  67. const id = portals.children.length;
  68. const rotation = THREE.MathUtils.degToRad( id * 45 );
  69. const material = new MeshStandardNodeMaterial( { color: 0x0066ff } );
  70. material.roughnessNode = float( .2 );
  71. material.metalnessNode = float( 0 );
  72. material.backdropNode = backdropNode;
  73. material.backdropAlphaNode = backdropAlphaNode;
  74. material.transparent = true;
  75. const mesh = new THREE.Mesh( geometry, material );
  76. mesh.position.set(
  77. Math.cos( rotation ) * distance,
  78. 1,
  79. Math.sin( rotation ) * distance
  80. );
  81. portals.add( mesh );
  82. }
  83. addBackdropSphere( viewportSharedTexture().bgr.hue( oscSine().mul( Math.PI ) ) );
  84. addBackdropSphere( viewportSharedTexture().rgb.oneMinus() );
  85. addBackdropSphere( viewportSharedTexture().rgb.saturation( 0 ) );
  86. addBackdropSphere( viewportSharedTexture().rgb.saturation( 10 ), oscSine() );
  87. addBackdropSphere( viewportSharedTexture().rgb.overlay( checker( uv().mul( 10 ) ) ) );
  88. addBackdropSphere( viewportSharedTexture( viewportTopLeft.mul( 40 ).floor().div( 40 ) ) );
  89. addBackdropSphere( viewportSharedTexture( viewportTopLeft.mul( 80 ).floor().div( 80 ) ).add( color( 0x0033ff ) ) );
  90. addBackdropSphere( vec3( 0, 0, viewportSharedTexture().b ) );
  91. //renderer
  92. renderer = new WebGPURenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.setAnimationLoop( animate );
  96. renderer.toneMappingNode = toneMapping( THREE.LinearToneMapping, .15 );
  97. document.body.appendChild( renderer.domElement );
  98. const controls = new OrbitControls( camera, renderer.domElement );
  99. controls.target.set( 0, 1, 0 );
  100. controls.addEventListener( 'start', () => rotate = false );
  101. controls.addEventListener( 'end', () => rotate = true );
  102. controls.update();
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function animate() {
  111. const delta = clock.getDelta();
  112. if ( mixer ) mixer.update( delta );
  113. if ( rotate ) portals.rotation.y += delta * 0.5;
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>