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