webgl_multiple_canvases_grid.html 7.1 KB

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