webgl_multiple_canvases_grid.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. }
  69. }
  70. </script>
  71. <script type="module">
  72. import * as THREE from 'three';
  73. const views = [];
  74. let scene, renderer;
  75. let mouseX = 0, mouseY = 0;
  76. const windowHalfX = window.innerWidth / 2;
  77. const windowHalfY = window.innerHeight / 2;
  78. init();
  79. animate();
  80. //
  81. function View( canvas, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  82. canvas.width = viewWidth * window.devicePixelRatio;
  83. canvas.height = viewHeight * window.devicePixelRatio;
  84. const context = canvas.getContext( '2d' );
  85. const camera = new THREE.PerspectiveCamera( 20, viewWidth / viewHeight, 1, 10000 );
  86. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  87. camera.position.z = 1800;
  88. this.render = function () {
  89. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  90. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  91. camera.lookAt( scene.position );
  92. renderer.render( scene, camera );
  93. context.drawImage( renderer.domElement, 0, 0 );
  94. };
  95. }
  96. //
  97. function init() {
  98. const canvas1 = document.getElementById( 'canvas1' );
  99. const canvas2 = document.getElementById( 'canvas2' );
  100. const canvas3 = document.getElementById( 'canvas3' );
  101. const canvas4 = document.getElementById( 'canvas4' );
  102. const w = 300, h = 200;
  103. const fullWidth = w * 2;
  104. const fullHeight = h * 2;
  105. views.push( new View( canvas1, fullWidth, fullHeight, w * 0, h * 0, w, h ) );
  106. views.push( new View( canvas2, fullWidth, fullHeight, w * 1, h * 0, w, h ) );
  107. views.push( new View( canvas3, fullWidth, fullHeight, w * 0, h * 1, w, h ) );
  108. views.push( new View( canvas4, fullWidth, fullHeight, w * 1, h * 1, w, h ) );
  109. //
  110. scene = new THREE.Scene();
  111. scene.background = new THREE.Color( 0xffffff );
  112. const light = new THREE.DirectionalLight( 0xffffff );
  113. light.position.set( 0, 0, 1 ).normalize();
  114. scene.add( light );
  115. // shadow
  116. const canvas = document.createElement( 'canvas' );
  117. canvas.width = 128;
  118. canvas.height = 128;
  119. const context = canvas.getContext( '2d' );
  120. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  121. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  122. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  123. context.fillStyle = gradient;
  124. context.fillRect( 0, 0, canvas.width, canvas.height );
  125. const shadowTexture = new THREE.CanvasTexture( canvas );
  126. const shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  127. const shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  128. let shadowMesh;
  129. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  130. shadowMesh.position.y = - 250;
  131. shadowMesh.rotation.x = - Math.PI / 2;
  132. scene.add( shadowMesh );
  133. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  134. shadowMesh.position.x = - 400;
  135. shadowMesh.position.y = - 250;
  136. shadowMesh.rotation.x = - Math.PI / 2;
  137. scene.add( shadowMesh );
  138. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  139. shadowMesh.position.x = 400;
  140. shadowMesh.position.y = - 250;
  141. shadowMesh.rotation.x = - Math.PI / 2;
  142. scene.add( shadowMesh );
  143. const radius = 200;
  144. const geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
  145. const count = geometry1.attributes.position.count;
  146. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  147. const geometry2 = geometry1.clone();
  148. const geometry3 = geometry1.clone();
  149. const color = new THREE.Color();
  150. const positions1 = geometry1.attributes.position;
  151. const positions2 = geometry2.attributes.position;
  152. const positions3 = geometry3.attributes.position;
  153. const colors1 = geometry1.attributes.color;
  154. const colors2 = geometry2.attributes.color;
  155. const colors3 = geometry3.attributes.color;
  156. for ( let i = 0; i < count; i ++ ) {
  157. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  158. colors1.setXYZ( i, color.r, color.g, color.b );
  159. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  160. colors2.setXYZ( i, color.r, color.g, color.b );
  161. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  162. colors3.setXYZ( i, color.r, color.g, color.b );
  163. }
  164. const material = new THREE.MeshPhongMaterial( {
  165. color: 0xffffff,
  166. flatShading: true,
  167. vertexColors: true,
  168. shininess: 0
  169. } );
  170. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  171. let mesh = new THREE.Mesh( geometry1, material );
  172. let wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  173. mesh.add( wireframe );
  174. mesh.position.x = - 400;
  175. mesh.rotation.x = - 1.87;
  176. scene.add( mesh );
  177. mesh = new THREE.Mesh( geometry2, material );
  178. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  179. mesh.add( wireframe );
  180. mesh.position.x = 400;
  181. scene.add( mesh );
  182. mesh = new THREE.Mesh( geometry3, material );
  183. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  184. mesh.add( wireframe );
  185. scene.add( mesh );
  186. renderer = new THREE.WebGLRenderer( { antialias: true } );
  187. renderer.setPixelRatio( window.devicePixelRatio );
  188. renderer.setSize( 300, 200 );
  189. document.addEventListener( 'mousemove', onDocumentMouseMove );
  190. }
  191. function onDocumentMouseMove( event ) {
  192. mouseX = event.clientX - windowHalfX;
  193. mouseY = event.clientY - windowHalfY;
  194. }
  195. function animate() {
  196. for ( let i = 0; i < views.length; ++ i ) {
  197. views[ i ].render();
  198. }
  199. requestAnimationFrame( animate );
  200. }
  201. </script>
  202. </body>
  203. </html>