webgpu_multiple_canvas.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - multiple canvas</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 - multiple canvas">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_multiple_canvas.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_multiple_canvas.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. * {
  14. box-sizing: border-box;
  15. -moz-box-sizing: border-box;
  16. }
  17. body {
  18. background-color: #fff;
  19. color: #444;
  20. }
  21. a {
  22. color: #08f;
  23. }
  24. #content {
  25. position: absolute;
  26. top: 0; width: 100%;
  27. z-index: 1;
  28. padding: 3em 0 0 0;
  29. }
  30. .list-item {
  31. display: inline-block;
  32. margin: 1em;
  33. padding: 1em;
  34. box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
  35. }
  36. .list-item > canvas:nth-child(1) {
  37. width: 200px;
  38. height: 200px;
  39. }
  40. .list-item > div:nth-child(2) {
  41. color: #888;
  42. font-family: sans-serif;
  43. font-size: large;
  44. width: 200px;
  45. margin-top: 0.5em;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="content">
  51. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple canvas</div>
  52. </div>
  53. <script type="importmap">
  54. {
  55. "imports": {
  56. "three": "../build/three.webgpu.js",
  57. "three/webgpu": "../build/three.webgpu.js",
  58. "three/tsl": "../build/three.tsl.js",
  59. "three/addons/": "./jsm/"
  60. }
  61. }
  62. </script>
  63. <script type="module">
  64. import * as THREE from 'three';
  65. import { color } from 'three/tsl';
  66. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  67. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  68. //
  69. if ( WebGPU.isAvailable() === false ) {
  70. document.body.appendChild( WebGPU.getErrorMessage() );
  71. throw new Error( 'No WebGPU support' );
  72. }
  73. //
  74. let renderer;
  75. const scenes = [];
  76. init();
  77. function init() {
  78. const geometries = [
  79. new THREE.BoxGeometry( 1, 1, 1 ),
  80. new THREE.SphereGeometry( 0.5, 12, 8 ),
  81. new THREE.DodecahedronGeometry( 0.5 ),
  82. new THREE.CylinderGeometry( 0.5, 0.5, 1, 12 )
  83. ];
  84. const content = document.getElementById( 'content' );
  85. for ( let i = 0; i < 40; i ++ ) {
  86. const scene = new THREE.Scene();
  87. scene.backgroundNode = color( 0xeeeeee );
  88. // make a list item
  89. const element = document.createElement( 'div' );
  90. element.className = 'list-item';
  91. const sceneCanvas = document.createElement( 'canvas' );
  92. element.appendChild( sceneCanvas );
  93. const descriptionElement = document.createElement( 'div' );
  94. descriptionElement.innerText = 'Scene ' + ( i + 1 );
  95. element.appendChild( descriptionElement );
  96. const canvasTarget = new THREE.CanvasTarget( sceneCanvas );
  97. canvasTarget.setPixelRatio( window.devicePixelRatio );
  98. canvasTarget.setSize( 200, 200 );
  99. // the element that represents the area we want to render the scene
  100. scene.userData.canvasTarget = canvasTarget;
  101. content.appendChild( element );
  102. const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  103. camera.position.z = 2;
  104. scene.userData.camera = camera;
  105. const controls = new OrbitControls( scene.userData.camera, scene.userData.canvasTarget.domElement );
  106. controls.minDistance = 2;
  107. controls.maxDistance = 5;
  108. controls.enablePan = false;
  109. controls.enableZoom = false;
  110. scene.userData.controls = controls;
  111. // add one random mesh to each scene
  112. const geometry = geometries[ geometries.length * Math.random() | 0 ];
  113. const material = new THREE.MeshStandardMaterial( {
  114. color: new THREE.Color().setHSL( Math.random(), 1, 0.75, THREE.SRGBColorSpace ),
  115. roughness: 0.5,
  116. metalness: 0,
  117. flatShading: true
  118. } );
  119. scene.add( new THREE.Mesh( geometry, material ) );
  120. scene.add( new THREE.HemisphereLight( 0xaaaaaa, 0x444444, 3 ) );
  121. const light = new THREE.DirectionalLight( 0xffffff, 1.5 );
  122. light.position.set( 1, 1, 1 );
  123. scene.add( light );
  124. scenes.push( scene );
  125. }
  126. renderer = new THREE.WebGPURenderer( { antialias: true } );
  127. renderer.setClearColor( 0xffffff, 1 );
  128. renderer.setPixelRatio( window.devicePixelRatio );
  129. renderer.setAnimationLoop( animate );
  130. }
  131. function animate() {
  132. scenes.forEach( function ( scene ) {
  133. // so something moves
  134. //scene.children[ 0 ].rotation.y = Date.now() * 0.001;
  135. // get the canvas and camera for this scene
  136. const { canvasTarget, camera } = scene.userData;
  137. //camera.aspect = width / height; // not changing in this example
  138. //camera.updateProjectionMatrix();
  139. //scene.userData.controls.update();
  140. renderer.setCanvasTarget( canvasTarget );
  141. renderer.render( scene, camera );
  142. } );
  143. }
  144. </script>
  145. </body>
  146. </html>
粤ICP备19079148号