webgl_multiple_canvases_grid.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple canvases - grid</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. html, body {
  10. height: 100%;
  11. }
  12. body {
  13. background-color: #fff;
  14. color: #444;
  15. }
  16. a {
  17. color: #08f;
  18. }
  19. #centerer {
  20. display: table;
  21. width: 100%;
  22. height: 100%;
  23. }
  24. #centerer-cell {
  25. display: table-cell;
  26. vertical-align: middle;
  27. }
  28. #container {
  29. margin-left: auto;
  30. margin-right: auto;
  31. width: 604px; /* 300*2 + border; */
  32. }
  33. #container div {
  34. float: left;
  35. }
  36. #canvas1, #canvas2, #canvas3, #canvas4 {
  37. position: relative;
  38. float: left;
  39. width: 300px;
  40. height: 200px;
  41. outline: 1px solid red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="centerer">
  47. <div id="centerer-cell">
  48. <div id="container">
  49. <div class="container-row">
  50. <canvas id="canvas1"></canvas>
  51. <canvas id="canvas2"></canvas>
  52. </div>
  53. <div class="container-row">
  54. <canvas id="canvas3"></canvas>
  55. <canvas id="canvas4"></canvas>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - multiple canvases - grid</div>
  61. <!-- Import maps polyfill -->
  62. <!-- Remove this when import maps will be widely supported -->
  63. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  64. <script type="importmap">
  65. {
  66. "imports": {
  67. "three": "../build/three.module.js",
  68. "three/addons/": "./jsm/"
  69. }
  70. }
  71. </script>
  72. <script type="module">
  73. import * as THREE from 'three';
  74. const views = [];
  75. let scene, renderer;
  76. let mouseX = 0, mouseY = 0;
  77. const windowHalfX = window.innerWidth / 2;
  78. const windowHalfY = window.innerHeight / 2;
  79. init();
  80. animate();
  81. //
  82. function View( canvas, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  83. canvas.width = viewWidth * window.devicePixelRatio;
  84. canvas.height = viewHeight * window.devicePixelRatio;
  85. const context = canvas.getContext( '2d' );
  86. const camera = new THREE.PerspectiveCamera( 20, viewWidth / viewHeight, 1, 10000 );
  87. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  88. camera.position.z = 1800;
  89. this.render = function () {
  90. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  91. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  92. camera.lookAt( scene.position );
  93. renderer.render( scene, camera );
  94. context.drawImage( renderer.domElement, 0, 0 );
  95. };
  96. }
  97. //
  98. function init() {
  99. const canvas1 = document.getElementById( 'canvas1' );
  100. const canvas2 = document.getElementById( 'canvas2' );
  101. const canvas3 = document.getElementById( 'canvas3' );
  102. const canvas4 = document.getElementById( 'canvas4' );
  103. const w = 300, h = 200;
  104. const fullWidth = w * 2;
  105. const fullHeight = h * 2;
  106. views.push( new View( canvas1, fullWidth, fullHeight, w * 0, h * 0, w, h ) );
  107. views.push( new View( canvas2, fullWidth, fullHeight, w * 1, h * 0, w, h ) );
  108. views.push( new View( canvas3, fullWidth, fullHeight, w * 0, h * 1, w, h ) );
  109. views.push( new View( canvas4, fullWidth, fullHeight, w * 1, h * 1, w, h ) );
  110. //
  111. scene = new THREE.Scene();
  112. scene.background = new THREE.Color( 0xffffff );
  113. const light = new THREE.DirectionalLight( 0xffffff );
  114. light.position.set( 0, 0, 1 ).normalize();
  115. scene.add( light );
  116. // shadow
  117. const canvas = document.createElement( 'canvas' );
  118. canvas.width = 128;
  119. canvas.height = 128;
  120. const context = canvas.getContext( '2d' );
  121. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  122. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  123. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  124. context.fillStyle = gradient;
  125. context.fillRect( 0, 0, canvas.width, canvas.height );
  126. const shadowTexture = new THREE.CanvasTexture( canvas );
  127. const shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  128. const shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  129. let shadowMesh;
  130. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  131. shadowMesh.position.y = - 250;
  132. shadowMesh.rotation.x = - Math.PI / 2;
  133. scene.add( shadowMesh );
  134. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  135. shadowMesh.position.x = - 400;
  136. shadowMesh.position.y = - 250;
  137. shadowMesh.rotation.x = - Math.PI / 2;
  138. scene.add( shadowMesh );
  139. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  140. shadowMesh.position.x = 400;
  141. shadowMesh.position.y = - 250;
  142. shadowMesh.rotation.x = - Math.PI / 2;
  143. scene.add( shadowMesh );
  144. const radius = 200;
  145. const geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
  146. const count = geometry1.attributes.position.count;
  147. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  148. const geometry2 = geometry1.clone();
  149. const geometry3 = geometry1.clone();
  150. const color = new THREE.Color();
  151. const positions1 = geometry1.attributes.position;
  152. const positions2 = geometry2.attributes.position;
  153. const positions3 = geometry3.attributes.position;
  154. const colors1 = geometry1.attributes.color;
  155. const colors2 = geometry2.attributes.color;
  156. const colors3 = geometry3.attributes.color;
  157. for ( let i = 0; i < count; i ++ ) {
  158. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  159. colors1.setXYZ( i, color.r, color.g, color.b );
  160. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  161. colors2.setXYZ( i, color.r, color.g, color.b );
  162. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  163. colors3.setXYZ( i, color.r, color.g, color.b );
  164. }
  165. const material = new THREE.MeshPhongMaterial( {
  166. color: 0xffffff,
  167. flatShading: true,
  168. vertexColors: true,
  169. shininess: 0
  170. } );
  171. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  172. let mesh = new THREE.Mesh( geometry1, material );
  173. let wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  174. mesh.add( wireframe );
  175. mesh.position.x = - 400;
  176. mesh.rotation.x = - 1.87;
  177. scene.add( mesh );
  178. mesh = new THREE.Mesh( geometry2, material );
  179. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  180. mesh.add( wireframe );
  181. mesh.position.x = 400;
  182. scene.add( mesh );
  183. mesh = new THREE.Mesh( geometry3, material );
  184. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  185. mesh.add( wireframe );
  186. scene.add( mesh );
  187. renderer = new THREE.WebGLRenderer( { antialias: true } );
  188. renderer.setPixelRatio( window.devicePixelRatio );
  189. renderer.setSize( 300, 200 );
  190. document.addEventListener( 'mousemove', onDocumentMouseMove );
  191. }
  192. function onDocumentMouseMove( event ) {
  193. mouseX = event.clientX - windowHalfX;
  194. mouseY = event.clientY - windowHalfY;
  195. }
  196. function animate() {
  197. for ( let i = 0; i < views.length; ++ i ) {
  198. views[ i ].render();
  199. }
  200. requestAnimationFrame( animate );
  201. }
  202. </script>
  203. </body>
  204. </html>