webgpu_multiple_canvas.html 4.6 KB

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