webgl_multiple_canvases_grid.html 7.0 KB

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