webgl_multiple_canvases_grid.html 7.3 KB

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